Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/archive/tutorialHS08/src/network/Client.cc @ 9659

Last change on this file since 9659 was 1769, checked in by scheusso, 16 years ago

sync frequencies do work now

  • Property svn:eol-style set to native
File size: 4.7 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Oliver Scheuss, (C) 2007
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29//
30// C++ Implementation: Client
31//
32// Description:
33//
34//
35// Author:  Oliver Scheuss, (C) 2007
36//
37// Copyright: See COPYING file that comes with this distribution
38//
39//
40
41#include "Client.h"
42#include "Host.h"
43#include "Synchronisable.h"
44#include "core/CoreIncludes.h"
45#include "core/ConsoleCommand.h"
46#include "packet/Packet.h"
47// #include "packet/Acknowledgement.h"
48
49namespace network
50{
51//   SetConsoleCommandShortcut(Client, chat);
52
53
54  /**
55  * Constructor for the Client class
56  * initializes the address and the port to default localhost:NETWORK_PORT
57  */
58  Client::Client(): client_connection(NETWORK_PORT,"127.0.0.1"){
59    // set server address to localhost
60    isConnected=false;
61    isSynched_=false;
62    gameStateFailure_=false;
63    isServer_ = false;
64  }
65
66  /**
67  * Constructor for the Client class
68  * @param address the server address
69  * @param port port of the application on the server
70  */
71  Client::Client(std::string address, int port) : client_connection(port, address){
72    isConnected=false;
73    isSynched_=false;
74    gameStateFailure_=false;
75    isServer_ = false;
76  }
77
78  /**
79  * Constructor for the Client class
80  * @param address the server address
81  * @param port port of the application on the server
82  */
83  Client::Client(const char *address, int port) : client_connection(port, address){
84    isConnected=false;
85    isSynched_=false;
86    gameStateFailure_=false;
87    isServer_ = false;
88  }
89
90  Client::~Client(){
91    if(isConnected)
92      closeConnection();
93  }
94
95  /**
96  * Establish the Connection to the Server
97  * @return true/false
98  */
99  bool Client::establishConnection(){
100    Synchronisable::setClient(true);
101    isConnected=client_connection.createConnection();
102    if(!isConnected)
103      COUT(1) << "could not create connection laber" << std::endl;
104    return isConnected;
105  }
106
107  /**
108  * closes the Connection to the Server
109  * @return true/false
110  */
111  bool Client::closeConnection(){
112    isConnected=false;
113    return client_connection.closeConnection();
114  }
115
116  bool Client::queuePacket(ENetPacket *packet, int clientID){
117    return client_connection.addPacket(packet);
118  }
119
120  bool Client::processChat(packet::Chat *message, unsigned int clientID){
121    return message->process();
122  }
123
124  /*bool Client::sendChat(packet::Chat *chat){
125    chat->process();
126    packet::Packet *p = new packet::Packet(chat);
127    return p->send();
128  }*/
129
130
131  /**
132  * submits a chat message to the server
133  * @param message message to send
134  * @return true/false
135  */
136  bool Client::sendChat( std::string message ){
137    // generate packet and add it to queue
138    if(!isConnected)
139      return false;
140    packet::Chat chat(message, 0);
141    return chat.send();
142    // send packets
143  }
144
145  /**
146  * Performs a GameState update
147  */
148  void Client::tick(float time){
149//     COUT(3) << ".";
150    if(client_connection.isConnected() && isSynched_){
151      COUT(4) << "popping partial gamestate: " << std::endl;
152      packet::Gamestate *gs = gamestate.getGamestate();
153      if(gs){
154        COUT(4) << "client tick: sending gs " << gs << std::endl;
155        if( !gs->send() )
156          COUT(3) << "Problem adding partial gamestate to queue" << std::endl;
157        // gs gets automatically deleted by enet callback
158      }
159    }
160    ENetEvent *event;
161    // stop if the packet queue is empty
162    while(!(client_connection.queueEmpty())){
163      event = client_connection.getEvent();
164      COUT(5) << "tick packet size " << event->packet->dataLength << std::endl;
165      packet::Packet *packet = packet::Packet::createPacket(event->packet, event->peer);
166      assert(packet->process());
167    }
168    if(gamestate.processGamestates())
169    {
170      if(!isSynched_)
171        isSynched_=true;
172    }else
173      COUT(3) << "gamestate has not been processed sucessfully" << std::endl;
174    gamestate.cleanup();
175    return;
176  }
177
178}
Note: See TracBrowser for help on using the repository browser.