summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/connectionhandler.h5
-rw-r--r--src/main.cpp116
-rw-r--r--src/netcomputer.h2
-rw-r--r--src/netsession.cpp57
-rw-r--r--src/netsession.h7
5 files changed, 183 insertions, 4 deletions
diff --git a/src/connectionhandler.h b/src/connectionhandler.h
index 2b4bd850..20ce9321 100644
--- a/src/connectionhandler.h
+++ b/src/connectionhandler.h
@@ -36,6 +36,11 @@ class ConnectionHandler
{
public:
/**
+ * Constructor.
+ */
+ ConnectionHandler();
+
+ /**
* Called when a computer connects to a network session.
*/
void computerConnected(NetComputer *computer);
diff --git a/src/main.cpp b/src/main.cpp
new file mode 100644
index 00000000..b90e0286
--- /dev/null
+++ b/src/main.cpp
@@ -0,0 +1,116 @@
+/*
+ * The Mana World Server
+ * Copyright 2004 The Mana World Development Team
+ *
+ * This file is part of The Mana World.
+ *
+ * The Mana World 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
+ * any later version.
+ *
+ * The Mana World 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 The Mana World; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * $Id$
+ */
+
+#include "netsession.h"
+#include <SDL.h>
+#include <SDL_net.h>
+
+#define TMW_WORLD_TICK SDL_USEREVENT
+#define SERVER_PORT 9601
+
+SDL_TimerID worldTimerID; /**< Timer ID of world timer */
+bool running = true; /**< Determines if server keeps running */
+
+/**
+ * SDL timer callback, sends a <code>TMW_WORLD_TICK</code> event.
+ */
+Uint32 worldTick(Uint32 interval, void *param)
+{
+ // Push the custom world tick event
+ SDL_Event event;
+ event.type = TMW_WORLD_TICK;
+ SDL_PushEvent(&event);
+ return interval;
+}
+
+/**
+ * Initializes the server.
+ */
+void initialize()
+{
+ // Initialize SDL
+ if (SDL_Init(SDL_INIT_TIMER) == -1) {
+ printf("SDL_Init: %s\n", SDL_GetError());
+ exit(1);
+ }
+
+ // Set SDL to quit on exit
+ atexit(SDL_Quit);
+
+ // Initialize SDL_net
+ if (SDLNet_Init() == -1) {
+ printf("SDLNet_Init: %s\n", SDLNet_GetError());
+ exit(2);
+ }
+
+ // Initialize world timer at 10 times per second
+ worldTimerID = SDL_AddTimer(100, world_tick, NULL);
+}
+
+/**
+ * Deinitializes the server.
+ */
+void deinitialize()
+{
+ // Stop world timer
+ SDL_RemoveTimer(worldTimerID);
+
+ // Quit SDL_net
+ SDLNet_Quit();
+}
+
+/**
+ * Main function, initializes and runs server.
+ */
+int main(int argc, char *argv[])
+{
+ initialize();
+
+ // Ready for server work...
+ ConnectionHandler *connectionHandler = new ConnectionHandler();
+ NetSession *session = new NetSession();
+
+ session->startListen(connectionHandler, SERVER_PORT);
+
+ SDL_Event event;
+
+ while (running)
+ {
+ while (SDL_PollEvent(&event))
+ {
+ if (event.type == TMW_WORLD_TICK)
+ {
+ // - Handle all messages that are in the message queue
+ // - Update all active objects/beings
+ }
+ else if (event.type == SDL_QUIT)
+ {
+ running = false;
+ }
+ }
+ }
+
+ session->stopListen(SERVER_PORT);
+
+ deinitialize();
+}
diff --git a/src/netcomputer.h b/src/netcomputer.h
index 7ac2f833..37224f5f 100644
--- a/src/netcomputer.h
+++ b/src/netcomputer.h
@@ -57,7 +57,7 @@ class NetComputer
* Sends a packet to this computer.
*
* Note: When we'd want to allow communication through UDP, we could
- * introduce the reliable argument, which would could a UDP message
+ * introduce the reliable argument, which would cause a UDP message
* to be sent when set to false.
*/
void send(Packet *p);
diff --git a/src/netsession.cpp b/src/netsession.cpp
index 1c60990e..603ea7f7 100644
--- a/src/netsession.cpp
+++ b/src/netsession.cpp
@@ -22,4 +22,61 @@
*/
#include "netsession.h"
+#include <SDL_net.h>
+NetSession::NetSession()
+{
+}
+
+NetSession::~NetSession()
+{
+ // Stop listening to any ports
+}
+
+void NetSession::startListen(ConnectionHandler *handler, Uint16 port)
+{
+ // Here we will probably need the creation of a listening thread, which
+ // will call connect/disconnect events on the given ConnectionHandler and
+ // will cut incoming data into Packets and send them there too.
+
+ IPaddress address;
+ address.host = INADDR_ANY;
+ address.port = port;
+
+ TCPsocket tcpsock = SDLNet_TCP_Open(address);
+ if (!tcpsock) {
+ printf("SDLNet_TCP_Open: %s\n", SDLNet_GetError());
+ exit(3);
+ }
+}
+
+void NetSession::stopListen(Uint16 port)
+{
+ // Here all open connections on this port will be closed, and listening
+ // thread is stopped.
+
+ // void SDLNet_TCP_Close(TCPsocket sock);
+}
+
+NetComputer *NetSession::connect(const std::string &host, Uint16 port)
+{
+ // Try to connect to given host:port, and return NetComputer objects that
+ // can be used to send messages that way, or NULL when failing to connect.
+ //
+ // An asynchroneous wrapper could be created around this method.
+
+ IPaddress address;
+
+ if (!SDLNet_ResolveHost(&address, host.c_str(), port))
+ {
+ TCPsocket tcpsock = SDLNet_TCP_Open(IPaddress *ip);
+ if (!tcpsock) {
+ printf("SDLNet_TCP_Open: %s\n", SDLNet_GetError());
+ exit(3);
+ }
+ }
+ else {
+ printf("SDLNet_ResolveHost: Could not resolve %s\n", host.c_str());
+ exit(4);
+ }
+}
diff --git a/src/netsession.h b/src/netsession.h
index e6097db2..67baf7fe 100644
--- a/src/netsession.h
+++ b/src/netsession.h
@@ -25,6 +25,7 @@
#include "netcomputer.h"
#include "connectionhandler.h"
+#include <SDL.h>
/**
* This class represents a network session. It implements listening for
@@ -47,17 +48,17 @@ class NetSession
* Start listening for connections and notify the given connection
* handler about events.
*/
- void startListen(ConnectionHandler *handler, int port);
+ void startListen(ConnectionHandler *handler, Uint16 port);
/**
* Stop listening for connections and disconnect any connected clients.
*/
- void stopListen(int port);
+ void stopListen(Uint16 port);
/**
* Connect to another network session.
*/
- NetComputer *connect(const std::string &ip, int port);
+ NetComputer *connect(const std::string &ip, Uint16 port);
private:
// This class probably needs to keep information about: