[Orxonox-commit 355] r2965 - branches/netp2/src/network
scheusso at orxonox.net
scheusso at orxonox.net
Mon May 11 13:42:10 CEST 2009
Author: scheusso
Date: 2009-05-11 13:42:10 +0200 (Mon, 11 May 2009)
New Revision: 2965
Modified:
branches/netp2/src/network/Client.cc
branches/netp2/src/network/Client.h
branches/netp2/src/network/ClientConnection.cc
branches/netp2/src/network/ClientConnection.h
branches/netp2/src/network/ConnectionManager.cc
branches/netp2/src/network/ConnectionManager.h
branches/netp2/src/network/Host.h
branches/netp2/src/network/Server.h
Log:
some fixes (bidirectional variables, ...), some changes (client tickrate, connection handling)
Modified: branches/netp2/src/network/Client.cc
===================================================================
--- branches/netp2/src/network/Client.cc 2009-05-11 11:30:17 UTC (rev 2964)
+++ branches/netp2/src/network/Client.cc 2009-05-11 11:42:10 UTC (rev 2965)
@@ -139,34 +139,40 @@
* @param time
*/
void Client::tick(float time){
-// COUT(3) << ".";
- if(client_connection.isConnected() && isSynched_){
- COUT(4) << "popping partial gamestate: " << std::endl;
- packet::Gamestate *gs = gamestate.getGamestate();
- if(gs){
- COUT(4) << "client tick: sending gs " << gs << std::endl;
- if( !gs->send() )
- COUT(3) << "Problem adding partial gamestate to queue" << std::endl;
+ //this steers our network frequency
+ timeSinceLastUpdate_+=time;
+ if(timeSinceLastUpdate_>=NETWORK_PERIOD){
+ timeSinceLastUpdate_ -= static_cast<unsigned int>( timeSinceLastUpdate_ / NETWORK_PERIOD ) * NETWORK_PERIOD;
+ // COUT(3) << ".";
+ if(client_connection.isConnected() && isSynched_){
+ COUT(4) << "popping partial gamestate: " << std::endl;
+ packet::Gamestate *gs = gamestate.getGamestate();
+ if(gs){
+ COUT(4) << "client tick: sending gs " << gs << std::endl;
+ if( !gs->send() )
+ COUT(3) << "Problem adding partial gamestate to queue" << std::endl;
// gs gets automatically deleted by enet callback
+ }
+ FunctionCallManager::sendCalls();
}
- FunctionCallManager::sendCalls();
- }
- ENetEvent *event;
+ ENetEvent *event;
// stop if the packet queue is empty
- while(!(client_connection.queueEmpty())){
- event = client_connection.getEvent();
- COUT(5) << "tick packet size " << event->packet->dataLength << std::endl;
- packet::Packet *packet = packet::Packet::createPacket(event->packet, event->peer);
+ while(!(client_connection.queueEmpty())){
+ event = client_connection.getEvent();
+ COUT(5) << "tick packet size " << event->packet->dataLength << std::endl;
+ packet::Packet *packet = packet::Packet::createPacket(event->packet, event->peer);
// note: packet commits suicide here except for the GameState. That is then deleted by a GamestateHandler
- bool b = packet->process();
- assert(b);
+ bool b = packet->process();
+ assert(b);
+ }
+ if(gamestate.processGamestates())
+ {
+ if(!isSynched_)
+ isSynched_=true;
+ }
+ gamestate.cleanup();
}
- if(gamestate.processGamestates())
- {
- if(!isSynched_)
- isSynched_=true;
- }
- gamestate.cleanup();
+
return;
}
Modified: branches/netp2/src/network/Client.h
===================================================================
--- branches/netp2/src/network/Client.h 2009-05-11 11:30:17 UTC (rev 2964)
+++ branches/netp2/src/network/Client.h 2009-05-11 11:42:10 UTC (rev 2965)
@@ -87,6 +87,7 @@
bool isSynched_;
bool gameStateFailure_;
+ float timeSinceLastUpdate_;
};
Modified: branches/netp2/src/network/ClientConnection.cc
===================================================================
--- branches/netp2/src/network/ClientConnection.cc 2009-05-11 11:30:17 UTC (rev 2964)
+++ branches/netp2/src/network/ClientConnection.cc 2009-05-11 11:42:10 UTC (rev 2965)
@@ -57,7 +57,7 @@
static boost::recursive_mutex enet_mutex_g;
ClientConnection::ClientConnection(int port, const std::string& address) {
- quit=false;
+ quit_=false;
server=NULL;
serverAddress = new ENetAddress();
enet_address_set_host(serverAddress, address.c_str());
@@ -66,7 +66,7 @@
}
ClientConnection::ClientConnection(int port, const char *address) {
- quit=false;
+ quit_=false;
server=NULL;
serverAddress = new ENetAddress();
enet_address_set_host(serverAddress, address);
@@ -106,7 +106,7 @@
}
bool ClientConnection::closeConnection() {
- quit=true;
+ quit_=true;
//network_threads.join_all();
receiverThread_->join();
established=false;
@@ -150,25 +150,26 @@
if(client==NULL) {
COUT(2) << "ClientConnection: could not create client host" << std::endl;
// add some error handling here ==========================
- quit=true;
+ quit_=true;
}
//connect to the server
if(!establishConnection()){
COUT(2) << "clientConn: receiver thread: could not establishConnection" << std::endl;
- quit=true;
+ quit_=true;
return;
}
event = new ENetEvent;
//main loop
- while(!quit){
+ while(!quit_){
//std::cout << "connection loop" << std::endl;
{
boost::recursive_mutex::scoped_lock lock(enet_mutex_g);
if(enet_host_service(client, event, NETWORK_CLIENT_WAIT_TIME)<0){
// we should never reach this point
- assert(0);
- quit=true;
- continue;
+// assert(0);
+ printf("ClientConnection: ENet returned with an error!\n");
+ quit_=true;
+ break;
// add some error handling here ========================
}
lock.unlock();
@@ -184,7 +185,8 @@
event = new ENetEvent;
break;
case ENET_EVENT_TYPE_DISCONNECT:
- quit=true;
+ quit_=true;
+ printf("Received disconnect Packet from Server!\n");
// server closed the connection
return;
break;
@@ -234,7 +236,7 @@
return false;
}
// handshake
- while(enet_host_service(client, &event, NETWORK_CLIENT_WAIT_TIME)>=0 && !quit){
+ while(enet_host_service(client, &event, NETWORK_CLIENT_WAIT_TIME)>=0 && !quit_){
if( event.type == ENET_EVENT_TYPE_CONNECT ){
established=true;
return true;
Modified: branches/netp2/src/network/ClientConnection.h
===================================================================
--- branches/netp2/src/network/ClientConnection.h 2009-05-11 11:30:17 UTC (rev 2964)
+++ branches/netp2/src/network/ClientConnection.h 2009-05-11 11:42:10 UTC (rev 2965)
@@ -53,7 +53,7 @@
const int NETWORK_PORT = 55556;
const int NETWORK_CLIENT_MAX_CONNECTIONS = 5;
const int NETWORK_CLIENT_WAIT_TIME = 10;
- const int NETWORK_CLIENT_CONNECT_TIMEOUT = 10000; // miliseconds
+ const int NETWORK_CLIENT_CONNECT_TIMEOUT = 3000; // miliseconds
const int NETWORK_CLIENT_CHANNELS = 2;
@@ -75,7 +75,8 @@
// send out all queued packets and save result in event
//bool sendPackets(ENetEvent *event);
bool waitEstablished(int milisec);
- bool isConnected(){return established;}
+ inline bool isConnected(){return established;}
+ inline bool checkConnection(){ return !quit_ && isConnected(); }
private:
ClientConnection(const ClientConnection& copy); // not used
bool processData(ENetEvent *event);
@@ -89,7 +90,7 @@
ENetHost *client;
ENetAddress *serverAddress;
// quit-variable (communication with threads)
- bool quit;
+ bool quit_;
bool established;
// clientlist
ENetPeer *server;
Modified: branches/netp2/src/network/ConnectionManager.cc
===================================================================
--- branches/netp2/src/network/ConnectionManager.cc 2009-05-11 11:30:17 UTC (rev 2964)
+++ branches/netp2/src/network/ConnectionManager.cc 2009-05-11 11:42:10 UTC (rev 2965)
@@ -70,7 +70,7 @@
ConnectionManager::ConnectionManager():receiverThread_(0){
assert(instance_==0);
instance_=this;
- quit=false;
+ quit_=false;
bindAddress = new ENetAddress();
bindAddress->host = ENET_HOST_ANY;
bindAddress->port = NETWORK_PORT;
@@ -79,7 +79,7 @@
ConnectionManager::ConnectionManager(int port){
assert(instance_==0);
instance_=this;
- quit=false;
+ quit_=false;
bindAddress = new ENetAddress();
bindAddress->host = ENET_HOST_ANY;
bindAddress->port = port;
@@ -88,7 +88,7 @@
ConnectionManager::ConnectionManager(int port, const std::string& address) :receiverThread_(0) {
assert(instance_==0);
instance_=this;
- quit=false;
+ quit_=false;
bindAddress = new ENetAddress();
enet_address_set_host (bindAddress, address.c_str());
bindAddress->port = NETWORK_PORT;
@@ -97,14 +97,14 @@
ConnectionManager::ConnectionManager(int port, const char *address) : receiverThread_(0) {
assert(instance_==0);
instance_=this;
- quit=false;
+ quit_=false;
bindAddress = new ENetAddress();
enet_address_set_host (bindAddress, address);
bindAddress->port = NETWORK_PORT;
}
ConnectionManager::~ConnectionManager(){
- if(!quit)
+ if(!quit_)
quitListener();
instance_=0;
delete bindAddress;
@@ -128,7 +128,7 @@
}
bool ConnectionManager::quitListener() {
- quit=true;
+ quit_=true;
receiverThread_->join();
return true;
}
@@ -184,18 +184,19 @@
}
if(server==NULL){
// add some error handling here ==========================
- quit=true;
+ quit_=true;
return;
}
event = new ENetEvent;
- while(!quit)
+ while(!quit_)
{
{ //mutex scope
boost::recursive_mutex::scoped_lock lock(enet_mutex_g);
if(enet_host_service(server, event, NETWORK_WAIT_TIMEOUT)<0){
// we should never reach this point
- quit=true;
+ printf("ConnectionManager: ENet returned with an error\n");
+ quit_=true;
continue;
printf("waaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaahhhhhhhhhhhhhhhh");
// add some error handling here ========================
Modified: branches/netp2/src/network/ConnectionManager.h
===================================================================
--- branches/netp2/src/network/ConnectionManager.h 2009-05-11 11:30:17 UTC (rev 2964)
+++ branches/netp2/src/network/ConnectionManager.h 2009-05-11 11:42:10 UTC (rev 2965)
@@ -80,6 +80,7 @@
bool sendPackets();
void disconnectClient(ClientInformation *client);
void syncClassid(unsigned int clientID);
+ bool checkReceiverThread(){ return !quit_; }
private:
ConnectionManager(const ConnectionManager& copy); // not used
@@ -94,7 +95,7 @@
ENetHost *server;
ENetAddress *bindAddress;
- bool quit; // quit-variable (communication with threads)
+ bool quit_; // quit-variable (communication with threads)
boost::thread *receiverThread_;
static ConnectionManager *instance_;
Modified: branches/netp2/src/network/Host.h
===================================================================
--- branches/netp2/src/network/Host.h 2009-05-11 11:30:17 UTC (rev 2964)
+++ branches/netp2/src/network/Host.h 2009-05-11 11:42:10 UTC (rev 2965)
@@ -35,6 +35,10 @@
namespace orxonox {
+ const int CLIENTID_SERVER = 0;
+ const unsigned int NETWORK_FREQUENCY = 25;
+ const float NETWORK_PERIOD = 1./NETWORK_FREQUENCY;
+
/**
* @brief Base class of Server and Client
* This is the Base class of the Server and Client classes
Modified: branches/netp2/src/network/Server.h
===================================================================
--- branches/netp2/src/network/Server.h 2009-05-11 11:30:17 UTC (rev 2964)
+++ branches/netp2/src/network/Server.h 2009-05-11 11:42:10 UTC (rev 2965)
@@ -50,9 +50,6 @@
namespace orxonox
{
- const int CLIENTID_SERVER = 0;
- const unsigned int NETWORK_FREQUENCY = 25;
- const float NETWORK_PERIOD = 1./NETWORK_FREQUENCY;
/**
* This class is the root class of the network module for a server.
More information about the Orxonox-commit
mailing list