author | terom |
Wed, 17 Dec 2008 00:40:22 +0000 | |
changeset 381 | 9b35bc329d23 |
parent 380 | d193dd1d8a7e |
child 400 | d64bf28c4340 |
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 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
4 |
#include <ClanLib/core.h> |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
5 |
|
186 | 6 |
#include "Socket.hh" |
7 |
#include "Address.hh" |
|
8 |
#include "Packet.hh" |
|
200
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
9 |
#include "Buffer.hh" |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
10 |
|
286 | 11 |
// @{ |
12 |
/** |
|
13 |
* Initial input/output buffer size given to NetworkTCPTransport::in/out constructor |
|
14 |
* |
|
15 |
* @see NetworkBufferBase |
|
16 |
*/ |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
17 |
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
|
18 |
const size_t NETWORK_TCP_INITIAL_OUT_BUF = 0; |
286 | 19 |
// @} |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
20 |
|
286 | 21 |
/** |
22 |
* A connected TCP socket, so either a client or a server-accept socket |
|
23 |
*/ |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
24 |
class NetworkTCPTransport { |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
25 |
protected: |
286 | 26 |
/** |
27 |
* The SOCK_STREAM socket |
|
28 |
*/ |
|
378
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
286
diff
changeset
|
29 |
NetworkSocket *socket; |
286 | 30 |
|
31 |
/** |
|
32 |
* Our input buffer associated with socket |
|
33 |
*/ |
|
34 |
NetworkBufferInput in; |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
35 |
|
286 | 36 |
/** |
37 |
* Our output buffer associated with socket |
|
38 |
*/ |
|
200
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
39 |
NetworkBufferOutput out; |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
40 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
41 |
CL_SlotContainer slots; |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
42 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
43 |
public: |
286 | 44 |
/** |
45 |
* Construct this using the given socket |
|
46 |
*/ |
|
378
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
286
diff
changeset
|
47 |
NetworkTCPTransport (NetworkSocket *socket); |
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
286
diff
changeset
|
48 |
|
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
286
diff
changeset
|
49 |
/** |
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
286
diff
changeset
|
50 |
* Deletes the socket |
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
286
diff
changeset
|
51 |
*/ |
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
286
diff
changeset
|
52 |
~NetworkTCPTransport (void); |
185
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 |
private: |
286 | 55 |
/** |
56 |
* Triggered when the socket is read for recv |
|
57 |
*/ |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
58 |
void on_read (void); |
286 | 59 |
|
60 |
/** |
|
61 |
* Triggered when the socket is read for send |
|
62 |
*/ |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
63 |
void on_write (void); |
286 | 64 |
|
65 |
/** |
|
66 |
* The packet-read signal |
|
67 |
*/ |
|
68 |
CL_Signal_v1<NetworkPacketInput&> _sig_packet; |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
69 |
|
286 | 70 |
/** |
71 |
* The disconnected-signal |
|
72 |
*/ |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
73 |
CL_Signal_v0 _sig_disconnect; |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
74 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
75 |
public: |
286 | 76 |
/** |
378
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
286
diff
changeset
|
77 |
* Get this TCP socket's local address |
286 | 78 |
*/ |
380 | 79 |
NetworkAddress get_local_address (void) { return socket->get_local_address(); } |
286 | 80 |
|
81 |
/** |
|
82 |
* Get this TCP socket's remote address |
|
83 |
*/ |
|
380 | 84 |
NetworkAddress get_remote_address (void) { return socket->get_remote_address(); } |
286 | 85 |
|
86 |
/** |
|
87 |
* Write the given packet to this socket output, buffering the data if need be |
|
88 |
* |
|
89 |
* @param packet the packet to send |
|
90 |
*/ |
|
200
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
91 |
void write_packet (const NetworkPacketBuffer &packet); |
380 | 92 |
|
93 |
/** |
|
94 |
* Handle disconnect, disable socket |
|
95 |
*/ |
|
96 |
void handle_disconnect (void); |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
97 |
|
286 | 98 |
/** |
99 |
* A full packet has been received from the remote end |
|
100 |
*/ |
|
200
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
101 |
CL_Signal_v1<NetworkPacketInput&>& sig_packet (void) { return _sig_packet; } |
286 | 102 |
|
103 |
/** |
|
104 |
* The connection has been lost (no specific error code given) |
|
105 |
*/ |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
106 |
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
|
107 |
}; |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
108 |
|
286 | 109 |
/** |
110 |
* A TCP server, listening on the given address, creating NetworkTCPTransports for clients that connect |
|
111 |
*/ |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
112 |
class NetworkTCPServer { |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
113 |
private: |
286 | 114 |
/** |
115 |
* The listen() socket |
|
116 |
*/ |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
117 |
NetworkSocket socket; |
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 |
CL_SlotContainer slots; |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
120 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
121 |
public: |
286 | 122 |
/** |
123 |
* Listen on the specific address. |
|
124 |
*/ |
|
381
9b35bc329d23
separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents:
380
diff
changeset
|
125 |
NetworkTCPServer (const NetworkEndpoint &listen_addr); |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
126 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
127 |
private: |
286 | 128 |
/** |
129 |
* The socket is read for read() |
|
130 |
*/ |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
131 |
void on_accept (void); |
286 | 132 |
|
133 |
/** |
|
134 |
* The on_accept signal |
|
135 |
*/ |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
136 |
CL_Signal_v1<NetworkTCPTransport *> _sig_client; |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
137 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
138 |
protected: |
286 | 139 |
/** |
140 |
* Called by on_accept to build the NetworkTCPTransport, can be used to override what class gets used. |
|
141 |
* |
|
142 |
* By default, this just constructs a new NetworkTCPTransport and returns it |
|
143 |
* |
|
144 |
* @param socket the socket returned by accept() |
|
145 |
*/ |
|
378
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
286
diff
changeset
|
146 |
virtual NetworkTCPTransport* buildTransport (NetworkSocket *socket); |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
147 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
148 |
public: |
286 | 149 |
/** |
150 |
* A new client has connected, and a NetworkTCPTransport has been connected for it |
|
151 |
*/ |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
152 |
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
|
153 |
}; |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
154 |
|
286 | 155 |
/** |
156 |
* A TCP Client that connects to the given address |
|
157 |
*/ |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
158 |
class NetworkTCPClient : public NetworkTCPTransport { |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
159 |
public: |
286 | 160 |
/** |
161 |
* Create a NetworkTCPTransport, and then connect our socket to the given address. |
|
162 |
* |
|
163 |
* @param connect_addr the address to connect to |
|
164 |
*/ |
|
381
9b35bc329d23
separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents:
380
diff
changeset
|
165 |
NetworkTCPClient (const NetworkEndpoint &connect_addr); |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
166 |
}; |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
167 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
168 |
#endif /* NETWORK_TCP_HH */ |