Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/FICN/src/network/Client.cc @ 410

Last change on this file since 410 was 405, checked in by scheusso, 17 years ago

corrected errors

File size: 3.8 KB
Line 
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
15namespace 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    // generate packet and add it to the queue
76    if(!client_connection.addPacket(pck_gen.mousem(x, y)))
77        return false;
78    // send packets
79    return client_connection.sendPackets();
80  }
81
82  /**
83   * submits a Keystrike to the server
84   * @param key_code code to submit
85   * @return true/false
86   */
87  bool Client::sendKeyboard(char key_code){
88    // generate packet and add it to queue
89    if(!client_connection.addPacket(pck_gen.keystrike(key_code)))
90        return false;
91    // send packets
92    return client_connection.sendPackets();
93  }
94
95  /**
96   * Adds a MouseAction to the PacketQueue
97   * @param x x Coordinate
98   * @param y y Coordinate
99   * @return true/false
100   */
101  bool Client::addMouse(double x, double y){
102    // generate packet and add it to the queue
103    if(client_connection.addPacket(pck_gen.mousem(x, y)))
104      return true;
105    else
106      return false;
107  }
108
109  /**
110   * Adds a Keystrike to the PacketQueue
111   * @param key_code
112   * @return true/false
113   */
114  bool Client::addKeyboard(char key_code){
115    // generate packet and add it to queue
116    if(client_connection.addPacket(pck_gen.keystrike(key_code)))
117      return true;
118    else
119      return false;
120  }
121
122  /**
123   * Sends out all the packets queued by addXXX
124   */
125  bool Client::sendPackets(){
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.loadSnapshot( *data );
150    return;
151  }
152 
153  void Client::processClassid(classid *clid){
154    orxonox::Identifier *id;
155    orxonox::ID(std::string(clid->message))->setNetworkID(clid->classid);
156  }
157 
158}
Note: See TracBrowser for help on using the repository browser.