author | Tero Marttila <terom@fixme.fi> |
Fri, 16 Jan 2009 22:03:49 +0200 | |
changeset 400 | d64bf28c4340 |
parent 381 | 9b35bc329d23 |
child 431 | c6d7272a164b |
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_SESSION_HH |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
2 |
#define NETWORK_SESSION_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 |
* Support for a groups of clients/servers with associated TCP/UDP sockets |
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 <map> |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
11 |
#include <stdint.h> |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
12 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
13 |
// forward-declare |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
14 |
class NetworkSession; |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
15 |
|
286 | 16 |
/** |
17 |
* A NetworkSession puts each packet onto a specific channel, which can the be used to run multiple different modules |
|
18 |
* on top of a single session. |
|
19 |
* |
|
20 |
* NetworkChannelID zero is reserved for internal NetworkSession use |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
21 |
*/ |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
22 |
typedef uint16_t NetworkChannelID; |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
23 |
|
286 | 24 |
/** |
25 |
* Size of a NetworkSession's packet header: |
|
26 |
* uint16 channel_id |
|
203
3ec7ab40755f
send initial terrain data to clients using a new NETCHAN_TERRAIN_ARRAY channel using raw BigNetworkPacket
terom
parents:
202
diff
changeset
|
27 |
*/ |
3ec7ab40755f
send initial terrain data to clients using a new NETCHAN_TERRAIN_ARRAY channel using raw BigNetworkPacket
terom
parents:
202
diff
changeset
|
28 |
const size_t NETWORK_SESSION_HEADER_SIZE = sizeof(uint16_t); |
3ec7ab40755f
send initial terrain data to clients using a new NETCHAN_TERRAIN_ARRAY channel using raw BigNetworkPacket
terom
parents:
202
diff
changeset
|
29 |
|
186 | 30 |
#include "TCP.hh" |
31 |
#include "UDP.hh" |
|
32 |
#include "Node.hh" |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
33 |
|
286 | 34 |
/** |
35 |
* A NetworkSession provides TCP/UDP Server and Client functionality, representing remote NetworkSessions with |
|
36 |
* NetworkNodes. A NetworkSession can then communicate with its NetworkNodes using TCP or UDP NetworkPackets. |
|
37 |
*/ |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
38 |
class NetworkSession { |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
39 |
friend class NetworkNode; |
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 |
private: |
286 | 42 |
/** |
43 |
* The application's magic ID |
|
44 |
*/ |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
45 |
uint64_t magic; |
286 | 46 |
|
47 |
/** |
|
48 |
* Our TCP server, if we're in listen() mode |
|
49 |
*/ |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
50 |
NetworkTCPServer *tcp_srv; |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
51 |
|
286 | 52 |
/** |
53 |
* Our UDP server, if we're in listen() mode |
|
54 |
*/ |
|
55 |
NetworkUDP *udp_srv; |
|
56 |
||
57 |
/** |
|
58 |
* Our UDP client, if we're in connect() mode |
|
59 |
*/ |
|
60 |
NetworkUDP *udp_client; |
|
61 |
||
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
62 |
CL_SlotContainer slots; |
286 | 63 |
|
64 |
/** |
|
65 |
* A map of NetworkAddress -> NetworkNode, manipulated when TCP connections are established/broken down, |
|
66 |
* and used to map UDP packets to their NetworkNode |
|
67 |
*/ |
|
68 |
std::map<NetworkAddress, NetworkNode*> nodes; |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
69 |
|
286 | 70 |
/** |
71 |
* A map of NetworkChannelID -> signal, used to signal our users when we receieve packets |
|
72 |
*/ |
|
200
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
73 |
std::map<NetworkChannelID, CL_Signal_v2<NetworkPacketInput&, NetworkNode *> > _map_sig_chan_message; |
185
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 |
/** |
77 |
* Construct an idle NetworkSession using the given application magic, which should be unique to tell different |
|
78 |
* applications apart from each other. |
|
79 |
* |
|
80 |
* @param magic unique application magic |
|
81 |
*/ |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
82 |
NetworkSession (uint64_t magic); |
286 | 83 |
|
84 |
/** |
|
85 |
* Have the NetworkSession enter server mode, listening on the given address using both TCP and UDP |
|
86 |
* |
|
87 |
* @param addr local address to listen on |
|
88 |
*/ |
|
381
9b35bc329d23
separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents:
286
diff
changeset
|
89 |
void listen (const NetworkEndpoint &addr); |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
90 |
|
286 | 91 |
/** |
92 |
* Have the NetworkSession enter client mode, establishing a TCP connection to the server, and then allocating |
|
93 |
* an UDP socket on the same local address as the TCP connection. |
|
94 |
* |
|
95 |
* @param addr remote address to connect to |
|
96 |
*/ |
|
381
9b35bc329d23
separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents:
286
diff
changeset
|
97 |
NetworkNode* connect (const NetworkEndpoint &addr); |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
98 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
99 |
protected: |
286 | 100 |
/** |
101 |
* Used to build a new NetworkNode by connect/on_tcp_client. Can be used to override what kind of NetworkNodes |
|
102 |
* get created. Type tells what kind of node this is. |
|
103 |
* |
|
104 |
* @param tcp the TCP transport for this node |
|
105 |
* @param udp the UDP socket to use for this node |
|
106 |
* @param addr the remote address |
|
107 |
* @param type the type of node |
|
108 |
* @see NetworkNodeType |
|
109 |
*/ |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
110 |
virtual NetworkNode *build_node (NetworkTCPTransport *tcp, NetworkUDP *udp, const NetworkAddress &addr, enum NetworkNodeType type); |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
111 |
|
286 | 112 |
/** |
113 |
* A NetworkNode's TCP connection has failed. Removes the node from our nodes-map (using node->getRemoteAddress) |
|
114 |
* |
|
115 |
* @param node the node that has disconnected |
|
116 |
*/ |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
117 |
void handle_disconnect (NetworkNode *node); |
286 | 118 |
|
119 |
/** |
|
120 |
* We have received a NetworkPacket from the given node (either TCP or UDP, we don't know) |
|
121 |
* |
|
122 |
* @param pkt the NetworkPacket that we received |
|
123 |
* @param node the node that sent it |
|
124 |
*/ |
|
200
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
125 |
void handle_message (NetworkPacketInput &pkt, NetworkNode *node); |
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 |
* Our tcp_srv has accept()'d a new client. |
|
130 |
* |
|
131 |
* Create a NetworkNode using build_node and udp_srv, add it to our node-map, and trigger sig_node_connected |
|
132 |
*/ |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
133 |
void on_tcp_client (NetworkTCPTransport *client); |
286 | 134 |
|
135 |
/** |
|
136 |
* Our udp_srv has recv()'d a NetworkPacket. |
|
137 |
* |
|
138 |
* Map it to a NetworkNode using our node-map and call handle_message |
|
139 |
*/ |
|
200
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
140 |
void on_udp_packet (NetworkPacketInput &pkt, const NetworkAddress &addr); |
286 | 141 |
|
142 |
/** |
|
143 |
* New-client signal |
|
144 |
*/ |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
145 |
CL_Signal_v1<NetworkNode*> _sig_node_connected; |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
146 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
147 |
public: |
286 | 148 |
/** |
149 |
* Send the given NetworkPacket to all our nodes using the given NetworkChannelID, using TCP if reliable, UDP otherwise. |
|
150 |
* |
|
151 |
* @param channel_id the NetworkChannelID to use |
|
152 |
* @param pkt the NetworkPacket to send |
|
153 |
* @param reliable Whether to use TCP or UDP |
|
154 |
*/ |
|
200
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
155 |
void send_all (NetworkChannelID channel_id, const NetworkPacketBuffer &pkt, bool reliable = true); |
286 | 156 |
|
157 |
/** |
|
158 |
* Like send_all, but do not send the packet to the specified node. If node is NULL, this behaves like |
|
159 |
* send_all. |
|
160 |
* |
|
161 |
* @see send_all |
|
162 |
*/ |
|
200
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
163 |
void send_all_except (NetworkChannelID channel_id, const NetworkPacketBuffer &pkt, const NetworkNode *node, bool reliable = true); |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
164 |
|
286 | 165 |
/** |
166 |
* A new node has connected to us |
|
167 |
*/ |
|
168 |
CL_Signal_v1<NetworkNode*>& sig_node_connected (void) { return _sig_node_connected; } |
|
202 | 169 |
|
286 | 170 |
/** |
171 |
* We have received a NetworkPacket from a NetworkNode on the given NetworkChannelID |
|
172 |
*/ |
|
200
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
186
diff
changeset
|
173 |
CL_Signal_v2<NetworkPacketInput&, NetworkNode *>& sig_chan_message (NetworkChannelID cid) { return _map_sig_chan_message[cid]; } |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
174 |
}; |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
175 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
176 |
#endif /* NETWORK_SESSION_HH */ |