1
0

udpconnection.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. ** JNetLib
  3. ** Copyright (C) 2000-2001 Nullsoft, Inc.
  4. ** Author: Justin Frankel
  5. ** File: udpconnection.h - JNL UDP 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 _UDPCONNECTION_H_
  55. #define _UDPCONNECTION_H_
  56. #include "asyncdns.h"
  57. #include "nu/RingBuffer.h"
  58. #include "jnetlib_defines.h"
  59. #define JNL_DEFAULT_BUFFER_SIZE 8192
  60. class JNL_UDPConnection : private Drainer, private Filler
  61. {
  62. public:
  63. typedef enum
  64. {
  65. STATE_ERROR = JNL_CONNECTION_STATE_ERROR,
  66. STATE_NOCONNECTION = JNL_CONNECTION_STATE_NOCONNECTION,
  67. STATE_RESOLVING = JNL_CONNECTION_STATE_RESOLVING,
  68. STATE_CONNECTING = JNL_CONNECTION_STATE_CONNECTING,
  69. STATE_CONNECTED = JNL_CONNECTION_STATE_CONNECTED,
  70. STATE_CLOSING = JNL_CONNECTION_STATE_CLOSING,
  71. STATE_CLOSED = JNL_CONNECTION_STATE_CLOSED,
  72. STATE_RESOLVED = JNL_CONNECTION_STATE_RESOLVED,
  73. } state;
  74. JNL_UDPConnection();
  75. JNL_UDPConnection( unsigned short port, JNL_AsyncDNS *dns, int sendbufsize = JNL_DEFAULT_BUFFER_SIZE, int recvbufsize = JNL_DEFAULT_BUFFER_SIZE );
  76. ~JNL_UDPConnection();
  77. void open( JNL_AsyncDNS *dns = JNL_AUTODNS, size_t sendbufsize = JNL_DEFAULT_BUFFER_SIZE, size_t recvbufsize = JNL_DEFAULT_BUFFER_SIZE );
  78. void open( int incoming_socket, JNL_AsyncDNS *dns = JNL_AUTODNS, size_t sendbufsize = JNL_DEFAULT_BUFFER_SIZE, size_t recvbufsize = JNL_DEFAULT_BUFFER_SIZE );
  79. void setpeer( const char *hostname, int port );
  80. void setpeer( sockaddr *addr, socklen_t length /* of addr */ );
  81. void run( size_t max_send_bytes = -1, size_t max_recv_bytes = -1, size_t *bytes_sent = NULL, size_t *bytes_rcvd = NULL );
  82. int get_state() { return m_state; }
  83. const char *get_errstr() { return m_errorstr; }
  84. void close( int quick = 0 );
  85. void flush_send( void ) { send_buffer.clear(); }
  86. size_t send_bytes_in_queue( void );
  87. size_t send_bytes_available( void );
  88. int send( const void *data, size_t length ); // returns -1 if not enough room
  89. int send_string( const char *line ); // returns -1 if not enough room
  90. size_t recv_bytes_available( void );
  91. size_t recv_bytes( void *data, size_t maxlength ); // returns actual bytes read
  92. unsigned int recv_int( void );
  93. int recv_lines_available( void );
  94. int recv_line( char *line, size_t maxlength ); // returns 0 if the line was terminated with a \r or \n, 1 if not.
  95. // (i.e. if you specify maxlength=10, and the line is 12 bytes long
  96. // it will return 1. or if there is no \r or \n and that's all the data
  97. // the connection has.)
  98. size_t peek_bytes( void *data, size_t maxlength ); // returns bytes peeked
  99. int get_interface( sockaddr *sin, socklen_t *sin_length ); // this returns the interface the connection is on
  100. short get_remote_port( void ) { return m_remote_port; } // this returns the remote port of connection
  101. void get_last_recv_msg_addr( sockaddr **addr, socklen_t *len ) { *addr = (sockaddr *)&m_last_addr; *len = m_last_addr_len; }
  102. void set_ttl( uint8_t new_ttl );
  103. protected:
  104. uint8_t ttl;
  105. SOCKET m_socket;
  106. unsigned short m_remote_port;
  107. RingBuffer recv_buffer;
  108. RingBuffer send_buffer;
  109. sockaddr *address;
  110. socklen_t address_len;
  111. sockaddr_storage m_last_addr;
  112. socklen_t m_last_addr_len;
  113. addrinfo *saddr;
  114. char *m_host;
  115. JNL_AsyncDNS *m_dns;
  116. int m_dns_owned;
  117. state m_state;
  118. const char *m_errorstr;
  119. private:
  120. void init(); // constructor helper function
  121. // functions for RingBuffer
  122. size_t Read( void *dest, size_t len );
  123. size_t Write( const void *dest, size_t len );
  124. };
  125. #endif // _UDPConnection_H_