diff options
author | Andrei Karas <akaras@inbox.ru> | 2013-04-08 14:48:08 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2013-04-08 14:48:08 +0300 |
commit | cda421c4c58e32f88d87641e0195a55a8f212757 (patch) | |
tree | 0ed67b9949b994ad3b3bf50dfcb88a120a8a042f | |
parent | efa3bc8c346aea723b8b02ea1f27d7d3e41ff134 (diff) | |
download | plus-cda421c4c58e32f88d87641e0195a55a8f212757.tar.gz plus-cda421c4c58e32f88d87641e0195a55a8f212757.tar.bz2 plus-cda421c4c58e32f88d87641e0195a55a8f212757.tar.xz plus-cda421c4c58e32f88d87641e0195a55a8f212757.zip |
move direct SDL_net calls to proxy file sdltcpnet.
-rw-r--r-- | src/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/Makefile.am | 2 | ||||
-rw-r--r-- | src/net/ea/network.cpp | 58 | ||||
-rw-r--r-- | src/net/ea/network.h | 5 | ||||
-rw-r--r-- | src/net/eathena/network.h | 6 | ||||
-rw-r--r-- | src/net/sdltcpnet.cpp | 88 | ||||
-rw-r--r-- | src/net/sdltcpnet.h | 62 | ||||
-rw-r--r-- | src/net/tmwa/network.h | 6 |
8 files changed, 185 insertions, 44 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 5367c8720..94bceed81 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -401,6 +401,8 @@ SET(SRCS net/net.h net/partyhandler.h net/playerhandler.h + net/sdltcpnet.cpp + net/sdltcpnet.h net/serverinfo.h net/skillhandler.h net/tradehandler.h diff --git a/src/Makefile.am b/src/Makefile.am index f0c95af86..923fecc15 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -402,6 +402,8 @@ manaplus_SOURCES += gui/widgets/avatarlistbox.cpp \ net/npchandler.h \ net/partyhandler.h \ net/playerhandler.h \ + net/sdltcpnet.cpp \ + net/sdltcpnet.h \ net/serverinfo.h \ net/skillhandler.h \ net/tradehandler.h \ diff --git a/src/net/ea/network.cpp b/src/net/ea/network.cpp index b0a2802bd..00c83d3b0 100644 --- a/src/net/ea/network.cpp +++ b/src/net/ea/network.cpp @@ -66,8 +66,7 @@ Network::Network() : mMutex(SDL_CreateMutex()), mSleep(config.getIntValue("networksleep")) { - SDLNet_Init(); - + TcpNet::init(); } Network::~Network() @@ -81,7 +80,7 @@ Network::~Network() delete []mInBuffer; delete []mOutBuffer; - SDLNet_Quit(); + TcpNet::quit(); } bool Network::connect(ServerInfo server) @@ -133,8 +132,7 @@ void Network::disconnect() if (mSocket) { - // need call SDLNet_TCP_DelSocket? - SDLNet_TCP_Close(mSocket); + TcpNet::closeSocket(mSocket); mSocket = nullptr; if (mSleep > 0) SDL_Delay(mSleep); @@ -149,12 +147,12 @@ void Network::flush() int ret; SDL_mutexP(mMutex); - ret = SDLNet_TCP_Send(mSocket, mOutBuffer, mOutSize); + ret = TcpNet::send(mSocket, mOutBuffer, mOutSize); DEBUGLOG(std::string("Send ").append(toString(mOutSize)).append(" bytes")); if (ret < static_cast<int>(mOutSize)) { - setError("Error in SDLNet_TCP_Send(): " + - std::string(SDLNet_GetError())); + setError("Error in TcpNet::send(): " + + std::string(TcpNet::getError())); } mOutSize = 0; SDL_mutexV(mMutex); @@ -188,23 +186,23 @@ bool Network::realConnect() { IPaddress ipAddress; - if (SDLNet_ResolveHost(&ipAddress, mServer.hostname.c_str(), + if (TcpNet::resolveHost(&ipAddress, mServer.hostname.c_str(), mServer.port) == -1) { std::string errorMessage = std::string(_("Unable to resolve host \"")) .append(mServer.hostname).append("\""); setError(errorMessage); - logger->log("SDLNet_ResolveHost: %s", errorMessage.c_str()); + logger->log("TcpNet::ResolveHost: %s", errorMessage.c_str()); return false; } mState = CONNECTING; - mSocket = SDLNet_TCP_Open(&ipAddress); + mSocket = TcpNet::open(&ipAddress); if (!mSocket) { - logger->log("Error in SDLNet_TCP_Open(): %s", SDLNet_GetError()); - setError(SDLNet_GetError()); + logger->log("Error in TcpNet::open(): %s", TcpNet::getError()); + setError(TcpNet::getError()); return false; } @@ -218,32 +216,32 @@ bool Network::realConnect() void Network::receive() { - SDLNet_SocketSet set; + TcpNet::SocketSet set; - if (!(set = SDLNet_AllocSocketSet(1))) + if (!(set = TcpNet::allocSocketSet(1))) { - setError("Error in SDLNet_AllocSocketSet(): " + - std::string(SDLNet_GetError())); + setError("Error in TcpNet::allocSocketSet(): " + + std::string(TcpNet::getError())); return; } - if (SDLNet_TCP_AddSocket(set, mSocket) == -1) + if (TcpNet::addSocket(set, mSocket) == -1) { - setError("Error in SDLNet_AddSocket(): " + - std::string(SDLNet_GetError())); + setError("Error in TcpNet::addSocket(): " + + std::string(TcpNet::getError())); } while (mState == CONNECTED) { // TODO Try to get this to block all the time while still being able // to escape the loop - const int numReady = SDLNet_CheckSockets( + const int numReady = TcpNet::checkSockets( set, (static_cast<uint32_t>(500))); int ret; switch (numReady) { case -1: - logger->log1("Error: SDLNet_CheckSockets"); + logger->log1("Error: TcpNet::checkSockets"); // FALLTHROUGH case 0: break; @@ -258,8 +256,8 @@ void Network::receive() continue; } - ret = SDLNet_TCP_Recv(mSocket, mInBuffer + mInSize, - BUFFER_SIZE - mInSize); + ret = TcpNet::recv(mSocket, mInBuffer + mInSize, + BUFFER_SIZE - mInSize); if (!ret) { @@ -270,7 +268,7 @@ void Network::receive() else if (ret < 0) { setError(_("Connection to server terminated. ") + - std::string(SDLNet_GetError())); + std::string(TcpNet::getError())); } else { @@ -298,17 +296,17 @@ void Network::receive() // more than one socket is ready.. // this should not happen since we only listen once socket. std::stringstream errorStream; - errorStream << "Error in SDLNet_TCP_Recv(), " << numReady - << " sockets are ready: " << SDLNet_GetError(); + errorStream << "Error in TcpNet::recv(), " << numReady + << " sockets are ready: " << TcpNet::getError(); setError(errorStream.str()); break; } } - if (SDLNet_TCP_DelSocket(set, mSocket) == -1) - logger->log("Error in SDLNet_DelSocket(): %s", SDLNet_GetError()); + if (TcpNet::delSocket(set, mSocket) == -1) + logger->log("Error in TcpNet::delSocket(): %s", TcpNet::getError()); - SDLNet_FreeSocketSet(set); + TcpNet::freeSocketSet(set); } void Network::setError(const std::string &error) diff --git a/src/net/ea/network.h b/src/net/ea/network.h index 83e1abe36..ea56848ee 100644 --- a/src/net/ea/network.h +++ b/src/net/ea/network.h @@ -28,7 +28,8 @@ #include "net/messagein.h" #include "net/messageout.h" -#include <SDL_net.h> +#include "net/sdltcpnet.h" + #include <SDL_thread.h> #include <map> @@ -92,7 +93,7 @@ class Network void receive(); - TCPsocket mSocket; + TcpNet::Socket mSocket; ServerInfo mServer; diff --git a/src/net/eathena/network.h b/src/net/eathena/network.h index c2f6e0bb6..8babc4d19 100644 --- a/src/net/eathena/network.h +++ b/src/net/eathena/network.h @@ -28,12 +28,6 @@ #include "net/eathena/messagehandler.h" #include "net/eathena/messagein.h" -#include <SDL_net.h> -#include <SDL_thread.h> - -#include <map> -#include <string> - /** * Protocol version, reported to the eAthena char and mapserver who can adjust * the protocol accordingly. diff --git a/src/net/sdltcpnet.cpp b/src/net/sdltcpnet.cpp new file mode 100644 index 000000000..28cac3aa3 --- /dev/null +++ b/src/net/sdltcpnet.cpp @@ -0,0 +1,88 @@ +/* + * The ManaPlus Client + * Copyright (C) 2013 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * 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 + * 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, see <http://www.gnu.org/licenses/>. + */ + +#include "net/sdltcpnet.h" + +#include "debug.h" + +void TcpNet::init() +{ + SDLNet_Init(); +} + +void TcpNet::quit() +{ + SDLNet_Quit(); +} + +void TcpNet::closeSocket(TcpNet::Socket socket) +{ + SDLNet_TCP_Close(socket); +} + +int TcpNet::send(TcpNet::Socket sock, const void *data, int len) +{ + return SDLNet_TCP_Send(sock, data, len); +} + +char *TcpNet::getError() +{ + return SDLNet_GetError(); +} + +int TcpNet::resolveHost(IPaddress *address, const char *host, Uint16 port) +{ + return SDLNet_ResolveHost(address, host, port); +} + +TcpNet::Socket TcpNet::open(IPaddress *ip) +{ + return SDLNet_TCP_Open(ip); +} + +TcpNet::SocketSet TcpNet::allocSocketSet(int maxsockets) +{ + return SDLNet_AllocSocketSet(maxsockets); +} + +int TcpNet::addSocket(TcpNet::SocketSet set, TcpNet::Socket sock) +{ + return SDLNet_TCP_AddSocket(set, sock); +} + +int TcpNet::checkSockets(TcpNet::SocketSet set, Uint32 timeout) +{ + return SDLNet_CheckSockets(set, timeout); +} + +int TcpNet::recv(TcpNet::Socket sock, void *data, int maxlen) +{ + return SDLNet_TCP_Recv(sock, data, maxlen); +} + +int TcpNet::delSocket(TcpNet::SocketSet set, TcpNet::Socket sock) +{ + return SDLNet_TCP_DelSocket(set, sock); +} + +void TcpNet::freeSocketSet(TcpNet::SocketSet set) +{ + SDLNet_FreeSocketSet(set); +} diff --git a/src/net/sdltcpnet.h b/src/net/sdltcpnet.h new file mode 100644 index 000000000..eaa5801a5 --- /dev/null +++ b/src/net/sdltcpnet.h @@ -0,0 +1,62 @@ +/* + * The ManaPlus Client + * Copyright (C) 2013 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * 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 + * 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, see <http://www.gnu.org/licenses/>. + */ + +#ifndef NET_SDLTCPNET_H +#define NET_SDLTCPNET_H + +#include <SDL_net.h> + +#include <string> + +#include "localconsts.h" + +namespace TcpNet +{ + typedef ::SDLNet_SocketSet SocketSet; + typedef ::TCPsocket Socket; + + void init(); + + void quit(); + + void closeSocket(TcpNet::Socket socket); + + int send(TcpNet::Socket sock, const void *data, int len); + + char *getError(); + + int resolveHost(IPaddress *address, const char *host, Uint16 port); + + TcpNet::Socket open(IPaddress *ip); + + SocketSet allocSocketSet(int maxsockets); + + int addSocket(TcpNet::SocketSet set, TcpNet::Socket sock); + + int checkSockets(TcpNet::SocketSet set, Uint32 timeout); + + int recv(TcpNet::Socket sock, void *data, int maxlen); + + int delSocket(TcpNet::SocketSet set, TcpNet::Socket sock); + + void freeSocketSet(TcpNet::SocketSet set); +} + +#endif diff --git a/src/net/tmwa/network.h b/src/net/tmwa/network.h index e2018fd37..1d89d9a76 100644 --- a/src/net/tmwa/network.h +++ b/src/net/tmwa/network.h @@ -28,12 +28,6 @@ #include "net/tmwa/messagehandler.h" #include "net/tmwa/messagein.h" -#include <SDL_net.h> -#include <SDL_thread.h> - -#include <map> -#include <string> - /** * Protocol version, reported to the eAthena char and mapserver who can adjust * the protocol accordingly. |