wac_network_connection_api.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #ifndef NULLSOFT_COMPONENT_WAC_NETWORK_CONNECTION_H
  2. #define NULLSOFT_COMPONENT_WAC_NETWORK_CONNECTION_H
  3. #define PACKET_SIZE 16384
  4. #include "bfc/dispatch.h"
  5. #include "bfc/platform/types.h"
  6. #include "wac_network_dns_api.h"
  7. enum
  8. {
  9. CONNECTION_STATE_ERROR,
  10. CONNECTION_STATE_NOCONNECTION,
  11. CONNECTION_STATE_RESOLVING,
  12. CONNECTION_STATE_CONNECTING,
  13. CONNECTION_STATE_CONNECTED,
  14. CONNECTION_STATE_CLOSING,
  15. CONNECTION_STATE_CLOSED,
  16. CONNECTION_STATE_RESOLVED, // happens after RESOLVING, but going here for compatability
  17. };
  18. class api_dns;
  19. class NOVTABLE api_connection : public Dispatchable
  20. {
  21. protected:
  22. api_connection() {}
  23. ~api_connection() {}
  24. public:
  25. DISPATCH_CODES
  26. {
  27. API_CONNECTION_OPEN = 10,
  28. API_CONNECTION_CONNECT = 20,
  29. API_CONNECTION_RUN = 30,
  30. API_CONNECTION_GETSTATE = 40,
  31. API_CONNECTION_GETERROR = 50,
  32. API_CONNECTION_CLOSE = 60,
  33. API_CONNECTION_FLUSHSEND = 70,
  34. API_CONNECTION_GETSENDBYTESINQUEUE = 80,
  35. API_CONNECTION_GETSENDBYTESAVAILABLE = 90,
  36. API_CONNECTION_SEND = 100,
  37. API_CONNECTION_SENDBYTES = 110,
  38. API_CONNECTION_SENDSTRING = 120,
  39. API_CONNECTION_GETRECEIVEBYTESAVAILABLE = 130,
  40. API_CONNECTION_RECEIVEBYTES = 140,
  41. // API_CONNECTION_RECEIVEINTEGER = 150,
  42. API_CONNECTION_GETRECEIVELINESAVAILABLE = 160,
  43. API_CONNECTION_RECEIVELINE = 170,
  44. API_CONNECTION_PEEKBYTES = 180,
  45. API_CONNECTION_GETINTERFACE = 190,
  46. API_CONNECTION_GETREMOTEADDRESS = 200,
  47. API_CONNECTION_GETREMOTEPORT = 210,
  48. };
  49. void Open( api_dns *dns = API_DNS_AUTODNS, size_t sendbufsize = 8192, size_t recvbufsize = 8192 );
  50. void connect( char *hostname, int port );
  51. void run( int max_send_bytes = -1, int max_recv_bytes = -1, int *bytes_sent = NULL, int *bytes_rcvd = NULL );
  52. int get_state();
  53. char *GetError();
  54. void Close( int quick = 0 );
  55. void FlushSend( void );
  56. size_t GetSendBytesInQueue( void );
  57. size_t GetSendBytesAvailable( void );
  58. int send( const void *data, int length ); // returns -1 if not enough room
  59. int SendString( const char *line ); // returns -1 if not enough room
  60. size_t GetReceiveBytesAvailable( void );
  61. size_t recv_bytes( void *data, size_t maxlength ); // returns actual bytes read
  62. int GetReceiveLinesAvailable( void );
  63. int ReceiveLine( char *line, int maxlength ); // returns 0 if the line was terminated with a \r or \n, 1 if not.
  64. // (i.e. if you specify maxlength=10, and the line is 12 bytes long
  65. // it will return 1. or if there is no \r or \n and that's all the data
  66. // the connection has.)
  67. size_t PeekBytes( void *data, size_t maxlength ); // returns bytes peeked
  68. unsigned long GetInterface( void ); // this returns the interface the connection is on
  69. unsigned long GetRemoteAddress( void ); // remote host ip.
  70. short GetRemotePort( void ); // this returns the remote port of connection
  71. };
  72. inline void api_connection::Open( api_dns *dns, size_t sendbufsize, size_t recvbufsize )
  73. {
  74. _voidcall( API_CONNECTION_OPEN, dns, sendbufsize, recvbufsize );
  75. }
  76. inline void api_connection::connect( char *hostname, int port )
  77. {
  78. _voidcall( API_CONNECTION_CONNECT, hostname, port );
  79. }
  80. inline void api_connection::run( int max_send_bytes, int max_recv_bytes, int *bytes_sent, int *bytes_rcvd )
  81. {
  82. _voidcall( API_CONNECTION_RUN, max_send_bytes, max_recv_bytes, bytes_sent, bytes_rcvd );
  83. }
  84. inline int api_connection::get_state()
  85. {
  86. return _call( API_CONNECTION_GETSTATE, (int)0 );
  87. }
  88. inline char *api_connection::GetError()
  89. {
  90. return _call( API_CONNECTION_GETERROR, (char *)NULL );
  91. }
  92. inline void api_connection::Close( int quick )
  93. {
  94. _voidcall( API_CONNECTION_CLOSE, quick );
  95. }
  96. inline void api_connection::FlushSend( void )
  97. {
  98. _voidcall( API_CONNECTION_FLUSHSEND );
  99. }
  100. inline size_t api_connection::GetSendBytesInQueue( void )
  101. {
  102. return _call( API_CONNECTION_GETSENDBYTESINQUEUE, (size_t)0 );
  103. }
  104. inline size_t api_connection::GetSendBytesAvailable( void )
  105. {
  106. return _call( API_CONNECTION_GETSENDBYTESAVAILABLE, (size_t)0 );
  107. }
  108. inline int api_connection::send( const void *data, int length )
  109. {
  110. return _call( API_CONNECTION_SEND, (int)0, data, length );
  111. }
  112. inline int api_connection::SendString( const char *line )
  113. {
  114. return _call( API_CONNECTION_SENDSTRING, (int)0, line );
  115. }
  116. inline size_t api_connection::GetReceiveBytesAvailable( void )
  117. {
  118. return _call( API_CONNECTION_GETRECEIVEBYTESAVAILABLE, (size_t)0 );
  119. }
  120. inline size_t api_connection::recv_bytes( void *data, size_t maxlength )
  121. {
  122. return _call( API_CONNECTION_RECEIVEBYTES, (size_t)0, data, maxlength );
  123. }
  124. inline int api_connection::GetReceiveLinesAvailable( void )
  125. {
  126. return _call( API_CONNECTION_GETRECEIVELINESAVAILABLE, (int)0 );
  127. }
  128. inline int api_connection::ReceiveLine( char *line, int maxlength )
  129. {
  130. return _call( API_CONNECTION_RECEIVELINE, (int)0, line, maxlength );
  131. }
  132. inline size_t api_connection::PeekBytes( void *data, size_t maxlength )
  133. {
  134. return _call( API_CONNECTION_PEEKBYTES, (size_t)0, data, maxlength );
  135. }
  136. inline unsigned long api_connection::GetInterface( void )
  137. {
  138. return _call( API_CONNECTION_GETINTERFACE, (unsigned long)0 );
  139. }
  140. inline unsigned long api_connection::GetRemoteAddress( void )
  141. {
  142. return _call( API_CONNECTION_GETREMOTEADDRESS, (unsigned long)0 );
  143. }
  144. inline short api_connection::GetRemotePort( void )
  145. {
  146. return _call( API_CONNECTION_GETREMOTEPORT, (short)0 );
  147. }
  148. // {049E1B1E-56DF-46e0-B88F-E7E319F131DD}
  149. static const GUID connectionFactoryGUID =
  150. { 0x49e1b1e, 0x56df, 0x46e0, { 0xb8, 0x8f, 0xe7, 0xe3, 0x19, 0xf1, 0x31, 0xdd } };
  151. // {8F0B5D22-699B-486b-B39D-650E5B09C1F1}
  152. static const GUID sslConnectionFactoryGUID =
  153. { 0x8f0b5d22, 0x699b, 0x486b, { 0xb3, 0x9d, 0x65, 0xe, 0x5b, 0x9, 0xc1, 0xf1 } };
  154. #endif // !NULLSOFT_COMPONENT_WAC_NETWORK_CONNECTION_H