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 | pck_gen = PacketGenerator(); |
---|
25 | gamestate = GameStateManager(); |
---|
26 | } |
---|
27 | |
---|
28 | /** |
---|
29 | * Constructor for the Client class |
---|
30 | * @param address the server address |
---|
31 | * @param port port of the application on the server |
---|
32 | */ |
---|
33 | Client::Client(std::string address, int port) : client_connection(port, address){ |
---|
34 | isConnected=false; |
---|
35 | pck_gen = PacketGenerator(); |
---|
36 | gamestate = GameStateManager(); |
---|
37 | } |
---|
38 | |
---|
39 | /** |
---|
40 | * Constructor for the Client class |
---|
41 | * @param address the server address |
---|
42 | * @param port port of the application on the server |
---|
43 | */ |
---|
44 | Client::Client(const char *address, int port) : client_connection(port, address){ |
---|
45 | isConnected=false; |
---|
46 | pck_gen = PacketGenerator(); |
---|
47 | gamestate = GameStateManager(); |
---|
48 | } |
---|
49 | |
---|
50 | /** |
---|
51 | * Establish the Connection to the Server |
---|
52 | * @return true/false |
---|
53 | */ |
---|
54 | bool Client::establishConnection(){ |
---|
55 | isConnected=client_connection.createConnection(); |
---|
56 | return isConnected; |
---|
57 | } |
---|
58 | |
---|
59 | /** |
---|
60 | * closes the Connection to the Server |
---|
61 | * @return true/false |
---|
62 | */ |
---|
63 | bool Client::closeConnection(){ |
---|
64 | isConnected=false; |
---|
65 | return client_connection.closeConnection(); |
---|
66 | } |
---|
67 | |
---|
68 | /** |
---|
69 | * submits a MouseAction to the server |
---|
70 | * @param x x Coordinate |
---|
71 | * @param y y Coordinate |
---|
72 | * @return true/false |
---|
73 | */ |
---|
74 | bool Client::sendMouse(double x, double y){ |
---|
75 | ENetEvent event; |
---|
76 | // generate packet and add it to the queue |
---|
77 | if(!client_connection.addPacket(pck_gen.mousem(x, y))) |
---|
78 | return false; |
---|
79 | // send packets |
---|
80 | return client_connection.sendPackets(); |
---|
81 | } |
---|
82 | |
---|
83 | /** |
---|
84 | * submits a Keystrike to the server |
---|
85 | * @param key_code code to submit |
---|
86 | * @return true/false |
---|
87 | */ |
---|
88 | bool Client::sendKeyboard(char key_code){ |
---|
89 | ENetEvent event; |
---|
90 | // generate packet and add it to queue |
---|
91 | if(!client_connection.addPacket(pck_gen.keystrike(key_code))) |
---|
92 | return false; |
---|
93 | // send packets |
---|
94 | return client_connection.sendPackets(); |
---|
95 | } |
---|
96 | |
---|
97 | /** |
---|
98 | * Adds a MouseAction to the PacketQueue |
---|
99 | * @param x x Coordinate |
---|
100 | * @param y y Coordinate |
---|
101 | * @return true/false |
---|
102 | */ |
---|
103 | bool Client::addMouse(double x, double y){ |
---|
104 | // generate packet and add it to the queue |
---|
105 | if(client_connection.addPacket(pck_gen.mousem(x, y))) |
---|
106 | return true; |
---|
107 | else |
---|
108 | return false; |
---|
109 | } |
---|
110 | |
---|
111 | /** |
---|
112 | * Adds a Keystrike to the PacketQueue |
---|
113 | * @param key_code |
---|
114 | * @return true/false |
---|
115 | */ |
---|
116 | bool Client::addKeyboard(char key_code){ |
---|
117 | ENetEvent event; |
---|
118 | // generate packet and add it to queue |
---|
119 | if(client_connection.addPacket(pck_gen.keystrike(key_code))) |
---|
120 | return true; |
---|
121 | else |
---|
122 | return false; |
---|
123 | } |
---|
124 | |
---|
125 | /** |
---|
126 | * Sends out all the packets queued by addXXX |
---|
127 | */ |
---|
128 | bool Client::sendPackets(){ |
---|
129 | ENetEvent event; |
---|
130 | // send packets |
---|
131 | client_connection.sendPackets(&event); |
---|
132 | if(event.type==ENET_EVENT_TYPE_NONE) |
---|
133 | return true; |
---|
134 | else |
---|
135 | return false; |
---|
136 | } |
---|
137 | |
---|
138 | /** |
---|
139 | * Performs a GameState update |
---|
140 | */ |
---|
141 | void Client::update(){ |
---|
142 | ENetPacket *packet; |
---|
143 | // stop if the packet queue is empty |
---|
144 | while(!client_connection.queueEmpty()){ |
---|
145 | packet = client_connection.getPacket(); |
---|
146 | elaborate(packet, 0); // ================= i guess we got to change this .... (client_ID is always same = server) |
---|
147 | } |
---|
148 | return; |
---|
149 | } |
---|
150 | |
---|
151 | void Client::processGamestate( GameState *data){ |
---|
152 | gamestate.loadSnapshot( *data ); |
---|
153 | return; |
---|
154 | } |
---|
155 | |
---|
156 | void Client::processClassid(classid *clid){ |
---|
157 | orxonox::Identifier *id; |
---|
158 | orxonox::ID(std::string(clid->message))->setNetworkID(clid->classid); |
---|
159 | } |
---|
160 | |
---|
161 | } |
---|