1
0

wac_network_connection.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. ** JNetLib
  3. ** Copyright (C) 2000-2007 Nullsoft, Inc.
  4. ** Author: Justin Frankel
  5. ** File: connection.h - JNL TCP connection interface
  6. ** License: see jnetlib.h
  7. **
  8. ** Usage:
  9. ** 1. Create a WAC_Network_Connection object, optionally specifying a wa::Components::WAC_Network_AsyncDNS
  10. ** object to use (or NULL for none, or WAC_NETWORK_CONNECTION_AUTODNS for auto),
  11. ** and the send and receive buffer sizes.
  12. ** 2. Call connect() to have it connect to a host/port (the hostname will be
  13. ** resolved if possible).
  14. ** 3. call run() with the maximum send/recv amounts, and optionally parameters
  15. ** so you can tell how much has been send/received. You want to do this a lot, while:
  16. ** 4. check get_state() to check the state of the connection. The states are:
  17. ** WAC_Network_Connection::STATE_ERROR
  18. ** - an error has occured on the connection. the connection has closed,
  19. ** and you can no longer write to the socket (there still might be
  20. ** data in the receive buffer - use recv_bytes_available()).
  21. ** WAC_Network_Connection::STATE_NOCONNECTION
  22. ** - no connection has been made yet. call connect() already! :)
  23. ** WAC_Network_Connection::STATE_RESOLVING
  24. ** - the connection is still waiting for a JNL_AsycnDNS to resolve the
  25. ** host.
  26. ** WAC_Network_Connection::STATE_CONNECTING
  27. ** - the asynchronous call to connect() is still running.
  28. ** WAC_Network_Connection::STATE_CONNECTED
  29. ** - the connection has connected, all is well.
  30. ** WAC_Network_Connection::STATE_CLOSING
  31. ** - the connection is closing. This happens after a call to close,
  32. ** without the quick parameter set. This means that the connection
  33. ** will close once the data in the send buffer is sent (data could
  34. ** still be being received when it would be closed). After it is
  35. ** closed, the state will transition to:
  36. ** WAC_Network_Connection::STATE_CLOSED
  37. ** - the connection has closed, generally without error. There still
  38. ** might be data in the receieve buffer, use recv_bytes_available().
  39. ** 5. Use send() and send_string() to send data. You can use
  40. ** send_bytes_in_queue() to see how much has yet to go out, or
  41. ** send_bytes_available() to see how much you can write. If you use send()
  42. ** or send_string() and not enough room is available, both functions will
  43. ** return error ( < 0)
  44. ** 6. Use recv() and recv_line() to get data. If you want to see how much data
  45. ** there is, use recv_bytes_available() and recv_lines_available(). If you
  46. ** call recv() and not enough data is available, recv() will return how much
  47. ** data was actually read. See comments at the function defs.
  48. **
  49. ** 7. To close, call close(1) for a quick close, or close() for a close that will
  50. ** make the socket close after sending all the data sent.
  51. **
  52. ** 8. delete ye' ol' object.
  53. */
  54. #ifndef NULLSOFT_WAC_NETWORK_CONNECTION_H
  55. #define NULLSOFT_WAC_NETWORK_CONNECTION_H
  56. #include "netinc.h"
  57. #include "wac_network_dns.h"
  58. #include "wac_network_dns_api.h"
  59. #include "wac_network_connection_api.h"
  60. #include "../nu/RingBuffer.h"
  61. #include <stddef.h>
  62. #if defined(_MSC_VER) && (_MSC_VER < 1200)
  63. typedef int intptr_t;
  64. #endif
  65. class WAC_Network_Connection : public api_connection, private Filler, private Drainer
  66. {
  67. public:
  68. typedef enum
  69. {
  70. STATE_ERROR = CONNECTION_STATE_ERROR,
  71. STATE_NOCONNECTION = CONNECTION_STATE_NOCONNECTION,
  72. STATE_RESOLVING = CONNECTION_STATE_RESOLVING,
  73. STATE_CONNECTING = CONNECTION_STATE_CONNECTING,
  74. STATE_CONNECTED = CONNECTION_STATE_CONNECTED,
  75. STATE_CLOSING = CONNECTION_STATE_CLOSING,
  76. STATE_CLOSED = CONNECTION_STATE_CLOSED,
  77. STATE_RESOLVED = CONNECTION_STATE_RESOLVED,
  78. } state;
  79. /*
  80. ** Joshua Teitelbaum, 1/27/2006 adding virtual
  81. */
  82. WAC_Network_Connection();
  83. WAC_Network_Connection( api_dns *dns, size_t sendbufsize, size_t recvbufsize );
  84. virtual ~WAC_Network_Connection();
  85. void open( api_dns *dns = API_DNS_AUTODNS, size_t sendbufsize = 8192, size_t recvbufsize = 8192 );
  86. void connect( const char *hostname, int port );
  87. virtual void connect( SOCKET sock, sockaddr *addr, socklen_t length /* of addr */ ); // used by the listen object, usually not needed by users.
  88. int set_recv_buffer_size( size_t new_buffer_size );
  89. /*
  90. ** Joshua Teitelbaum 2/2/2006
  91. ** Need to make this virtual to ensure SSL can init properly
  92. */
  93. virtual void run( size_t max_send_bytes = -1, size_t max_recv_bytes = -1, size_t *bytes_sent = NULL, size_t *bytes_rcvd = NULL );
  94. int get_state()
  95. {
  96. return m_state;
  97. }
  98. char *get_errstr()
  99. {
  100. return m_errorstr;
  101. }
  102. void close( int quick = 0 );
  103. void flush_send( void )
  104. {
  105. send_buffer.clear();
  106. }
  107. size_t send_bytes_in_queue( void );
  108. size_t send_bytes_available( void );
  109. int send( const void *data, size_t length ); // returns -1 if not enough room
  110. inline int send_bytes( const void *data, size_t length )
  111. {
  112. return send( data, length );
  113. }
  114. int send_string( const char *line ); // returns -1 if not enough room
  115. size_t recv_bytes_available( void );
  116. size_t recv_bytes( void *data, size_t maxlength ); // returns actual bytes read
  117. unsigned int recv_int( void );
  118. int recv_lines_available( void );
  119. int recv_line( char *line, size_t maxlength ); // returns 0 if the line was terminated with a \r or \n, 1 if not.
  120. // (i.e. if you specify maxlength=10, and the line is 12 bytes long
  121. // it will return 1. or if there is no \r or \n and that's all the data
  122. // the connection has.)
  123. size_t peek_bytes( void *data, size_t maxlength ); // returns bytes peeked
  124. unsigned long get_interface( void ); // this returns the interface the connection is on
  125. unsigned long get_remote( void ); // remote host ip.
  126. unsigned short get_remote_port( void ); // this returns the remote port of connection
  127. void set_dns( api_dns *dns );
  128. void reuse();
  129. protected:
  130. SOCKET m_socket;
  131. unsigned short m_remote_port;
  132. RingBuffer recv_buffer;
  133. RingBuffer send_buffer;
  134. addrinfo *saddr;
  135. sockaddr *address;
  136. char m_host[ 256 ];
  137. api_dns *m_dns;
  138. bool m_dns_owned;
  139. state m_state;
  140. char *m_errorstr;
  141. /*
  142. ** Joshua Teitelbaum 1/27/2006 Adding new BSD socket analogues for SSL compatibility
  143. */
  144. virtual void socket_shutdown();
  145. virtual ssize_t socket_recv( char *buf, size_t len, int options );
  146. virtual ssize_t socket_send( const char *buf, size_t len, int options );
  147. virtual int socket_connect();
  148. virtual void on_socket_connected();
  149. private:
  150. void init(); // constructor helper function
  151. // functions for RingBuffer
  152. size_t Read( void *dest, size_t len ) override;
  153. size_t Write( const void *dest, size_t len ) override;
  154. protected:
  155. RECVS_DISPATCH;
  156. };
  157. #endif //!NULLSOFT_WAC_NETWORK_CONNECTION_H