using lambda

This commit is contained in:
Andre Antunes 2011-04-08 15:18:26 -03:00
parent 4c6d1269a0
commit bda40b218a
5 changed files with 55 additions and 23 deletions

View File

@ -54,17 +54,13 @@ void Connection::connect(const std::string& ip, uint16 port)
m_ip = ip;
m_port = port;
logDebug("connecting...");
//first resolve dns
boost::asio::ip::tcp::resolver::query query(ip, convertType<std::string, uint16>(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)
void Connection::onResolveDns(const boost::system::error_code& error, boost::asio::ip::tcp::resolver::iterator endpointIt)
{
logDebug("resolving dns..");
m_lastError = error;
if(error){
@ -73,7 +69,7 @@ void Connection::onResolveDns(const boost::system::error_code& error, boost::asi
}
//lets connect
m_socket.async_connect(*endpoint_iterator, boost::bind(&Connection::onConnect, this, boost::asio::placeholders::error));
m_socket.async_connect(*endpointIt, boost::bind(&Connection::onConnect, this, boost::asio::placeholders::error));
}
void Connection::onConnect(const boost::system::error_code& error)
@ -87,5 +83,10 @@ void Connection::onConnect(const boost::system::error_code& error)
m_connected = true;
logInfo("Connected on %s.", m_ip.c_str());
if(!m_callback){
logError("onConnect::m_callback not set.");
return;
}
m_callback();
}

View File

@ -28,6 +28,8 @@
#include <boost/asio.hpp>
class TestState;
class Connection
{
public:
@ -43,8 +45,10 @@ public:
void resetLastError() { m_lastError = boost::system::error_code(); }
void setCallback(std::function<void()> f) { m_callback = f; }
private:
void onResolveDns(const boost::system::error_code& error, boost::asio::ip::tcp::resolver::iterator endpoint_iterator);
void onResolveDns(const boost::system::error_code& error, boost::asio::ip::tcp::resolver::iterator endpointIt);
void onConnect(const boost::system::error_code& error);
private:
@ -57,8 +61,11 @@ private:
std::string m_ip;
uint16_t m_port;
std::function<void()> m_callback;
};
typedef std::shared_ptr<Connection> ConnectionPtr;
#endif //CONNECTION_h

View File

@ -109,8 +109,8 @@ int main(int argc, const char *argv[])
// state scope
{
std::shared_ptr<MenuState> initialState(new MenuState);
//std::shared_ptr<TestState> initialState(new TestState);
//std::shared_ptr<MenuState> initialState(new MenuState);
std::shared_ptr<TestState> initialState(new TestState);
g_engine.changeState(initialState.get());
Platform::showWindow();

View File

@ -28,9 +28,26 @@
#include "framework/engine.h"
#include "framework/input.h"
#include "framework/net/connections.h"
void TestState::onEnter()
{
m_connection = g_connections.createConnection();
m_connection->setCallback([this]() {
this->onConnect();
});
m_connection->connect("www.google.com.br", 80);
}
void TestState::onConnect()
{
if(m_connection->isConnected()){
logInfo("Connected.");
}
else{
logError("Not connected: %d", m_connection->getLastError().message().c_str());
}
}
void TestState::onLeave()
@ -57,4 +74,3 @@ void TestState::update(int ticks, int elapsedTicks)
{
}

View File

@ -27,6 +27,8 @@
#include "framework/gamestate.h"
#include "framework/net/connection.h"
class TestState : public GameState
{
public:
@ -40,6 +42,12 @@ public:
virtual void render();
virtual void update(int ticks, int elapsedTicks);
private:
void onConnect();
private:
ConnectionPtr m_connection;
};
#endif // TESTSTATE_H