From da57770f88d300d945274711d7a356be21a35cba Mon Sep 17 00:00:00 2001 From: Eduardo Bart Date: Sat, 16 Apr 2011 23:48:13 -0300 Subject: [PATCH] network --- CMakeLists.txt | 3 +- src/framework/core/engine.cpp | 4 +- src/framework/net/connection.cpp | 141 +++++++++--------- src/framework/net/connection.h | 50 +++---- src/framework/net/networkmessage.cpp | 5 +- src/framework/net/networkmessage.h | 6 +- src/framework/net/protocol.cpp | 15 +- src/framework/net/protocol.h | 12 +- src/menustate.cpp | 10 +- src/menustate.h | 2 + src/net/protocoltibia87.cpp | 112 -------------- src/net/protocoltibia87.h | 56 ------- .../net/connections.cpp => protocollogin.cpp} | 35 ++++- .../net/connections.h => protocollogin.h} | 31 ++-- src/teststate.cpp | 5 +- src/teststate.h | 5 +- 16 files changed, 160 insertions(+), 332 deletions(-) delete mode 100644 src/net/protocoltibia87.cpp delete mode 100644 src/net/protocoltibia87.h rename src/{framework/net/connections.cpp => protocollogin.cpp} (65%) rename src/{framework/net/connections.h => protocollogin.h} (72%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 11e99543..c2cc7c9d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -59,7 +59,7 @@ SET(SOURCES src/teststate.cpp # game net - src/net/protocoltibia87.cpp + src/protocollogin.cpp # framework core src/framework/core/dispatcher.cpp @@ -108,7 +108,6 @@ SET(SOURCES # framework net src/framework/net/connection.cpp - src/framework/net/connections.cpp src/framework/net/protocol.cpp src/framework/net/networkmessage.cpp ) diff --git a/src/framework/core/engine.cpp b/src/framework/core/engine.cpp index 34116cb7..d2f85669 100644 --- a/src/framework/core/engine.cpp +++ b/src/framework/core/engine.cpp @@ -28,9 +28,9 @@ #include "graphics/graphics.h" #include "configs.h" #include "dispatcher.h" -#include "net/connections.h" #include "ui/uicontainer.h" #include "graphics/fonts.h" +#include "net/connection.h" Engine g_engine; @@ -70,7 +70,7 @@ void Engine::run() Platform::poll(); // poll network events - g_connections.poll(); + Connection::poll(); // poll diaptcher tasks g_dispatcher.poll(); diff --git a/src/framework/net/connection.cpp b/src/framework/net/connection.cpp index 27ae405a..7affcb03 100644 --- a/src/framework/net/connection.cpp +++ b/src/framework/net/connection.cpp @@ -23,38 +23,52 @@ #include "connection.h" -#include +static boost::asio::io_service ioService; + +Connection::Connection() : + m_socket(ioService), + m_resolver(ioService), + m_connecting(false), + m_connected(false), + m_port(0) +{ + logTrace(); +} + +Connection::~Connection() +{ + logTrace(); +} -Connection::Connection(boost::asio::io_service& ioService) - : m_socket(ioService), m_resolver(ioService) +void Connection::poll() { - m_connected = false; - m_connecting = false; - m_port = 0; + ioService.poll(); + ioService.reset(); } -void Connection::stop() +void Connection::close() { - if(m_connecting){ + logTrace(); + if(m_connecting) { m_resolver.cancel(); m_socket.cancel(); - m_connecting = false; + m_connected = false; + closeSocket(); } } bool Connection::connect(const std::string& ip, uint16 port, const Callback& callback) { + logTrace(); - logInfo("[Connection::connect]: Ip: %s - Port: %d", ip.c_str(), port); - - if(m_connecting){ - logError("Already is connecting."); + if(m_connecting) { + logTraceError("already connecting."); return false; } - if(m_connected){ - logError("Already is connected."); + if(m_connected) { + logTraceError("already connected."); return false; } @@ -63,123 +77,116 @@ bool Connection::connect(const std::string& ip, uint16 port, const Callback& cal m_ip = ip; m_port = port; - //first resolve dns boost::asio::ip::tcp::resolver::query query(ip, convertType(port)); - m_resolver.async_resolve(query, boost::bind(&Connection::onResolveDns, this, boost::asio::placeholders::error, boost::asio::placeholders::iterator)); + m_resolver.async_resolve(query, boost::bind(&Connection::onResolveDns, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::iterator)); return true; } void Connection::onResolveDns(const boost::system::error_code& error, boost::asio::ip::tcp::resolver::iterator endpointIt) { - logInfo("[Connection::onResolveDns]"); - if(error){ - m_connecting = false; - logInfo("Error"); - m_errorCallback(error, __FUNCTION__); + logTrace(); + if(error) { + handleError(error); return; } //lets connect - m_socket.async_connect(*endpointIt, boost::bind(&Connection::onConnect, this, boost::asio::placeholders::error)); + m_socket.async_connect(*endpointIt, boost::bind(&Connection::onConnect, shared_from_this(), boost::asio::placeholders::error)); } void Connection::onConnect(const boost::system::error_code& error) { - if(error){ - logInfo("Error"); - m_connecting = false; - m_errorCallback(error, __FUNCTION__); + logTrace(); + if(error) { + handleError(error); return; } - m_connected = true; - m_connectCallback(); } void Connection::handleError(const boost::system::error_code& error) { - stop(); + logTrace(); - if(isConnected()){ - closeSocket(); - } + close(); + m_errorCallback(error); } void Connection::closeSocket() { + logTrace(); + boost::system::error_code error; m_socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both, error); - - if(error) { - logError("Connection::closeSocket(): %s", error.message().c_str()); - } - m_socket.close(error); - - if(error) { - logError("Connection::closeSocket(): %s", error.message().c_str()); - } } -void Connection::send(NetworkMessagePtr networkMessage, ConnectionCallback onSend) +void Connection::send(const NetworkMessage& networkMessage, const ConnectionCallback& onSend) { + logTrace(); + boost::asio::async_write(m_socket, - boost::asio::buffer(networkMessage->getBuffer(), NetworkMessage::header_length), + boost::asio::buffer(networkMessage.getBuffer(), NetworkMessage::header_length), boost::bind(&Connection::onSendHeader, shared_from_this(), networkMessage, onSend, boost::asio::placeholders::error)); } -void Connection::recv(RecvCallback onRecv) +void Connection::recv(const RecvCallback& onRecv) { - NetworkMessagePtr networkMessage(new NetworkMessage); + logTrace(); + static NetworkMessage networkMessage; boost::asio::async_read(m_socket, - boost::asio::buffer(networkMessage->getBuffer(), NetworkMessage::header_length), + boost::asio::buffer(networkMessage.getBuffer(), NetworkMessage::header_length), boost::bind(&Connection::onRecvHeader, shared_from_this(), networkMessage, onRecv, boost::asio::placeholders::error)); } -void Connection::onRecvHeader(ConnectionPtr connection, NetworkMessagePtr networkMessage, RecvCallback onRecv, const boost::system::error_code& error) +void Connection::onRecvHeader(const NetworkMessage& networkMessage, const RecvCallback& onRecv, const boost::system::error_code& error) { - if(error){ - connection->handleError(error); - connection->onError(error, __FUNCTION__); + logTrace(); + + if(error) { + handleError(error); return; } - boost::asio::async_read(connection->getSocket(), - boost::asio::buffer(networkMessage->getBodyBuffer(), networkMessage->getMessageLength()), - boost::bind(&Connection::onRecvBody, connection, networkMessage, onRecv, boost::asio::placeholders::error)); + boost::asio::async_read(m_socket, + boost::asio::buffer(networkMessage.getBodyBuffer(), networkMessage.getMessageLength()), + boost::bind(&Connection::onRecvBody, shared_from_this(), networkMessage, onRecv, boost::asio::placeholders::error)); } -void Connection::onRecvBody(ConnectionPtr connection, NetworkMessagePtr networkMessage, RecvCallback onRecv, const boost::system::error_code& error) +void Connection::onRecvBody(const NetworkMessage& networkMessage, const RecvCallback& onRecv, const boost::system::error_code& error) { + logTrace(); + if(error){ - connection->handleError(error); - connection->onError(error, __FUNCTION__); + handleError(error); return; } onRecv(networkMessage); } -void Connection::onSendHeader(ConnectionPtr connection, NetworkMessagePtr networkMessage, ConnectionCallback onSend, const boost::system::error_code& error) +void Connection::onSendHeader(const NetworkMessage& networkMessage, const ConnectionCallback& onSend, const boost::system::error_code& error) { + logTrace(); + if(error){ - connection->handleError(error); - connection->onError(error, __FUNCTION__); + handleError(error); return; } - boost::asio::async_write(connection->getSocket(), - boost::asio::buffer(networkMessage->getBodyBuffer(), networkMessage->getMessageLength()), - boost::bind(&Connection::onSendBody, connection, networkMessage, onSend, boost::asio::placeholders::error)); + boost::asio::async_write(m_socket, + boost::asio::buffer(networkMessage.getBodyBuffer(), networkMessage.getMessageLength()), + boost::bind(&Connection::onSendBody, shared_from_this(), networkMessage, onSend, boost::asio::placeholders::error)); } -void Connection::onSendBody(ConnectionPtr connection, NetworkMessagePtr networkMessage, ConnectionCallback onSend, const boost::system::error_code& error) +void Connection::onSendBody(const NetworkMessage& networkMessage, const ConnectionCallback& onSend, const boost::system::error_code& error) { - if(error){ - connection->handleError(error); - connection->onError(error, __FUNCTION__); + logTrace(); + + if(error) { + handleError(error); return; } diff --git a/src/framework/net/connection.h b/src/framework/net/connection.h index 63d33f0a..8e518304 100644 --- a/src/framework/net/connection.h +++ b/src/framework/net/connection.h @@ -25,58 +25,47 @@ #define CONNECTION_H #include "prerequisites.h" - -#include - #include "networkmessage.h" +#include -class TestState; class Protocol; -class Connections; class Connection; +typedef boost::shared_ptr ConnectionPtr; + +typedef boost::function ConnectionCallback; +typedef boost::function RecvCallback; +typedef boost::function ErrorCallback; class Connection : public boost::enable_shared_from_this { public: - typedef boost::function ConnectionCallback; - typedef boost::function RecvCallback; - typedef boost::function ErrorCallback; - - typedef boost::shared_ptr ConnectionPtr; - -private: - Connection(boost::asio::io_service& ioService); + Connection(); + ~Connection(); bool connect(const std::string& ip, uint16 port, const Callback& callback); - void stop(); + void close(); - void setErrorCallback(const ErrorCallback& callback) { m_errorCallback = callback; } + void setOnError(const ErrorCallback& callback) { m_errorCallback = callback; } - void recv(RecvCallback onSend); - void send(NetworkMessagePtr networkMessage, ConnectionCallback onRecv); + void recv(const RecvCallback& onSend); + void send(const NetworkMessage& networkMessage, const ConnectionCallback& onRecv); bool isConnecting() const { return m_connecting; } bool isConnected() const { return m_connected; } - boost::asio::ip::tcp::socket& getSocket() { return m_socket; } + static void poll(); - void onError(const boost::system::error_code& error, const std::string& msg) { m_errorCallback(error, msg); } - private: - static void onSendHeader(ConnectionPtr connection, NetworkMessagePtr networkMessage, ConnectionCallback onSend, const boost::system::error_code& error); - static void onSendBody(ConnectionPtr connection, NetworkMessagePtr networkMessage, ConnectionCallback onSend, const boost::system::error_code& error); + void onSendHeader(const NetworkMessage& networkMessage, const ConnectionCallback& onSend, const boost::system::error_code& error); + void onSendBody(const NetworkMessage& networkMessage, const ConnectionCallback& onSend, const boost::system::error_code& error); - static void onRecvHeader(ConnectionPtr connection, NetworkMessagePtr networkMessage, RecvCallback onRecv, const boost::system::error_code& error); - static void onRecvBody(ConnectionPtr connection, NetworkMessagePtr networkMessage, RecvCallback onRecv, const boost::system::error_code& error); + void onRecvHeader(const NetworkMessage& networkMessage, const RecvCallback& onRecv, const boost::system::error_code& error); + void onRecvBody(const NetworkMessage& networkMessage, const RecvCallback& onRecv, const boost::system::error_code& error); -private: void onResolveDns(const boost::system::error_code& error, boost::asio::ip::tcp::resolver::iterator endpointIt); void onConnect(const boost::system::error_code& error); -private: void closeSocket(); - -private: void handleError(const boost::system::error_code& error); boost::asio::ip::tcp::socket m_socket; @@ -90,11 +79,6 @@ private: Callback m_connectCallback; ErrorCallback m_errorCallback; - - friend class Protocol; - friend class Connections; }; -typedef boost::shared_ptr ConnectionPtr; - #endif //CONNECTION_h diff --git a/src/framework/net/networkmessage.cpp b/src/framework/net/networkmessage.cpp index 38789abf..4aa008bc 100644 --- a/src/framework/net/networkmessage.cpp +++ b/src/framework/net/networkmessage.cpp @@ -168,11 +168,10 @@ uint64 NetworkMessage::getU64() return v; } -char* NetworkMessage::getBuffer() { +char* NetworkMessage::getBuffer() const { return (char*)&m_msgBuf[0]; } -char* NetworkMessage::getBodyBuffer() { - m_readPos = 2; +char* NetworkMessage::getBodyBuffer() const { return (char*)&m_msgBuf[header_length]; } diff --git a/src/framework/net/networkmessage.h b/src/framework/net/networkmessage.h index c3edce6e..a055ebb5 100644 --- a/src/framework/net/networkmessage.h +++ b/src/framework/net/networkmessage.h @@ -78,8 +78,8 @@ public: void setMessageLength(int32 newSize); int32 getReadPos() const; int32 getHeaderSize(); - char* getBuffer(); - char* getBodyBuffer(); + char* getBuffer() const; + char* getBodyBuffer() const; void updateHeaderLength(); @@ -92,6 +92,4 @@ protected: uint8 m_msgBuf[NETWORKMESSAGE_MAXSIZE]; }; -typedef boost::shared_ptr NetworkMessagePtr; - #endif //NETWORKMESSAGE_H \ No newline at end of file diff --git a/src/framework/net/protocol.cpp b/src/framework/net/protocol.cpp index 7f31d811..41ffe9f2 100644 --- a/src/framework/net/protocol.cpp +++ b/src/framework/net/protocol.cpp @@ -22,21 +22,20 @@ */ #include "protocol.h" -#include "connections.h" -Protocol::Protocol() +Protocol::Protocol() : + m_connection(new Connection) { - logInfo("Protocol()"); - m_connection = g_connections.createConnection(); - m_connection->setErrorCallback(boost::bind(&Protocol::onError, this, boost::asio::placeholders::error, _2)); + logTrace(); + m_connection->setOnError(boost::bind(&Protocol::onError, this, boost::asio::placeholders::error)); } Protocol::~Protocol() { - logInfo("~Protocol()"); + logTrace(); } -void Protocol::send(NetworkMessagePtr networkMessage, Connection::ConnectionCallback onSend) +void Protocol::send(const NetworkMessage& networkMessage, const ConnectionCallback& onSend) { m_connection->send(networkMessage, onSend); } @@ -46,7 +45,7 @@ bool Protocol::connect(const std::string& ip, uint16 port, const Callback& callb return m_connection->connect(ip, port, callback); } -void Protocol::recv(Connection::RecvCallback onRecv) +void Protocol::recv(const RecvCallback& onRecv) { m_connection->recv(onRecv); } diff --git a/src/framework/net/protocol.h b/src/framework/net/protocol.h index 2389de3f..24eb5b03 100644 --- a/src/framework/net/protocol.h +++ b/src/framework/net/protocol.h @@ -31,17 +31,13 @@ class Protocol { public: Protocol(); - ~Protocol(); - - virtual void begin() = 0; + virtual ~Protocol(); protected: - void send(NetworkMessagePtr networkMessage, Connection::ConnectionCallback onSend); - void recv(Connection::RecvCallback onRecv); - + void send(const NetworkMessage& networkMessage, const ConnectionCallback& onSend); + void recv(const RecvCallback& onRecv); bool connect(const std::string& ip, uint16 port, const Callback& callback); - - virtual void onError(const boost::system::error_code& error, const std::string& msg) = 0; + virtual void onError(const boost::system::error_code& error) = 0; ConnectionPtr m_connection; }; diff --git a/src/menustate.cpp b/src/menustate.cpp index 722bf091..b040ee65 100644 --- a/src/menustate.cpp +++ b/src/menustate.cpp @@ -30,10 +30,9 @@ #include "graphics/fonts.h" #include "core/dispatcher.h" #include "ui/ui.h" -#include "net/connections.h" -#include "net/protocoltibia87.h" +#include "net/connection.h" #include "graphics/borderedimage.h" - +#include "protocollogin.h" void MenuState::onEnter() { @@ -101,8 +100,7 @@ void MenuState::enterGameWindowOkButton_clicked() std::string accountName = boost::static_pointer_cast(enterGameWindow->getChildById("accountNameTextEdit"))->getText(); std::string password = boost::static_pointer_cast(enterGameWindow->getChildById("passwordTextEdit"))->getText(); - //ProtocolTibia87Ptr protocol = ProtocolTibia87Ptr(new ProtocolTibia87); - ProtocolTibia87 *protocol = new ProtocolTibia87; - protocol->login(accountName, password); + m_protocolLogin = ProtocolLoginPtr(new ProtocolLogin); + m_protocolLogin->login(accountName, password); } diff --git a/src/menustate.h b/src/menustate.h index 0c83c10c..0c891c20 100644 --- a/src/menustate.h +++ b/src/menustate.h @@ -29,6 +29,7 @@ #include "graphics/texture.h" #include "net/connection.h" #include "ui/uipanel.h" +#include "protocollogin.h" class MenuState : public GameState { @@ -52,6 +53,7 @@ private: UIPanelPtr m_menuPanel; TexturePtr m_background; + ProtocolLoginPtr m_protocolLogin; }; #endif // MENUSTATE_H diff --git a/src/net/protocoltibia87.cpp b/src/net/protocoltibia87.cpp deleted file mode 100644 index 398dc95f..00000000 --- a/src/net/protocoltibia87.cpp +++ /dev/null @@ -1,112 +0,0 @@ -/* The MIT License - * - * Copyright (c) 2010 OTClient, https://github.com/edubart/otclient - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#include "protocoltibia87.h" -#include "util/rsa.h" - -const char* ProtocolTibia87::rsa = "4673033022358411862216018001503683214873298680851934467521055526294025873980576" - "6860224610646919605860206328024326703361630109888417839241959507572247284807035" - "2355696191737922927869078457919049551036016528225191219083671878855092700253886" - "41700821735345222087940578381210879116823013776808975766851829020659073"; - -ProtocolTibia87::ProtocolTibia87() -{ - -} - -void ProtocolTibia87::begin() -{ - /* - - */ -} - -void ProtocolTibia87::login(const std::string& account, const std::string& password) -{ - logInfo("Account: %s - Password: %s", account.c_str(), password.c_str()); - connect("google.com", 80, boost::bind(&ProtocolTibia87::afterConnect, this)); - - //sendAccount(account, password); -} - -void ProtocolTibia87::sendAccount(const std::string& account, const std::string& password) -{ - NetworkMessagePtr networkMessage(new NetworkMessage); - - networkMessage->addByte(0x01);//login Server - networkMessage->addU16(0x00);//OS - networkMessage->addU16(0x00);//VERSION - networkMessage->addPaddingBytes(12);// - - int32 m_notEncriptedLen = networkMessage->getMessageLength(); - - //begin RSA encrypt - networkMessage->addU32(1); //xtea - networkMessage->addU32(2); //xtea - networkMessage->addU32(3); //xtea - networkMessage->addU32(4); //xtea - - networkMessage->addString(account); - networkMessage->addString(password); - - Rsa::encrypt(networkMessage->getBodyBuffer() + m_notEncriptedLen, networkMessage->getMessageLength() - m_notEncriptedLen, ProtocolTibia87::rsa); - - networkMessage->setMessageLength(m_notEncriptedLen + 128); - networkMessage->updateHeaderLength(); - - /* - send(networkMessage, - [this](){ - this->afterSendAccount(); - } - ); - */ -} - -void ProtocolTibia87::afterConnect() -{ - logError("[ProtocolTibia87::afterConnect]: Connected!"); - login("9418347", "lollol"); -} - -void ProtocolTibia87::afterSendAccount() -{ - /* - recv( - [this](NetworkMessagePtr networkMessage){ - this->parseCharacterList(networkMessage); - } - ); - */ -} - -void ProtocolTibia87::onError(const boost::system::error_code& error, const std::string& msg) -{ - logError("%s; %s", error.message().c_str(), msg.c_str()); -} - -void ProtocolTibia87::parseCharacterList(NetworkMessagePtr networkMessage) -{ - logInfo("Parsing characters. msglen: %d", networkMessage->getMessageLength()); - -} diff --git a/src/net/protocoltibia87.h b/src/net/protocoltibia87.h deleted file mode 100644 index 7870aabf..00000000 --- a/src/net/protocoltibia87.h +++ /dev/null @@ -1,56 +0,0 @@ -/* The MIT License - * - * Copyright (c) 2010 OTClient, https://github.com/edubart/otclient - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#ifndef PROTOCOLTIBIA87_H -#define PROTOCOLTIBIA87_H - -#include "prerequisites.h" -#include "net/protocol.h" - -class ProtocolTibia87 : public Protocol -{ -public: - ProtocolTibia87(); - - virtual void begin(); - void login(const std::string& account, const std::string& password); - -protected: - - void sendAccount(const std::string& account, const std::string& password); - void parseCharacterList(NetworkMessagePtr networkMessage); - - void readCharacterList(); - - void afterConnect(); - void afterSendAccount(); - - virtual void onError(const boost::system::error_code& error, const std::string& msg); - -private: - static const char* rsa; -}; - -typedef boost::shared_ptr ProtocolTibia87Ptr; - -#endif //PROTOCOLTIBIA87_H diff --git a/src/framework/net/connections.cpp b/src/protocollogin.cpp similarity index 65% rename from src/framework/net/connections.cpp rename to src/protocollogin.cpp index 75e1b17d..89be2d48 100644 --- a/src/framework/net/connections.cpp +++ b/src/protocollogin.cpp @@ -21,19 +21,38 @@ * THE SOFTWARE. */ -#include "connections.h" -Connections g_connections; +#include "protocollogin.h" +#include "util/rsa.h" -size_t Connections::poll() +ProtocolLogin::ProtocolLogin() { - return m_ioService.poll(); + logTrace(); } -ConnectionPtr Connections::createConnection() +ProtocolLogin::~ProtocolLogin() { - ConnectionPtr connection(new Connection(m_ioService)); - m_connections.push_back(connection); + logTrace(); +} + +void ProtocolLogin::login(const std::string& account, const std::string& password) +{ + logTrace(); + m_connection = ConnectionPtr(new Connection); + m_connection->connect("www.google.com", 80, boost::bind(&ProtocolLogin::afterConnect, this)); +} + +void ProtocolLogin::afterConnect() +{ + logTrace(); - return connection; } + +void ProtocolLogin::onError(const boost::system::error_code& error, const std::string& msg) +{ + logTrace(); + logError("Connection error: %s", error.message().c_str()); +} + + + diff --git a/src/framework/net/connections.h b/src/protocollogin.h similarity index 72% rename from src/framework/net/connections.h rename to src/protocollogin.h index 0063086c..aa7e1863 100644 --- a/src/framework/net/connections.h +++ b/src/protocollogin.h @@ -21,27 +21,28 @@ * THE SOFTWARE. */ -#ifndef CONNECTIONS_H -#define CONNECTIONS_H -#include "prerequisites.h" +#ifndef PROTOCOLLOGIN_H +#define PROTOCOLLOGIN_H -#include "connection.h" +#include "prerequisites.h" +#include "net/connection.h" -class Connections +class ProtocolLogin { public: - size_t poll(); - - ConnectionPtr createConnection(); - + ProtocolLogin(); + ~ProtocolLogin(); + + void login(const std::string& account, const std::string& password); + void afterConnect(); + void sendAccount(); + void onError(const boost::system::error_code& error, const std::string& msg); + private: - boost::asio::io_service m_ioService; - - typedef std::vector ConnectionVector; - ConnectionVector m_connections; + ConnectionPtr m_connection; }; -extern Connections g_connections; +typedef boost::shared_ptr ProtocolLoginPtr; -#endif //CONNECTIONS_H +#endif // PROTOCOLLOGIN_H diff --git a/src/teststate.cpp b/src/teststate.cpp index 3bdf398b..2a48bd1e 100644 --- a/src/teststate.cpp +++ b/src/teststate.cpp @@ -25,13 +25,10 @@ #include "teststate.h" #include "graphics/graphics.h" #include "core/engine.h" -#include "net/connections.h" -#include "net/protocoltibia87.h" void TestState::onEnter() { - m_protocol = ProtocolTibia87Ptr(new ProtocolTibia87); - m_protocol->begin(); + } void TestState::onLeave() diff --git a/src/teststate.h b/src/teststate.h index 208d2996..7c5cec98 100644 --- a/src/teststate.h +++ b/src/teststate.h @@ -25,8 +25,8 @@ #ifndef TESTSTATE_H #define TESTSTATE_H +#include "prerequisites.h" #include "core/gamestate.h" -#include "net/protocoltibia87.h" class TestState : public GameState { @@ -42,9 +42,6 @@ public: virtual void render(); virtual void update(int ticks, int elapsedTicks); - -private: - ProtocolTibia87Ptr m_protocol; }; #endif // TESTSTATE_H