Advertisement
imk0tter

Untitled

Apr 9th, 2011
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
mIRC 4.61 KB | None | 0 0
  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ; Description: This is a Socket class which contains   ;
  3. ; the basic structure of a class                       ;
  4. ; Usage: $Socket(<Instance>,<Params>,..).<Method>      ;
  5. ; Example: var %x $Socket(127.0.0.1,1337,LocalSock)    ;
  6. ; Example: $Socket(%x,SockOpenAlias).sockopen          ;
  7. ; Example: $Socket(%x,SockReadAlias).sockread          ;
  8. ; Example: $Socket(%x).connect                         ;
  9. ; Example: $Socket(%x).close                           ;
  10. ; Example: $Socket(%x).delete                          ;
  11. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  12. ; Public Declarations ;
  13. ;;;;;;;;;;;;;;;;;;;;;;;
  14. alias Socket.DELETE.Public
  15. alias Socket.PORT.Public
  16. alias Socket.IP.Public
  17. alias Socket.OPEN.Public
  18. alias Socket.CONNECT.Public
  19. alias Socket.LISTEN.Public
  20. alias Socket.CLOSE.Public
  21. alias Socket.SOCKREAD.Public
  22. alias Socket.SOCKOPEN.Public
  23. alias Socket.SOCKCLOSE.Public
  24. alias Socket.SOCKACCEPT.Public
  25. alias Socket.TAG.Public
  26. alias Socket.setvTAG.Public
  27. alias Socket.setvIP.Public
  28. alias Socket.setvPORT.Public
  29. alias Socket.WRITE.Public
  30. ;;;;;;;;;;;;;;;;;;;;;;;;;;
  31. ; Exception Declarations ;
  32. ;;;;;;;;;;;;;;;;;;;;;;;;;;
  33. alias Socket.EXCEPTION.SockErr {
  34.   $Class($1,SockErr).getv $1-
  35.   return $iif($bvar(&SockErr,0),$bvar(&SockErr,1-).text)
  36. }
  37. ;;;;;;;;;;;;;;;;
  38. ; Socket Alias ;
  39. ;;;;;;;;;;;;;;;;
  40. alias Socket {
  41.   if (!$prop) goto init
  42.   if ($IsPublic(Socket,$prop)) goto $prop
  43.   return $catch($1,MemberErr, $qt($prop) is not a public member of Socket)
  44.   :init
  45.   return $Socket.INIT($1,$2,$3)
  46.   :delete
  47.   return $Socket.DELETE($1)
  48.   :open
  49.   :connect
  50.   if ($sock($1).status) $catch($1,SockOpenErr,Socket $1 is already open)
  51.   sockopen $1 $Class($1,IP).getv $Class($1,PORT).getv
  52.   return
  53.   :close
  54.   if ($sock($1).status) sockclose $1
  55.   else return $catch($1,SockCloseErr,Sock $1 was not open)
  56.   return
  57.   :listen
  58.   if ($sock($1).status) $catch($1,SockOpenErr,Socket $1 is already open)
  59.   var %x $iif($Class($1,IP).getv,$Class1),%y $iif(%x,-d)
  60.   socklisten %y %x $1 $Class($1,PORT).getv
  61.   return
  62.   :sockclose
  63.   return $Class($1,CLOSE,$2).setv
  64.   :sockopen
  65.   return $Class($1,OPEN,$2).setv
  66.   :sockaccept
  67.   return $Class($1,ACCEPT,$2).setv
  68.   :sockread
  69.   return $Class($1,READ,$2).setv
  70.   :settag
  71.   $Class($1,Tag,$2).setv
  72.   :tag
  73.   return $Class($1,Tag).getv
  74.   :setip
  75.   return $Class($1,IP,$2).setv
  76.   :setport
  77.   return $Class($1,PORT,$2).setv
  78.   :ip
  79.   return $Class($1,IP).getv
  80.   :port
  81.   return $Class($1,PORT).getv
  82.   ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  83.   ; Put your own method handlers here ;
  84.   ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  85.   :write
  86.   if ($sock($1).status != active) return $catch($1,SockErr,Socket $1 is not connected)
  87.   sockwrite $1 $2
  88.   return
  89.   :MethodHandler
  90. }
  91.  
  92. ;;;;;;;;;;;;;;;;;;
  93. ; Socket Methods ;
  94. ;;;;;;;;;;;;;;;;;;
  95. alias Socket.INIT {
  96.   var %instance $Class
  97.   $inheritsFrom(%instance,Socket)
  98.   $Class(%instance,IP,$1).setv
  99.   $Class(%instance,PORT,$2).setv
  100.   $Class(%instance,OPEN,Socket.DefaultSockOpen).setv
  101.   $Class(%instance,CLOSE,Socket.DefaultSockClose).setv
  102.   $Class(%instance,ACCEPT,Socket.DefaultSockAccept).setv
  103.   $Class(%instance,READ,Socket.DefaultSockRead).setv
  104.   $Class(%instance,SockErr,Socket.DefaultSockErr).setv
  105.   $Class(%instance,Tag,$iif($isinstance($3),$3,$string($3))).setv
  106.   ;;;;;;;;;;;;;;;;;;;;;;;;
  107.   ; Do Initializing here ;
  108.   ;;;;;;;;;;;;;;;;;;;;;;;;
  109.   return %instance
  110. }
  111. alias Socket.DELETE {
  112.   ;;;;;;;;;;;;;;;;;;;;;;
  113.   ; Do Destroying here ;
  114.   ;;;;;;;;;;;;;;;;;;;;;;
  115.   if ($sock($1).status) sockclose $1
  116.   return $Class($1).DELETE
  117. }
  118. ;;;;;;;;;;;;;;;;;;;;;;;;;;
  119. ; Default Event Handlers ;
  120. ;;;;;;;;;;;;;;;;;;;;;;;;;;
  121. alias Socket.DefaultSockOpen {
  122.   if ($Window(@Debug)) echo @Debug Successfully connected to $sock($sockname).ip $+ : $+ $sock($sockname).port
  123. }
  124. alias Socket.DefaultSockClose {
  125.   if ($Window(@Debug)) echo @Debug Successfully disconnected from $sock($sockname).ip $+ : $+ $sock($sockname).port
  126.   $Socket($1).delete
  127. }
  128. alias Socket.DefaultSockRead {
  129.   sockread %temp
  130.   if ($window(@Debug)) echo @Debug %temp
  131. }
  132. alias Socket.DefaultSockAccept {
  133. }
  134. alias Socket.DefaultSockErr {
  135.   sockclose $1
  136. }
  137. alias SockErr {
  138.   bset -t &SockErr $1-
  139. }
  140. ;;;;;;;;;;;;;;;;;;;;
  141. ; Trigger Handlers ;
  142. ;;;;;;;;;;;;;;;;;;;;
  143. on 1:sockopen:*: $Class($left($sockname,-1),OPEN).getv $left($sockname,-1)
  144. on 1:sockclose:*: $Class($left($sockname,-1),CLOSE).getv $left($sockname,-1)
  145. on 1:socklisten:*: $Class($left($sockname,-1),ACCEPT).getv $left($sockname,-1)
  146. on 1:sockread:*: $Class($left($sockname,-1),READ).getv $left($sockname,-1)
  147. ;;;;;;;;;;;;;;
  148. ; End Socket ;
  149. ;;;;;;;;;;;;;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement