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