[Orxonox-commit 3099] r7793 - in code/branches/presentation2/src/libraries/network: . packet
scheusso at orxonox.net
scheusso at orxonox.net
Wed Dec 22 03:05:32 CET 2010
Author: scheusso
Date: 2010-12-22 03:05:31 +0100 (Wed, 22 Dec 2010)
New Revision: 7793
Added:
code/branches/presentation2/src/libraries/network/WANDiscoverable.cc
code/branches/presentation2/src/libraries/network/WANDiscoverable.h
Modified:
code/branches/presentation2/src/libraries/network/CMakeLists.txt
code/branches/presentation2/src/libraries/network/Connection.cc
code/branches/presentation2/src/libraries/network/MasterServerComm.cc
code/branches/presentation2/src/libraries/network/MasterServerComm.h
code/branches/presentation2/src/libraries/network/Server.cc
code/branches/presentation2/src/libraries/network/Server.h
code/branches/presentation2/src/libraries/network/packet/ServerInformation.cc
code/branches/presentation2/src/libraries/network/packet/ServerInformation.h
Log:
some changes in WANDiscovering:
- fixed a bug which caused memory corruptions (was caused by double frees)
- separated WANDiscovery into WANDiscoverable and WANDiscovery in order to prevent crashes when destructing GSServer and Closing the Server connection
Modified: code/branches/presentation2/src/libraries/network/CMakeLists.txt
===================================================================
--- code/branches/presentation2/src/libraries/network/CMakeLists.txt 2010-12-21 22:23:07 UTC (rev 7792)
+++ code/branches/presentation2/src/libraries/network/CMakeLists.txt 2010-12-22 02:05:31 UTC (rev 7793)
@@ -31,6 +31,7 @@
GamestateHandler.cc
LANDiscoverable.cc
LANDiscovery.cc
+ WANDiscoverable.cc
WANDiscovery.cc
MasterServerComm.cc
NetworkFunction.cc
@@ -58,6 +59,7 @@
Host.h
LANDiscoverable.h
LANDiscovery.h
+ WANDiscoverable.h
WANDiscovery.h
MasterServerComm.h
NetworkFunction.h
Modified: code/branches/presentation2/src/libraries/network/Connection.cc
===================================================================
--- code/branches/presentation2/src/libraries/network/Connection.cc 2010-12-21 22:23:07 UTC (rev 7792)
+++ code/branches/presentation2/src/libraries/network/Connection.cc 2010-12-22 02:05:31 UTC (rev 7793)
@@ -111,7 +111,6 @@
void Connection::communicationThread()
{
- COUT(0) << "starting communication thread" << endl;
ENetEvent event;
while( bCommunicationThreadRunning_ )
Modified: code/branches/presentation2/src/libraries/network/MasterServerComm.cc
===================================================================
--- code/branches/presentation2/src/libraries/network/MasterServerComm.cc 2010-12-21 22:23:07 UTC (rev 7792)
+++ code/branches/presentation2/src/libraries/network/MasterServerComm.cc 2010-12-22 02:05:31 UTC (rev 7793)
@@ -47,7 +47,7 @@
}
/* initialize the event holder */
- this->event = (ENetEvent *)calloc( sizeof(ENetEvent), 1 );
+// this->event = (ENetEvent *)calloc( sizeof(ENetEvent), 1 );
/* initiate the client */
@@ -89,8 +89,8 @@
}
/* Wait up to 2 seconds for the connection attempt to succeed. */
- if (enet_host_service (this->client, this->event, 500) > 0 &&
- this->event->type == ENET_EVENT_TYPE_CONNECT )
+ if (enet_host_service (this->client, &this->event, 500) > 0 &&
+ this->event.type == ENET_EVENT_TYPE_CONNECT )
COUT(3) << "Connection to master server succeeded.\n";
else
{
@@ -102,20 +102,27 @@
/* all fine */
return 0;
}
+
+void MasterServerComm::update()
+{
+ while( enet_host_service( this->client, &this->event, 1 ) );
+}
+
int MasterServerComm::disconnect( void )
{
+ while( enet_host_service( this->client, &this->event, 1 ) );
enet_peer_disconnect( this->peer, 0 );
/* Allow up to 1 second for the disconnect to succeed
* and drop any packets received packets.
*/
- while (enet_host_service (this->client, this->event, 1000) > 0)
+ while (enet_host_service (this->client, &this->event, 1000) > 0)
{
- switch (this->event->type)
+ switch (this->event.type)
{
case ENET_EVENT_TYPE_RECEIVE:
- enet_packet_destroy (event->packet);
+ enet_packet_destroy (event.packet);
break;
case ENET_EVENT_TYPE_DISCONNECT:
@@ -153,10 +160,10 @@
/* enet_host_service returns 0 if no event occured */
/* just newly set below test to >0 from >= 0, to be tested */
- if( enet_host_service( this->client, this->event, delayms ) > 0 )
+ if( enet_host_service( this->client, &this->event, delayms ) > 0 )
{
/* check what type of event it is and react accordingly */
- switch (this->event->type)
+ switch (this->event.type)
{ /* new connection, not supposed to happen. */
case ENET_EVENT_TYPE_CONNECT: break;
@@ -172,23 +179,23 @@
}
/* resolve IP */
- enet_address_get_host_ip( &(this->event->peer->address),
+ enet_address_get_host_ip( &(this->event.peer->address),
addrconv, 49 );
/* DEBUG */
COUT(3) << "MasterServer Debug: A packet of length "
- << this->event->packet->dataLength
- << " containing " << this->event->packet->data
+ << this->event.packet->dataLength
+ << " containing " << this->event.packet->data
<< " was received from " << addrconv
- << " on channel " << this->event->channelID;
+ << " on channel " << this->event.channelID;
/* END DEBUG */
/* call the supplied callback, if any. */
if( (*callback) != NULL )
- retval = (*callback)( addrconv, (this->event) );
+ retval = (*callback)( addrconv, &(this->event) );
/* clean up */
- enet_packet_destroy( event->packet );
+ enet_packet_destroy( event.packet );
if( addrconv )
free( addrconv );
@@ -219,7 +226,8 @@
enet_host_flush( this->client );
/* free the packet */
- enet_packet_destroy( packet );
+ // PLEASE: never do this, because enet will free the packet once it's delivered. this will cause double frees
+// enet_packet_destroy( packet );
/* all done. */
return 0;
@@ -238,7 +246,8 @@
/* One could just use enet_host_service() instead. */
enet_host_flush( this->client );
- enet_packet_destroy( packet );
+ // PLEASE: never do this, because enet will free the packet once it's delivered. this will cause double frees
+// enet_packet_destroy( packet );
/* all done. */
return 0;
Modified: code/branches/presentation2/src/libraries/network/MasterServerComm.h
===================================================================
--- code/branches/presentation2/src/libraries/network/MasterServerComm.h 2010-12-21 22:23:07 UTC (rev 7792)
+++ code/branches/presentation2/src/libraries/network/MasterServerComm.h 2010-12-22 02:05:31 UTC (rev 7793)
@@ -52,6 +52,8 @@
* Initialize everything for the master server communication
*/
int initialize();
+
+ void update();
/** \param address Address to connect to (Host name or IP in text form)
@@ -91,16 +93,16 @@
private:
/** client handle */
- ENetHost *client;
+ ENetHost* client;
/** event data holder */
- ENetEvent *event;
+ ENetEvent event;
/** address holder */
ENetAddress address;
/** peer data holder */
- ENetPeer *peer;
+ ENetPeer* peer;
};
}
Modified: code/branches/presentation2/src/libraries/network/Server.cc
===================================================================
--- code/branches/presentation2/src/libraries/network/Server.cc 2010-12-21 22:23:07 UTC (rev 7792)
+++ code/branches/presentation2/src/libraries/network/Server.cc 2010-12-22 02:05:31 UTC (rev 7793)
@@ -103,8 +103,8 @@
/** helper that connects to the master server */
void Server::helper_ConnectToMasterserver()
{
- WANDiscovery::getInstance().msc.sendRequest( MSPROTO_GAME_SERVER " "
- MSPROTO_REGISTER_SERVER );
+// WANDiscovery::getInstance().msc.sendRequest( MSPROTO_GAME_SERVER " "
+// MSPROTO_REGISTER_SERVER );
}
/**
@@ -120,10 +120,11 @@
LANDiscoverable::setActivity(true);
/* make discoverable on WAN */
+ WANDiscoverable::setActivity(true);
/* TODO this needs to be optional, we need a switch from the UI to
* enable/disable this
*/
- helper_ConnectToMasterserver();
+// helper_ConnectToMasterserver();
/* done */
return;
@@ -141,8 +142,7 @@
/* tell master server we're closing */
COUT(2) << "disconnecting." << endl;
- WANDiscovery::getInstance().msc.sendRequest( MSPROTO_GAME_SERVER " "
- MSPROTO_SERVERDC );
+ WANDiscoverable::setActivity(false);
COUT(2) << "disconnecting done" << endl;
LANDiscoverable::setActivity(false);
@@ -201,7 +201,7 @@
// receive and process incoming discovery packets
LANDiscoverable::update();
-
+
// receive and process requests from master server
/* todo */
//helper_HandleMasterServerRequests();
Modified: code/branches/presentation2/src/libraries/network/Server.h
===================================================================
--- code/branches/presentation2/src/libraries/network/Server.h 2010-12-21 22:23:07 UTC (rev 7792)
+++ code/branches/presentation2/src/libraries/network/Server.h 2010-12-22 02:05:31 UTC (rev 7793)
@@ -39,8 +39,9 @@
// #include "GamestateManager.h"
#include "ServerConnection.h"
#include "LANDiscoverable.h"
-#include "MasterServerComm.h"
-#include "MasterServerProtocol.h"
+#include "WANDiscoverable.h"
+// #include "MasterServerComm.h"
+// #include "MasterServerProtocol.h"
namespace orxonox
@@ -50,7 +51,8 @@
* This class is the root class of the network module for a server.
* It implements all functions necessary for a Server
*/
- class _NetworkExport Server : public Host, public ServerConnection, public LANDiscoverable{
+ class _NetworkExport Server : public Host, public ServerConnection, public LANDiscoverable, public WANDiscoverable
+ {
public:
Server();
Server(int port);
Added: code/branches/presentation2/src/libraries/network/WANDiscoverable.cc
===================================================================
--- code/branches/presentation2/src/libraries/network/WANDiscoverable.cc (rev 0)
+++ code/branches/presentation2/src/libraries/network/WANDiscoverable.cc 2010-12-22 02:05:31 UTC (rev 7793)
@@ -0,0 +1,124 @@
+/*
+ * ORXONOX - the hottest 3D action shooter ever to exist
+ * > www.orxonox.net <
+ *
+ *
+ * License notice:
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Author:
+ * Fabian 'x3n' Landau (original)
+ * Co-authors:
+ * Sandro 'smerkli' Merkli (adaptions to WAN)
+ * ...
+ *
+ */
+
+#include "WANDiscoverable.h"
+
+#include <enet/enet.h>
+#include <cstring>
+
+#include "MasterServerProtocol.h"
+#include "core/ConfigValueIncludes.h"
+#include "core/CoreIncludes.h"
+
+
+namespace orxonox
+{
+
+ WANDiscoverable::WANDiscoverable(): bActive_(false)
+ {
+ /* debugging output */
+ COUT(4) << "Creating WANDiscoverable.\n";
+
+ /* register object in orxonox */
+ RegisterObject(WANDiscoverable);
+
+ /* check for the masterserver address option in orxonox.ini */
+ this->setConfigValues();
+
+ }
+
+ void WANDiscoverable::setConfigValues()
+ {
+ /* update msaddress string from orxonox.ini config file, if it
+ * has changed.
+ */
+ SetConfigValueExternal(msaddress, "WANDiscovery", "msaddress", "orxonox.net");
+// SetConfigValue( msaddress, "orxonox.net");
+ }
+
+ WANDiscoverable::~WANDiscoverable()
+ {
+ if( this->bActive_ )
+ this->disconnect();
+ }
+
+ void WANDiscoverable::setActivity(bool bActive)
+ {
+ if( bActive==this->bActive_ )
+ return;
+
+ if( bActive )
+ {
+ if( this->connect() )
+ this->bActive_ = true;
+ }
+ else
+ {
+ this->disconnect();
+ this->bActive_ = false;
+ }
+ }
+
+ bool WANDiscoverable::connect()
+ {
+ /* initialize it and see if it worked */
+ if( msc.initialize() )
+ {
+ COUT(2) << "Error: could not initialize master server communications!\n";
+ return false;
+ }
+
+ /* connect and see if it worked */
+ if( msc.connect( this->msaddress.c_str(), ORX_MSERVER_PORT ) )
+ {
+ COUT(2) << "Error: could not connect to master server at "
+ << this->msaddress << std::endl;
+ return false;
+ }
+
+ /* debugging output */
+ COUT(4) << "Initialization of WANDiscoverable complete.\n";
+
+
+ // Now register the server at the master server
+ this->msc.sendRequest( MSPROTO_GAME_SERVER " " MSPROTO_REGISTER_SERVER );
+
+ return true;
+ }
+
+ void WANDiscoverable::disconnect()
+ {
+ this->msc.sendRequest( MSPROTO_GAME_SERVER " " MSPROTO_SERVERDC );
+ msc.disconnect();
+ }
+
+
+
+
+} // namespace orxonox
Added: code/branches/presentation2/src/libraries/network/WANDiscoverable.h
===================================================================
--- code/branches/presentation2/src/libraries/network/WANDiscoverable.h (rev 0)
+++ code/branches/presentation2/src/libraries/network/WANDiscoverable.h 2010-12-22 02:05:31 UTC (rev 7793)
@@ -0,0 +1,77 @@
+/*
+ * ORXONOX - the hottest 3D action shooter ever to exist > www.orxonox.net <
+ *
+ *
+ * License notice:
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 2 of the License, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc., 51
+ * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Author: Fabian 'x3n' Landau (original) Co-authors: Sandro 'smerkli' Merkli
+ * (copied and adapted to WAN)
+ *
+ */
+
+#ifndef _WANDiscoverable_H__
+#define _WANDiscoverable_H__
+
+#include "NetworkPrereqs.h"
+// #include "core/OrxonoxClass.h"
+#include "core/CoreIncludes.h"
+#include "MasterServerComm.h"
+
+namespace orxonox
+{
+
+ class _NetworkExport WANDiscoverable: public OrxonoxClass
+ {
+ public:
+ /** constructor */
+ WANDiscoverable();
+
+ /** destructor */
+ ~WANDiscoverable();
+
+ /** \return Address of the master server
+ *
+ * Get the master server address
+ */
+ std::string getMSAddress()
+ { return this->msaddress; }
+
+ /** Function used for the configuration file parameter update */
+ void setConfigValues();
+
+ /** Function used to set the activity/discoverability */
+ void setActivity( bool bActive );
+
+ /** Master server communications object */
+ MasterServerComm msc;
+
+ private:
+ /** Function used to connect to the master server */
+ bool connect();
+
+ /** Function used to disconnect from the master server */
+ void disconnect();
+
+ /** master server address */
+ std::string msaddress;
+ bool bActive_;
+
+ };
+
+}
+
+#endif // _WANDiscoverable_H__
Modified: code/branches/presentation2/src/libraries/network/packet/ServerInformation.cc
===================================================================
--- code/branches/presentation2/src/libraries/network/packet/ServerInformation.cc 2010-12-21 22:23:07 UTC (rev 7792)
+++ code/branches/presentation2/src/libraries/network/packet/ServerInformation.cc 2010-12-22 02:05:31 UTC (rev 7793)
@@ -20,7 +20,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Author:
- * Fabian 'x3n' Landau
+ * Oliver Scheuss
* Co-authors:
* ...
*
@@ -59,7 +59,8 @@
loadAndIncrease((char*&)ack, temp);
/* Fabian, what is this used for? it crashes the masterserver, hence commenting it */
- //assert(strcmp(ack, (const char*)LAN_DISCOVERY_ACK)==0);
+ // written by Oli: this is just to make sure that loadAndIncrease really writes the whole ACK string into char* ack
+// assert(strcmp(ack, (const char*)LAN_DISCOVERY_ACK)==0);
// Save Server Name
loadAndIncrease(this->serverName_, temp);
Modified: code/branches/presentation2/src/libraries/network/packet/ServerInformation.h
===================================================================
--- code/branches/presentation2/src/libraries/network/packet/ServerInformation.h 2010-12-21 22:23:07 UTC (rev 7792)
+++ code/branches/presentation2/src/libraries/network/packet/ServerInformation.h 2010-12-22 02:05:31 UTC (rev 7793)
@@ -20,7 +20,7 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Author:
- * Fabian 'x3n' Landau
+ * Oliver Scheuss
* Co-authors:
* ...
*
More information about the Orxonox-commit
mailing list