Debian Codegraph @

Contrib/NSISdl/connection.h

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