1 | // |
---|
2 | // C++ Interface: ClientConnection |
---|
3 | // |
---|
4 | // Description: The Class ClientConnection 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 "ClientConnection.h" |
---|
13 | |
---|
14 | #ifdef WIN32 |
---|
15 | #include <windows.h> |
---|
16 | #define usleep(x) Sleep((x)/1000) |
---|
17 | #else |
---|
18 | #include <unistd.h> |
---|
19 | #endif |
---|
20 | |
---|
21 | namespace network{ |
---|
22 | |
---|
23 | static boost::thread_group network_threads; |
---|
24 | |
---|
25 | ClientConnection::ClientConnection(int port, std::string address){ |
---|
26 | quit=false; |
---|
27 | server=NULL; |
---|
28 | enet_address_set_host(&serverAddress, address.c_str()); |
---|
29 | serverAddress.port = NETWORK_PORT; |
---|
30 | established=false; |
---|
31 | } |
---|
32 | |
---|
33 | ClientConnection::ClientConnection(int port, const char *address){ |
---|
34 | quit=false; |
---|
35 | server=NULL; |
---|
36 | enet_address_set_host(&serverAddress, address); |
---|
37 | serverAddress.port = NETWORK_PORT; |
---|
38 | established=false; |
---|
39 | } |
---|
40 | |
---|
41 | bool ClientConnection::waitEstablished(int milisec){ |
---|
42 | for(int i=0; i<=milisec && !established; i++) |
---|
43 | usleep(1000); |
---|
44 | |
---|
45 | return established; |
---|
46 | } |
---|
47 | |
---|
48 | |
---|
49 | ENetPacket *ClientConnection::getPacket(ENetAddress &address){ |
---|
50 | if(!buffer.isEmpty()) |
---|
51 | return buffer.pop(address); |
---|
52 | else |
---|
53 | return NULL; |
---|
54 | } |
---|
55 | |
---|
56 | bool ClientConnection::queueEmpty(){ |
---|
57 | return buffer.isEmpty(); |
---|
58 | } |
---|
59 | |
---|
60 | bool ClientConnection::createConnection(){ |
---|
61 | network_threads.create_thread(boost::bind(boost::mem_fn(&ClientConnection::receiverThread), this)); |
---|
62 | // wait 10 seconds for the connection to be established |
---|
63 | return waitEstablished(10000); |
---|
64 | } |
---|
65 | |
---|
66 | bool ClientConnection::closeConnection(){ |
---|
67 | quit=true; |
---|
68 | network_threads.join_all(); |
---|
69 | established=false; |
---|
70 | return true; |
---|
71 | } |
---|
72 | |
---|
73 | |
---|
74 | bool ClientConnection::addPacket(ENetPacket *packet){ |
---|
75 | if(server==NULL) |
---|
76 | return false; |
---|
77 | if(enet_peer_send(server, 1, packet)!=0) |
---|
78 | return false; |
---|
79 | else |
---|
80 | return true; |
---|
81 | } |
---|
82 | |
---|
83 | bool ClientConnection::sendPackets(ENetEvent *event){ |
---|
84 | if(server==NULL) |
---|
85 | return false; |
---|
86 | if(enet_host_service(client, event, NETWORK_SEND_WAIT)>=0) |
---|
87 | return true; |
---|
88 | else |
---|
89 | return false; |
---|
90 | } |
---|
91 | |
---|
92 | bool ClientConnection::sendPackets(){ |
---|
93 | ENetEvent event; |
---|
94 | if(server==NULL) |
---|
95 | return false; |
---|
96 | if(enet_host_service(client, &event, NETWORK_SEND_WAIT)>=0) |
---|
97 | return true; |
---|
98 | else |
---|
99 | return false; |
---|
100 | } |
---|
101 | |
---|
102 | void ClientConnection::receiverThread(){ |
---|
103 | // what about some error-handling here ? |
---|
104 | enet_initialize(); |
---|
105 | atexit(enet_deinitialize); |
---|
106 | ENetEvent event; |
---|
107 | client = enet_host_create(NULL, NETWORK_CLIENT_MAX_CONNECTIONS, 0, 0); |
---|
108 | if(client==NULL) |
---|
109 | // add some error handling here ========================== |
---|
110 | quit=true; |
---|
111 | //connect to the server |
---|
112 | if(!establishConnection()) |
---|
113 | quit=true; |
---|
114 | //main loop |
---|
115 | while(!quit){ |
---|
116 | if(enet_host_service(client, &event, NETWORK_WAIT_TIMEOUT)<0){ |
---|
117 | // we should never reach this point |
---|
118 | quit=true; |
---|
119 | // add some error handling here ======================== |
---|
120 | } |
---|
121 | switch(event.type){ |
---|
122 | // log handling ================ |
---|
123 | case ENET_EVENT_TYPE_CONNECT: |
---|
124 | case ENET_EVENT_TYPE_RECEIVE: |
---|
125 | processData(&event); |
---|
126 | break; |
---|
127 | case ENET_EVENT_TYPE_DISCONNECT: |
---|
128 | // add some error/log handling here |
---|
129 | // extend ===================== |
---|
130 | break; |
---|
131 | case ENET_EVENT_TYPE_NONE: |
---|
132 | continue; |
---|
133 | } |
---|
134 | } |
---|
135 | // now disconnect |
---|
136 | |
---|
137 | if(!disconnectConnection()) |
---|
138 | // if disconnecting failed destroy conn. |
---|
139 | enet_peer_reset(server); |
---|
140 | return; |
---|
141 | } |
---|
142 | |
---|
143 | bool ClientConnection::disconnectConnection(){ |
---|
144 | ENetEvent event; |
---|
145 | // enet_peer_disconnect(server); |
---|
146 | enet_peer_disconnect(server, 0); |
---|
147 | while(enet_host_service(client, &event, NETWORK_WAIT_TIMEOUT) > 0){ |
---|
148 | switch (event.type) |
---|
149 | { |
---|
150 | case ENET_EVENT_TYPE_NONE: |
---|
151 | case ENET_EVENT_TYPE_CONNECT: |
---|
152 | case ENET_EVENT_TYPE_RECEIVE: |
---|
153 | enet_packet_destroy(event.packet); |
---|
154 | break; |
---|
155 | case ENET_EVENT_TYPE_DISCONNECT: |
---|
156 | return true; |
---|
157 | } |
---|
158 | } |
---|
159 | enet_peer_reset(server); |
---|
160 | return false; |
---|
161 | } |
---|
162 | |
---|
163 | bool ClientConnection::establishConnection(){ |
---|
164 | ENetEvent event; |
---|
165 | // connect to peer |
---|
166 | server = enet_host_connect(client, &serverAddress, NETWORK_CLIENT_CHANNELS); |
---|
167 | if(server==NULL) |
---|
168 | // error handling |
---|
169 | return false; |
---|
170 | // handshake |
---|
171 | if(enet_host_service(client, &event, NETWORK_WAIT_TIMEOUT)>0 && event.type == ENET_EVENT_TYPE_CONNECT){ |
---|
172 | established=true; |
---|
173 | return true; |
---|
174 | } |
---|
175 | else |
---|
176 | return false; |
---|
177 | } |
---|
178 | |
---|
179 | bool ClientConnection::processData(ENetEvent *event){ |
---|
180 | // just add packet to the buffer |
---|
181 | // this can be extended with some preprocessing |
---|
182 | return buffer.push(event); |
---|
183 | } |
---|
184 | |
---|
185 | |
---|
186 | } |
---|