From 8952bddb7a308fac559144fbffcf8f07acec2b33 Mon Sep 17 00:00:00 2001 From: Henrique Santiago Date: Wed, 20 Apr 2011 03:40:31 -0300 Subject: [PATCH] init network --- CMakeLists.txt | 6 +-- src/framework/core/engine.cpp | 4 +- src/framework/net/connection.cpp | 90 ++++++++++++++++++++++++++++++++ src/framework/net/connection.h | 67 ++++++++++++++++++++++++ src/framework/net/protocol.cpp | 46 ++++++++++++++++ src/framework/net/protocol.h | 43 +++++++++++++++ src/main.cpp | 3 ++ src/menustate.cpp | 4 +- src/menustate.h | 4 +- src/protocollogin.cpp | 67 ++++++++++++++++++++++++ src/protocollogin.h | 48 +++++++++++++++++ 11 files changed, 373 insertions(+), 9 deletions(-) create mode 100644 src/framework/net/connection.cpp create mode 100644 src/framework/net/connection.h create mode 100644 src/framework/net/protocol.cpp create mode 100644 src/framework/net/protocol.h create mode 100644 src/protocollogin.cpp create mode 100644 src/protocollogin.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 0d937222..a2960066 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -59,7 +59,7 @@ SET(SOURCES src/teststate.cpp # game net - #src/protocollogin.cpp + src/protocollogin.cpp # framework core src/framework/core/dispatcher.cpp @@ -104,8 +104,8 @@ SET(SOURCES src/framework/ui/uicheckbox.cpp # framework net - #src/framework/net/connection.cpp - #src/framework/net/protocol.cpp + src/framework/net/connection.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 a7ec344e..31b1fb13 100644 --- a/src/framework/core/engine.cpp +++ b/src/framework/core/engine.cpp @@ -29,7 +29,7 @@ #include #include #include -//#include +#include Engine g_engine; @@ -69,7 +69,7 @@ void Engine::run() Platform::poll(); // poll network events - //Connection::poll(); + Connection::poll(); // poll diaptcher tasks g_dispatcher.poll(); diff --git a/src/framework/net/connection.cpp b/src/framework/net/connection.cpp new file mode 100644 index 00000000..38a012a7 --- /dev/null +++ b/src/framework/net/connection.cpp @@ -0,0 +1,90 @@ +/* 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 +#include + +static boost::asio::io_service ioService; + +Connection::Connection() : + m_connectionState(CONNECTION_STATE_IDLE), + m_timer(ioService), + m_resolver(ioService), + m_socket(ioService) +{ +} + +void Connection::poll() +{ + ioService.poll(); + ioService.reset(); +} + +void Connection::connect(const std::string& host, uint16 port, const Callback& callback) +{ + m_connectCallback = callback; + m_connectionState = CONNECTION_STATE_RESOLVING; + + boost::asio::ip::tcp::resolver::query query(host, convertType(port)); + m_resolver.async_resolve(query, boost::bind(&Connection::onResolve, this, boost::asio::placeholders::error, boost::asio::placeholders::iterator)); + + m_timer.expires_from_now(boost::posix_time::seconds(2)); + m_timer.async_wait(boost::bind(&Connection::onTimeout, this, boost::asio::placeholders::error)); +} + +void Connection::onTimeout(const boost::system::error_code& error) +{ + if(error != boost::asio::error::operation_aborted) + g_dispatcher.addTask(boost::bind(m_errorCallback, error)); +} + +void Connection::onResolve(const boost::system::error_code& error, boost::asio::ip::tcp::resolver::iterator endpointIterator) +{ + logTrace(); + + m_timer.cancel(); + + if(error) { + g_dispatcher.addTask(boost::bind(m_errorCallback, error)); + return; + } + + m_socket.async_connect(*endpointIterator, boost::bind(&Connection::onConnect, this, boost::asio::placeholders::error)); + + m_timer.expires_from_now(boost::posix_time::seconds(2)); + m_timer.async_wait(boost::bind(&Connection::onTimeout, this, boost::asio::placeholders::error)); +} + +void Connection::onConnect(const boost::system::error_code& error) +{ + logTrace(); + + m_timer.cancel(); + + if(error) { + g_dispatcher.addTask(boost::bind(m_errorCallback, error)); + return; + } + + g_dispatcher.addTask(m_connectCallback); +} diff --git a/src/framework/net/connection.h b/src/framework/net/connection.h new file mode 100644 index 00000000..7a044cc0 --- /dev/null +++ b/src/framework/net/connection.h @@ -0,0 +1,67 @@ +/* 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 CONNECTION_H +#define CONNECTION_H + +#include +#include + +typedef boost::function ErrorCallback; + +class Connection +{ +public: + Connection(); + + static void poll(); + + void connect(const std::string& host, uint16 port, const Callback& callback); + void setErrorCallback(const ErrorCallback& errorCallback) { m_errorCallback = errorCallback; } + + void onTimeout(const boost::system::error_code& error); + void onResolve(const boost::system::error_code& error, boost::asio::ip::tcp::resolver::iterator endpointIterator); + void onConnect(const boost::system::error_code& error); + + enum ConnectionState_t { + CONNECTION_STATE_IDLE = 0, + CONNECTION_STATE_RESOLVING, + CONNECTION_STATE_CONNECTING, + CONNECTION_STATE_CONNECTED + }; + +private: + ErrorCallback m_errorCallback; + Callback m_connectCallback; + ConnectionState_t m_connectionState; + + boost::asio::deadline_timer m_timer; + boost::asio::ip::tcp::resolver m_resolver; + boost::asio::ip::tcp::socket m_socket; + +}; + +typedef boost::shared_ptr ConnectionPtr; + +#endif diff --git a/src/framework/net/protocol.cpp b/src/framework/net/protocol.cpp new file mode 100644 index 00000000..01ee8596 --- /dev/null +++ b/src/framework/net/protocol.cpp @@ -0,0 +1,46 @@ +/* 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 + +Protocol::Protocol() : + m_connection(new Connection) +{ + m_connection->setErrorCallback(boost::bind(&Protocol::onError, this, _1)); +} + +void Protocol::connect(const std::string& host, uint16 port, const Callback& callback) +{ + m_connection->connect(host, port, callback); +} + +void Protocol::onError(const boost::system::error_code& error) +{ + logError(error.message().c_str()); + + // invalid hostname + // connection timeouted + + // displays a dialog, finish protocol +} diff --git a/src/framework/net/protocol.h b/src/framework/net/protocol.h new file mode 100644 index 00000000..47643e71 --- /dev/null +++ b/src/framework/net/protocol.h @@ -0,0 +1,43 @@ +/* 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 PROTOCOL_H +#define PROTOCOL_H + +#include + +class Protocol +{ +public: + Protocol(); + + void connect(const std::string& host, uint16 port, const Callback& callback); + + virtual void onError(const boost::system::error_code& error); + +private: + ConnectionPtr m_connection; +}; + +#endif diff --git a/src/main.cpp b/src/main.cpp index 16eb0ba5..2b81273d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -84,6 +84,9 @@ int main(int argc, const char *argv[]) // init platform stuff Platform::init("OTClient"); + // init random numbers + srand(time(NULL)); + // init resources g_resources.init(argv[0]); if(g_resources.setWriteDir(Platform::getAppUserDir())) diff --git a/src/menustate.cpp b/src/menustate.cpp index 59904741..b5c2d199 100644 --- a/src/menustate.cpp +++ b/src/menustate.cpp @@ -123,8 +123,8 @@ 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(); - //m_protocolLogin = ProtocolLoginPtr(new ProtocolLogin); - //m_protocolLogin->login(accountName, password); + m_protocolLogin = ProtocolLoginPtr(new ProtocolLogin); + m_protocolLogin->login(accountName, password); } void MenuState::optionsButton_clicked() diff --git a/src/menustate.h b/src/menustate.h index c95007e6..3b2978a4 100644 --- a/src/menustate.h +++ b/src/menustate.h @@ -28,7 +28,7 @@ #include #include #include -//#include "protocollogin.h" +#include "protocollogin.h" class MenuState : public GameState { @@ -53,7 +53,7 @@ private: void enterGameWindowOkButton_clicked(); TexturePtr m_background; - //ProtocolLoginPtr m_protocolLogin; + ProtocolLoginPtr m_protocolLogin; }; #endif // MENUSTATE_H diff --git a/src/protocollogin.cpp b/src/protocollogin.cpp new file mode 100644 index 00000000..c7cfcd5e --- /dev/null +++ b/src/protocollogin.cpp @@ -0,0 +1,67 @@ +/* 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 "protocollogin.h" + +ProtocolLogin::ProtocolLogin() +{ + +} + +void ProtocolLogin::login(const std::string& accountName, const std::string& password) +{ + // return error if acc or password is empty or any other condition + + m_accountName = accountName; + m_password = password; + + static const char hosts[][32] = { + "login01.tibia.com", + "login02.tibia.com", + "login03.tibia.com", + "login04.tibia.com", + "login05.tibia.com", + "tibia01.cipsoft.com", + "tibia02.cipsoft.com", + "tibia03.cipsoft.com", + "tibia04.cipsoft.com", + "tibia05.cipsoft.com" + }; + + const std::string host = hosts[rand() % 10]; + const uint16 port = 7171; + + connect(host, port, boost::bind(&ProtocolLogin::onConnect, this)); +} + +void ProtocolLogin::onConnect() +{ + logTrace(); + sendPacket(); +} + +void ProtocolLogin::sendPacket() +{ + +} diff --git a/src/protocollogin.h b/src/protocollogin.h new file mode 100644 index 00000000..6a040aaf --- /dev/null +++ b/src/protocollogin.h @@ -0,0 +1,48 @@ +/* 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 PROTOCOLLOGIN_H +#define PROTOCOLLOGIN_H + +#include + +class ProtocolLogin : public Protocol +{ +public: + ProtocolLogin(); + + void login(const std::string& accountName, const std::string& password); + + void onConnect(); + + void sendPacket(); + +private: + std::string m_accountName, m_password; + +}; + +typedef boost::shared_ptr ProtocolLoginPtr; + +#endif