Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; Description: This is a Socket class which contains ;
- ; the basic structure of a class ;
- ; Usage: $Socket(<Instance>,<Params>,..).<Method> ;
- ; Example: var %x $Socket(127.0.0.1,1337,LocalSock) ;
- ; Example: $Socket(%x,SockOpenAlias).sockopen ;
- ; Example: $Socket(%x,SockReadAlias).sockread ;
- ; Example: $Socket(%x).connect ;
- ; Example: $Socket(%x).close ;
- ; Example: $Socket(%x).delete ;
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; Public Declarations ;
- ;;;;;;;;;;;;;;;;;;;;;;;
- alias Socket.DELETE.Public
- alias Socket.PORT.Public
- alias Socket.IP.Public
- alias Socket.OPEN.Public
- alias Socket.CONNECT.Public
- alias Socket.LISTEN.Public
- alias Socket.CLOSE.Public
- alias Socket.SOCKREAD.Public
- alias Socket.SOCKOPEN.Public
- alias Socket.SOCKCLOSE.Public
- alias Socket.SOCKACCEPT.Public
- alias Socket.TAG.Public
- alias Socket.setvTAG.Public
- alias Socket.setvIP.Public
- alias Socket.setvPORT.Public
- alias Socket.WRITE.Public
- ;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; Exception Declarations ;
- ;;;;;;;;;;;;;;;;;;;;;;;;;;
- alias Socket.EXCEPTION.SockErr {
- $Class($1,SockErr).getv $1-
- return $iif($bvar(&SockErr,0),$bvar(&SockErr,1-).text)
- }
- ;;;;;;;;;;;;;;;;
- ; Socket Alias ;
- ;;;;;;;;;;;;;;;;
- alias Socket {
- if (!$prop) goto init
- if ($IsPublic(Socket,$prop)) goto $prop
- return $catch($1,MemberErr, $qt($prop) is not a public member of Socket)
- :init
- return $Socket.INIT($1,$2,$3)
- :delete
- return $Socket.DELETE($1)
- :open
- :connect
- if ($sock($1).status) $catch($1,SockOpenErr,Socket $1 is already open)
- sockopen $1 $Class($1,IP).getv $Class($1,PORT).getv
- return
- :close
- if ($sock($1).status) sockclose $1
- else return $catch($1,SockCloseErr,Sock $1 was not open)
- return
- :listen
- if ($sock($1).status) $catch($1,SockOpenErr,Socket $1 is already open)
- var %x $iif($Class($1,IP).getv,$Class1),%y $iif(%x,-d)
- socklisten %y %x $1 $Class($1,PORT).getv
- return
- :sockclose
- return $Class($1,CLOSE,$2).setv
- :sockopen
- return $Class($1,OPEN,$2).setv
- :sockaccept
- return $Class($1,ACCEPT,$2).setv
- :sockread
- return $Class($1,READ,$2).setv
- :settag
- $Class($1,Tag,$2).setv
- :tag
- return $Class($1,Tag).getv
- :setip
- return $Class($1,IP,$2).setv
- :setport
- return $Class($1,PORT,$2).setv
- :ip
- return $Class($1,IP).getv
- :port
- return $Class($1,PORT).getv
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; Put your own method handlers here ;
- ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- :write
- if ($sock($1).status != active) return $catch($1,SockErr,Socket $1 is not connected)
- sockwrite $1 $2
- return
- :MethodHandler
- }
- ;;;;;;;;;;;;;;;;;;
- ; Socket Methods ;
- ;;;;;;;;;;;;;;;;;;
- alias Socket.INIT {
- var %instance $Class
- $inheritsFrom(%instance,Socket)
- $Class(%instance,IP,$1).setv
- $Class(%instance,PORT,$2).setv
- $Class(%instance,OPEN,Socket.DefaultSockOpen).setv
- $Class(%instance,CLOSE,Socket.DefaultSockClose).setv
- $Class(%instance,ACCEPT,Socket.DefaultSockAccept).setv
- $Class(%instance,READ,Socket.DefaultSockRead).setv
- $Class(%instance,SockErr,Socket.DefaultSockErr).setv
- $Class(%instance,Tag,$iif($isinstance($3),$3,$string($3))).setv
- ;;;;;;;;;;;;;;;;;;;;;;;;
- ; Do Initializing here ;
- ;;;;;;;;;;;;;;;;;;;;;;;;
- return %instance
- }
- alias Socket.DELETE {
- ;;;;;;;;;;;;;;;;;;;;;;
- ; Do Destroying here ;
- ;;;;;;;;;;;;;;;;;;;;;;
- if ($sock($1).status) sockclose $1
- return $Class($1).DELETE
- }
- ;;;;;;;;;;;;;;;;;;;;;;;;;;
- ; Default Event Handlers ;
- ;;;;;;;;;;;;;;;;;;;;;;;;;;
- alias Socket.DefaultSockOpen {
- if ($Window(@Debug)) echo @Debug Successfully connected to $sock($sockname).ip $+ : $+ $sock($sockname).port
- }
- alias Socket.DefaultSockClose {
- if ($Window(@Debug)) echo @Debug Successfully disconnected from $sock($sockname).ip $+ : $+ $sock($sockname).port
- $Socket($1).delete
- }
- alias Socket.DefaultSockRead {
- sockread %temp
- if ($window(@Debug)) echo @Debug %temp
- }
- alias Socket.DefaultSockAccept {
- }
- alias Socket.DefaultSockErr {
- sockclose $1
- }
- alias SockErr {
- bset -t &SockErr $1-
- }
- ;;;;;;;;;;;;;;;;;;;;
- ; Trigger Handlers ;
- ;;;;;;;;;;;;;;;;;;;;
- on 1:sockopen:*: $Class($left($sockname,-1),OPEN).getv $left($sockname,-1)
- on 1:sockclose:*: $Class($left($sockname,-1),CLOSE).getv $left($sockname,-1)
- on 1:socklisten:*: $Class($left($sockname,-1),ACCEPT).getv $left($sockname,-1)
- on 1:sockread:*: $Class($left($sockname,-1),READ).getv $left($sockname,-1)
- ;;;;;;;;;;;;;;
- ; End Socket ;
- ;;;;;;;;;;;;;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement