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