home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 March / CMCD0304.ISO / Software / Freeware / Programare / nullsoft / nsis20.exe / Contrib / NSISdl / connection.h < prev    next >
C/C++ Source or Header  |  2002-08-02  |  6KB  |  136 lines

  1. /*
  2. ** JNetLib
  3. ** Copyright (C) 2000-2001 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 JNL_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.  
  55. #ifndef _CONNECTION_H_
  56. #define _CONNECTION_H_
  57.  
  58. #include "asyncdns.h"
  59.  
  60. #define JNL_CONNECTION_AUTODNS ((JNL_AsyncDNS*)-1)
  61.  
  62. class JNL_Connection
  63. {
  64.   public:
  65.     typedef enum 
  66.     { 
  67.       STATE_ERROR, 
  68.       STATE_NOCONNECTION,
  69.       STATE_RESOLVING, 
  70.       STATE_CONNECTING, 
  71.       STATE_CONNECTED, 
  72.       STATE_CLOSING, 
  73.       STATE_CLOSED 
  74.     } state;
  75.  
  76.     JNL_Connection(JNL_AsyncDNS *dns=JNL_CONNECTION_AUTODNS, int sendbufsize=8192, int recvbufsize=8192);
  77.     ~JNL_Connection();
  78.  
  79.     void connect(char *hostname, int port);
  80.     void connect(int sock, struct sockaddr_in *loc=NULL); // used by the listen object, usually not needed by users.
  81.  
  82.     void run(int max_send_bytes=-1, int max_recv_bytes=-1, int *bytes_sent=NULL, int *bytes_rcvd=NULL);
  83.     int  get_state() { return m_state; }
  84.     char *get_errstr() { return m_errorstr; }
  85.  
  86.     void close(int quick=0);
  87.     void flush_send(void) { m_send_len=m_send_pos=0; }
  88.  
  89.     int send_bytes_in_queue(void);
  90.     int send_bytes_available(void);
  91.     int send(char *data, int length); // returns -1 if not enough room
  92.     int send_string(char *line);      // returns -1 if not enough room
  93.  
  94.  
  95.     int recv_bytes_available(void);
  96.     int recv_bytes(char *data, int maxlength); // returns actual bytes read
  97.     unsigned int recv_int(void);
  98.     int recv_lines_available(void);
  99.     int recv_line(char *line, int maxlength); // returns 0 if the line was terminated with a \r or \n, 1 if not.
  100.                                               // (i.e. if you specify maxlength=10, and the line is 12 bytes long
  101.                                               // it will return 1. or if there is no \r or \n and that's all the data
  102.                                               // the connection has.)
  103.     int peek_bytes(char *data, int maxlength); // returns bytes peeked
  104.  
  105.     unsigned long get_interface(void);        // this returns the interface the connection is on
  106.     unsigned long get_remote(void) { return m_saddr.sin_addr.s_addr; } // remote host ip.
  107.     short get_remote_port(void) { return m_remote_port; } // this returns the remote port of connection
  108.   
  109.   protected:
  110.     int  m_socket;
  111.     short m_remote_port;
  112.     char *m_recv_buffer;
  113.     char *m_send_buffer;
  114.     int m_recv_buffer_len;
  115.     int m_send_buffer_len;
  116.  
  117.     int  m_recv_pos;
  118.     int  m_recv_len;
  119.     int  m_send_pos;
  120.     int  m_send_len;
  121.  
  122.     struct sockaddr_in m_saddr;
  123.     char m_host[256];
  124.  
  125.     JNL_AsyncDNS *m_dns;
  126.     int m_dns_owned;
  127.  
  128.     state m_state;
  129.     char *m_errorstr;
  130.  
  131.     int getbfromrecv(int pos, int remove); // used by recv_line*
  132.  
  133. };
  134.  
  135. #endif // _Connection_H_
  136.