From 6f952adae2d5dc9ba85519ee0dbc56391be4161a Mon Sep 17 00:00:00 2001 From: Andre Antunes Date: Thu, 7 Apr 2011 15:02:06 -0300 Subject: [PATCH] connection is now working properly --- src/framework/engine.cpp | 19 +------------------ src/framework/net/connection.cpp | 9 ++++++--- src/framework/net/connection.h | 2 ++ src/menustate.cpp | 9 ++++++++- src/menustate.h | 3 +++ 5 files changed, 20 insertions(+), 22 deletions(-) diff --git a/src/framework/engine.cpp b/src/framework/engine.cpp index d04bfc26..252c20c5 100644 --- a/src/framework/engine.cpp +++ b/src/framework/engine.cpp @@ -76,7 +76,7 @@ void Engine::run() Platform::poll(); //poll network events - //logDebug("%d", g_connections.poll()); + g_connections.poll(); // update before redering ticks = Platform::getTicks(); @@ -107,23 +107,6 @@ void Engine::run() // swap buffers Platform::swapBuffers(); - - /* - static ConnectionPtr connection = g_connections.createConnection(); - - if(connection->getLastError()){ - logError("%s", connection->getLastError().message().c_str()); - } - else{ - if(!connection->isConnecting() && !connection->isConnected()){ - connection->connect("www.google.com.br", 80); - } - - if(!connection->isConnected()){ - logDebug("still not connected."); - } - } - */ //} } } diff --git a/src/framework/net/connection.cpp b/src/framework/net/connection.cpp index 4ef2ddd4..200612cd 100644 --- a/src/framework/net/connection.cpp +++ b/src/framework/net/connection.cpp @@ -57,16 +57,18 @@ void Connection::connect(const std::string& ip, uint16 port) logDebug("connecting..."); //first resolve dns - boost::asio::ip::tcp::resolver::query query(ip, "80"); + 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)); } void Connection::onResolveDns(const boost::system::error_code& error, boost::asio::ip::tcp::resolver::iterator endpoint_iterator) { logDebug("resolving dns.."); + + m_lastError = error; + if(error){ m_connecting = false; - m_lastError = error; return; } @@ -76,9 +78,10 @@ void Connection::onResolveDns(const boost::system::error_code& error, boost::asi void Connection::onConnect(const boost::system::error_code& error) { + m_lastError = error; + if(error){ m_connecting = false; - m_lastError = error; return; } diff --git a/src/framework/net/connection.h b/src/framework/net/connection.h index 6353d1ca..d26c9386 100644 --- a/src/framework/net/connection.h +++ b/src/framework/net/connection.h @@ -40,6 +40,8 @@ public: bool isConnected() const { return m_connected; } const boost::system::error_code& getLastError() const { return m_lastError; } + + void resetLastError() { m_lastError = boost::system::error_code(); } private: void onResolveDns(const boost::system::error_code& error, boost::asio::ip::tcp::resolver::iterator endpoint_iterator); diff --git a/src/menustate.cpp b/src/menustate.cpp index 96393132..2ccc6345 100644 --- a/src/menustate.cpp +++ b/src/menustate.cpp @@ -30,6 +30,7 @@ #include "framework/engine.h" #include "framework/rect.h" #include "framework/fonts.h" +#include "framework/net/connections.h" TexturePtr background; @@ -37,6 +38,9 @@ void MenuState::onEnter() { m_background = g_textures.get("background.png"); m_background->enableBilinearFilter(); + + m_connection = g_connections.createConnection(); + m_connection->connect("www.google.com.br", 80); } void MenuState::onLeave() @@ -74,5 +78,8 @@ void MenuState::render() void MenuState::update(int ticks, int elapsedTicks) { - + if(m_connection->getLastError()){ + logError("%s", m_connection->getLastError().message().c_str()); + m_connection->resetLastError(); + } } diff --git a/src/menustate.h b/src/menustate.h index 033f5cf3..5b46f035 100644 --- a/src/menustate.h +++ b/src/menustate.h @@ -27,6 +27,7 @@ #include "framework/gamestate.h" #include "framework/texture.h" +#include "framework/net/connection.h" class MenuState : public GameState { @@ -45,6 +46,8 @@ public: private: TexturePtr m_background; + ConnectionPtr m_connection; + int m_connectionTicks; }; #endif // MENUSTATE_H