[Orxonox-commit 3043] r7739 - in code/branches/presentation: . data/gui/scripts src/libraries/network src/libraries/network/packet src/orxonox src/orxonox/gamestates
dafrick at orxonox.net
dafrick at orxonox.net
Wed Dec 8 16:25:52 CET 2010
Author: dafrick
Date: 2010-12-08 16:25:52 +0100 (Wed, 08 Dec 2010)
New Revision: 7739
Added:
code/branches/presentation/src/libraries/network/MasterServer.cc
code/branches/presentation/src/libraries/network/MasterServer.h
code/branches/presentation/src/libraries/network/MasterServerComm.cc
code/branches/presentation/src/libraries/network/MasterServerComm.h
code/branches/presentation/src/libraries/network/MasterServerProtocol.h
code/branches/presentation/src/libraries/network/PeerList.cc
code/branches/presentation/src/libraries/network/PeerList.h
code/branches/presentation/src/libraries/network/ServerList.cc
code/branches/presentation/src/libraries/network/ServerList.h
code/branches/presentation/src/libraries/network/WANDiscovery.cc
code/branches/presentation/src/libraries/network/WANDiscovery.h
code/branches/presentation/src/orxonox/gamestates/GSMasterServer.cc
code/branches/presentation/src/orxonox/gamestates/GSMasterServer.h
Modified:
code/branches/presentation/
code/branches/presentation/data/gui/scripts/MultiplayerMenu.lua
code/branches/presentation/src/libraries/network/CMakeLists.txt
code/branches/presentation/src/libraries/network/Server.cc
code/branches/presentation/src/libraries/network/Server.h
code/branches/presentation/src/libraries/network/packet/ServerInformation.h
code/branches/presentation/src/orxonox/Main.cc
code/branches/presentation/src/orxonox/gamestates/CMakeLists.txt
Log:
Merging masterserver branch to new presentation branch.
Property changes on: code/branches/presentation
___________________________________________________________________
Modified: svn:mergeinfo
- /code/branches/ai:6592-7033
/code/branches/buildsystem:1874-2276,2278-2400
/code/branches/buildsystem2:2506-2658
/code/branches/buildsystem3:2662-2708
/code/branches/ceguilua:1802-1808
/code/branches/chat:6527-6797
/code/branches/chat2:6836-6910
/code/branches/console:5941-6104
/code/branches/consolecommands2:6451-7178
/code/branches/consolecommands3:7178-7283
/code/branches/core3:1572-1739
/code/branches/core4:3221-3224,3227,3234-3238,3242,3244-3250,3252-3254,3256,3259-3261,3264-3265,3268-3275,3277-3278,3280,3284-3285,3287,3289-3294,3305,3309-3310
/code/branches/core5:5768-5928,6009
/code/branches/data_cleanup:7537-7686
/code/branches/doc:7290-7400
/code/branches/dynamicmatch:6584-7030
/code/branches/fps:6591-7072
/code/branches/gamestate:6430-6572,6621-6661
/code/branches/gamestates2:6594-6745
/code/branches/gametypes:2826-3031
/code/branches/gcc43:1580
/code/branches/gui:1635-1723,2795-2894
/code/branches/hudelements:6584-6941
/code/branches/ingamemenu:6000-6023
/code/branches/input:1629-1636
/code/branches/ipv6:7293-7458
/code/branches/lastmanstanding:7479-7644
/code/branches/libraries:5612-5692
/code/branches/libraries2:5703-5737
/code/branches/lod:6586-6911
/code/branches/lodfinal:2372-2411
/code/branches/map:2801-3086,3089
/code/branches/menu:5941-6146,6148,7536-7687
/code/branches/miniprojects:2754-2824
/code/branches/netp2:2835-2988
/code/branches/netp3:2988-3082
/code/branches/netp6:3214-3302
/code/branches/network:2356
/code/branches/network2:6434-6465
/code/branches/network64:2210-2355
/code/branches/notifications:7314-7401
/code/branches/objecthierarchy:1911-2085,2100,2110-2169
/code/branches/objecthierarchy2:2171-2479
/code/branches/overlay:2117-2385
/code/branches/particles:2829-3085
/code/branches/particles2:6050-6106,6109
/code/branches/pch:3113-3194
/code/branches/physics:1912-2055,2107-2439
/code/branches/physics_merge:2436-2457
/code/branches/pickup2:5942-6405
/code/branches/pickup3:6418-6523
/code/branches/pickup4:6594-6710
/code/branches/pickups:1926-2086,2127,2827-2915
/code/branches/pickups2:2107-2497,2915-3071
/code/branches/png2:7262-7263
/code/branches/ppspickups1:6552-6708
/code/branches/ppspickups2:6527-6532,6554-6709
/code/branches/ppspickups3:6757-6997
/code/branches/ppspickups4:7003-7089
/code/branches/presentation:2369-2652,2654-2660
/code/branches/presentation2:6106-6416
/code/branches/presentation3:6913-7162
/code/branches/questsystem:1894-2088
/code/branches/questsystem2:2107-2259
/code/branches/questsystem5:2776-2905
/code/branches/releasetodo:7614-7647
/code/branches/resource:3327-3366
/code/branches/resource2:3372-5694
/code/branches/rocket:6523-6950
/code/branches/rocket2:6953-6970
/code/branches/script_trigger:1295-1953,1955
/code/branches/skybox2:6559-6989
/code/branches/sound:2829-3010
/code/branches/sound3:5941-6102
/code/branches/steering:5949-6091
/code/branches/weapon:1925-2094
/code/branches/weapon2:2107-2488
/code/branches/weapons:2897-3051
/code/branches/weaponsystem:2742-2890
+ /code/branches/ai:6592-7033
/code/branches/buildsystem:1874-2276,2278-2400
/code/branches/buildsystem2:2506-2658
/code/branches/buildsystem3:2662-2708
/code/branches/ceguilua:1802-1808
/code/branches/chat:6527-6797
/code/branches/chat2:6836-6910
/code/branches/console:5941-6104
/code/branches/consolecommands2:6451-7178
/code/branches/consolecommands3:7178-7283
/code/branches/core3:1572-1739
/code/branches/core4:3221-3224,3227,3234-3238,3242,3244-3250,3252-3254,3256,3259-3261,3264-3265,3268-3275,3277-3278,3280,3284-3285,3287,3289-3294,3305,3309-3310
/code/branches/core5:5768-5928,6009
/code/branches/data_cleanup:7537-7686
/code/branches/doc:7290-7400
/code/branches/dynamicmatch:6584-7030
/code/branches/fps:6591-7072
/code/branches/gamestate:6430-6572,6621-6661
/code/branches/gamestates2:6594-6745
/code/branches/gametypes:2826-3031
/code/branches/gcc43:1580
/code/branches/gui:1635-1723,2795-2894
/code/branches/hudelements:6584-6941
/code/branches/ingamemenu:6000-6023
/code/branches/input:1629-1636
/code/branches/ipv6:7293-7458
/code/branches/lastmanstanding:7479-7644
/code/branches/libraries:5612-5692
/code/branches/libraries2:5703-5737
/code/branches/lod:6586-6911
/code/branches/lodfinal:2372-2411
/code/branches/map:2801-3086,3089
/code/branches/masterserver:7502-7738
/code/branches/menu:5941-6146,6148,7536-7687
/code/branches/miniprojects:2754-2824
/code/branches/netp2:2835-2988
/code/branches/netp3:2988-3082
/code/branches/netp6:3214-3302
/code/branches/network:2356
/code/branches/network2:6434-6465
/code/branches/network64:2210-2355
/code/branches/notifications:7314-7401
/code/branches/objecthierarchy:1911-2085,2100,2110-2169
/code/branches/objecthierarchy2:2171-2479
/code/branches/overlay:2117-2385
/code/branches/particles:2829-3085
/code/branches/particles2:6050-6106,6109
/code/branches/pch:3113-3194
/code/branches/physics:1912-2055,2107-2439
/code/branches/physics_merge:2436-2457
/code/branches/pickup2:5942-6405
/code/branches/pickup3:6418-6523
/code/branches/pickup4:6594-6710
/code/branches/pickups:1926-2086,2127,2827-2915
/code/branches/pickups2:2107-2497,2915-3071
/code/branches/png2:7262-7263
/code/branches/ppspickups1:6552-6708
/code/branches/ppspickups2:6527-6532,6554-6709
/code/branches/ppspickups3:6757-6997
/code/branches/ppspickups4:7003-7089
/code/branches/presentation:2369-2652,2654-2660
/code/branches/presentation2:6106-6416
/code/branches/presentation3:6913-7162
/code/branches/questsystem:1894-2088
/code/branches/questsystem2:2107-2259
/code/branches/questsystem5:2776-2905
/code/branches/releasetodo:7614-7647
/code/branches/resource:3327-3366
/code/branches/resource2:3372-5694
/code/branches/rocket:6523-6950
/code/branches/rocket2:6953-6970
/code/branches/script_trigger:1295-1953,1955
/code/branches/skybox2:6559-6989
/code/branches/sound:2829-3010
/code/branches/sound3:5941-6102
/code/branches/steering:5949-6091
/code/branches/weapon:1925-2094
/code/branches/weapon2:2107-2488
/code/branches/weapons:2897-3051
/code/branches/weaponsystem:2742-2890
Modified: code/branches/presentation/data/gui/scripts/MultiplayerMenu.lua
===================================================================
--- code/branches/presentation/data/gui/scripts/MultiplayerMenu.lua 2010-12-08 15:13:02 UTC (rev 7738)
+++ code/branches/presentation/data/gui/scripts/MultiplayerMenu.lua 2010-12-08 15:25:52 UTC (rev 7739)
@@ -41,10 +41,12 @@
function P.LanButton_clicked(e)
P.joinMode = 1
+ P.showServerList()
end
function P.InternetButton_clicked(e)
P.joinMode = 2
+ P.showServerList()
end
function P.MultiplayerHostButton_clicked(e)
@@ -54,21 +56,15 @@
function P.MultiplayerJoinButton_clicked(e)
local choice = winMgr:getWindow("orxonox/MultiplayerListbox"):getFirstSelectedItem()
-
- if P.joinMode == 2 then
- if choice then
- local client = orxonox.Client:getInstance()
- local index = tolua.cast(choice, "CEGUI::ListboxItem"):getID()
- client:setDestination( P.serverList[index][2], 55556 )
- else
- return
- end
- orxonox.execute("startClient")
- hideAllMenuSheets()
+ if choice then
+ local client = orxonox.Client:getInstance()
+ local index = tolua.cast(choice, "CEGUI::ListboxItem"):getID()
+ client:setDestination( P.serverList[index][2], 55556 )
else
- --wait for Sandro's function
+ return
end
-
+ orxonox.execute("startClient")
+ hideAllMenuSheets()
end
function P.MultiplayerBackButton_clicked(e)
@@ -76,34 +72,70 @@
end
function P.showServerList()
- local listbox = winMgr:getWindow("orxonox/MultiplayerListbox")
- CEGUI.toListbox(listbox):resetList()
- local discovery = orxonox.LANDiscovery:getInstance()
- discovery:discover()
- P.serverList = {}
- local index = 0
- local servername = ""
- local serverip = ""
- while true do
- servername = discovery:getServerListItemName(index)
- if servername == "" then
- break
- end
- serverip = discovery:getServerListItemIP(index)
- if serverip == "" then
- break
- end
- table.insert(P.serverList, {servername, serverip})
- index = index + 1
- end
- index = 1
- for k,v in pairs(P.serverList) do
- local item = CEGUI.createListboxTextItem( v[1] .. ": " .. v[2] )
- item:setID(index)
- index = index + 1
- item:setSelectionBrushImage(menuImageSet, "MultiListSelectionBrush")
- CEGUI.toListbox(listbox):addItem(item)
- end
+ -- LAN Discovery
+ if P.joinMode == 1 then
+ local listbox = winMgr:getWindow("orxonox/MultiplayerListbox")
+ CEGUI.toListbox(listbox):resetList()
+ local discovery = orxonox.LANDiscovery:getInstance()
+ discovery:discover()
+ P.serverList = {}
+ local index = 0
+ local servername = ""
+ local serverip = ""
+ while true do
+ servername = discovery:getServerListItemName(index)
+ if servername == "" then
+ break
+ end
+ serverip = discovery:getServerListItemIP(index)
+ if serverip == "" then
+ break
+ end
+ table.insert(P.serverList, {servername, serverip})
+ index = index + 1
+ end
+ index = 1
+ for k,v in pairs(P.serverList) do
+ local item = CEGUI.createListboxTextItem( v[1] .. ": " .. v[2] )
+ item:setID(index)
+ index = index + 1
+ item:setSelectionBrushImage(menuImageSet, "MultiListSelectionBrush")
+ CEGUI.toListbox(listbox):addItem(item)
+ end
+ -- WAN Discovery
+ elseif P.joinMode == 2 then
+ local listbox = winMgr:getWindow("orxonox/MultiplayerListbox")
+ CEGUI.toListbox(listbox):resetList()
+ local discovery = orxonox.WANDiscovery:getInstance()
+ cout(0, "discovering.\n" )
+ discovery:discover()
+ cout(0, "discovered.\n" )
+ P.serverList = {}
+ local index = 0
+ local servername = ""
+ local serverip = ""
+ while true do
+ servername = discovery:getServerListItemName(index)
+ if servername == "" then
+ break
+ end
+ serverip = discovery:getServerListItemIP(index)
+ if serverip == "" then
+ break
+ end
+ table.insert(P.serverList, {servername, serverip})
+ index = index + 1
+ end
+ index = 1
+ for k,v in pairs(P.serverList) do
+ local item = CEGUI.createListboxTextItem( v[1] .. ": " .. v[2] )
+ item:setID(index)
+ index = index + 1
+ item:setSelectionBrushImage(menuImageSet, "MultiListSelectionBrush")
+ CEGUI.toListbox(listbox):addItem(item)
+ end
+ end
+
end
function P.onKeyPressed()
Modified: code/branches/presentation/src/libraries/network/CMakeLists.txt
===================================================================
--- code/branches/presentation/src/libraries/network/CMakeLists.txt 2010-12-08 15:13:02 UTC (rev 7738)
+++ code/branches/presentation/src/libraries/network/CMakeLists.txt 2010-12-08 15:25:52 UTC (rev 7739)
@@ -31,9 +31,14 @@
GamestateHandler.cc
LANDiscoverable.cc
LANDiscovery.cc
+ WANDiscovery.cc
+ MasterServerComm.cc
NetworkFunction.cc
Host.cc
Server.cc
+ MasterServer.cc
+ PeerList.cc
+ ServerList.cc
ServerConnection.cc
TrafficControl.cc
)
@@ -53,10 +58,15 @@
Host.h
LANDiscoverable.h
LANDiscovery.h
+ WANDiscovery.h
+ MasterServerComm.h
NetworkFunction.h
NetworkPrecompiledHeaders.h
NetworkPrereqs.h
Server.h
+ MasterServer.h
+ PeerList.h
+ ServerList.h
ServerConnection.h
TrafficControl.h
)
@@ -68,6 +78,7 @@
TOLUA_FILES
Client.h
LANDiscovery.h
+ WANDiscovery.h
PCH_FILE
NetworkPrecompiledHeaders.h
LINK_LIBRARIES
Copied: code/branches/presentation/src/libraries/network/MasterServer.cc (from rev 7737, code/branches/masterserver/src/libraries/network/MasterServer.cc)
===================================================================
--- code/branches/presentation/src/libraries/network/MasterServer.cc (rev 0)
+++ code/branches/presentation/src/libraries/network/MasterServer.cc 2010-12-08 15:25:52 UTC (rev 7739)
@@ -0,0 +1,301 @@
+/*
+ * 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:
+ * Sandro 'smerkli' Merkli
+ * Co-authors:
+ * ...
+ *
+ */
+
+#include "MasterServer.h"
+#include "util/ScopedSingletonManager.h"
+#include "core/CoreIncludes.h"
+#include "core/CorePrereqs.h"
+
+namespace orxonox
+{
+ /* helpers */
+ static void
+ helper_output_debug( ENetEvent *event, char *addrconv )
+ {
+ COUT(4) << "A packet of length"
+ << event->packet->dataLength
+ << " containing "
+ << (const char*)event->packet->data
+ << " was received from "
+ << addrconv
+ << " on channel "
+ << event->channelID << "\n";
+ }
+
+ void
+ MasterServer::helper_sendlist( ENetEvent *event )
+ {
+ /* get an iterator */
+ std::list<packet::ServerInformation>::iterator i;
+
+ /* packet holder */
+ ENetPacket *reply;
+
+ /* loop through list elements */
+ for( i = mainlist.serverlist.begin(); i
+ != mainlist.serverlist.end(); ++i )
+ {
+ /* WORK MARK */
+ /* send this particular server */
+ /* build reply string */
+ char *tosend = (char *)calloc( (*i).getServerIP().length()
+ + MSPROTO_SERVERLIST_ITEM_LEN + 2,1 );
+ if( !tosend )
+ { COUT(2) << "Masterserver.cc: Memory allocation failed.\n";
+ continue;
+ }
+ sprintf( tosend, "%s %s", MSPROTO_SERVERLIST_ITEM,
+ (*i).getServerIP().c_str() );
+
+ /* create packet from it */
+ reply = enet_packet_create( tosend,
+ strlen( tosend ) + 1,
+ ENET_PACKET_FLAG_RELIABLE);
+
+ /* Send the reply to the peer over channel id 0. */
+ enet_peer_send( event->peer, 0, reply );
+
+ /* One could just use enet_host_service() instead. */
+ enet_host_flush( this->server );
+
+ /* free the tosend buffer */
+ free( tosend );
+ }
+
+ /* send end-of-list packet */
+ reply = enet_packet_create( MSPROTO_SERVERLIST_END,
+ MSPROTO_SERVERLIST_END_LEN + 1,
+ ENET_PACKET_FLAG_RELIABLE );
+
+ enet_peer_send( event->peer, 0, reply );
+
+ /* One could just use enet_host_service() instead. */
+ enet_host_flush( this->server );
+ }
+
+
+
+
+ /***** EVENTS *****/
+ /* connect event */
+ int
+ MasterServer::eventConnect( ENetEvent *event )
+ { /* check for bad parameters */
+ if( !event )
+ { COUT(2) << "MasterServer::eventConnect: No event given.\n" ;
+ return -1;
+ }
+
+ /* convert address to string. */
+ char *addrconv = (char *) calloc( 50, 1 );
+ enet_address_get_host_ip( &(event->peer->address), addrconv, 49 );
+
+ /* output debug info */
+ COUT(4) << "A new client connected from "
+ << addrconv
+ << " on port "
+ << event->peer->address.port << "\n";
+
+ /* store string form of address here */
+ event->peer->data = addrconv;
+
+ /* all fine. */
+ return 0;
+ }
+
+ /* disconnect event */
+ int
+ MasterServer::eventDisconnect( ENetEvent *event )
+ { /* check for bad parameters */
+ if( !event )
+ { COUT(2) << "No event given.\n";
+ return -1;
+ }
+
+ /* output that the disconnect happened */
+ COUT(4) << (char*)event->peer->data << " disconnected.\n";
+
+ /* create string from peer data */
+ std::string name = std::string( (char*)event->peer->data );
+
+ /* remove the server from the list it belongs to */
+ this->mainlist.delServerByName( name );
+
+ /* Reset the peer's client information. */
+ if( event->peer->data ) free( event->peer->data );
+
+ /* done */
+ return 0;
+ }
+
+ /* data event */
+ int
+ MasterServer::eventData( ENetEvent *event )
+ { /* validate packet */
+ if( !event || !(event->packet) || !(event->peer) )
+ //|| !(event->packet->data) || !strlen(event->packet->data) )
+ { COUT(2) << "No complete event given.\n";
+ return -1;
+ }
+
+ /* generate address in readable form */
+ char *addrconv = (char *) calloc( 50, 1 );
+ enet_address_get_host_ip( &(event->peer->address), addrconv, 49 );
+
+ /* DEBUG */
+ /* output debug info about the data that has come, to be removed */
+ helper_output_debug( event, addrconv );
+
+ /* GAME SERVER OR CLIENT CONNECTION? */
+ if( !strncmp( (char *)event->packet->data, MSPROTO_GAME_SERVER,
+ MSPROTO_GAME_SERVER_LEN ) )
+ { /* Game server */
+
+ if( !strncmp( (char *)event->packet->data
+ + MSPROTO_GAME_SERVER_LEN+1,
+ MSPROTO_REGISTER_SERVER, MSPROTO_REGISTER_SERVER_LEN ) )
+ { /* register new server */
+ mainlist.addServer( packet::ServerInformation( event ) );
+
+ /* tell people we did so */
+ COUT(2) << "Added new server to list: " <<
+ packet::ServerInformation( event ).getServerIP() << "\n";
+ }
+ }
+ else if( !strncmp( (char *)event->packet->data, MSPROTO_CLIENT,
+ MSPROTO_CLIENT_LEN) )
+ { /* client */
+ if( !strncmp( (char *)event->packet->data + MSPROTO_CLIENT_LEN+1,
+ MSPROTO_REQ_LIST, MSPROTO_REQ_LIST_LEN ) )
+ /* send server list */
+ helper_sendlist( event );
+ }
+ else
+ { /* bad message, don't do anything. */ }
+
+ /* delete addrconv */
+ if( addrconv ) free( addrconv );
+
+ /* Clean up the packet now that we're done using it. */
+ enet_packet_destroy( event->packet );
+ return 0;
+ }
+
+
+ /**** MAIN ROUTINE *****/
+ int
+ MasterServer::run()
+ {
+ /***** ENTER MAIN LOOP *****/
+ ENetEvent *event = (ENetEvent *)calloc(sizeof(ENetEvent), sizeof(char));
+ if( event == NULL )
+ {
+ COUT(1) << "Could not create ENetEvent structure, exiting.\n";
+ exit( EXIT_FAILURE );
+ }
+
+ /* tell people we're now initialized and blocking. */
+ COUT(0) << "MasterServer initialized, waiting for connections.\n";
+
+ /* endless loop until we quit */
+ while( this->quit == false )
+ {
+ /* create an iterator for the loop */
+ while( enet_host_service( this->server, event, 1000 ) >= 0 )
+ { /* check what type of event it is and react accordingly */
+ switch (event->type)
+ { /* new connection */
+ case ENET_EVENT_TYPE_CONNECT:
+ eventConnect( event ); break;
+
+ /* disconnect */
+ case ENET_EVENT_TYPE_DISCONNECT:
+ eventDisconnect( event ); break;
+
+ /* incoming data */
+ case ENET_EVENT_TYPE_RECEIVE: eventData( event ); break;
+ default: break;
+ }
+ }
+ }
+
+ /* done */
+ return 0;
+ }
+
+ /* constructor */
+ MasterServer::MasterServer()
+ {
+ /***** INITIALIZE NETWORKING *****/
+ if( enet_initialize () != 0)
+ { COUT(1) << "An error occurred while initializing ENet.\n";
+ exit( EXIT_FAILURE );
+ }
+
+ /* register deinitialization */
+ atexit( enet_deinitialize );
+
+ /* set the quit flag to false */
+ this->quit = false;
+
+ /* Bind the server to the default localhost and port ORX_MSERVER_PORT */
+ this->address.host = ENET_HOST_ANY;
+ this->address.port = ORX_MSERVER_PORT;
+
+ /* create a host with the above settings (the last two 0 mean: accept
+ * any input/output bandwidth */
+ this->server = enet_host_create( &this->address, ORX_MSERVER_MAXCONNS,
+ ORX_MSERVER_MAXCHANS, 0, 0 );
+
+ /* see if creation worked */
+ if( !this->server )
+ { COUT(1) <<
+ "An error occurred while trying to create an ENet server host.\n";
+ exit( EXIT_FAILURE );
+ }
+
+ /***** INITIALIZE GAME SERVER AND PEER LISTS *****/
+ //this->mainlist = new ServerList();
+ this->peers = new PeerList();
+ }
+
+ /* destructor */
+ MasterServer::~MasterServer()
+ {
+ /***** CLEANUP PROCESS *****/
+ /* terminate all networking connections */
+ enet_host_destroy( this->server );
+
+ /* free all used memory */
+ /* clear the list of connected game servers */
+ /* clear the list of connected game clients */
+
+ }
+
+/* end of namespace */
+}
Copied: code/branches/presentation/src/libraries/network/MasterServer.h (from rev 7737, code/branches/masterserver/src/libraries/network/MasterServer.h)
===================================================================
--- code/branches/presentation/src/libraries/network/MasterServer.h (rev 0)
+++ code/branches/presentation/src/libraries/network/MasterServer.h 2010-12-08 15:25:52 UTC (rev 7739)
@@ -0,0 +1,82 @@
+/*
+ * 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:
+ * Sandro 'smerkli' Merkli
+ * Co-authors:
+ * ...
+ *
+ */
+
+#ifndef _MasterServer_
+#define _MasterServer_
+
+/* orxonox includes */
+#include <enet/enet.h>
+#include <network/packet/Chat.h>
+#include <network/packet/ClassID.h>
+#include <network/packet/DeleteObjects.h>
+#include <network/packet/FunctionIDs.h>
+#include <network/packet/Gamestate.h>
+#include <network/packet/Welcome.h>
+#include <network/MasterServerProtocol.h>
+
+/* my includes */
+#include "ServerList.h"
+#include "PeerList.h"
+
+/* c compatibility */
+#include <cstdio>
+
+namespace orxonox
+{
+ /* singleton */
+ class MasterServer
+ {
+ public:
+ MasterServer();
+ ~MasterServer();
+
+ /* main routine */
+ int run();
+
+ private:
+ /* methods */
+ int eventConnect( ENetEvent *event );
+ int eventDisconnect( ENetEvent *event );
+ int eventData( ENetEvent *event );
+
+ /* helpers */
+ void helper_sendlist( ENetEvent *event );
+
+ /* members */
+ ENetAddress address;
+ ENetHost *server;
+ ServerList mainlist;
+ PeerList *peers;
+
+ unsigned int port;
+ bool quit;
+
+ };
+}
+
+#endif /* _MasterServer_ */
Copied: code/branches/presentation/src/libraries/network/MasterServerComm.cc (from rev 7737, code/branches/masterserver/src/libraries/network/MasterServerComm.cc)
===================================================================
--- code/branches/presentation/src/libraries/network/MasterServerComm.cc (rev 0)
+++ code/branches/presentation/src/libraries/network/MasterServerComm.cc 2010-12-08 15:25:52 UTC (rev 7739)
@@ -0,0 +1,253 @@
+/*
+ * 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:
+ * Sandro 'smerkli' Merkli
+ * Co-authors:
+ * ...
+ *
+ */
+
+#include "MasterServerComm.h"
+
+namespace orxonox
+{
+
+ MasterServerComm::MasterServerComm()
+ { /* nothing anymore, everything's been outsourced to
+ * the initialize method to facilitate debugging
+ */
+ }
+
+
+ int MasterServerComm::initialize()
+ {
+ /* initialize Enet */
+ if (enet_initialize () != 0)
+ { COUT(1) << "An error occurred while initializing ENet.\n";
+ return 1;
+ }
+
+ /* initialize the event holder */
+ this->event = (ENetEvent *)calloc( sizeof(ENetEvent), 1 );
+
+ /* install atexit handler for enet */
+ atexit( enet_deinitialize );
+
+
+ /* initiate the client */
+ this->client = enet_host_create( NULL /* create a client host */,
+ 1,
+ 2, /* allow up 2 channels to be used, 0 and 1 */
+ 0,
+ 0 );
+
+ /* see if it worked */
+ if (this->client == NULL)
+ { COUT(1) << "An error occurred while trying to create an ENet client host.\n";
+ return 1;
+ }
+
+ return 0;
+ }
+
+ MasterServerComm::~MasterServerComm()
+ {
+ /* destroy the enet facilities */
+ enet_host_destroy(this->client);
+ }
+
+ int MasterServerComm::connect( const char *address, unsigned int port )
+ {
+ /* Connect to address:port. */
+ enet_address_set_host( &this->address, address );
+ this->address.port = port;
+
+ /* Initiate the connection, allocating the two channels 0 and 1. */
+ this->peer = enet_host_connect(this->client, &this->address, 2, 0);
+
+ if (this->peer == NULL )
+ { COUT(2) << "ERROR: No available peers for initiating an ENet connection.\n";
+ //exit (EXIT_FAILURE);
+ return -1;
+ }
+
+ /* Wait up to 2 seconds for the connection attempt to succeed. */
+ if (enet_host_service (this->client, this->event, 2000) > 0 &&
+ this->event->type == ENET_EVENT_TYPE_CONNECT )
+ fprintf( stdout, "Connection to server succeeded." );
+ else
+ {
+ enet_peer_reset (this->peer);
+ fprintf( stdout, "Connection to %s failed.", address );
+ //exit(EXIT_FAILURE);
+ return -1;
+ }
+
+ return 0;
+ }
+
+ int MasterServerComm::pollForReply( int (*callback)( char*, ENetEvent* ) )
+ {
+ /* see whether anything happened */
+ /* WORK MARK REMOVE THIS OUTPUT */
+ COUT(2) << "polling masterserver...\n";
+
+ /* address buffer */
+ char *addrconv = NULL;
+ int retval = 0;
+
+ /* 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, 1000 ) > 0 )
+ {
+ /* check what type of event it is and react accordingly */
+ switch (this->event->type)
+ { /* new connection, not supposed to happen. */
+ case ENET_EVENT_TYPE_CONNECT: break;
+
+ /* disconnect */
+ case ENET_EVENT_TYPE_DISCONNECT: /* ?? */ break;
+
+ /* incoming data */
+ case ENET_EVENT_TYPE_RECEIVE:
+ addrconv = (char *) calloc( 50, 1 );
+ if( !addrconv )
+ { COUT(2) << "MasterServerComm.cc: Could not allocate memory!\n";
+ break;
+ }
+
+ /* resolve IP */
+ enet_address_get_host_ip( &(this->event->peer->address),
+ addrconv, 49 );
+
+ /* DEBUG */
+ COUT(3) << "A packet of length " << this->event->packet->dataLength
+ << " containing " << this->event->packet->data
+ << " was received from " << addrconv
+ << " on channel " << this->event->channelID;
+ //printf( "A packet of length %u containing %s was "
+ //"received from %s on channel %u.\n",
+ //this->event->packet->dataLength,
+ //this->event->packet->data,
+ //addrconv,
+ //this->event->channelID );
+ /* END DEBUG */
+
+ /* call the supplied callback, if any. */
+ if( (*callback) != NULL )
+ retval = (*callback)( addrconv, (this->event) );
+
+ enet_packet_destroy( event->packet );
+ if( addrconv ) free( addrconv );
+ break;
+ default: break;
+ }
+
+ /* event handled, return 0 */
+ return retval;
+ }
+
+ /* show that no event occured */
+ return 0;
+ }
+
+ int MasterServerComm::sendRequest( const char *data )
+ {
+ /* send the data to the friend */
+ /* Create a reliable packet of size 7 containing "packet\0" */
+ ENetPacket * packet = enet_packet_create( data,
+ strlen( data ) + 1,
+ ENET_PACKET_FLAG_RELIABLE);
+
+ /* Send the packet to the peer over channel id 0. */
+ enet_peer_send (this->peer, 0, packet);
+
+ /* One could just use enet_host_service() instead. */
+ enet_host_flush( this->client );
+
+ /* free the packet */
+ enet_packet_destroy( packet );
+
+ /* all done. */
+ return 0;
+ }
+
+ int MasterServerComm::sendRequest( std::string data )
+ {
+ /* send the data to the friend */
+ /* Create a reliable packet of size 7 containing "packet\0" */
+ ENetPacket * packet = enet_packet_create( data.c_str(),
+ data.length() + 1,
+ ENET_PACKET_FLAG_RELIABLE);
+
+ /* Send the packet to the peer over channel id 0. */
+ enet_peer_send (this->peer, 0, packet);
+
+ /* One could just use enet_host_service() instead. */
+ enet_host_flush( this->client );
+ if( packet ) free( packet );
+
+ /* all done. */
+ return 0;
+ }
+
+}
+
+
+/* DON'T DELETE THIS I MIGHT NEED IT AGAIN -smerkli */
+/* not needed anymore, only here for testing purposes */
+/*
+//[> sample callback to output debugging info. <]
+//int callb( char *addr, ENetEvent *ev )
+//{
+ //printf( "A packet of length %u containing %s was "
+ //"received from %s on channel %u.\n",
+ //ev->packet->dataLength,
+ //ev->packet->data,
+ //addr,
+ //ev->channelID );
+ //return 0;
+//}
+
+//[> small testing implementation <]
+//int
+//main( int argc, char *argv[] )
+//{
+ //[> setup object and connect <]
+ //MasterServerComm msc = MasterServerComm();
+ //if( msc.connect( argv[1], 1234 ) )
+ //exit(EXIT_FAILURE);
+
+ //[> send some data and poll for replies <]
+ //char *theinput = (char *)calloc( 100,1 );
+ //while( true )
+ //{
+ //fgets( theinput, 90, stdin );
+ //if( !strncmp( theinput, "quit", 4 ) )
+ //break;
+
+ //msc.sendRequest( theinput );
+ //msc.pollForReply( &callb );
+ //}
+
+//}
+*/
Copied: code/branches/presentation/src/libraries/network/MasterServerComm.h (from rev 7737, code/branches/masterserver/src/libraries/network/MasterServerComm.h)
===================================================================
--- code/branches/presentation/src/libraries/network/MasterServerComm.h (rev 0)
+++ code/branches/presentation/src/libraries/network/MasterServerComm.h 2010-12-08 15:25:52 UTC (rev 7739)
@@ -0,0 +1,96 @@
+/*
+ * 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:
+ * Sandro 'smerkli' Merkli
+ * Co-authors:
+ * ...
+ *
+ */
+
+#include <cstdlib>
+#include <cstdio>
+#include <string>
+#include <cstring>
+#include <enet/enet.h>
+
+namespace orxonox
+{
+ class MasterServerComm
+ {
+ public:
+ /** constructor */
+ MasterServerComm();
+
+ /** destructor */
+ ~MasterServerComm();
+
+ /** \return 0 for success, other for error
+ *
+ * Initialize everything for the master server communication
+ */
+ int initialize();
+
+
+ /** \param address Address to connect to (Host name or IP in text form)
+ * \param port Port to connect on
+ * \return 0 for success, other for error
+ *
+ * Connect to the master server with the given address on the given port.
+ */
+ int connect( const char *address, unsigned int port );
+
+ /** \param data The data to be sent.
+ * \return 0 for success, other for error.
+ *
+ * Send a request to the master server containing data specified in data
+ */
+ int sendRequest( const char *data );
+
+ /** \param data The data to be sent.
+ * \return 0 for success, other for error.
+ *
+ * Send a request to the master server containing data specified in data
+ * (string version)
+ */
+ int sendRequest( std::string data );
+
+ /** \param callback The callback function to call with data receivced.
+ * \return 0 for success, other for error
+ *
+ * Poll the master server for new data and act accordingly */
+ int pollForReply( int (*callback)( char*, ENetEvent* ) );
+
+ private:
+ /** client handle */
+ ENetHost *client;
+
+ /** event data holder */
+ ENetEvent *event;
+
+ /** address holder */
+ ENetAddress address;
+
+ /** peer data holder */
+ ENetPeer *peer;
+ };
+
+}
Copied: code/branches/presentation/src/libraries/network/MasterServerProtocol.h (from rev 7737, code/branches/masterserver/src/libraries/network/MasterServerProtocol.h)
===================================================================
--- code/branches/presentation/src/libraries/network/MasterServerProtocol.h (rev 0)
+++ code/branches/presentation/src/libraries/network/MasterServerProtocol.h 2010-12-08 15:25:52 UTC (rev 7739)
@@ -0,0 +1,69 @@
+/*
+ * 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:
+ * Sandro 'smerkli' Merkli
+ * Co-authors:
+ * ...
+ *
+ */
+
+#ifndef MASTER_SERVER_PROTO
+#define MASTER_SERVER_PROTO
+
+/* master server address (to be moved elsewhere later) */
+#define MS_ADDRESS "129.132.3.8"
+
+#define MSPROTO_CLIENT "CL"
+#define MSPROTO_CLIENT_LEN 2
+
+#define MSPROTO_REQ_LIST "REQ:LIST"
+#define MSPROTO_REQ_LIST_LEN 8
+
+
+
+
+#define MSPROTO_GAME_SERVER "GS"
+#define MSPROTO_GAME_SERVER_LEN 2
+
+#define MSPROTO_REGISTER_SERVER "REG:SER"
+#define MSPROTO_REGISTER_SERVER_LEN 7
+
+#define MSPROTO_SERVERLIST_ITEM "SI"
+#define MSPROTO_SERVERLIST_ITEM_LEN 2
+#define MSPROTO_SERVERLIST_END "SL_END"
+#define MSPROTO_SERVERLIST_END_LEN 6
+
+
+
+
+
+
+#define ORX_MSERVER_PORT 55566
+
+
+#define ORX_MSERVER_MAXCONNS 32
+#define ORX_MSERVER_MAXCHANS 2
+
+
+
+
+#endif /* MASTER_SERVER_PROTO */
Copied: code/branches/presentation/src/libraries/network/PeerList.cc (from rev 7737, code/branches/masterserver/src/libraries/network/PeerList.cc)
===================================================================
--- code/branches/presentation/src/libraries/network/PeerList.cc (rev 0)
+++ code/branches/presentation/src/libraries/network/PeerList.cc 2010-12-08 15:25:52 UTC (rev 7739)
@@ -0,0 +1,96 @@
+/*
+ * 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:
+ * Sandro 'smerkli' Merkli
+ * Co-authors:
+ * ...
+ *
+ */
+
+#include "PeerList.h"
+#include <network/packet/ServerInformation.h>
+#include <cstdio>
+
+namespace orxonox
+{
+ PeerList::PeerList() { }
+ PeerList::~PeerList() { }
+
+ int
+ PeerList::addPeer( ENetPeer *toadd )
+ { /* error correction */
+ if( toadd == NULL )
+ { fprintf( stderr, "PeerList::addPeer: empty peer given.\n" );
+ return -1;
+ }
+
+ /* if all went well, push to back of list */
+ this->peerlist.push_back( toadd );
+ return 0;
+ }
+
+ bool sub_compAddr( ENetAddress addr1, ENetAddress addr2 )
+ {
+ for( int i = 0; i < 16; ++i )
+ if( addr1.host.addr[i] < addr2.host.addr[i] )
+ return -i;
+ else if( addr1.host.addr[i] > addr2.host.addr[i] )
+ return i;
+
+ return 0;
+ }
+
+
+ bool
+ PeerList::remPeerByAddr( ENetAddress addr )
+ { /* get an iterator */
+ std::list<ENetPeer *>::iterator i;
+
+ /* loop through list elements */
+ for( i = peerlist.begin(); i != peerlist.end(); ++i )
+ if( !sub_compAddr((*i)->address, addr ) )
+ { /* found this name, remove and quit */
+ this->peerlist.remove( *i );
+ return true;
+ }
+
+ /* not found */
+ return false;
+ }
+
+ ENetPeer *
+ PeerList::findPeerByAddr( ENetAddress addr )
+ { /* get an iterator */
+ std::list<ENetPeer *>::iterator i;
+
+ /* loop through list elements */
+ for( i = peerlist.begin(); i != peerlist.end(); ++i )
+ if( !sub_compAddr((*i)->address, addr ) )
+ /* found this name, remove and quit */
+ return *i;
+
+ /* not found */
+ return NULL;
+ }
+
+}
+
Copied: code/branches/presentation/src/libraries/network/PeerList.h (from rev 7737, code/branches/masterserver/src/libraries/network/PeerList.h)
===================================================================
--- code/branches/presentation/src/libraries/network/PeerList.h (rev 0)
+++ code/branches/presentation/src/libraries/network/PeerList.h 2010-12-08 15:25:52 UTC (rev 7739)
@@ -0,0 +1,83 @@
+/*
+ * 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:
+ * Sandro 'smerkli' Merkli
+ * Co-authors:
+ * ...
+ *
+ */
+
+#ifndef _PeerList_
+#define _PeerList_
+
+#include <list>
+#include <string>
+#include <enet/enet.h>
+
+/* peer list */
+namespace orxonox
+{
+ /** This class keeps a list of open connections
+ * and some info about them.
+ */
+ class PeerList
+ { public:
+ /** constructor */
+ PeerList();
+
+ /** destructor */
+ ~PeerList();
+
+ /** \param toadd The peer to add
+ * \return 0 for success, -1 for error.
+ *
+ * Add new peer to list
+ */
+ int addPeer( ENetPeer *toadd );
+
+ /** \param addr Address to look for
+ * \return if the peer was found or not
+ *
+ * Remove peer from list by address
+ */
+ bool remPeerByAddr( ENetAddress addr );
+
+ /** \param addr The address to find by
+ *
+ * Find a connection by address */
+ ENetPeer *findPeerByAddr( ENetAddress addr );
+
+ /* NOTE: making this list public so it can easily
+ * be iterated. This makes sense since iterating it
+ * will happen all the time, and using getter methods
+ * for the next in list would slow things down unnecessarily.
+ */
+ /** the list of servers for internal storage */
+ std::list<ENetPeer *> peerlist;
+ private:
+ /* nothing so far. */
+ };
+}
+
+
+
+#endif /* _PeerList_ */
Modified: code/branches/presentation/src/libraries/network/Server.cc
===================================================================
--- code/branches/presentation/src/libraries/network/Server.cc 2010-12-08 15:13:02 UTC (rev 7738)
+++ code/branches/presentation/src/libraries/network/Server.cc 2010-12-08 15:25:52 UTC (rev 7739)
@@ -98,6 +98,26 @@
{
}
+
+ /* TODO */
+ void Server::helper_ConnectToMasterserver()
+ {
+ /* initialize it and see if it worked */
+ if( msc.initialize() )
+ { COUT(1) << "Error: could not initialize master server communications!\n";
+ return;
+ }
+
+ /* connect and see if it worked */
+ if( msc.connect( MS_ADDRESS, 1234 ) )
+ { COUT(1) << "Error: could not connect to master server!\n";
+ return;
+ }
+
+ /* now send the master server some note we're here */
+ msc.sendRequest( MSPROTO_GAME_SERVER " " MSPROTO_REGISTER_SERVER );
+ }
+
/**
* This function opens the server by creating the listener thread
*/
@@ -106,7 +126,14 @@
Host::setActive(true);
COUT(4) << "opening server" << endl;
this->openListener();
+
+ /* make discoverable on LAN */
LANDiscoverable::setActivity(true);
+
+ /* make discoverable on WAN */
+ helper_ConnectToMasterserver();
+
+ /* done */
return;
}
@@ -139,6 +166,20 @@
}
+ /* TODO */
+ int rephandler( char *addr, ENetEvent *ev )
+ {
+ /* handle incoming data */
+
+ /* done handling, return all ok code 0 */
+ return 0;
+ }
+
+ void Server::helper_HandleMasterServerRequests()
+ {
+ this->msc.pollForReply( rephandler );
+ }
+
/**
* Run this function once every tick
* calls processQueue and updateGamestate
@@ -148,9 +189,13 @@
{
// receive incoming packets
Connection::processQueue();
+
// receive and process incoming discovery packets
LANDiscoverable::update();
+ // receive and process requests from master server
+ helper_HandleMasterServerRequests();
+
if ( ClientInformation::hasClients() )
{
// process incoming gamestates
Modified: code/branches/presentation/src/libraries/network/Server.h
===================================================================
--- code/branches/presentation/src/libraries/network/Server.h 2010-12-08 15:13:02 UTC (rev 7738)
+++ code/branches/presentation/src/libraries/network/Server.h 2010-12-08 15:25:52 UTC (rev 7739)
@@ -37,7 +37,10 @@
#include "GamestateManager.h"
#include "ServerConnection.h"
#include "LANDiscoverable.h"
+#include "MasterServerComm.h"
+#include "MasterServerProtocol.h"
+
namespace orxonox
{
@@ -52,6 +55,11 @@
Server(int port, const std::string& bindAddress);
~Server();
+ /* helpers */
+ void helper_ConnectToMasterserver();
+ void helper_HandleMasterServerRequests();
+ int replyhandler( char *addr, ENetEvent *ev );
+
void open();
void close();
bool processChat(const std::string& message, unsigned int playerID);
@@ -81,6 +89,7 @@
void syncClassid(unsigned int clientID);
float timeSinceLastUpdate_;
+ MasterServerComm msc;
};
Copied: code/branches/presentation/src/libraries/network/ServerList.cc (from rev 7737, code/branches/masterserver/src/libraries/network/ServerList.cc)
===================================================================
--- code/branches/presentation/src/libraries/network/ServerList.cc (rev 0)
+++ code/branches/presentation/src/libraries/network/ServerList.cc 2010-12-08 15:25:52 UTC (rev 7739)
@@ -0,0 +1,102 @@
+/*
+ * 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:
+ * Sandro 'smerkli' Merkli
+ * Co-authors:
+ * ...
+ *
+ */
+
+#include "ServerList.h"
+
+namespace orxonox
+{
+ ServerList::ServerList()
+ { /* create a new list */ }
+
+ ServerList::~ServerList()
+ { /* delete the list */
+ serverlist.clear();
+ }
+
+ int
+ ServerList::addServer( packet::ServerInformation toadd )
+ { this->serverlist.push_back( toadd );
+ return 0;
+ }
+
+ bool
+ ServerList::delServerByName( std::string name )
+ {
+ /* get an iterator */
+ std::list<packet::ServerInformation>::iterator i;
+
+ /* loop through list elements */
+ for( i = serverlist.begin(); i != serverlist.end(); ++i )
+ if( (*i).getServerName() == name )
+ { /* found this name, remove and quit */
+ this->serverlist.erase( i );
+ return true;
+ }
+ return false;
+ }
+
+ bool ServerList::delServerByAddress( std::string address )
+ {
+ /* get an iterator */
+ std::list<packet::ServerInformation>::iterator i;
+
+ /* loop through list elements */
+ for( i=serverlist.begin(); i != serverlist.end(); ++i )
+ if( (*i).getServerIP() == address )
+ { /* found this name, remove and quit */
+ this->serverlist.erase( i );
+ return true;
+ }
+ return false;
+ }
+
+
+ /* sort by name */
+ bool sub_compare_names( packet::ServerInformation no1,
+ packet::ServerInformation no2 )
+ { return no1.getServerName() > no2.getServerName(); }
+
+ void ServerList::sortByName()
+ {
+ this->serverlist.sort( sub_compare_names );
+ }
+
+ /* sort by ping */
+ bool sub_compare_pings( packet::ServerInformation no1,
+ packet::ServerInformation no2 )
+ {
+ /* TODO */
+ return no1.getServerName() > no2.getServerName();
+ }
+
+ void ServerList::sortByPing()
+ {
+ this->serverlist.sort( sub_compare_pings );
+ }
+
+}
Copied: code/branches/presentation/src/libraries/network/ServerList.h (from rev 7737, code/branches/masterserver/src/libraries/network/ServerList.h)
===================================================================
--- code/branches/presentation/src/libraries/network/ServerList.h (rev 0)
+++ code/branches/presentation/src/libraries/network/ServerList.h 2010-12-08 15:25:52 UTC (rev 7739)
@@ -0,0 +1,85 @@
+/*
+ * 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:
+ * Sandro 'smerkli' Merkli
+ * Co-authors:
+ * ...
+ *
+ */
+
+#ifndef _ServerList_
+#define _ServerList_
+
+#include <list>
+#include <string>
+#include <network/packet/ServerInformation.h>
+
+/* methods necessary */
+namespace orxonox
+{
+ /** This class is keeps a list of game servers
+ * and some info about them.
+ */
+ class ServerList
+ { public:
+ /** constructor */
+ ServerList();
+
+ /** destructor */
+ ~ServerList();
+
+
+ /* BASIC MANIPULATION */
+ /** \param toadd the server to add.
+ *
+ * Add server to the game server list
+ */
+ int addServer( packet::ServerInformation toadd );
+
+ /** \param name Name of the server to remove
+ *
+ * Remove server by name
+ */
+ bool delServerByName( std::string name );
+
+ /** \param address IP address of the server to remove
+ *
+ * Remove server by address
+ */
+ bool delServerByAddress( std::string address );
+
+
+ /* SORTING (to be implemented) */
+
+ /** sort by name */
+ void sortByName();
+
+ /** sort by ping */
+ void sortByPing();
+
+ /** the list of servers for internal storage */
+ std::list<packet::ServerInformation> serverlist;
+ private:
+ };
+}
+
+#endif /*_ServerList_*/
Copied: code/branches/presentation/src/libraries/network/WANDiscovery.cc (from rev 7737, code/branches/masterserver/src/libraries/network/WANDiscovery.cc)
===================================================================
--- code/branches/presentation/src/libraries/network/WANDiscovery.cc (rev 0)
+++ code/branches/presentation/src/libraries/network/WANDiscovery.cc 2010-12-08 15:25:52 UTC (rev 7739)
@@ -0,0 +1,164 @@
+/*
+ * 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 "WANDiscovery.h"
+
+#include <enet/enet.h>
+#include <cstring>
+
+#include "util/ScopedSingletonManager.h"
+#include "core/CoreIncludes.h"
+
+
+namespace orxonox
+{
+ ManageScopedSingleton(WANDiscovery, ScopeID::Root, true);
+
+
+ WANDiscovery::WANDiscovery()
+ {
+ /* debugging output */
+ COUT(4) << "Creating WANDiscovery.\n";
+
+ /* register object with orxonox main class */
+ RegisterObject(WANDiscovery);
+
+ /* ... and register a config value function for it */
+ this->setConfigValues();
+
+ /* initialize it and see if it worked */
+ if( msc.initialize() )
+ COUT(2) << "Error: could not initialize master server communications!\n";
+
+ /* connect and see if it worked */
+ if( msc.connect( this->msaddress.c_str(), 1234 ) )
+ COUT(2) << "Error: could not connect to master server at "
+ << this->msaddress << std::endl;
+
+ /* debugging output */
+ COUT(4) << "Initialization of WANDiscovery complete.\n";
+ }
+
+ void WANDiscovery::setConfigValues()
+ {
+ SetConfigValue( msaddress, "localhost");
+ }
+
+ WANDiscovery::~WANDiscovery()
+ {
+ /* clear server list */
+ this->servers_.clear();
+ }
+
+ /* callback for the network reply poller */
+ int rhandler( char *addr, ENetEvent *ev )
+ {
+ /* error recognition */
+ if( !ev || !ev->packet || !ev->packet->data )
+ { COUT(2) << "Bad arguments received in WANDiscovery's reply handler.\n";
+ return 0;
+ }
+
+ /* handle incoming data */
+ /* if a list entry arrives add to list */
+ if( !strncmp( (char*)ev->packet->data, MSPROTO_SERVERLIST_ITEM,
+ MSPROTO_SERVERLIST_ITEM_LEN ) )
+ {
+ /* create server structure from that item */
+ packet::ServerInformation toadd;
+
+ /* fill in data */
+ toadd.setServerName( std::string((char*)ev->packet->data +
+ MSPROTO_SERVERLIST_ITEM_LEN) );
+ toadd.setServerIP( std::string((char*)ev->packet->data +
+ MSPROTO_SERVERLIST_ITEM_LEN) );
+
+ /* add to list */
+ WANDiscovery::getInstance().servers_.push_back( toadd );
+ }
+ else if( !strncmp( (char*)ev->packet->data, MSPROTO_SERVERLIST_END,
+ MSPROTO_SERVERLIST_END_LEN ) )
+ {
+ /* this is the only case where 1 should be returned,
+ * as 1 is used to signal that we're done receiving
+ * the list
+ */
+ return 2;
+ }
+
+ /* done handling, return all ok code 0 */
+ return 1;
+ }
+
+ void WANDiscovery::discover()
+ {
+ /* clear list */
+ this->servers_.clear();
+
+ /* send request to server */
+ this->msc.sendRequest( MSPROTO_CLIENT " " MSPROTO_REQ_LIST );
+
+ /* poll for replies */
+ int i = WANDISC_MAXTRIES;
+ while( i > 0 )
+ {
+ /* poll for reply and act according to what was received */
+ switch( this->msc.pollForReply( rhandler ) )
+ { case 0: /* no event occured, decrease timeout */
+ --i; break;
+ case 1: /* got a list element, continue */
+ break;
+ case 2: /* done. */
+ i = 0; break;
+ }
+ }
+ }
+
+ std::string WANDiscovery::getServerListItemName(unsigned int index)
+ {
+ /* if the index is out of range, return empty */
+ if( index >= this->servers_.size() )
+ return BLANKSTRING;
+ else
+ /* else return the name of the server */
+ return this->servers_[index].getServerName();
+ }
+
+ std::string WANDiscovery::getServerListItemIP(unsigned int index)
+ {
+ /* if the index is out of range, return empty */
+ if( index >= this->servers_.size() )
+ return BLANKSTRING;
+ else
+ /* else return the IP of the server */
+ return this->servers_[index].getServerIP();
+ }
+
+
+} // namespace orxonox
Copied: code/branches/presentation/src/libraries/network/WANDiscovery.h (from rev 7737, code/branches/masterserver/src/libraries/network/WANDiscovery.h)
===================================================================
--- code/branches/presentation/src/libraries/network/WANDiscovery.h (rev 0)
+++ code/branches/presentation/src/libraries/network/WANDiscovery.h 2010-12-08 15:25:52 UTC (rev 7739)
@@ -0,0 +1,108 @@
+/*
+ * 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 WANDISCOVERY_H
+#define WANDISCOVERY_H
+
+#include "NetworkPrereqs.h"
+#include "packet/ServerInformation.h"
+#include "core/ConfigFileManager.h"
+#include "util/Singleton.h"
+#include "core/OrxonoxClass.h"
+#include "core/ConfigValueIncludes.h"
+#include "core/CoreIncludes.h"
+#include "MasterServerComm.h"
+#include "MasterServerProtocol.h"
+
+#include <vector>
+
+#define WANDISC_MAXTRIES 5
+
+// tolua_begin
+namespace orxonox
+{
+
+ class _NetworkExport WANDiscovery
+// tolua_end
+ : public Singleton<WANDiscovery>, public OrxonoxClass
+ { // tolua_export
+ friend class Singleton<WANDiscovery>;
+ public:
+ /** constructor */
+ WANDiscovery();
+
+ /** destructor */
+ ~WANDiscovery();
+
+ /** ask server for server list */
+ void discover(); // tolua_export
+
+ /** \param index Index to get the name of
+ * \return The name of the server
+ *
+ * Get the name of the server at index index.
+ */
+ std::string getServerListItemName( unsigned int index ); // tolua_export
+
+ /** \param index Index to get the IP of
+ * \return The IP of the server
+ *
+ * Get the IP of the server at index index.
+ */
+ std::string getServerListItemIP( unsigned int index ); // tolua_export
+
+ /** \return an instance of WANDiscovery
+ *
+ * Create and return an instance of WANDiscovery.
+ */
+ static WANDiscovery& getInstance() { return Singleton<WANDiscovery>::getInstance(); } // tolua_export
+
+ /* todo: might make this private and use getter/setter methods
+ * at some later time.
+ */
+ /** game server list */
+ std::vector<packet::ServerInformation> servers_;
+
+ /** Function used for the configuration file parameter update */
+ void setConfigValues();
+
+ private:
+ /** Singleton pointer */
+ static WANDiscovery* singletonPtr_s;
+
+ /** Master server communications object */
+ MasterServerComm msc;
+
+ /** master server address */
+ std::string msaddress;
+
+ }; // tolua_export
+
+} // tolua_export
+
+#endif // WANDISCOVERY_H
Modified: code/branches/presentation/src/libraries/network/packet/ServerInformation.h
===================================================================
--- code/branches/presentation/src/libraries/network/packet/ServerInformation.h 2010-12-08 15:13:02 UTC (rev 7738)
+++ code/branches/presentation/src/libraries/network/packet/ServerInformation.h 2010-12-08 15:25:52 UTC (rev 7739)
@@ -49,6 +49,7 @@
std::string getServerIP() { return this->serverIP_; }
std::string getServerName() { return this->serverName_; }
void setServerName(std::string name) { this->serverName_ = name; }
+ void setServerIP( std::string IP ) { this->serverIP_ = IP; }
uint32_t getServerRTT() { return this->serverRTT_; }
private:
Modified: code/branches/presentation/src/orxonox/Main.cc
===================================================================
--- code/branches/presentation/src/orxonox/Main.cc 2010-12-08 15:13:02 UTC (rev 7738)
+++ code/branches/presentation/src/orxonox/Main.cc 2010-12-08 15:25:52 UTC (rev 7739)
@@ -53,6 +53,9 @@
SetCommandLineSwitch(standalone).information("Start in standalone mode");
SetCommandLineSwitch(dedicatedClient).information("Start in dedicated client mode");
+ /* ADD masterserver command */
+ SetCommandLineSwitch(masterserver).information("Start in masterserver mode");
+
SetCommandLineArgument(generateDoc, "")
.information("Generates a Doxygen file from things like SetConsoleCommand");
@@ -66,6 +69,7 @@
if (CommandLineParser::getValue("generateDoc").getString().empty())
{
+ /* TODO make this clear */
game->setStateHierarchy(
"root"
" graphics"
@@ -89,6 +93,9 @@
Game::getInstance().requestStates("server, level");
else if (CommandLineParser::getValue("dedicatedClient").getBool())
Game::getInstance().requestStates("client, level");
+ /* ADD masterserver command */
+ else if (CommandLineParser::getValue("masterserver").getBool())
+ Game::getInstance().requestStates("masterserver");
else
{
if (!CommandLineParser::getValue("console").getBool())
Modified: code/branches/presentation/src/orxonox/gamestates/CMakeLists.txt
===================================================================
--- code/branches/presentation/src/orxonox/gamestates/CMakeLists.txt 2010-12-08 15:13:02 UTC (rev 7738)
+++ code/branches/presentation/src/orxonox/gamestates/CMakeLists.txt 2010-12-08 15:25:52 UTC (rev 7739)
@@ -5,5 +5,6 @@
GSMainMenu.cc
GSRoot.cc
GSServer.cc
+ GSMasterServer.cc
GSStandalone.cc
)
Copied: code/branches/presentation/src/orxonox/gamestates/GSMasterServer.cc (from rev 7737, code/branches/masterserver/src/orxonox/gamestates/GSMasterServer.cc)
===================================================================
--- code/branches/presentation/src/orxonox/gamestates/GSMasterServer.cc (rev 0)
+++ code/branches/presentation/src/orxonox/gamestates/GSMasterServer.cc 2010-12-08 15:25:52 UTC (rev 7739)
@@ -0,0 +1,66 @@
+/*
+ * 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:
+ * Sandro 'smerkli' Merkli
+ * Co-authors:
+ * Reto Grieder (original file for GSServer.cc)
+ * Fabian 'x3n' Landau (original file for GSServer.cc)
+ *
+ */
+
+#include "GSMasterServer.h"
+
+#include "util/Debug.h"
+#include "core/Game.h"
+#include "core/GameMode.h"
+
+namespace orxonox
+{
+ DeclareGameState(GSMasterServer, "masterserver", false, false);
+
+ GSMasterServer::GSMasterServer(const GameStateInfo& info)
+ : GameState(info)
+ {
+
+ }
+
+ GSMasterServer::~GSMasterServer()
+ {
+ }
+
+ void GSMasterServer::activate()
+ {
+ /* TODO make this work for masterserver as well */
+ //GameMode::setIsServer(true);
+
+ this->mserver = new MasterServer();
+ COUT(0) << "Loading masterserver mode" << std::endl;
+
+ this->mserver->run();
+ }
+
+ void GSMasterServer::deactivate()
+ { /* nothing so far */ }
+
+ void GSMasterServer::update(const Clock& time)
+ { /* nothing so far */ }
+}
Copied: code/branches/presentation/src/orxonox/gamestates/GSMasterServer.h (from rev 7737, code/branches/masterserver/src/orxonox/gamestates/GSMasterServer.h)
===================================================================
--- code/branches/presentation/src/orxonox/gamestates/GSMasterServer.h (rev 0)
+++ code/branches/presentation/src/orxonox/gamestates/GSMasterServer.h 2010-12-08 15:25:52 UTC (rev 7739)
@@ -0,0 +1,58 @@
+/*
+ * 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:
+ * Sandro 'smerkli' Merkli
+ * Co-authors:
+ * Reto Grieder (Original GSServer.h file this was adapted from)
+ * ...
+ *
+ */
+
+#ifndef _GSMasterServer_H__
+#define _GSMasterServer_H__
+
+#include "OrxonoxPrereqs.h"
+
+#include "core/GameState.h"
+#include "network/NetworkPrereqs.h"
+#include "network/MasterServer.h"
+
+namespace orxonox
+{
+ class _OrxonoxExport GSMasterServer : public GameState
+ {
+ public:
+ GSMasterServer(const GameStateInfo& info);
+ ~GSMasterServer();
+
+ void activate();
+ void deactivate();
+ void update(const Clock& time);
+
+ private:
+ MasterServer *mserver;
+
+
+ };
+}
+
+#endif /* _GSServer_H__ */
More information about the Orxonox-commit
mailing list