1 | // |
---|
2 | // C++ Implementation: Client |
---|
3 | // |
---|
4 | // Description: |
---|
5 | // |
---|
6 | // |
---|
7 | // Author: Oliver Scheuss, (C) 2007 |
---|
8 | // |
---|
9 | // Copyright: See COPYING file that comes with this distribution |
---|
10 | // |
---|
11 | // |
---|
12 | |
---|
13 | #include "Client.h" |
---|
14 | |
---|
15 | namespace network{ |
---|
16 | |
---|
17 | /** |
---|
18 | * Constructor for the Client class |
---|
19 | * initializes the address and the port to default localhost:NETWORK_PORT |
---|
20 | */ |
---|
21 | Client::Client(): client_connection(NETWORK_PORT,"127.0.0.1"){ |
---|
22 | // set server address to localhost |
---|
23 | isConnected=false; |
---|
24 | } |
---|
25 | |
---|
26 | /** |
---|
27 | * Constructor for the Client class |
---|
28 | * @param address the server address |
---|
29 | * @param port port of the application on the server |
---|
30 | */ |
---|
31 | Client::Client(std::string address, int port) : client_connection(port, address){ |
---|
32 | isConnected=false; |
---|
33 | } |
---|
34 | |
---|
35 | /** |
---|
36 | * Constructor for the Client class |
---|
37 | * @param address the server address |
---|
38 | * @param port port of the application on the server |
---|
39 | */ |
---|
40 | Client::Client(const char *address, int port) : client_connection(port, address){ |
---|
41 | isConnected=false; |
---|
42 | } |
---|
43 | |
---|
44 | /** |
---|
45 | * Establish the Connection to the Server |
---|
46 | * @return true/false |
---|
47 | */ |
---|
48 | bool Client::establishConnection(){ |
---|
49 | isConnected=client_connection.createConnection(); |
---|
50 | return isConnected; |
---|
51 | } |
---|
52 | |
---|
53 | /** |
---|
54 | * closes the Connection to the Server |
---|
55 | * @return true/false |
---|
56 | */ |
---|
57 | bool Client::closeConnection(){ |
---|
58 | isConnected=false; |
---|
59 | return client_connection.closeConnection(); |
---|
60 | } |
---|
61 | |
---|
62 | /** |
---|
63 | * submits a MouseAction to the server |
---|
64 | * @param x x Coordinate |
---|
65 | * @param y y Coordinate |
---|
66 | * @return true/false |
---|
67 | */ |
---|
68 | bool Client::sendMouse(double x, double y){ |
---|
69 | // generate packet and add it to the queue |
---|
70 | if(!isConnected) |
---|
71 | return false; |
---|
72 | if(!client_connection.addPacket(pck_gen.mousem(x, y))) |
---|
73 | return false; |
---|
74 | // send packets |
---|
75 | return client_connection.sendPackets(); |
---|
76 | } |
---|
77 | |
---|
78 | /** |
---|
79 | * submits a Keystrike to the server |
---|
80 | * @param key_code code to submit |
---|
81 | * @return true/false |
---|
82 | */ |
---|
83 | bool Client::sendKeyboard(char key_code){ |
---|
84 | // generate packet and add it to queue |
---|
85 | if(!isConnected) |
---|
86 | return false; |
---|
87 | if(!client_connection.addPacket(pck_gen.keystrike(key_code))) |
---|
88 | return false; |
---|
89 | // send packets |
---|
90 | return client_connection.sendPackets(); |
---|
91 | } |
---|
92 | |
---|
93 | /** |
---|
94 | * Adds a MouseAction to the PacketQueue |
---|
95 | * @param x x Coordinate |
---|
96 | * @param y y Coordinate |
---|
97 | * @return true/false |
---|
98 | */ |
---|
99 | bool Client::addMouse(double x, double y){ |
---|
100 | // generate packet and add it to the queue |
---|
101 | if(client_connection.addPacket(pck_gen.mousem(x, y))) |
---|
102 | return true; |
---|
103 | else |
---|
104 | return false; |
---|
105 | } |
---|
106 | |
---|
107 | /** |
---|
108 | * Adds a Keystrike to the PacketQueue |
---|
109 | * @param key_code |
---|
110 | * @return true/false |
---|
111 | */ |
---|
112 | bool Client::addKeyboard(char key_code){ |
---|
113 | // generate packet and add it to queue |
---|
114 | if(client_connection.addPacket(pck_gen.keystrike(key_code))) |
---|
115 | return true; |
---|
116 | else |
---|
117 | return false; |
---|
118 | } |
---|
119 | |
---|
120 | /** |
---|
121 | * Sends out all the packets queued by addXXX |
---|
122 | */ |
---|
123 | bool Client::sendPackets(){ |
---|
124 | if(!isConnected) |
---|
125 | return false; |
---|
126 | ENetEvent event; |
---|
127 | // send packets |
---|
128 | client_connection.sendPackets(&event); |
---|
129 | if(event.type==ENET_EVENT_TYPE_NONE) |
---|
130 | return true; |
---|
131 | else |
---|
132 | return false; |
---|
133 | } |
---|
134 | |
---|
135 | /** |
---|
136 | * Performs a GameState update |
---|
137 | */ |
---|
138 | void Client::update(){ |
---|
139 | ENetPacket *packet; |
---|
140 | // stop if the packet queue is empty |
---|
141 | while(!client_connection.queueEmpty()){ |
---|
142 | packet = client_connection.getPacket(); |
---|
143 | elaborate(packet, 0); // ================= i guess we got to change this .... (client_ID is always same = server) |
---|
144 | } |
---|
145 | return; |
---|
146 | } |
---|
147 | |
---|
148 | void Client::processGamestate( GameStateCompressed *data){ |
---|
149 | gamestate.pushGameState(*data); |
---|
150 | client_connection.addPacket(pck_gen.acknowledgement(data->id)); |
---|
151 | client_connection.sendPackets(); |
---|
152 | return; |
---|
153 | } |
---|
154 | |
---|
155 | void Client::processClassid(classid *clid){ |
---|
156 | orxonox::Identifier *id; |
---|
157 | id=orxonox::ID(std::string(clid->message)); |
---|
158 | if(id!=NULL) |
---|
159 | id->setNetworkID(clid->clid); |
---|
160 | return; |
---|
161 | } |
---|
162 | |
---|
163 | void Client::processChat( chat *data){ |
---|
164 | std::cout << "Server: " << data->message << std::endl; |
---|
165 | } |
---|
166 | |
---|
167 | } |
---|