1
0

connection.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 JNL_Connection object, optionally specifying a JNL_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. ** JNL_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. ** JNL_Connection::STATE_NOCONNECTION
  22. ** - no connection has been made yet. call connect() already! :)
  23. ** JNL_Connection::STATE_RESOLVING
  24. ** - the connection is still waiting for a JNL_AsycnDNS to resolve the
  25. ** host.
  26. ** JNL_Connection::STATE_CONNECTING
  27. ** - the asynchronous call to connect() is still running.
  28. ** JNL_Connection::STATE_CONNECTED
  29. ** - the connection has connected, all is well.
  30. ** JNL_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. ** JNL_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 _CONNECTION_H_
  55. #define _CONNECTION_H_
  56. #include "netinc.h"
  57. #include "asyncdns.h"
  58. #include "../nu/RingBuffer.h"
  59. #include "jnetlib_defines.h"
  60. #include <stddef.h>
  61. #include "nswasabi/ReferenceCounted.h"
  62. #if defined(_MSC_VER) && (_MSC_VER < 1200)
  63. typedef int intptr_t;
  64. #endif
  65. #define PACKET_SIZE 16384
  66. class JNL_Connection : private Filler, private Drainer, public ReferenceCountedBase<JNL_Connection>
  67. {
  68. public:
  69. typedef enum
  70. {
  71. STATE_ERROR = JNL_CONNECTION_STATE_ERROR,
  72. STATE_NOCONNECTION = JNL_CONNECTION_STATE_NOCONNECTION,
  73. STATE_RESOLVING = JNL_CONNECTION_STATE_RESOLVING,
  74. STATE_CONNECTING = JNL_CONNECTION_STATE_CONNECTING,
  75. STATE_CONNECTED = JNL_CONNECTION_STATE_CONNECTED,
  76. STATE_CLOSING = JNL_CONNECTION_STATE_CLOSING,
  77. STATE_CLOSED = JNL_CONNECTION_STATE_CLOSED,
  78. STATE_RESOLVED = JNL_CONNECTION_STATE_RESOLVED,
  79. } state;
  80. /*
  81. ** Joshua Teitelbaum, 1/27/2006 adding virtual
  82. */
  83. JNL_Connection();
  84. JNL_Connection(JNL_AsyncDNS *dns, size_t sendbufsize, size_t recvbufsize);
  85. virtual ~JNL_Connection();
  86. void open( JNL_AsyncDNS *dns = JNL_AUTODNS, size_t sendbufsize = 8192, size_t recvbufsize = 8192 );
  87. void connect( const char *hostname, int port );
  88. virtual void connect( SOCKET sock, sockaddr *addr, socklen_t length /* of addr */ ); // used by the listen object, usually not needed by users.
  89. int set_recv_buffer_size(size_t new_buffer_size);
  90. /*
  91. ** Joshua Teitelbaum 2/2/2006
  92. ** Need to make this virtual to ensure SSL can init properly
  93. */
  94. 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 );
  95. int get_state() { return m_state; }
  96. char *get_errstr() { return m_errorstr; }
  97. void close( int quick = 0 );
  98. void flush_send( void ) { send_buffer.clear(); }
  99. size_t send_bytes_in_queue( void );
  100. size_t send_bytes_available( void );
  101. int send( const void *data, size_t length ); // returns -1 if not enough room
  102. inline int send_bytes( const void *data, size_t length ) { return send( data, length ); }
  103. int send_string( const char *line ); // returns -1 if not enough room
  104. size_t recv_bytes_available( void );
  105. size_t recv_bytes( void *data, size_t maxlength ); // returns actual bytes read
  106. unsigned int recv_int( void );
  107. int recv_lines_available( void );
  108. int recv_line( char *line, size_t maxlength ); // returns 0 if the line was terminated with a \r or \n, 1 if not.
  109. // (i.e. if you specify maxlength=10, and the line is 12 bytes long
  110. // it will return 1. or if there is no \r or \n and that's all the data
  111. // the connection has.)
  112. size_t peek_bytes( void *data, size_t maxlength ); // returns bytes peeked
  113. unsigned long get_interface( void ); // this returns the interface the connection is on
  114. unsigned long get_remote( void ); // remote host ip.
  115. unsigned short get_remote_port( void ); // this returns the remote port of connection
  116. void set_dns( JNL_AsyncDNS *dns );
  117. void reuse();
  118. protected:
  119. SOCKET m_socket;
  120. unsigned short m_remote_port;
  121. RingBuffer recv_buffer;
  122. RingBuffer send_buffer;
  123. addrinfo *saddr;
  124. sockaddr *address;
  125. char m_host[256];
  126. JNL_AsyncDNS *m_dns;
  127. bool m_dns_owned;
  128. state m_state;
  129. char *m_errorstr;
  130. /*
  131. ** Joshua Teitelbaum 1/27/2006 Adding new BSD socket analogues for SSL compatibility
  132. */
  133. virtual void socket_shutdown();
  134. virtual ssize_t socket_recv( char *buf, size_t len, int options );
  135. virtual ssize_t socket_send( const char *buf, size_t len, int options );
  136. virtual int socket_connect();
  137. virtual void on_socket_connected();
  138. private:
  139. void init(); // constructor helper function
  140. // functions for RingBuffer
  141. size_t Read( void *dest, size_t len ) override;
  142. size_t Write( const void *dest, size_t len ) override;
  143. };
  144. #endif // _Connection_H_