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

@ -38,7 +38,7 @@ void Connection::stop()
if(m_connecting){
m_resolver.cancel();
m_socket.cancel();
m_connecting = false;
}
}
@ -53,27 +53,23 @@ void Connection::connect(const std::string& ip, uint16 port)
m_connecting = true;
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){
m_connecting = false;
return;
}
//lets connect
m_socket.async_connect(*endpoint_iterator, boost::bind(&Connection::onConnect, this, boost::asio::placeholders::error));
//lets connect
m_socket.async_connect(*endpointIt, boost::bind(&Connection::onConnect, this, boost::asio::placeholders::error));
}
void Connection::onConnect(const boost::system::error_code& error)
@ -86,6 +82,11 @@ 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,37 +28,44 @@
#include <boost/asio.hpp>
class TestState;
class Connection
{
public:
Connection(boost::asio::io_service& ioService);
void connect(const std::string& ip, uint16 port);
void stop();
bool isConnecting() const { return m_connecting; }
bool isConnected() const { return m_connected; }
const boost::system::error_code& getLastError() const { return m_lastError; }
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:
boost::asio::ip::tcp::socket m_socket;
boost::asio::ip::tcp::resolver m_resolver;
boost::system::error_code m_lastError;
bool m_connecting;
bool m_connected;
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