author | terom |
Tue, 16 Dec 2008 23:21:26 +0000 | |
changeset 380 | d193dd1d8a7e |
parent 378 | 5589abf5e61b |
child 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 |
|
186 | 2 |
#include "TCP.hh" |
3 |
#include "../Engine.hh" |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
4 |
|
200
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
5 |
/* |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
6 |
* NetworkTCPTransport |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
7 |
*/ |
378
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
263
diff
changeset
|
8 |
NetworkTCPTransport::NetworkTCPTransport (NetworkSocket *socket) : |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
9 |
socket(socket), in(socket, NETWORK_TCP_INITIAL_IN_BUF), out(socket, NETWORK_TCP_INITIAL_OUT_BUF) { |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
10 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
11 |
// connect signals |
378
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
263
diff
changeset
|
12 |
slots.connect(socket->sig_read(), this, &NetworkTCPTransport::on_read); |
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
263
diff
changeset
|
13 |
slots.connect(socket->sig_write(), this, &NetworkTCPTransport::on_write); |
380 | 14 |
|
15 |
// activate read polling, but leave write polling for later |
|
16 |
socket->set_poll_read(true); |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
17 |
} |
378
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
263
diff
changeset
|
18 |
|
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
263
diff
changeset
|
19 |
NetworkTCPTransport::~NetworkTCPTransport (void) { |
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
263
diff
changeset
|
20 |
// release socket |
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
263
diff
changeset
|
21 |
delete socket; |
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
263
diff
changeset
|
22 |
} |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
23 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
24 |
void NetworkTCPTransport::on_read (void) { |
203
3ec7ab40755f
send initial terrain data to clients using a new NETCHAN_TERRAIN_ARRAY channel using raw BigNetworkPacket
terom
parents:
200
diff
changeset
|
25 |
uint32_t length; |
200
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
26 |
char *buf_ptr; |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
27 |
|
227 | 28 |
try { |
29 |
// let the in stream read length-prefixed packets and pass them on to handle_packet |
|
30 |
do { |
|
31 |
// not enough data -> return and wait |
|
32 |
if (in.peek_data<uint32_t>(length, buf_ptr) == false) |
|
33 |
break; |
|
200
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
34 |
|
227 | 35 |
// allocate the NetworkPacketBuffer with the given buf_ptr/length |
36 |
NetworkPacketBuffer packet(buf_ptr, length, length); |
|
37 |
||
38 |
// pass the packet on |
|
39 |
_sig_packet(packet); |
|
40 |
||
41 |
// flush it |
|
42 |
in.flush_data<uint32_t>(); |
|
43 |
} while (true); |
|
44 |
||
45 |
} catch (NetworkSocketError &e) { |
|
46 |
// log and disconnect |
|
47 |
Engine::log(ERROR, "tcp.on_read") << "socket error: " << e.what(); |
|
48 |
||
380 | 49 |
handle_disconnect(); |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
50 |
} |
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 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
53 |
void NetworkTCPTransport::on_write (void) { |
227 | 54 |
try { |
380 | 55 |
// just flush the output buffer, and deactivate output polling if done |
56 |
if (!out.flush_write()) |
|
57 |
socket->set_poll_write(false); |
|
227 | 58 |
|
59 |
} catch (NetworkSocketError &e) { |
|
60 |
// log and disconnect |
|
61 |
Engine::log(ERROR, "tcp.on_write") << "socket error: " << e.what(); |
|
62 |
||
380 | 63 |
handle_disconnect(); |
227 | 64 |
} |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
65 |
} |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
66 |
|
200
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
67 |
void NetworkTCPTransport::write_packet (const NetworkPacketBuffer &packet) { |
203
3ec7ab40755f
send initial terrain data to clients using a new NETCHAN_TERRAIN_ARRAY channel using raw BigNetworkPacket
terom
parents:
200
diff
changeset
|
68 |
uint32_t prefix = packet.get_data_size(); |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
69 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
70 |
if (prefix != packet.get_data_size()) |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
71 |
throw CL_Error("send prefix overflow"); |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
72 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
73 |
try { |
380 | 74 |
// just write to the output buffer, but activate output polling if needed |
75 |
if (out.write_prefix((char *) packet.get_buf(), prefix)) |
|
76 |
socket->set_poll_write(true); |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
77 |
|
227 | 78 |
} catch (NetworkSocketError &e) { |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
79 |
const char *err = e.what(); |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
80 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
81 |
Engine::log(ERROR, "tcp.write_packet") << err; |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
82 |
|
263
8c999cf4c182
weapon projectile radiuses and fix network play (local_player == NULL, Rope releasing upon being hit
terom
parents:
246
diff
changeset
|
83 |
// XXX: these are not handled anywhere :( |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
84 |
throw; |
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 |
} |
380 | 87 |
|
88 |
void NetworkTCPTransport::handle_disconnect (void) { |
|
89 |
// disable events on our socket |
|
90 |
socket->set_poll_read(false); |
|
91 |
socket->set_poll_write(false); |
|
92 |
||
93 |
// signal |
|
94 |
_sig_disconnect(); |
|
95 |
} |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
96 |
|
378
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
263
diff
changeset
|
97 |
/* |
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
263
diff
changeset
|
98 |
* NetworkTCPServer |
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
263
diff
changeset
|
99 |
*/ |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
100 |
NetworkTCPServer::NetworkTCPServer (const NetworkAddress &listen_addr) : |
378
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
263
diff
changeset
|
101 |
socket(AF_UNSPEC, SOCK_STREAM) { |
246
687d9896763a
change mkcmake.sh to not use /tmp anymore, and fix some ClanLib idiocy
terom
parents:
227
diff
changeset
|
102 |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
103 |
// bind |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
104 |
socket.bind(listen_addr); |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
105 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
106 |
// assign slots |
378
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
263
diff
changeset
|
107 |
slots.connect(socket.sig_read(), this, &NetworkTCPServer::on_accept); |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
108 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
109 |
// listen |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
110 |
socket.listen(NETWORK_LISTEN_BACKLOG); |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
111 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
112 |
// use nonblocking sockets |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
113 |
socket.set_nonblocking(true); |
380 | 114 |
|
115 |
// activate polling |
|
116 |
socket.set_poll_read(true); |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
117 |
} |
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 |
void NetworkTCPServer::on_accept (void) { |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
120 |
// accept a new socket |
378
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
263
diff
changeset
|
121 |
NetworkSocket *client_sock = socket.accept(NULL); |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
122 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
123 |
// create a new NetworkTCPTransport |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
124 |
NetworkTCPTransport *client = buildTransport(client_sock); |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
125 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
126 |
// let our user handle it |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
127 |
_sig_client(client); |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
128 |
} |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
129 |
|
378
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
263
diff
changeset
|
130 |
NetworkTCPTransport* NetworkTCPServer::buildTransport (NetworkSocket *socket) { |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
131 |
return new NetworkTCPTransport(socket); |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
132 |
} |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
133 |
|
378
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
263
diff
changeset
|
134 |
/* |
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
263
diff
changeset
|
135 |
* NetworkTCPClient |
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
263
diff
changeset
|
136 |
*/ |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
137 |
NetworkTCPClient::NetworkTCPClient (const NetworkAddress &connect_addr) : |
378
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
263
diff
changeset
|
138 |
NetworkTCPTransport(new NetworkSocket(AF_UNSPEC, SOCK_STREAM)) { |
246
687d9896763a
change mkcmake.sh to not use /tmp anymore, and fix some ClanLib idiocy
terom
parents:
227
diff
changeset
|
139 |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
140 |
// connect |
378
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
263
diff
changeset
|
141 |
socket->connect(connect_addr); |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
142 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
143 |
// use nonblocking sockets |
378
5589abf5e61b
break the network code. Too late to set up a branch for this now
terom
parents:
263
diff
changeset
|
144 |
socket->set_nonblocking(true); |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
145 |
} |