Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/tutorial/src/network/Client.cc @ 1835

Last change on this file since 1835 was 859, checked in by landauf, 17 years ago

more or less a copy of the trunk

File size: 5.3 KB
Line 
1/*
2*   ORXONOX - the hottest 3D action shooter ever to exist
3*
4*
5*   License notice:
6*
7*   This program is free software; you can redistribute it and/or
8*   modify it under the terms of the GNU General Public License
9*   as published by the Free Software Foundation; either version 2
10*   of the License, or (at your option) any later version.
11*
12*   This program is distributed in the hope that it will be useful,
13*   but WITHOUT ANY WARRANTY; without even the implied warranty of
14*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*   GNU General Public License for more details.
16*
17*   You should have received a copy of the GNU General Public License
18*   along with this program; if not, write to the Free Software
19*   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20*
21*   Author:
22*      Oliver Scheuss, (C) 2007
23*   Co-authors:
24*      ...
25*
26*/
27
28//
29// C++ Implementation: Client
30//
31// Description:
32//
33//
34// Author:  Oliver Scheuss, (C) 2007
35//
36// Copyright: See COPYING file that comes with this distribution
37//
38//
39
40#include "core/CoreIncludes.h"
41#include "Client.h"
42
43namespace network
44{
45  /**
46  * Constructor for the Client class
47  * initializes the address and the port to default localhost:NETWORK_PORT
48  */
49  Client::Client(): client_connection(NETWORK_PORT,"127.0.0.1"){
50    // set server address to localhost
51    isConnected=false;
52  }
53
54  /**
55  * Constructor for the Client class
56  * @param address the server address
57  * @param port port of the application on the server
58  */
59  Client::Client(std::string address, int port) : client_connection(port, address){
60    isConnected=false;
61  }
62
63  /**
64  * Constructor for the Client class
65  * @param address the server address
66  * @param port port of the application on the server
67  */
68  Client::Client(const char *address, int port) : client_connection(port, address){
69    isConnected=false;
70  }
71
72  /**
73  * Establish the Connection to the Server
74  * @return true/false
75  */
76  bool Client::establishConnection(){
77    isConnected=client_connection.createConnection();
78    return isConnected;
79  }
80
81  /**
82  * closes the Connection to the Server
83  * @return true/false
84  */
85  bool Client::closeConnection(){
86    isConnected=false;
87    return client_connection.closeConnection();
88  }
89
90  /**
91  * submits a MouseAction to the server
92  * @param x x Coordinate
93  * @param y y Coordinate
94  * @return true/false
95  */
96  bool Client::sendMouse(double x, double y){
97    // generate packet and add it to the queue
98    if(!isConnected)
99      return false;
100    if(!client_connection.addPacket(pck_gen.mousem(x, y)))
101      return false;
102    // send packets
103    return client_connection.sendPackets();
104  }
105
106  /**
107  * submits a Keystrike to the server
108  * @param key_code code to submit
109  * @return true/false
110  */
111  bool Client::sendKeyboard(char key_code){
112    // generate packet and add it to queue
113    if(!isConnected)
114      return false;
115    if(!client_connection.addPacket(pck_gen.keystrike(key_code)))
116      return false;
117    // send packets
118    return client_connection.sendPackets();
119  }
120
121  /**
122  * submits a chat message to the server
123  * @param message message to send
124  * @return true/false
125  */
126  bool Client::sendChat( std::string message ){
127    // generate packet and add it to queue
128    if(!isConnected)
129      return false;
130    if(client_connection.addPacket(pck_gen.chatMessage( message.c_str() )))
131      return client_connection.sendPackets();
132    // send packets
133    return false;
134  }
135
136  /**
137  * Adds a MouseAction to the PacketQueue
138  * @param x x Coordinate
139  * @param y y Coordinate
140  * @return true/false
141  */
142  bool Client::addMouse(double x, double y){
143    // generate packet and add it to the queue
144    if(client_connection.addPacket(pck_gen.mousem(x, y)))
145      return true;
146    else
147      return false;
148  }
149
150  /**
151  * Adds a Keystrike to the PacketQueue
152  * @param key_code
153  * @return true/false
154  */
155  bool Client::addKeyboard(char key_code){
156    // generate packet and add it to queue
157    if(client_connection.addPacket(pck_gen.keystrike(key_code)))
158      return true;
159    else
160      return false;
161  }
162
163  /**
164  * Sends out all the packets queued by addXXX
165  */
166  bool Client::sendPackets(){
167    if(!isConnected)
168      return false;
169    ENetEvent event;
170    // send packets
171    client_connection.sendPackets(&event);
172    if(event.type==ENET_EVENT_TYPE_NONE)
173      return true;
174    else
175      return false;
176  }
177
178  /**
179  * Performs a GameState update
180  */
181  void Client::tick(float time){
182    ENetPacket *packet;
183    // stop if the packet queue is empty
184    while(!(client_connection.queueEmpty())){
185      packet = client_connection.getPacket();
186      //std::cout << "tick packet size " << packet->dataLength << std::endl;
187      elaborate(packet, 0); // ================= i guess we got to change this .... (client_ID is always same = server)
188    }
189    return;
190  }
191
192  void Client::processGamestate( GameStateCompressed *data){
193    gamestate.pushGameState(data);
194    std::cout << "received gamestate id: " << data->id << std::endl;
195    client_connection.addPacket(pck_gen.acknowledgement(data->id));
196    client_connection.sendPackets();
197    return;
198  }
199
200  void Client::processClassid(classid *clid){
201    orxonox::Identifier *id;
202    id=ID(std::string(clid->message));
203    if(id!=NULL)
204      id->setNetworkID(clid->clid);
205    return;
206  }
207
208  void Client::processChat( chat *data){
209    std::cout << "Server: " << data->message << std::endl;
210  }
211
212}
Note: See TracBrowser for help on using the repository browser.