author | Tero Marttila <terom@fixme.fi> |
Fri, 16 Jan 2009 22:03:49 +0200 | |
changeset 400 | d64bf28c4340 |
parent 381 | 9b35bc329d23 |
permissions | -rw-r--r-- |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
1 |
#ifndef NETWORK_TCP_HH |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
2 |
#define NETWORK_TCP_HH |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
3 |
|
400
d64bf28c4340
more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents:
381
diff
changeset
|
4 |
/** |
d64bf28c4340
more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents:
381
diff
changeset
|
5 |
* @file |
d64bf28c4340
more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents:
381
diff
changeset
|
6 |
* |
d64bf28c4340
more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents:
381
diff
changeset
|
7 |
* Simple message-based TCP implementation |
d64bf28c4340
more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents:
381
diff
changeset
|
8 |
*/ |
d64bf28c4340
more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents:
381
diff
changeset
|
9 |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
10 |
#include <ClanLib/core.h> |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
11 |
|
186 | 12 |
#include "Socket.hh" |
13 |
#include "Address.hh" |
|
14 |
#include "Packet.hh" |
|
200
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
15 |
#include "Buffer.hh" |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
16 |
|
286 | 17 |
// @{ |
18 |
/** |
|
19 |
* Initial input/output buffer size given to NetworkTCPTransport::in/out constructor |
|
20 |
* |
|
21 |
* @see NetworkBufferBase |
|
22 |
*/ |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
23 |
const size_t NETWORK_TCP_INITIAL_IN_BUF = 4096; |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
24 |
const size_t NETWORK_TCP_INITIAL_OUT_BUF = 0; |
286 | 25 |
// @} |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
26 |
|
286 | 27 |
/** |
400
d64bf28c4340
more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents:
381
diff
changeset
|
28 |
* A connected TCP socket, so either a client or a server's accept'd socket. Can be used to send/receive messages. |
d64bf28c4340
more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents:
381
diff
changeset
|
29 |
* |
d64bf28c4340
more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents:
381
diff
changeset
|
30 |
* Not instanted directly, use NetworkTCPServer/NetworkTCPClient. |
d64bf28c4340
more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents:
381
diff
changeset
|
31 |
* |
d64bf28c4340
more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents:
381
diff
changeset
|
32 |
* Most of the functionality is present in NetworkSocket/NetworkBuffer*, so mostly this just implements the |
d64bf28c4340
more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents:
381
diff
changeset
|
33 |
* higher-level logic for reading messages, triggering signals, driving connect/listen and maintaing the poll flags. |
286 | 34 |
*/ |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
35 |
class NetworkTCPTransport { |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
36 |
protected: |
286 | 37 |
/** |
38 |
* The SOCK_STREAM socket |
|
39 |
*/ |
|
378
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
286
diff
changeset
|
40 |
NetworkSocket *socket; |
286 | 41 |
|
42 |
/** |
|
43 |
* Our input buffer associated with socket |
|
44 |
*/ |
|
45 |
NetworkBufferInput in; |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
46 |
|
286 | 47 |
/** |
48 |
* Our output buffer associated with socket |
|
49 |
*/ |
|
200
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
50 |
NetworkBufferOutput out; |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
51 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
52 |
CL_SlotContainer slots; |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
53 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
54 |
public: |
286 | 55 |
/** |
56 |
* Construct this using the given socket |
|
57 |
*/ |
|
378
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
286
diff
changeset
|
58 |
NetworkTCPTransport (NetworkSocket *socket); |
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
286
diff
changeset
|
59 |
|
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
286
diff
changeset
|
60 |
/** |
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
286
diff
changeset
|
61 |
* Deletes the socket |
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
286
diff
changeset
|
62 |
*/ |
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
286
diff
changeset
|
63 |
~NetworkTCPTransport (void); |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
64 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
65 |
private: |
286 | 66 |
/** |
67 |
* Triggered when the socket is read for recv |
|
68 |
*/ |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
69 |
void on_read (void); |
286 | 70 |
|
71 |
/** |
|
72 |
* Triggered when the socket is read for send |
|
73 |
*/ |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
74 |
void on_write (void); |
286 | 75 |
|
76 |
/** |
|
77 |
* The packet-read signal |
|
78 |
*/ |
|
79 |
CL_Signal_v1<NetworkPacketInput&> _sig_packet; |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
80 |
|
286 | 81 |
/** |
82 |
* The disconnected-signal |
|
83 |
*/ |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
84 |
CL_Signal_v0 _sig_disconnect; |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
85 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
86 |
public: |
286 | 87 |
/** |
378
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
286
diff
changeset
|
88 |
* Get this TCP socket's local address |
286 | 89 |
*/ |
380 | 90 |
NetworkAddress get_local_address (void) { return socket->get_local_address(); } |
286 | 91 |
|
92 |
/** |
|
93 |
* Get this TCP socket's remote address |
|
94 |
*/ |
|
380 | 95 |
NetworkAddress get_remote_address (void) { return socket->get_remote_address(); } |
286 | 96 |
|
97 |
/** |
|
98 |
* Write the given packet to this socket output, buffering the data if need be |
|
99 |
* |
|
100 |
* @param packet the packet to send |
|
101 |
*/ |
|
200
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
102 |
void write_packet (const NetworkPacketBuffer &packet); |
380 | 103 |
|
104 |
/** |
|
105 |
* Handle disconnect, disable socket |
|
106 |
*/ |
|
107 |
void handle_disconnect (void); |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
108 |
|
286 | 109 |
/** |
110 |
* A full packet has been received from the remote end |
|
111 |
*/ |
|
200
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
112 |
CL_Signal_v1<NetworkPacketInput&>& sig_packet (void) { return _sig_packet; } |
286 | 113 |
|
114 |
/** |
|
115 |
* The connection has been lost (no specific error code given) |
|
116 |
*/ |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
117 |
CL_Signal_v0& sig_disconnect (void) { return _sig_disconnect; } |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
118 |
}; |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
119 |
|
286 | 120 |
/** |
121 |
* A TCP server, listening on the given address, creating NetworkTCPTransports for clients that connect |
|
122 |
*/ |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
123 |
class NetworkTCPServer { |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
124 |
private: |
286 | 125 |
/** |
126 |
* The listen() socket |
|
127 |
*/ |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
128 |
NetworkSocket socket; |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
129 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
130 |
CL_SlotContainer slots; |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
131 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
132 |
public: |
286 | 133 |
/** |
400
d64bf28c4340
more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents:
381
diff
changeset
|
134 |
* Listen on the specific endpoint for incoming client connections. \a listen_addr may be a hostname, a |
d64bf28c4340
more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents:
381
diff
changeset
|
135 |
* suitable address will be chosen. |
286 | 136 |
*/ |
381
9b35bc329d23
separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents:
380
diff
changeset
|
137 |
NetworkTCPServer (const NetworkEndpoint &listen_addr); |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
138 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
139 |
private: |
286 | 140 |
/** |
141 |
* The socket is read for read() |
|
142 |
*/ |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
143 |
void on_accept (void); |
286 | 144 |
|
145 |
/** |
|
146 |
* The on_accept signal |
|
147 |
*/ |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
148 |
CL_Signal_v1<NetworkTCPTransport *> _sig_client; |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
149 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
150 |
protected: |
286 | 151 |
/** |
152 |
* Called by on_accept to build the NetworkTCPTransport, can be used to override what class gets used. |
|
153 |
* |
|
154 |
* By default, this just constructs a new NetworkTCPTransport and returns it |
|
155 |
* |
|
156 |
* @param socket the socket returned by accept() |
|
157 |
*/ |
|
378
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
286
diff
changeset
|
158 |
virtual NetworkTCPTransport* buildTransport (NetworkSocket *socket); |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
159 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
160 |
public: |
286 | 161 |
/** |
162 |
* A new client has connected, and a NetworkTCPTransport has been connected for it |
|
163 |
*/ |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
164 |
CL_Signal_v1<NetworkTCPTransport *>& sig_client (void) { return _sig_client; } |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
165 |
}; |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
166 |
|
286 | 167 |
/** |
168 |
* A TCP Client that connects to the given address |
|
169 |
*/ |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
170 |
class NetworkTCPClient : public NetworkTCPTransport { |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
171 |
public: |
286 | 172 |
/** |
400
d64bf28c4340
more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents:
381
diff
changeset
|
173 |
* Create a NetworkTCPTransport, and then connect our socket to the given remote endpoint |
d64bf28c4340
more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents:
381
diff
changeset
|
174 |
* |
d64bf28c4340
more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents:
381
diff
changeset
|
175 |
* This is address-family-agnostic, and fully supports endpoints with multiple addresses. They will be tried in |
d64bf28c4340
more documentation tweaking, all Network/ files now have a @file comment. Fix Platform.h -> Platform.hh, and Buffer.hh + Packet.cc
Tero Marttila <terom@fixme.fi>
parents:
381
diff
changeset
|
176 |
* turn until one works. |
286 | 177 |
* |
178 |
* @param connect_addr the address to connect to |
|
179 |
*/ |
|
381
9b35bc329d23
separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents:
380
diff
changeset
|
180 |
NetworkTCPClient (const NetworkEndpoint &connect_addr); |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
181 |
}; |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
182 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
183 |
#endif /* NETWORK_TCP_HH */ |