socketstream.bmx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. SuperStrict
  2. Rem
  3. bbdoc: Streams/Socket streams
  4. End Rem
  5. Module BRL.SocketStream
  6. ModuleInfo "Version: 1.07"
  7. ModuleInfo "Author: Mark Sibly"
  8. ModuleInfo "License: zlib/libpng"
  9. ModuleInfo "Copyright: Blitz Research Ltd"
  10. ModuleInfo "Modserver: BRL"
  11. ModuleInfo "History: 1.07"
  12. ModuleInfo "History: Fixed passing incorrect argument to AddrInfo()"
  13. ModuleInfo "History: 1.06"
  14. ModuleInfo "History: Module is now SuperStrict"
  15. ModuleInfo "History: 1.05 Release"
  16. ModuleInfo "History: CreateStream port handling fix documented"
  17. ModuleInfo "History: 1.04 Release"
  18. ModuleInfo "History: TSocketStream now just wraps a TSocket"
  19. Import BRL.Socket
  20. Import BRL.Stream
  21. Type TSocketStream Extends TStream
  22. Method Read:Long( buf:Byte Ptr,count:Long ) Override
  23. Return _socket.Recv( buf,Size_T(count) )
  24. End Method
  25. Method Write:Long( buf:Byte Ptr,count:Long ) Override
  26. Return _socket.Send( buf,Size_T(count) )
  27. End Method
  28. Method Eof:Int() Override
  29. If Not _socket Return True
  30. If _socket.Connected() Return False
  31. Close
  32. Return True
  33. End Method
  34. Method Close() Override
  35. If Not _socket Return
  36. If _autoClose _socket.Close
  37. _socket=Null
  38. End Method
  39. Method Socket:TSocket()
  40. Return _socket
  41. End Method
  42. Function Create:TSocketStream( socket:TSocket,autoClose:Int=True )
  43. Local t:TSocketStream=New TSocketStream
  44. t._socket=socket
  45. t._autoClose=autoClose
  46. Return t
  47. End Function
  48. Function CreateClient:TSocketStream( remoteHost$,remotePort:Int, family:Int = AF_INET_ )
  49. Local AddrInfo:TAddrInfo[] = AddrInfo(remoteHost, remotePort, family)
  50. If Not AddrInfo Return Null
  51. Local socket:TSocket=TSocket.CreateTCP()
  52. If socket
  53. If socket.Connect( AddrInfo[0] )
  54. Return Create( socket,True )
  55. EndIf
  56. socket.Close
  57. EndIf
  58. End Function
  59. Field _socket:TSocket,_autoClose:Int
  60. End Type
  61. Type TSocketStreamFactory Extends TStreamFactory
  62. Method CreateStream:TSocketStream( url:Object,proto$,path$,readable:Int,writeable:Int ) Override
  63. If proto$="tcp"
  64. Local i:Int=path.Find( ":",0 ),server$,port:Int
  65. If i>=0 Return TSocketStream.CreateClient( path[..i],Int(path[i+1..]) )
  66. Return TSocketStream.CreateClient( path,80 )
  67. EndIf
  68. End Method
  69. End Type
  70. New TSocketStreamFactory
  71. Rem
  72. bbdoc: Create a socket stream
  73. returns: A new socket stream
  74. about:
  75. A socket stream allows you to treat a socket as if it were a stream.
  76. If @autoClose is true, @socket will be automatically closed when the socket
  77. stream is closed. Otherwise, it is up to you to somehow close @socket at
  78. a later time.
  79. End Rem
  80. Function CreateSocketStream:TSocketStream( socket:TSocket,autoClose:Int=True )
  81. Return TSocketStream.Create( socket,autoClose )
  82. End Function
  83. Rem
  84. bbdoc: Get underlying socket from a socket stream
  85. returns: The socket used to create the socket stream
  86. End Rem
  87. Function SocketStreamSocket:TSocket( stream:TSocketStream )
  88. Return stream.Socket()
  89. End Function