1 | // |
---|
2 | // C++ Interface: ConnectionManager |
---|
3 | // |
---|
4 | // Description: The Class ConnectionManager manages the servers conenctions to the clients. |
---|
5 | // each connection is provided by a new process. communication between master process and |
---|
6 | // connection processes is provided by ... |
---|
7 | // |
---|
8 | // |
---|
9 | // Author: Oliver Scheuss |
---|
10 | // |
---|
11 | |
---|
12 | #include "ConnectionManager.h" |
---|
13 | |
---|
14 | namespace std{ |
---|
15 | bool operator< (ENetAddress a, ENetAddress b){ |
---|
16 | if(a.host <= b.host) |
---|
17 | return true; |
---|
18 | else |
---|
19 | return false; |
---|
20 | } |
---|
21 | } |
---|
22 | |
---|
23 | namespace network{ |
---|
24 | |
---|
25 | boost::thread_group network_threads; |
---|
26 | |
---|
27 | ConnectionManager::ConnectionManager(){ |
---|
28 | quit=false; |
---|
29 | bindAddress.host = ENET_HOST_ANY; |
---|
30 | bindAddress.port = NETWORK_PORT; |
---|
31 | } |
---|
32 | |
---|
33 | ConnectionManager::ConnectionManager(int port, std::string address){ |
---|
34 | quit=false; |
---|
35 | enet_address_set_host (& bindAddress, address.c_str()); |
---|
36 | bindAddress.port = NETWORK_PORT; |
---|
37 | } |
---|
38 | |
---|
39 | ConnectionManager::ConnectionManager(int port, const char *address){ |
---|
40 | quit=false; |
---|
41 | enet_address_set_host (& bindAddress, address); |
---|
42 | bindAddress.port = NETWORK_PORT; |
---|
43 | } |
---|
44 | |
---|
45 | ENetPacket *ConnectionManager::getPacket(ENetAddress &address){ |
---|
46 | if(!buffer.isEmpty()) |
---|
47 | return buffer.pop(address); |
---|
48 | else |
---|
49 | return NULL; |
---|
50 | } |
---|
51 | |
---|
52 | ENetPacket *ConnectionManager::getPacket(int &clientID){ |
---|
53 | ENetAddress address; |
---|
54 | ENetPacket *packet=getPacket(address); |
---|
55 | clientID=clientMap.find(address)->second; |
---|
56 | return packet; |
---|
57 | } |
---|
58 | |
---|
59 | bool ConnectionManager::queueEmpty(){ |
---|
60 | return buffer.isEmpty(); |
---|
61 | } |
---|
62 | |
---|
63 | void ConnectionManager::createListener(){ |
---|
64 | network_threads.create_thread(boost::bind(boost::mem_fn(&ConnectionManager::receiverThread), this)); |
---|
65 | // boost::thread thr(boost::bind(boost::mem_fn(&ConnectionManager::receiverThread), this)); |
---|
66 | return; |
---|
67 | } |
---|
68 | |
---|
69 | bool ConnectionManager::quitListener(){ |
---|
70 | quit=true; |
---|
71 | network_threads.join_all(); |
---|
72 | return true; |
---|
73 | } |
---|
74 | |
---|
75 | bool ConnectionManager::addPacket(ENetPacket *packet, ENetPeer *peer){ |
---|
76 | if(clientVector.size()==0) |
---|
77 | return false; |
---|
78 | if(enet_peer_send(peer, clientMap.find(peer->address)->second , packet)!=0) |
---|
79 | return false; |
---|
80 | return true; |
---|
81 | } |
---|
82 | |
---|
83 | bool ConnectionManager::addPacket(ENetPacket *packet, int clientID){ |
---|
84 | if(clientVector.size()==0) |
---|
85 | return false; |
---|
86 | if(enet_peer_send(&(peerMap.find(clientVector[clientID])->second), clientID, packet)!=0) |
---|
87 | return false; |
---|
88 | return true; |
---|
89 | } |
---|
90 | |
---|
91 | bool ConnectionManager::addPacketAll(ENetPacket *packet){ |
---|
92 | for(int i=0; i<clientVector.size(); i++){ |
---|
93 | if(enet_peer_send(&(peerMap.find(clientVector[i])->second), i, packet)!=0) |
---|
94 | return false; |
---|
95 | } |
---|
96 | return true; |
---|
97 | } |
---|
98 | |
---|
99 | bool ConnectionManager::sendPackets(ENetEvent *event){ |
---|
100 | if(server==NULL) |
---|
101 | return false; |
---|
102 | if(enet_host_service(server, event, NETWORK_SEND_WAIT)>=0) |
---|
103 | return true; |
---|
104 | else |
---|
105 | return false; |
---|
106 | } |
---|
107 | |
---|
108 | bool ConnectionManager::sendPackets(){ |
---|
109 | ENetEvent event; |
---|
110 | if(server==NULL) |
---|
111 | return false; |
---|
112 | if(enet_host_service(server, &event, NETWORK_SEND_WAIT)>=0) |
---|
113 | return true; |
---|
114 | else |
---|
115 | return false; |
---|
116 | } |
---|
117 | |
---|
118 | void ConnectionManager::receiverThread(){ |
---|
119 | // what about some error-handling here ? |
---|
120 | enet_initialize(); |
---|
121 | atexit(enet_deinitialize); |
---|
122 | ENetEvent event; |
---|
123 | server = enet_host_create(&bindAddress, NETWORK_MAX_CONNECTIONS, 0, 0); |
---|
124 | if(server==NULL){ |
---|
125 | // add some error handling here ========================== |
---|
126 | quit=true; |
---|
127 | return; |
---|
128 | } |
---|
129 | |
---|
130 | while(!quit){ |
---|
131 | if(enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT)<0){ |
---|
132 | // we should never reach this point |
---|
133 | quit=true; |
---|
134 | // add some error handling here ======================== |
---|
135 | } |
---|
136 | switch(event.type){ |
---|
137 | // log handling ================ |
---|
138 | case ENET_EVENT_TYPE_CONNECT: |
---|
139 | addClient(&event); |
---|
140 | break; |
---|
141 | case ENET_EVENT_TYPE_RECEIVE: |
---|
142 | processData(&event); |
---|
143 | break; |
---|
144 | case ENET_EVENT_TYPE_DISCONNECT: |
---|
145 | // add some error/log handling here |
---|
146 | clientDisconnect(event.peer); |
---|
147 | break; |
---|
148 | case ENET_EVENT_TYPE_NONE: |
---|
149 | break; |
---|
150 | } |
---|
151 | } |
---|
152 | disconnectClients(); |
---|
153 | // if we're finishied, destroy server |
---|
154 | enet_host_destroy(server); |
---|
155 | } |
---|
156 | |
---|
157 | void ConnectionManager::disconnectClients(){ |
---|
158 | bool disconnected=false; |
---|
159 | ENetEvent event; |
---|
160 | for(int i=0; i<clientVector.size(); i++){ |
---|
161 | enet_peer_disconnect(&(peerMap.find(clientVector[i])->second), 0); |
---|
162 | while( !disconnected && enet_host_service(server, &event, NETWORK_WAIT_TIMEOUT) > 0){ |
---|
163 | switch (event.type) |
---|
164 | { |
---|
165 | case ENET_EVENT_TYPE_NONE: |
---|
166 | case ENET_EVENT_TYPE_CONNECT: |
---|
167 | case ENET_EVENT_TYPE_RECEIVE: |
---|
168 | enet_packet_destroy(event.packet); |
---|
169 | break; |
---|
170 | case ENET_EVENT_TYPE_DISCONNECT: |
---|
171 | disconnected=true; |
---|
172 | break; |
---|
173 | } |
---|
174 | } |
---|
175 | disconnected=false; |
---|
176 | } |
---|
177 | return; |
---|
178 | } |
---|
179 | |
---|
180 | bool ConnectionManager::processData(ENetEvent *event){ |
---|
181 | // just add packet to the buffer |
---|
182 | // this can be extended with some preprocessing |
---|
183 | return buffer.push(event); |
---|
184 | } |
---|
185 | |
---|
186 | bool ConnectionManager::clientDisconnect(ENetPeer *peer){ |
---|
187 | return clientDisconnect(*peer); |
---|
188 | } |
---|
189 | |
---|
190 | bool ConnectionManager::clientDisconnect(ENetPeer peer){ |
---|
191 | std::map<ENetAddress, int>::iterator it; |
---|
192 | it=clientMap.find(peer.address); |
---|
193 | clientVector.erase(clientVector.begin()+it->second); |
---|
194 | clientMap.erase(it); |
---|
195 | peerMap.erase(peerMap.find(peer.address)); |
---|
196 | return true; |
---|
197 | } |
---|
198 | |
---|
199 | bool ConnectionManager::addClient(ENetEvent *event){ |
---|
200 | int id=clientVector.size(); |
---|
201 | clientVector.push_back((event->peer->address)); |
---|
202 | clientMap[event->peer->address]=id; |
---|
203 | peerMap[event->peer->address]=*(event->peer); |
---|
204 | syncClassid(id); |
---|
205 | return true; |
---|
206 | } |
---|
207 | |
---|
208 | int ConnectionManager::getClientID(ENetPeer peer){ |
---|
209 | return clientMap.find(peer.address)->second; |
---|
210 | } |
---|
211 | |
---|
212 | int ConnectionManager::getClientID(ENetAddress address){ |
---|
213 | return clientMap.find(address)->second; |
---|
214 | } |
---|
215 | |
---|
216 | ENetPeer ConnectionManager::getClientPeer(int clientID){ |
---|
217 | return peerMap.find(clientVector[clientID])->second; |
---|
218 | } |
---|
219 | |
---|
220 | void ConnectionManager::syncClassid(int clientID){ |
---|
221 | int i=0; |
---|
222 | std::string classname; |
---|
223 | bool abort=false; |
---|
224 | orxonox::Identifier *id; |
---|
225 | while(!abort){ |
---|
226 | id = orxonox::ID(i); |
---|
227 | if(id == NULL) |
---|
228 | abort=true; |
---|
229 | else{ |
---|
230 | classname = id->getName(); |
---|
231 | addPacket(packet_gen.clid( i, classname ),clientID); |
---|
232 | } |
---|
233 | ++i; |
---|
234 | } |
---|
235 | sendPackets(); |
---|
236 | } |
---|
237 | |
---|
238 | } |
---|