socket.monkey2 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. Namespace std.socket
  2. #If __TARGET__="windows"
  3. #Import "<libWs2_32.a>"
  4. #Endif
  5. #Import "native/socket.cpp"
  6. #Import "native/socket.h"
  7. Extern
  8. #rem monkeydoc @hidden
  9. #end
  10. Function socket_connect:Int( hostname:String,service:String )="bbSocket::connect"
  11. #rem monkeydoc @hidden
  12. #end
  13. Function socket_listen:Int( service:String,queue:Int=128 )="bbSocket::listen"
  14. #rem monkeydoc @hidden
  15. #end
  16. Function socket_accept:Int( socket:Int )="bbSocket::accept"
  17. #rem monkeydoc @hidden
  18. #end
  19. Function socket_close( socket:Int )="bbSocket::close"
  20. #rem monkeydoc @hidden
  21. #end
  22. Function socket_send:Int( socket:Int,data:Void Ptr,size:Int )="bbSocket::send"
  23. #rem monkeydoc @hidden
  24. #end
  25. Function socket_recv:Int( socket:Int,data:Void Ptr,size:Int )="bbSocket::recv"
  26. #rem monkeydoc @hidden
  27. #end
  28. Function socket_setopt( socket:Int,opt:String,value:Int )="bbSocket::setopt"
  29. #rem monkeydoc @hidden
  30. #end
  31. Function socket_getopt:Int( socket:Int,opt:String )="bbSocket::getopt"
  32. Public
  33. Class Socket Extends std.stream.Stream
  34. #rem monkeydoc True if socket has been closed.
  35. #end
  36. Property Eof:Bool() Override
  37. Return _socket<0
  38. End
  39. #rem monkeydoc Always 0.
  40. #end
  41. Property Position:Int() Override
  42. Return 0
  43. End
  44. #rem monkeydoc Always -1.
  45. #end
  46. Property Length:Int() Override
  47. Return -1
  48. End
  49. #rem monkeydoc Closes the socket.
  50. #end
  51. Method Close:Void() Override
  52. If _socket<0 Return
  53. socket_close( _socket )
  54. _socket=0
  55. End
  56. #rem monkeydoc No operation.
  57. #end
  58. Method Seek( position:Int ) Override
  59. End
  60. #rem monkeydoc Reads data from the socket.
  61. Reads at most `count` bytes from the socket.
  62. Returns 0 if the socket has been closed by the peer.
  63. Can return less than `count`, in which case you may have to read again if you know there's more data coming.
  64. @param buf The memory buffer to read data into.
  65. @param count The number of bytes to read from the socket.
  66. @return The number of bytes actually read.
  67. #end
  68. Method Read:Int( buf:Void Ptr,count:Int ) Override
  69. If _socket<0 Return 0
  70. Return socket_recv( _socket,buf,count )
  71. End
  72. #rem monkeydoc Writes data to the socket.
  73. Writes `count` bytes to the socket.
  74. Returns the number of bytes actually written.
  75. Can return less than `count` if the socket has been closed by the peer or if an error occured.
  76. @param buf The memory buffer to read data from.
  77. @param count The number of bytes to write to the socket.
  78. @return The number of bytes actually written.
  79. #end
  80. Method Write:Int( buf:Void Ptr,count:Int ) Override
  81. If _socket<0 Return 0
  82. Return socket_send( _socket,buf,count )
  83. End
  84. #rem monkeydoc Sets a socket option.
  85. Currently, only "TCP_NODELAY" is supported, which should be 1 to enable, 0 to disable.
  86. #end
  87. Method SetOption( name:String,value:Int )
  88. If _socket<0 Return
  89. socket_setopt( _socket,name,value )
  90. End
  91. #rem monkeydoc Gets a socket option.
  92. Currently, only "TCP_NODELAY" is supported.
  93. #end
  94. Method GetOption:Int( name:String )
  95. If _socket<0 Return -1
  96. Return socket_getopt( _socket,name )
  97. End
  98. #rem monkeydoc Connects to a host/service.
  99. Returns a new socket if successful, else null.
  100. `service` can be an integer port number.
  101. @param hostname The name of the host to connect to.
  102. @param service The service or port to connect to.
  103. #end
  104. Function Connect:Socket( hostname:String,service:String )
  105. Local socket:=socket_connect( hostname,service )
  106. If socket<0 Return Null
  107. Return New Socket( socket )
  108. End
  109. Private
  110. Field _socket:Int
  111. Method New( socket:Int )
  112. _socket=socket
  113. End
  114. End
  115. Class SocketServer
  116. #rem monkeydoc Closes the server.
  117. #end
  118. Method Close()
  119. If _socket<0 Return
  120. socket_close( _socket )
  121. End
  122. #rem monkeydoc Accepts a new connection.
  123. Waits until a new incoming connection is available.
  124. @return A new connection, or null if there is a network error.
  125. #end
  126. Method Accept:Socket()
  127. If _socket<0 Return Null
  128. Local socket:=socket_accept( _socket )
  129. If socket<0 Return Null
  130. Return New Socket( socket )
  131. End
  132. #rem monkeydoc Creates a server and starts listening.
  133. Returns a new server if successful, else null.
  134. `service` can be an integer port number.
  135. @param service The service or port to listen on.
  136. @param queue The number of incoming connections that can be queued.
  137. #end
  138. Function Listen:SocketServer( service:String,queue:Int=128 )
  139. Local socket:=socket_listen( service,queue )
  140. If socket<0 Return Null
  141. Return New SocketServer( socket )
  142. End
  143. Private
  144. Field _socket:Int
  145. Method New( socket:Int )
  146. _socket=socket
  147. End
  148. End