|
@@ -10,13 +10,15 @@ Namespace std.socket
|
|
|
|
|
|
Extern
|
|
Extern
|
|
|
|
|
|
|
|
+'Note: should probably just make this an extern bbSocket struct, ala bbProcess.
|
|
|
|
+
|
|
#rem monkeydoc @hidden
|
|
#rem monkeydoc @hidden
|
|
#end
|
|
#end
|
|
Function socket_connect:Int( hostname:String,service:String )="bbSocket::connect"
|
|
Function socket_connect:Int( hostname:String,service:String )="bbSocket::connect"
|
|
|
|
|
|
#rem monkeydoc @hidden
|
|
#rem monkeydoc @hidden
|
|
#end
|
|
#end
|
|
-Function socket_listen:Int( service:String,queue:Int=128 )="bbSocket::listen"
|
|
|
|
|
|
+Function socket_listen:Int( service:String,queue:Int )="bbSocket::listen"
|
|
|
|
|
|
#rem monkeydoc @hidden
|
|
#rem monkeydoc @hidden
|
|
#end
|
|
#end
|
|
@@ -44,186 +46,144 @@ Function socket_getopt:Int( socket:Int,opt:String )="bbSocket::getopt"
|
|
|
|
|
|
Public
|
|
Public
|
|
|
|
|
|
-Class Socket Extends std.stream.Stream
|
|
|
|
-
|
|
|
|
- #rem monkeydoc True if socket has been closed.
|
|
|
|
- #end
|
|
|
|
- Property Eof:Bool() Override
|
|
|
|
-
|
|
|
|
- Return _socket<0
|
|
|
|
- End
|
|
|
|
|
|
+Struct Socket
|
|
|
|
|
|
- #rem monkeydoc Always 0.
|
|
|
|
|
|
+ #rem monkeydoc True if socket is currently open.
|
|
#end
|
|
#end
|
|
- Property Position:Int() Override
|
|
|
|
|
|
+ Property IsOpen:Bool()
|
|
|
|
|
|
- Return 0
|
|
|
|
|
|
+ Return _socket<>-1
|
|
End
|
|
End
|
|
|
|
|
|
- #rem monkeydoc Always -1.
|
|
|
|
|
|
+ #rem monkeydoc Accepts a new incoming connection on a listening socket.
|
|
|
|
+
|
|
|
|
+ Returns null if there was an error, otherwise blocks until an incoming connection has been made.
|
|
|
|
+
|
|
|
|
+ @return new incomnig connection or null if there was an error.
|
|
|
|
+
|
|
#end
|
|
#end
|
|
- Property Length:Int() Override
|
|
|
|
|
|
+ Method Accept:Socket()
|
|
|
|
+ If _socket=-1 Return Null
|
|
|
|
|
|
- Return -1
|
|
|
|
|
|
+ Local socket:=socket_accept( _socket )
|
|
|
|
+ If socket=-1 Return Null
|
|
|
|
+
|
|
|
|
+ Return New Socket( socket )
|
|
End
|
|
End
|
|
|
|
|
|
- #rem monkeydoc Closes the socket.
|
|
|
|
- #end
|
|
|
|
- Method OnClose() Override
|
|
|
|
- If _socket<0 Return
|
|
|
|
|
|
+ #rem monkeydoc Closes a socket.
|
|
|
|
|
|
- socket_close( _socket )
|
|
|
|
- _socket=0
|
|
|
|
- End
|
|
|
|
|
|
+ Once closed, a socket should not be used anymore.
|
|
|
|
+
|
|
|
|
+ #end
|
|
|
|
+ Method Close()
|
|
|
|
+ If _socket=-1 Return
|
|
|
|
|
|
- #rem monkeydoc No operation.
|
|
|
|
- #end
|
|
|
|
- Method Seek( position:Int ) Override
|
|
|
|
|
|
+ socket_close( _socket )
|
|
|
|
+ _socket=-1
|
|
End
|
|
End
|
|
|
|
+
|
|
|
|
+ #rem monkeydoc Sends data on a connected socket.
|
|
|
|
|
|
- #rem monkeydoc Reads data from the socket.
|
|
|
|
|
|
+ Writes `size` bytes to the socket.
|
|
|
|
|
|
- Reads at most `count` bytes from the socket.
|
|
|
|
|
|
+ Returns the number of bytes actually written.
|
|
|
|
|
|
- Returns 0 if the socket has been closed by the peer.
|
|
|
|
|
|
+ Can return less than `sizet` if the socket has been closed by the peer or if an error occured.
|
|
|
|
|
|
- Can return less than `count`, in which case you may have to read again if you know there's more data coming.
|
|
|
|
|
|
+ @param buf The memory buffer to write data from.
|
|
|
|
|
|
- @param buf The memory buffer to read data into.
|
|
|
|
|
|
+ @param size The number of bytes to write to the socket.
|
|
|
|
|
|
- @param count The number of bytes to read from the socket.
|
|
|
|
|
|
+ @return The number of bytes actually written.
|
|
|
|
|
|
- @return The number of bytes actually read.
|
|
|
|
-
|
|
|
|
#end
|
|
#end
|
|
- Method Read:Int( buf:Void Ptr,count:Int ) Override
|
|
|
|
- If _socket<0 Return 0
|
|
|
|
-
|
|
|
|
- Return socket_recv( _socket,buf,count )
|
|
|
|
|
|
+ Method Send:Int( data:Void Ptr,size:Int )
|
|
|
|
+ If _socket=-1 Return 0
|
|
|
|
+
|
|
|
|
+ Return socket_send( _socket,data,size )
|
|
End
|
|
End
|
|
|
|
|
|
- #rem monkeydoc Writes data to the socket.
|
|
|
|
|
|
+ #rem monkeydoc Receives data on a connected socket.
|
|
|
|
|
|
- Writes `count` bytes to the socket.
|
|
|
|
|
|
+ Reads at most `size` bytes from the socket.
|
|
|
|
|
|
- Returns the number of bytes actually written.
|
|
|
|
|
|
+ Returns 0 if the socket has been closed by the peer.
|
|
|
|
|
|
- Can return less than `count` if the socket has been closed by the peer or if an error occured.
|
|
|
|
|
|
+ Can return less than `size`, in which case you may have to read again if you know there's more data coming.
|
|
|
|
|
|
- @param buf The memory buffer to read data from.
|
|
|
|
|
|
+ @param buf The memory buffer to read data into.
|
|
|
|
|
|
- @param count The number of bytes to write to the socket.
|
|
|
|
|
|
+ @param size The number of bytes to read from the socket.
|
|
|
|
+
|
|
|
|
+ @return The number of bytes actually read.
|
|
|
|
|
|
- @return The number of bytes actually written.
|
|
|
|
-
|
|
|
|
#end
|
|
#end
|
|
- Method Write:Int( buf:Void Ptr,count:Int ) Override
|
|
|
|
- If _socket<0 Return 0
|
|
|
|
|
|
+ Method Receive:Int( data:Void Ptr,size:Int )
|
|
|
|
+ If _socket=-1 Return 0
|
|
|
|
|
|
- Return socket_send( _socket,buf,count )
|
|
|
|
|
|
+ Return socket_recv( _socket,data,size )
|
|
End
|
|
End
|
|
-
|
|
|
|
- #rem monkeydoc Sets a socket option.
|
|
|
|
|
|
|
|
|
|
+ #rem monkeydoc Sets a socket option.
|
|
|
|
+
|
|
Currently, only "TCP_NODELAY" is supported, which should be 1 to enable, 0 to disable.
|
|
Currently, only "TCP_NODELAY" is supported, which should be 1 to enable, 0 to disable.
|
|
|
|
|
|
- #end
|
|
|
|
- Method SetOption( name:String,value:Int )
|
|
|
|
- If _socket<0 Return
|
|
|
|
|
|
+ #end
|
|
|
|
+ Method SetOption( opt:String,value:Int )
|
|
|
|
+ If _socket=-1 Return
|
|
|
|
|
|
- socket_setopt( _socket,name,value )
|
|
|
|
|
|
+ socket_setopt( _socket,opt,value )
|
|
End
|
|
End
|
|
|
|
|
|
#rem monkeydoc Gets a socket option.
|
|
#rem monkeydoc Gets a socket option.
|
|
|
|
+ #end
|
|
|
|
+ Method GetOption:Int( opt:String )
|
|
|
|
+ If _socket=-1 Return -1
|
|
|
|
|
|
- Currently, only "TCP_NODELAY" is supported.
|
|
|
|
-
|
|
|
|
- #end
|
|
|
|
- Method GetOption:Int( name:String )
|
|
|
|
- If _socket<0 Return -1
|
|
|
|
-
|
|
|
|
- Return socket_getopt( _socket,name )
|
|
|
|
|
|
+ Return socket_getopt( _socket,opt )
|
|
End
|
|
End
|
|
|
|
|
|
- #rem monkeydoc Connects to a host/service.
|
|
|
|
-
|
|
|
|
- Returns a new socket if successful, else null.
|
|
|
|
|
|
+ #rem monkeydoc Creates a connected socket.
|
|
|
|
|
|
- `service` can be an integer port number.
|
|
|
|
|
|
+ Attempts to connect to the host at `hostname` and service at `service` and returns a new connected socket if successful.
|
|
|
|
|
|
- @param hostname The name of the host to connect to.
|
|
|
|
|
|
+ Returns a closed socket upon failue.
|
|
|
|
|
|
- @param service The service or port to connect to.
|
|
|
|
|
|
+ @Return A new socket.
|
|
|
|
|
|
#end
|
|
#end
|
|
Function Connect:Socket( hostname:String,service:String )
|
|
Function Connect:Socket( hostname:String,service:String )
|
|
-
|
|
|
|
|
|
+
|
|
Local socket:=socket_connect( hostname,service )
|
|
Local socket:=socket_connect( hostname,service )
|
|
- If socket<0 Return Null
|
|
|
|
|
|
+ If socket=-1 Return Null
|
|
|
|
|
|
Return New Socket( socket )
|
|
Return New Socket( socket )
|
|
End
|
|
End
|
|
-
|
|
|
|
- Private
|
|
|
|
-
|
|
|
|
- Field _socket:Int
|
|
|
|
-
|
|
|
|
- Method New( socket:Int )
|
|
|
|
- _socket=socket
|
|
|
|
- End
|
|
|
|
-
|
|
|
|
-End
|
|
|
|
-
|
|
|
|
-Class SocketServer
|
|
|
|
-
|
|
|
|
- #rem monkeydoc Closes the server.
|
|
|
|
- #end
|
|
|
|
- Method Close()
|
|
|
|
- If _socket<0 Return
|
|
|
|
-
|
|
|
|
- socket_close( _socket )
|
|
|
|
- End
|
|
|
|
|
|
|
|
- #rem monkeydoc Accepts a new connection.
|
|
|
|
-
|
|
|
|
- Waits until a new incoming connection is available.
|
|
|
|
-
|
|
|
|
- @return A new connection, or null if there is a network error.
|
|
|
|
|
|
+ #rem monkeydoc Creates a server socket.
|
|
|
|
|
|
- #end
|
|
|
|
- Method Accept:Socket()
|
|
|
|
- If _socket<0 Return Null
|
|
|
|
|
|
+ Returns a new server socket listening at `service` if successful.
|
|
|
|
|
|
- Local socket:=socket_accept( _socket )
|
|
|
|
- If socket<0 Return Null
|
|
|
|
-
|
|
|
|
- Return New Socket( socket )
|
|
|
|
- End
|
|
|
|
|
|
+ Returns a closed socket upon failure.
|
|
|
|
|
|
- #rem monkeydoc Creates a server and starts listening.
|
|
|
|
-
|
|
|
|
- Returns a new server if successful, else null.
|
|
|
|
-
|
|
|
|
- `service` can be an integer port number.
|
|
|
|
-
|
|
|
|
- @param service The service or port to listen on.
|
|
|
|
-
|
|
|
|
- @param queue The number of incoming connections that can be queued.
|
|
|
|
|
|
+ @return A new socket.
|
|
|
|
|
|
#end
|
|
#end
|
|
- Function Listen:SocketServer( service:String,queue:Int=128 )
|
|
|
|
|
|
+ Function Listen:Socket( service:String,queue:Int=128 )
|
|
|
|
|
|
Local socket:=socket_listen( service,queue )
|
|
Local socket:=socket_listen( service,queue )
|
|
- If socket<0 Return Null
|
|
|
|
|
|
+ If socket=-1 Return Null
|
|
|
|
|
|
- Return New SocketServer( socket )
|
|
|
|
|
|
+ Return New Socket( socket )
|
|
End
|
|
End
|
|
|
|
|
|
Private
|
|
Private
|
|
|
|
|
|
- Field _socket:Int
|
|
|
|
|
|
+ Field _socket:Int=-1
|
|
|
|
|
|
Method New( socket:Int )
|
|
Method New( socket:Int )
|
|
|
|
+
|
|
_socket=socket
|
|
_socket=socket
|
|
End
|
|
End
|
|
|
|
|