summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRogier Polak <rogier.l.a.polak@gmail.com>2007-03-11 00:39:29 +0000
committerRogier Polak <rogier.l.a.polak@gmail.com>2007-03-11 00:39:29 +0000
commit0a74cd8c92844e730cf6f56bdc3dd578c65a10d0 (patch)
tree8fc705e7b0b5edd35e82f53c3041a09a16eb09e5 /src
parent8b56248ef58323c6e28264b5317d39c22c59db04 (diff)
downloadmanaserv-0a74cd8c92844e730cf6f56bdc3dd578c65a10d0.tar.gz
manaserv-0a74cd8c92844e730cf6f56bdc3dd578c65a10d0.tar.bz2
manaserv-0a74cd8c92844e730cf6f56bdc3dd578c65a10d0.tar.xz
manaserv-0a74cd8c92844e730cf6f56bdc3dd578c65a10d0.zip
Added a utils::processor namespace and a function to determine if, the processor the program is running on, is little-endian or big-endian.
Diffstat (limited to 'src')
-rw-r--r--src/Makefile.am4
-rw-r--r--src/account-server/main-account.cpp6
-rw-r--r--src/game-server/main-game.cpp6
-rw-r--r--src/net/connectionhandler.cpp21
-rw-r--r--src/net/connectionhandler.hpp6
-rw-r--r--src/net/netcomputer.cpp17
-rw-r--r--src/utils/processorutils.cpp37
-rw-r--r--src/utils/processorutils.hpp52
8 files changed, 120 insertions, 29 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index dbc67865..544d2eba 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -56,6 +56,8 @@ tmwserv_account_SOURCES = \
utils/functors.h \
utils/logger.h \
utils/logger.cpp \
+ utils/processorutils.hpp \
+ utils/processorutils.cpp \
utils/stringfilter.h \
utils/stringfilter.cpp \
utils/tokendispenser.hpp \
@@ -122,6 +124,8 @@ tmwserv_game_SOURCES = \
utils/mathutils.cpp \
utils/logger.h \
utils/logger.cpp \
+ utils/processorutils.hpp \
+ utils/processorutils.cpp \
utils/stringfilter.h \
utils/stringfilter.cpp \
utils/timer.h \
diff --git a/src/account-server/main-account.cpp b/src/account-server/main-account.cpp
index 209e4a24..fd3037b2 100644
--- a/src/account-server/main-account.cpp
+++ b/src/account-server/main-account.cpp
@@ -42,6 +42,7 @@
#include "net/connectionhandler.hpp"
#include "net/messageout.hpp"
#include "utils/logger.h"
+#include "utils/processorutils.hpp"
#include "utils/stringfilter.h"
// Default options that automake should be able to override.
@@ -176,7 +177,10 @@ void initialize()
config.setValue("dbpass", "");
config.setValue("dbhost", "");
- //Seed the random number generator
+ // Initialize the processor utility functions
+ utils::processor::init();
+
+ // Seed the random number generator
std::srand( time(NULL) );
}
diff --git a/src/game-server/main-game.cpp b/src/game-server/main-game.cpp
index a63dc585..f579ece2 100644
--- a/src/game-server/main-game.cpp
+++ b/src/game-server/main-game.cpp
@@ -42,6 +42,7 @@
#include "net/connectionhandler.hpp"
#include "net/messageout.hpp"
#include "utils/logger.h"
+#include "utils/processorutils.hpp"
#include "utils/stringfilter.h"
#include "utils/timer.h"
#include "utils/mathutils.h"
@@ -211,7 +212,10 @@ void initialize()
// Pre-calulate the needed trigomic function values
utils::math::init();
- //Seed the random number generator
+ // Initialize the processor utility functions
+ utils::processor::init();
+
+ // Seed the random number generator
std::srand( time(NULL) );
}
diff --git a/src/net/connectionhandler.cpp b/src/net/connectionhandler.cpp
index 53ec3446..04bf0169 100644
--- a/src/net/connectionhandler.cpp
+++ b/src/net/connectionhandler.cpp
@@ -31,18 +31,6 @@
#include "script.h"
#endif
-
-std::string
-ip4ToString(unsigned int ip4addr)
-{
- std::stringstream ss;
- ss << (ip4addr & 0x000000ff) << "."
- << ((ip4addr & 0x0000ff00) >> 8) << "."
- << ((ip4addr & 0x00ff0000) >> 16) << "."
- << ((ip4addr & 0xff000000) >> 24);
- return ss.str();
-}
-
bool ConnectionHandler::startListen(enet_uint16 port)
{
// Bind the server to the default localhost.
@@ -93,12 +81,11 @@ void ConnectionHandler::process(enet_uint32 timeout)
switch (event.type) {
case ENET_EVENT_TYPE_CONNECT:
{
- LOG_INFO("A new client connected from " <<
- ip4ToString(event.peer->address.host) << ":" <<
- event.peer->address.port << " to port " <<
- host->address.port);
NetComputer *comp = computerConnected(event.peer);
clients.push_back(comp);
+ LOG_INFO("A new client connected from " << *comp << ":"
+ << event.peer->address.port << " to port "
+ << host->address.port);
// Store any relevant client information here.
event.peer->data = (void *)comp;
@@ -140,7 +127,7 @@ void ConnectionHandler::process(enet_uint32 timeout)
case ENET_EVENT_TYPE_DISCONNECT:
{
NetComputer *comp = (NetComputer *)event.peer->data;
- LOG_INFO(ip4ToString(event.peer->address.host) << " disconnected.");
+ LOG_INFO("" << *comp << " disconnected.");
// Reset the peer's client information.
computerDisconnected(comp);
clients.erase(std::find(clients.begin(), clients.end(), comp));
diff --git a/src/net/connectionhandler.hpp b/src/net/connectionhandler.hpp
index 76bea198..5787375a 100644
--- a/src/net/connectionhandler.hpp
+++ b/src/net/connectionhandler.hpp
@@ -32,12 +32,6 @@ class MessageOut;
class NetComputer;
/**
- * Convert a IP4 address into its string representation
- */
-std::string
-ip4ToString(unsigned int ip4addr);
-
-/**
* This class represents the connection handler interface. The connection
* handler will respond to connect/reconnect/disconnect events and handle
* incoming messages, passing them on to registered message handlers.
diff --git a/src/net/netcomputer.cpp b/src/net/netcomputer.cpp
index 3e4a4dbc..f0b677ec 100644
--- a/src/net/netcomputer.cpp
+++ b/src/net/netcomputer.cpp
@@ -29,6 +29,7 @@
#include "net/messageout.hpp"
#include "net/netcomputer.hpp"
#include "utils/logger.h"
+#include "utils/processorutils.hpp"
NetComputer::NetComputer(ENetPeer *peer):
mPeer(peer)
@@ -84,10 +85,18 @@ std::ostream&
operator <<(std::ostream &os, const NetComputer &comp)
{
// address.host contains the ip-address in network-byte-order
+ if (utils::processor::isLittleEndian)
+ os << ( comp.mPeer->address.host & 0x000000ff) << "."
+ << ((comp.mPeer->address.host & 0x0000ff00) >> 8) << "."
+ << ((comp.mPeer->address.host & 0x00ff0000) >> 16) << "."
+ << ((comp.mPeer->address.host & 0xff000000) >> 24);
+ else
+ // big-endian
+ // TODO: test this
+ os << ((comp.mPeer->address.host & 0x000000ff) << 24) << "."
+ << ((comp.mPeer->address.host & 0x0000ff00) << 16) << "."
+ << ((comp.mPeer->address.host & 0x00ff0000) << 8) << "."
+ << ((comp.mPeer->address.host & 0xff000000) << 0);
- os << ((comp.mPeer->address.host & 0xff000000) >> 24) << "."
- << ((comp.mPeer->address.host & 0x00ff0000) >> 16) << "."
- << ((comp.mPeer->address.host & 0x0000ff00) >> 8) << "."
- << ((comp.mPeer->address.host & 0x000000ff) >> 0);
return os;
}
diff --git a/src/utils/processorutils.cpp b/src/utils/processorutils.cpp
new file mode 100644
index 00000000..8cbab3e7
--- /dev/null
+++ b/src/utils/processorutils.cpp
@@ -0,0 +1,37 @@
+/*
+ * The Mana World Server
+ * Copyright 2007 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 "utils/processorutils.hpp"
+
+bool utils::processor::isLittleEndian;
+
+void utils::processor::init()
+{
+ utils::processor::isLittleEndian = utils::processor::littleEndianCheck();
+}
+
+bool utils::processor::littleEndianCheck()
+{
+ short int word = 0x0001; // Store 0x0001 in a 16-bit int.
+ char *byte = (char *) &word; // 'byte' points to the first byte in word.
+ return(byte[0]); // byte[0] will be 1 on little-endian processors.
+}
diff --git a/src/utils/processorutils.hpp b/src/utils/processorutils.hpp
new file mode 100644
index 00000000..f426781e
--- /dev/null
+++ b/src/utils/processorutils.hpp
@@ -0,0 +1,52 @@
+/*
+ * The Mana World Server
+ * Copyright 2007 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$
+ */
+
+#ifndef _TMWSERV_PROCESSORUTILS_HPP
+#define _TMWSERV_PROCESSORUTILS_HPP
+
+namespace utils
+{
+ namespace processor
+ {
+ /**
+ * \brief Initialises the processor utils.
+ *
+ * Does runtime checks, of which the results are stored in variables
+ * in this namespace.
+ */
+ void init();
+
+ /**
+ * True if the processor is little-endian
+ *
+ */
+ extern bool isLittleEndian;
+
+ /**
+ * Returns true if the processor is little-endian.
+ */
+ bool littleEndianCheck();
+
+ } // namespace processor
+} // namespace utils
+
+#endif // _TMWSERV_TOKENDISPENSER_HPP