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(!client_connection.addPacket(pck_gen.mousem(x, y))) |
---|
71 | return false; |
---|
72 | // send packets |
---|
73 | return client_connection.sendPackets(); |
---|
74 | } |
---|
75 | |
---|
76 | /** |
---|
77 | * submits a Keystrike to the server |
---|
78 | * @param key_code code to submit |
---|
79 | * @return true/false |
---|
80 | */ |
---|
81 | bool Client::sendKeyboard(char key_code){ |
---|
82 | // generate packet and add it to queue |
---|
83 | if(!client_connection.addPacket(pck_gen.keystrike(key_code))) |
---|
84 | return false; |
---|
85 | // send packets |
---|
86 | return client_connection.sendPackets(); |
---|
87 | } |
---|
88 | |
---|
89 | /** |
---|
90 | * Adds a MouseAction to the PacketQueue |
---|
91 | * @param x x Coordinate |
---|
92 | * @param y y Coordinate |
---|
93 | * @return true/false |
---|
94 | */ |
---|
95 | bool Client::addMouse(double x, double y){ |
---|
96 | // generate packet and add it to the queue |
---|
97 | if(client_connection.addPacket(pck_gen.mousem(x, y))) |
---|
98 | return true; |
---|
99 | else |
---|
100 | return false; |
---|
101 | } |
---|
102 | |
---|
103 | /** |
---|
104 | * Adds a Keystrike to the PacketQueue |
---|
105 | * @param key_code |
---|
106 | * @return true/false |
---|
107 | */ |
---|
108 | bool Client::addKeyboard(char key_code){ |
---|
109 | // generate packet and add it to queue |
---|
110 | if(client_connection.addPacket(pck_gen.keystrike(key_code))) |
---|
111 | return true; |
---|
112 | else |
---|
113 | return false; |
---|
114 | } |
---|
115 | |
---|
116 | /** |
---|
117 | * Sends out all the packets queued by addXXX |
---|
118 | */ |
---|
119 | bool Client::sendPackets(){ |
---|
120 | ENetEvent event; |
---|
121 | // send packets |
---|
122 | client_connection.sendPackets(&event); |
---|
123 | if(event.type==ENET_EVENT_TYPE_NONE) |
---|
124 | return true; |
---|
125 | else |
---|
126 | return false; |
---|
127 | } |
---|
128 | |
---|
129 | /** |
---|
130 | * Performs a GameState update |
---|
131 | */ |
---|
132 | void Client::update(){ |
---|
133 | ENetPacket *packet; |
---|
134 | // stop if the packet queue is empty |
---|
135 | while(!client_connection.queueEmpty()){ |
---|
136 | packet = client_connection.getPacket(); |
---|
137 | elaborate(packet, 0); // ================= i guess we got to change this .... (client_ID is always same = server) |
---|
138 | } |
---|
139 | return; |
---|
140 | } |
---|
141 | |
---|
142 | void Client::processGamestate( GameStateCompressed *data){ |
---|
143 | gamestate.pushGameState(*data); |
---|
144 | return; |
---|
145 | } |
---|
146 | |
---|
147 | void Client::processClassid(classid *clid){ |
---|
148 | orxonox::Identifier *id; |
---|
149 | id=orxonox::ID(std::string(clid->message)); |
---|
150 | if(id!=NULL) |
---|
151 | id->setNetworkID(clid->classid); |
---|
152 | return; |
---|
153 | } |
---|
154 | |
---|
155 | } |
---|