diff --git a/src/framework/net/connection.cpp b/src/framework/net/connection.cpp index ad807392..27ae405a 100644 --- a/src/framework/net/connection.cpp +++ b/src/framework/net/connection.cpp @@ -43,8 +43,11 @@ void Connection::stop() } } -bool Connection::connect(const std::string& ip, uint16 port, ConnectionCallback onConnect) +bool Connection::connect(const std::string& ip, uint16 port, const Callback& callback) { + + logInfo("[Connection::connect]: Ip: %s - Port: %d", ip.c_str(), port); + if(m_connecting){ logError("Already is connecting."); return false; @@ -55,7 +58,7 @@ bool Connection::connect(const std::string& ip, uint16 port, ConnectionCallback return false; } - m_connectCallback = onConnect; + m_connectCallback = callback; m_connecting = true; m_ip = ip; m_port = port; @@ -63,14 +66,15 @@ bool Connection::connect(const std::string& ip, uint16 port, ConnectionCallback //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)); - 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__); return; } @@ -82,6 +86,7 @@ void Connection::onResolveDns(const boost::system::error_code& error, boost::asi void Connection::onConnect(const boost::system::error_code& error) { if(error){ + logInfo("Error"); m_connecting = false; m_errorCallback(error, __FUNCTION__); return; diff --git a/src/framework/net/connection.h b/src/framework/net/connection.h index eab21c40..63d33f0a 100644 --- a/src/framework/net/connection.h +++ b/src/framework/net/connection.h @@ -47,10 +47,10 @@ public: private: Connection(boost::asio::io_service& ioService); - bool connect(const std::string& ip, uint16 port, ConnectionCallback onConnect); + bool connect(const std::string& ip, uint16 port, const Callback& callback); void stop(); - void setErrorCallback(ErrorCallback c) { m_errorCallback = c; } + void setErrorCallback(const ErrorCallback& callback) { m_errorCallback = callback; } void recv(RecvCallback onSend); void send(NetworkMessagePtr networkMessage, ConnectionCallback onRecv); @@ -88,7 +88,7 @@ private: std::string m_ip; uint16_t m_port; - ConnectionCallback m_connectCallback; + Callback m_connectCallback; ErrorCallback m_errorCallback; friend class Protocol; diff --git a/src/framework/net/protocol.cpp b/src/framework/net/protocol.cpp index bc1c3f94..7f31d811 100644 --- a/src/framework/net/protocol.cpp +++ b/src/framework/net/protocol.cpp @@ -26,12 +26,14 @@ Protocol::Protocol() { + logInfo("Protocol()"); m_connection = g_connections.createConnection(); - /*m_connection->setErrorCallback( - [this](const boost::system::error_code& error, const std::string& msg){ - this->onError(error, msg); - } - );*/ + m_connection->setErrorCallback(boost::bind(&Protocol::onError, this, boost::asio::placeholders::error, _2)); +} + +Protocol::~Protocol() +{ + logInfo("~Protocol()"); } void Protocol::send(NetworkMessagePtr networkMessage, Connection::ConnectionCallback onSend) @@ -39,9 +41,9 @@ void Protocol::send(NetworkMessagePtr networkMessage, Connection::ConnectionCall m_connection->send(networkMessage, onSend); } -bool Protocol::connect(const std::string& ip, uint16 port, Connection::ConnectionCallback onConnect) +bool Protocol::connect(const std::string& ip, uint16 port, const Callback& callback) { - return m_connection->connect(ip, port, onConnect); + return m_connection->connect(ip, port, callback); } void Protocol::recv(Connection::RecvCallback onRecv) diff --git a/src/framework/net/protocol.h b/src/framework/net/protocol.h index 169471a9..2389de3f 100644 --- a/src/framework/net/protocol.h +++ b/src/framework/net/protocol.h @@ -31,6 +31,7 @@ class Protocol { public: Protocol(); + ~Protocol(); virtual void begin() = 0; @@ -38,7 +39,7 @@ protected: void send(NetworkMessagePtr networkMessage, Connection::ConnectionCallback onSend); void recv(Connection::RecvCallback onRecv); - bool connect(const std::string& ip, uint16 port, Connection::ConnectionCallback onConnect); + 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; diff --git a/src/menustate.cpp b/src/menustate.cpp index 5dcbc48e..722bf091 100644 --- a/src/menustate.cpp +++ b/src/menustate.cpp @@ -31,6 +31,7 @@ #include "core/dispatcher.h" #include "ui/ui.h" #include "net/connections.h" +#include "net/protocoltibia87.h" #include "graphics/borderedimage.h" @@ -85,9 +86,23 @@ void MenuState::render() void MenuState::enterGameButton_clicked() { - UIElementPtr window = UIContainer::getRootContainer()->getChildById("enterGameWindow"); + UIContainerPtr window = boost::static_pointer_cast(UIContainer::getRootContainer()->getChildById("enterGameWindow")); if(!window) - window = UILoader::loadFile("ui/enterGameWindow.yml"); - window->getParent()->setEnabled(false); + window = UILoader::loadFile("ui/enterGameWindow.yml")->asUIContainer(); + + UIButtonPtr button = boost::static_pointer_cast(window->getChildById("okButton")); + button->setOnClick(boost::bind(&MenuState::enterGameWindowOkButton_clicked, this)); +} + +void MenuState::enterGameWindowOkButton_clicked() +{ + UIContainerPtr enterGameWindow = boost::static_pointer_cast(UIContainer::getRootContainer()->getChildById("enterGameWindow")); + + 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); } diff --git a/src/menustate.h b/src/menustate.h index 1f3c35f8..0c83c10c 100644 --- a/src/menustate.h +++ b/src/menustate.h @@ -48,6 +48,8 @@ public: private: void enterGameButton_clicked(); + void enterGameWindowOkButton_clicked(); + UIPanelPtr m_menuPanel; TexturePtr m_background; }; diff --git a/src/net/protocoltibia87.cpp b/src/net/protocoltibia87.cpp index 64603a04..398dc95f 100644 --- a/src/net/protocoltibia87.cpp +++ b/src/net/protocoltibia87.cpp @@ -37,17 +37,16 @@ ProtocolTibia87::ProtocolTibia87() void ProtocolTibia87::begin() { /* - connect("icechaw.otland.net", 7171, - [this](){ - this->afterConnect(); - } - ); + */ } void ProtocolTibia87::login(const std::string& account, const std::string& password) { - sendAccount(account, 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) @@ -86,6 +85,7 @@ void ProtocolTibia87::sendAccount(const std::string& account, const std::string& void ProtocolTibia87::afterConnect() { + logError("[ProtocolTibia87::afterConnect]: Connected!"); login("9418347", "lollol"); } diff --git a/src/net/protocoltibia87.h b/src/net/protocoltibia87.h index 8e9d5a8a..7870aabf 100644 --- a/src/net/protocoltibia87.h +++ b/src/net/protocoltibia87.h @@ -33,9 +33,10 @@ public: ProtocolTibia87(); virtual void begin(); + void login(const std::string& account, const std::string& password); protected: - void login(const std::string& account, const std::string& password); + void sendAccount(const std::string& account, const std::string& password); void parseCharacterList(NetworkMessagePtr networkMessage); @@ -52,4 +53,4 @@ private: typedef boost::shared_ptr ProtocolTibia87Ptr; -#endif //PROTOCOLTIBIA87_H \ No newline at end of file +#endif //PROTOCOLTIBIA87_H