[Orxonox-commit 3078] r7772 - in code/branches/network5/src/libraries/network: . packet
scheusso at orxonox.net
scheusso at orxonox.net
Fri Dec 17 10:41:25 CET 2010
Author: scheusso
Date: 2010-12-17 10:41:24 +0100 (Fri, 17 Dec 2010)
New Revision: 7772
Modified:
code/branches/network5/src/libraries/network/Client.cc
code/branches/network5/src/libraries/network/Client.h
code/branches/network5/src/libraries/network/ClientConnection.cc
code/branches/network5/src/libraries/network/ClientConnection.h
code/branches/network5/src/libraries/network/Connection.cc
code/branches/network5/src/libraries/network/Connection.h
code/branches/network5/src/libraries/network/Host.cc
code/branches/network5/src/libraries/network/Host.h
code/branches/network5/src/libraries/network/NetworkPrereqs.h
code/branches/network5/src/libraries/network/Server.cc
code/branches/network5/src/libraries/network/Server.h
code/branches/network5/src/libraries/network/ServerConnection.cc
code/branches/network5/src/libraries/network/ServerConnection.h
code/branches/network5/src/libraries/network/packet/Packet.cc
code/branches/network5/src/libraries/network/packet/Packet.h
Log:
network is now multithreaded again
further testing needed
Modified: code/branches/network5/src/libraries/network/Client.cc
===================================================================
--- code/branches/network5/src/libraries/network/Client.cc 2010-12-16 16:46:47 UTC (rev 7771)
+++ code/branches/network5/src/libraries/network/Client.cc 2010-12-17 09:41:24 UTC (rev 7772)
@@ -113,11 +113,9 @@
ClientConnection::setPort(port);
}
- bool Client::queuePacket(ENetPacket *packet, int clientID)
+ void Client::queuePacket(ENetPacket *packet, int clientID, uint8_t channelID)
{
- bool b = ClientConnection::addPacket(packet);
- assert(b);
- return b;
+ ClientConnection::addPacket(packet, channelID);
}
bool Client::processChat(const std::string& message, unsigned int playerID)
@@ -169,7 +167,7 @@
FunctionCallManager::sendCalls();
}
}
- sendPackets(); // flush the enet queue
+// sendPackets(); // flush the enet queue
Connection::processQueue();
if(gamestate->processGamestates())
@@ -179,7 +177,7 @@
isSynched_=true;
}
gamestate->cleanup();
- Connection::sendPackets();
+// Connection::sendPackets();
return;
}
Modified: code/branches/network5/src/libraries/network/Client.h
===================================================================
--- code/branches/network5/src/libraries/network/Client.h 2010-12-16 16:46:47 UTC (rev 7771)
+++ code/branches/network5/src/libraries/network/Client.h 2010-12-17 09:41:24 UTC (rev 7772)
@@ -77,7 +77,7 @@
bool establishConnection();
void setDestination( const std::string& serverAddress, unsigned int port ); // tolua_export
bool closeConnection();
- bool queuePacket(ENetPacket *packet, int clientID);
+ void queuePacket(ENetPacket* packet, int clientID, uint8_t channelID);
bool processChat(const std::string& message, unsigned int playerID);
virtual bool chat(const std::string& message);
virtual bool broadcast(const std::string& message) { return false; }
Modified: code/branches/network5/src/libraries/network/ClientConnection.cc
===================================================================
--- code/branches/network5/src/libraries/network/ClientConnection.cc 2010-12-16 16:46:47 UTC (rev 7771)
+++ code/branches/network5/src/libraries/network/ClientConnection.cc 2010-12-17 09:41:24 UTC (rev 7772)
@@ -98,6 +98,7 @@
if( enet_host_service(this->host_, &event, NETWORK_CLIENT_WAIT_TIME)>=0 && event.type == ENET_EVENT_TYPE_CONNECT )
{
this->established_=true;
+ Connection::startCommunicationThread();
return true;
}
}
@@ -111,6 +112,7 @@
if ( !this->established_ )
return true;
this->established_ = false;
+ Connection::stopCommunicationThread();
enet_peer_disconnect(this->server_, 0);
for( unsigned int i=0; i<NETWORK_CLIENT_CONNECTION_TIMEOUT/NETWORK_CLIENT_WAIT_TIME; i++)
{
@@ -137,10 +139,10 @@
}
- bool ClientConnection::addPacket(ENetPacket *packet) {
+ void ClientConnection::addPacket(ENetPacket *packet, uint8_t channelID) {
assert( this->server_ );
assert( packet );
- return Connection::addPacket( packet, this->server_ );
+ return Connection::addPacket( packet, this->server_, channelID );
}
void ClientConnection::addPeer(ENetEvent* event)
Modified: code/branches/network5/src/libraries/network/ClientConnection.h
===================================================================
--- code/branches/network5/src/libraries/network/ClientConnection.h 2010-12-16 16:46:47 UTC (rev 7771)
+++ code/branches/network5/src/libraries/network/ClientConnection.h 2010-12-17 09:41:24 UTC (rev 7772)
@@ -50,7 +50,7 @@
virtual bool establishConnection();
virtual bool closeConnection();
// add a packet to queue for the server
- bool addPacket(ENetPacket *packet);
+ void addPacket(ENetPacket *packet, uint8_t channelID);
inline bool isConnected(){ return this->established_; }
protected:
virtual void connectionClosed()=0;
Modified: code/branches/network5/src/libraries/network/Connection.cc
===================================================================
--- code/branches/network5/src/libraries/network/Connection.cc 2010-12-16 16:46:47 UTC (rev 7771)
+++ code/branches/network5/src/libraries/network/Connection.cc 2010-12-17 09:41:24 UTC (rev 7772)
@@ -29,57 +29,160 @@
#include "Connection.h"
#include <cassert>
+#include <deque>
#define WIN32_LEAN_AND_MEAN
#include <enet/enet.h>
+#include <boost/thread.hpp>
+#include <boost/thread/mutex.hpp>
+#include <boost/date_time.hpp>
+
#include "packet/Packet.h"
namespace orxonox
{
-// Connection *Connection::instance_=0;
+ const boost::posix_time::millisec NETWORK_COMMUNICATION_THREAD_WAIT_TIME(20);
Connection::Connection():
- host_(0)
+ host_(0), bCommunicationThreadRunning_(false)
{
-// assert(instance_==0);
-// Connection::instance_=this;
enet_initialize();
atexit(enet_deinitialize);
+ this->incomingEventsMutex_ = new boost::mutex;
+ this->outgoingEventsMutex_ = new boost::mutex;
}
- Connection::~Connection(){
-// Connection::instance_=0;
+ Connection::~Connection()
+ {
+ delete this->incomingEventsMutex_;
+ delete this->outgoingEventsMutex_;
}
- int Connection::service(ENetEvent* event) {
- return enet_host_service( this->host_, event, NETWORK_WAIT_TIMEOUT );
+ void Connection::startCommunicationThread()
+ {
+ this->bCommunicationThreadRunning_ = true;
+ this->communicationThread_ = new boost::thread(&Connection::communicationThread, this);
}
+
+ void Connection::stopCommunicationThread()
+ {
+ this->bCommunicationThreadRunning_ = false;
+ if( !this->communicationThread_->timed_join(NETWORK_COMMUNICATION_THREAD_WAIT_TIME) )
+ {
+ // force thread to stop
+ this->communicationThread_->interrupt();
+ }
+ delete this->communicationThread_;
+ }
- void Connection::disconnectPeer(ENetPeer *peer) {
- enet_peer_disconnect(peer, 0);
+
+// int Connection::service(ENetEvent* event) {
+// return enet_host_service( this->host_, event, NETWORK_WAIT_TIMEOUT );
+// }
+
+ void Connection::disconnectPeer(ENetPeer *peer)
+ {
+ outgoingEvent outEvent = { peer, outgoingEventType::disconnectPeer, (ENetPacket*)10, 15 };
+
+ this->outgoingEventsMutex_->lock();
+ this->outgoingEvents_.push_back(outEvent);
+ this->outgoingEventsMutex_->unlock();
}
- bool Connection::addPacket(ENetPacket *packet, ENetPeer *peer) {
- if(enet_peer_send(peer, NETWORK_DEFAULT_CHANNEL, packet)!=0)
- return false;
- else
- return true;
+ void Connection::addPacket(ENetPacket *packet, ENetPeer *peer, uint8_t channelID)
+ {
+ outgoingEvent outEvent = { peer, outgoingEventType::sendPacket, packet, channelID };
+
+ this->outgoingEventsMutex_->lock();
+ this->outgoingEvents_.push_back(outEvent);
+ this->outgoingEventsMutex_->unlock();
}
+
+ void Connection::broadcastPacket(ENetPacket* packet, uint8_t channelID)
+ {
+ outgoingEvent outEvent = { 0, outgoingEventType::broadcastPacket, packet, channelID };
+
+ this->outgoingEventsMutex_->lock();
+ this->outgoingEvents_.push_back(outEvent);
+ this->outgoingEventsMutex_->unlock();
+ }
- bool Connection::sendPackets() {
- if ( /*!Connection::instance_ || */this->host_==NULL )
- return false;
- enet_host_flush(this->host_);
- return true;
+
+ void Connection::communicationThread()
+ {
+ COUT(0) << "starting communication thread" << endl;
+ ENetEvent event;
+
+ while( bCommunicationThreadRunning_ )
+ {
+ // Receive all pending incoming Events (such as packets, connects and disconnects)
+ while( enet_host_check_events( this->host_, &event ) > 0 )
+ {
+// COUT(0) << "incoming event" << endl;
+ // received an event
+ this->incomingEventsMutex_->lock();
+ this->incomingEvents_.push_back(event);
+ this->incomingEventsMutex_->unlock();
+ }
+
+ // Send all waiting outgoing packets
+ this->outgoingEventsMutex_->lock();
+ uint32_t outgoingEventsCount = this->outgoingEvents_.size();
+ this->outgoingEventsMutex_->unlock();
+ while( outgoingEventsCount > 0 )
+ {
+// COUT(0) << "outgoing event" << endl;
+ this->outgoingEventsMutex_->lock();
+ outgoingEvent outEvent = this->outgoingEvents_.front();
+ this->outgoingEvents_.pop_front();
+ this->outgoingEventsMutex_->unlock();
+
+ switch( outEvent.type )
+ {
+ case outgoingEventType::sendPacket:
+ enet_peer_send( outEvent.peer, outEvent.channelID, outEvent.packet );
+ break;
+ case outgoingEventType::disconnectPeer:
+ enet_peer_disconnect(outEvent.peer, 0);
+ break;
+ case outgoingEventType::broadcastPacket:
+ enet_host_broadcast( this->host_, outEvent.channelID, outEvent.packet );
+ break;
+ default:
+ assert(0);
+ }
+ this->outgoingEventsMutex_->lock();
+ outgoingEventsCount = this->outgoingEvents_.size();
+ this->outgoingEventsMutex_->unlock();
+ }
+
+ // Wait for incoming events (at most NETWORK_WAIT_TIMEOUT ms)
+ if( enet_host_service( this->host_, &event, NETWORK_WAIT_TIMEOUT ) > 0 )
+ {
+// COUT(0) << "incoming event after wait" << endl;
+ //received an event
+ this->incomingEventsMutex_->lock();
+ this->incomingEvents_.push_back(event);
+ this->incomingEventsMutex_->unlock();
+ }
+ }
}
- void Connection::processQueue() {
+ void Connection::processQueue()
+ {
ENetEvent event;
- assert(this->host_);
-
- while( enet_host_service( this->host_, &event, NETWORK_WAIT_TIMEOUT ) > 0 )
+ this->incomingEventsMutex_->lock();
+ uint32_t incomingEventsCount = this->incomingEvents_.size();
+ this->incomingEventsMutex_->unlock();
+ while( incomingEventsCount > 0 )
{
- switch(event.type){
+ this->incomingEventsMutex_->lock();
+ event = this->incomingEvents_.front();
+ this->incomingEvents_.pop_front();
+ this->incomingEventsMutex_->unlock();
+
+ switch(event.type)
+ {
// log handling ================
case ENET_EVENT_TYPE_CONNECT:
addPeer( &event );
@@ -88,15 +191,21 @@
removePeer( &event );
break;
case ENET_EVENT_TYPE_RECEIVE:
+// COUT(0) << "ENET_EVENT_TYPE_RECEIVE" << endl;
processPacket( &event );
break;
case ENET_EVENT_TYPE_NONE:
break;
}
+
+ this->incomingEventsMutex_->lock();
+ incomingEventsCount = this->incomingEvents_.size();
+ this->incomingEventsMutex_->unlock();
}
}
- bool Connection::processPacket(ENetEvent* event) {
+ bool Connection::processPacket(ENetEvent* event)
+ {
packet::Packet *p = packet::Packet::createPacket(event->packet, event->peer);
return p->process();
}
Modified: code/branches/network5/src/libraries/network/Connection.h
===================================================================
--- code/branches/network5/src/libraries/network/Connection.h 2010-12-16 16:46:47 UTC (rev 7771)
+++ code/branches/network5/src/libraries/network/Connection.h 2010-12-17 09:41:24 UTC (rev 7772)
@@ -42,27 +42,57 @@
#include "NetworkPrereqs.h"
+#include <deque>
+
+namespace boost
+{
+ class thread;
+ class mutex;
+}
+
namespace orxonox
{
- const unsigned int NETWORK_PORT = 55556;
- const unsigned int NETWORK_MAX_CONNECTIONS = 50;
- const unsigned int NETWORK_WAIT_TIMEOUT = 0;
- const unsigned int NETWORK_DEFAULT_CHANNEL = 0;
- const unsigned int NETWORK_MAX_QUEUE_PROCESS_TIME = 5;
-
- class _NetworkExport Connection{
+ const unsigned int NETWORK_PORT = 55556;
+ const unsigned int NETWORK_MAX_CONNECTIONS = 50;
+ const unsigned int NETWORK_WAIT_TIMEOUT = 1;
+ const unsigned int NETWORK_MAX_QUEUE_PROCESS_TIME = 5;
+
+ namespace outgoingEventType
+ {
+ enum Value
+ {
+ sendPacket = 1,
+ disconnectPeer = 2,
+ broadcastPacket = 3
+ };
+
+ }
+
+ struct _NetworkExport outgoingEvent
+ {
+ ENetPeer* peer;
+ outgoingEventType::Value type;
+ ENetPacket* packet;
+ ENetChannelID channelID;
+ };
+
+ class _NetworkExport Connection
+ {
public:
virtual ~Connection();
- static bool addPacket(ENetPacket *packet, ENetPeer *peer);
- bool sendPackets();
+ void addPacket(ENetPacket *packet, ENetPeer *peer, uint8_t channelID);
+ void broadcastPacket(ENetPacket* packet, uint8_t channelID);
ENetHost* getHost(){ return this->host_; }
protected:
Connection();
// static Connection* getInstance(){ return Connection::instance_; }
- int service(ENetEvent* event);
+// int service(ENetEvent* event);
+ void startCommunicationThread();
+ void stopCommunicationThread();
+ void communicationThread();
virtual void disconnectPeer(ENetPeer *peer);
void processQueue();
@@ -70,9 +100,15 @@
virtual void removePeer(ENetEvent* event)=0;
virtual bool processPacket(ENetEvent* event);
- ENetHost *host_;
+ ENetHost* host_;
+ boost::mutex* incomingEventsMutex_;
+ boost::mutex* outgoingEventsMutex_;
private:
- ENetAddress *bindAddress_;
+ boost::thread* communicationThread_;
+ bool bCommunicationThreadRunning_;
+ ENetAddress* bindAddress_;
+ std::deque<ENetEvent> incomingEvents_;
+ std::deque<outgoingEvent> outgoingEvents_;
// static Connection *instance_;
Modified: code/branches/network5/src/libraries/network/Host.cc
===================================================================
--- code/branches/network5/src/libraries/network/Host.cc 2010-12-16 16:46:47 UTC (rev 7771)
+++ code/branches/network5/src/libraries/network/Host.cc 2010-12-17 09:41:24 UTC (rev 7772)
@@ -75,18 +75,15 @@
* @param clientID ID of the client the packet should be sent to
* @return success?
*/
- bool Host::addPacket(ENetPacket *packet, int clientID)
+ void Host::addPacket(ENetPacket *packet, int clientID, uint8_t channelID)
{
- bool result = true;
for( std::vector<Host*>::iterator it = instances_s.begin(); it!=instances_s.end(); ++it )
{
if( (*it)->isActive() )
{
- if( !(*it)->queuePacket(packet, clientID) )
- result = false;
+ (*it)->queuePacket(packet, clientID, channelID);
}
}
- return result;
}
void Host::Chat(const std::string& message)
Modified: code/branches/network5/src/libraries/network/Host.h
===================================================================
--- code/branches/network5/src/libraries/network/Host.h 2010-12-16 16:46:47 UTC (rev 7771)
+++ code/branches/network5/src/libraries/network/Host.h 2010-12-17 09:41:24 UTC (rev 7772)
@@ -53,7 +53,7 @@
//TODO add these functions or adequate
//virtual bool processChat(packet::Chat *message, unsigned int clientID)=0;
//virtual bool sendChat(packet::Chat *chat)=0;
- virtual bool queuePacket(ENetPacket *packet, int clientID)=0;
+ virtual void queuePacket(ENetPacket *packet, int clientID, uint8_t channelID)=0;
virtual bool chat(const std::string& message)=0;
virtual bool broadcast(const std::string& message)=0;
virtual bool processChat(const std::string& message, unsigned int playerID)=0;
@@ -70,7 +70,7 @@
public:
// static Host* getInstance(){ return instance_; }
static bool running(){ return instances_s.size(); }
- static bool addPacket(ENetPacket *packet, int clientID=0);
+ static void addPacket(ENetPacket* packet, int clientID = NETWORK_PEER_ID_SERVER, uint8_t channelID = 0);
//static bool chat(std::string& message);
// static bool receiveChat(packet::Chat *message, unsigned int clientID);
static unsigned int getPlayerID(){ return clientID_s; }
Modified: code/branches/network5/src/libraries/network/NetworkPrereqs.h
===================================================================
--- code/branches/network5/src/libraries/network/NetworkPrereqs.h 2010-12-16 16:46:47 UTC (rev 7771)
+++ code/branches/network5/src/libraries/network/NetworkPrereqs.h 2010-12-17 09:41:24 UTC (rev 7772)
@@ -63,12 +63,14 @@
namespace orxonox
{
- static const unsigned int GAMESTATEID_INITIAL = static_cast<unsigned int>(-1);
- static const unsigned int CLIENTID_UNKNOWN = static_cast<unsigned int>(-2);
+ static const unsigned int GAMESTATEID_INITIAL = static_cast<unsigned int>(-1);
+ static const unsigned int CLIENTID_UNKNOWN = static_cast<unsigned int>(-2);
extern const char* LAN_DISCOVERY_MESSAGE;
extern const char* LAN_DISCOVERY_ACK;
- static const unsigned int LAN_DISCOVERY_PORT = 55557;
- static const unsigned int NETWORK_PEER_ID_SERVER = 0;
+ static const unsigned int LAN_DISCOVERY_PORT = 55557;
+ static const unsigned int NETWORK_PEER_ID_SERVER = 0;
+ static const unsigned int NETWORK_CHANNEL_DEFAULT = 0;
+ static const unsigned int NETWORK_CHANNEL_RELIABLE = 1;
}
//-----------------------------------------------------------------------
@@ -97,15 +99,16 @@
// from ENet
struct _ENetPeer;
-typedef _ENetPeer ENetPeer;
+typedef _ENetPeer ENetPeer;
struct _ENetPacket;
-typedef _ENetPacket ENetPacket;
+typedef _ENetPacket ENetPacket;
struct _ENetEvent;
-typedef _ENetEvent ENetEvent;
+typedef _ENetEvent ENetEvent;
struct _ENetHost;
-typedef _ENetHost ENetHost;
+typedef _ENetHost ENetHost;
struct _ENetAddress;
-typedef _ENetAddress ENetAddress;
+typedef _ENetAddress ENetAddress;
+typedef uint8_t ENetChannelID;
namespace orxonox
{
@@ -161,4 +164,10 @@
class SynchronisableVariableBidirectional;
}
+namespace boost
+{
+ class mutex;
+ class thread;
+}
+
#endif /* _NetworkPrereqs_H__ */
Modified: code/branches/network5/src/libraries/network/Server.cc
===================================================================
--- code/branches/network5/src/libraries/network/Server.cc 2010-12-16 16:46:47 UTC (rev 7771)
+++ code/branches/network5/src/libraries/network/Server.cc 2010-12-17 09:41:24 UTC (rev 7772)
@@ -228,13 +228,13 @@
timeSinceLastUpdate_ -= static_cast<unsigned int>( timeSinceLastUpdate_ / NETWORK_PERIOD ) * NETWORK_PERIOD;
updateGamestate();
}
- sendPackets(); // flush the enet queue
+// sendPackets(); // flush the enet queue
}
}
- bool Server::queuePacket(ENetPacket *packet, int clientID)
+ void Server::queuePacket(ENetPacket *packet, int clientID, uint8_t channelID)
{
- return ServerConnection::addPacket(packet, clientID);
+ ServerConnection::addPacket(packet, clientID, channelID);
}
/**
Modified: code/branches/network5/src/libraries/network/Server.h
===================================================================
--- code/branches/network5/src/libraries/network/Server.h 2010-12-16 16:46:47 UTC (rev 7771)
+++ code/branches/network5/src/libraries/network/Server.h 2010-12-17 09:41:24 UTC (rev 7772)
@@ -63,7 +63,7 @@
void open();
void close();
bool processChat(const std::string& message, unsigned int playerID);
- bool queuePacket(ENetPacket *packet, int clientID);
+ void queuePacket(ENetPacket *packet, int clientID, uint8_t channelID);
void update(const Clock& time);
unsigned int getRTT(unsigned int clientID);
virtual void printRTT();
Modified: code/branches/network5/src/libraries/network/ServerConnection.cc
===================================================================
--- code/branches/network5/src/libraries/network/ServerConnection.cc 2010-12-16 16:46:47 UTC (rev 7771)
+++ code/branches/network5/src/libraries/network/ServerConnection.cc 2010-12-17 09:41:24 UTC (rev 7772)
@@ -48,13 +48,15 @@
this->bindAddress_->port = NETWORK_PORT;
}
- ServerConnection::~ServerConnection(){
+ ServerConnection::~ServerConnection()
+ {
if ( this->bListening_ )
closeListener();
delete this->bindAddress_;
}
- void ServerConnection::setBindAddress( const std::string& bindAddress ) {
+ void ServerConnection::setBindAddress( const std::string& bindAddress )
+ {
if (enet_address_set_host (this->bindAddress_, bindAddress.c_str()) < 0)
COUT(1) << "Error: Could not resolve \"" << bindAddress << "\"." << std::endl;
}
@@ -63,7 +65,8 @@
this->bindAddress_->port = port;
}
- bool ServerConnection::openListener() {
+ bool ServerConnection::openListener()
+ {
this->host_ = enet_host_create(this->bindAddress_, NETWORK_MAX_CONNECTIONS, 0, 0, 0);
if ( this->host_ == NULL )
{
@@ -77,89 +80,72 @@
COUT(2) << "Warning: IPv6 Socket failed." << std::endl;
else
COUT(3) << "Info: Using IPv4 and IPv6 Sockets." << std::endl;
+
+ Connection::startCommunicationThread();
return true;
}
- bool ServerConnection::closeListener() {
+ bool ServerConnection::closeListener()
+ {
this->bListening_=false;
disconnectClients();
enet_host_destroy(this->host_);
return true;
}
- bool ServerConnection::addPacket(ENetPacket *packet, unsigned int clientID) {
+ void ServerConnection::addPacket(ENetPacket *packet, unsigned int clientID, uint8_t channelID)
+ {
if ( clientID == CLIENTID_UNKNOWN )
{
- return addPacketAll(packet);
+ broadcastPacket(packet, channelID);
}
else
{
ClientInformation *temp = ClientInformation::findClient(clientID);
if(!temp){
COUT(3) << "C.Man: addPacket findClient failed" << std::endl;
- return false;
}
- return Connection::addPacket(packet, temp->getPeer());
+ Connection::addPacket(packet, temp->getPeer(), channelID);
}
}
- bool ServerConnection::addPacketAll(ENetPacket *packet) {
-// if ( !Connection::getInstance() )
-// return false;
- enet_host_broadcast( Connection::getHost(), 0, packet);
- return true;
- }
-
void ServerConnection::disconnectClient(ClientInformation *client)
{
Connection::disconnectPeer( client->getPeer() );
}
- void ServerConnection::disconnectClient(int clientID){
+ void ServerConnection::disconnectClient(int clientID)
+ {
ClientInformation *client = ClientInformation::findClient(clientID);
if(client)
ServerConnection::disconnectClient(client);
}
- void ServerConnection::disconnectClients() {
- ENetEvent event;
+ void ServerConnection::disconnectClients()
+ {
ClientInformation *temp = ClientInformation::getBegin();
- while(temp!=0){
+ while(temp!=0)
+ {
ServerConnection::disconnectClient( temp );
temp = temp->next();
}
- temp = ClientInformation::getBegin();
- while( temp!=0 ){
- if( service( &event ) )
- {
- switch (event.type)
- {
- case ENET_EVENT_TYPE_NONE: break;
- case ENET_EVENT_TYPE_CONNECT: break;
- case ENET_EVENT_TYPE_RECEIVE:
- enet_packet_destroy(event.packet);
- break;
- case ENET_EVENT_TYPE_DISCONNECT:
- removePeer( &event );
- temp = ClientInformation::getBegin();
- break;
- }
- }
- }
return;
}
- int ServerConnection::getClientID(ENetPeer* peer) {
+ int ServerConnection::getClientID(ENetPeer* peer)
+ {
return getClientID(&(peer->address));
}
- int ServerConnection::getClientID(ENetAddress* address) {
+ int ServerConnection::getClientID(ENetAddress* address)
+ {
return ClientInformation::findClient(address)->getID();
}
- ENetPeer *ServerConnection::getClientPeer(int clientID) {
+ ENetPeer *ServerConnection::getClientPeer(int clientID)
+ {
return ClientInformation::findClient(clientID)->getPeer();
}
Modified: code/branches/network5/src/libraries/network/ServerConnection.h
===================================================================
--- code/branches/network5/src/libraries/network/ServerConnection.h 2010-12-16 16:46:47 UTC (rev 7771)
+++ code/branches/network5/src/libraries/network/ServerConnection.h 2010-12-17 09:41:24 UTC (rev 7772)
@@ -55,8 +55,7 @@
bool openListener();
bool closeListener();
- bool addPacket(ENetPacket *packet, unsigned int ID);
- bool addPacketAll(ENetPacket *packet);
+ void addPacket(ENetPacket *packet, unsigned int ID, uint8_t channelID);
virtual void disconnectClient(ClientInformation *client);
void disconnectClient(int clientID);
protected:
Modified: code/branches/network5/src/libraries/network/packet/Packet.cc
===================================================================
--- code/branches/network5/src/libraries/network/packet/Packet.cc 2010-12-16 16:46:47 UTC (rev 7771)
+++ code/branches/network5/src/libraries/network/packet/Packet.cc 2010-12-17 09:41:24 UTC (rev 7772)
@@ -34,6 +34,7 @@
#define WIN32_LEAN_AND_MEAN
#include <enet/enet.h>
#include <boost/static_assert.hpp>
+#include <boost/thread/mutex.hpp>
#include "util/Debug.h"
#include "Acknowledgement.h"
@@ -60,6 +61,7 @@
#define _PACKETID 0
std::map<size_t, Packet *> Packet::packetMap_;
+boost::mutex Packet::packetMapMutex_;
Packet::Packet()
{
@@ -141,7 +143,9 @@
{
// Assures we don't create a packet and destroy it right after in another thread
// without having a reference in the packetMap_
+ Packet::packetMapMutex_.lock();
packetMap_[reinterpret_cast<size_t>(enetPacket_)] = this;
+ Packet::packetMapMutex_.unlock();
}
}
#ifndef NDEBUG
@@ -163,8 +167,10 @@
#endif
// ENetPacket *temp = enetPacket_;
// enetPacket_ = 0; // otherwise we have a double free because enet already handles the deallocation of the packet
- if(!Host::addPacket( enetPacket_, clientID_))
- enet_packet_destroy(this->enetPacket_); // if we could not add the packet to the enet queue delete it manually
+ if( this->flags_ & PacketFlag::Reliable )
+ Host::addPacket( enetPacket_, clientID_, 0);
+ else
+ Host::addPacket( enetPacket_, clientID_, 0);
return true;
}
@@ -227,12 +233,14 @@
*/
void Packet::deletePacket(ENetPacket *enetPacket){
// Get our Packet from a global map with all Packets created in the send() method of Packet.
+ Packet::packetMapMutex_.lock();
std::map<size_t, Packet*>::iterator it = packetMap_.find(reinterpret_cast<size_t>(enetPacket));
assert(it != packetMap_.end());
// Make sure we don't delete it again in the destructor
it->second->enetPacket_ = 0;
delete it->second;
packetMap_.erase(it);
+ Packet::packetMapMutex_.unlock();
// COUT(6) << "PacketMap size: " << packetMap_.size() << std::endl;
}
Modified: code/branches/network5/src/libraries/network/packet/Packet.h
===================================================================
--- code/branches/network5/src/libraries/network/packet/Packet.h 2010-12-16 16:46:47 UTC (rev 7771)
+++ code/branches/network5/src/libraries/network/packet/Packet.h 2010-12-17 09:41:24 UTC (rev 7772)
@@ -95,6 +95,7 @@
bool bDataENetAllocated_;
private:
static std::map<size_t, Packet *> packetMap_;
+ static boost::mutex packetMapMutex_;
ENetPacket *enetPacket_;
};
More information about the Orxonox-commit
mailing list