connection update

This commit is contained in:
Andre Antunes 2011-04-08 20:24:51 -03:00
parent bda40b218a
commit 02c58f16cd
7 changed files with 118 additions and 50 deletions

View File

@ -55,6 +55,9 @@ SET(SOURCES
src/menustate.cpp src/menustate.cpp
src/teststate.cpp src/teststate.cpp
# game net
src/net/protocoltibia87.cpp
# framework sources # framework sources
src/framework/dispatcher.cpp src/framework/dispatcher.cpp
src/framework/framebuffer.cpp src/framework/framebuffer.cpp
@ -79,7 +82,9 @@ SET(SOURCES
# network # network
src/framework/net/connection.cpp src/framework/net/connection.cpp
src/framework/net/connections.cpp) src/framework/net/connections.cpp
src/framework/net/protocol.cpp
src/framework/net/networkmessage.cpp)
IF(WIN32) IF(WIN32)
SET(SOURCES ${SOURCES} src/framework/win32platform.cpp) SET(SOURCES ${SOURCES} src/framework/win32platform.cpp)

View File

@ -43,13 +43,19 @@ void Connection::stop()
} }
} }
void Connection::connect(const std::string& ip, uint16 port) bool Connection::connect(const std::string& ip, uint16 port, ConnectionCallback onConnect)
{ {
if(m_connecting){ if(m_connecting){
logError("Already is connecting."); logError("Already is connecting.");
return; return false;
} }
if(m_connected){
logError("Already is connected.");
return false;
}
m_connectCallback = onConnect;
m_connecting = true; m_connecting = true;
m_ip = ip; m_ip = ip;
m_port = port; m_port = port;
@ -57,14 +63,15 @@ void Connection::connect(const std::string& ip, uint16 port)
//first resolve dns //first resolve dns
boost::asio::ip::tcp::resolver::query query(ip, convertType<std::string, uint16>(port)); 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)); 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) void Connection::onResolveDns(const boost::system::error_code& error, boost::asio::ip::tcp::resolver::iterator endpointIt)
{ {
m_lastError = error;
if(error){ if(error){
m_connecting = false; m_connecting = false;
m_connectCallback(error);
return; return;
} }
@ -74,19 +81,75 @@ void Connection::onResolveDns(const boost::system::error_code& error, boost::asi
void Connection::onConnect(const boost::system::error_code& error) void Connection::onConnect(const boost::system::error_code& error)
{ {
m_lastError = error; if(!error){
m_connected = true;
}
m_connecting = false;
m_connectCallback(error);
}
void Connection::handleError(const boost::system::error_code& error)
{
if(isConnected()){
closeSocket();
}
stop();
}
void Connection::closeSocket()
{
boost::system::error_code error;
m_socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both, error);
if(error) { if(error) {
m_connecting = false; logError("Connection::closeSocket(): %s", error.message().c_str());
}
m_socket.close(error);
if(error) {
logError("Connection::closeSocket(): %s", error.message().c_str());
}
}
void Connection::send(NetworkMessagePtr networkMessage, ConnectionCallback onSend)
{
m_socket.async_send(
boost::asio::buffer(networkMessage->getBuffer(), NetworkMessage::header_length),
boost::bind(&Connection::onSendHeader, shared_from_this(), networkMessage, onSend, boost::asio::placeholders::error)
);
}
void Connection::onSendHeader(ConnectionPtr connection, NetworkMessagePtr networkMessage, ConnectionCallback onSend, const boost::system::error_code& error)
{
if(!connection->isConnected()){
return; return;
} }
m_connected = true; if(error){
connection->handleError(error);
if(!m_callback){ onSend(error);
logError("onConnect::m_callback not set.");
return; return;
} }
m_callback(); connection->getSocket().async_send(
boost::asio::buffer(networkMessage->getBodyBuffer(), networkMessage->getMessageLength()),
boost::bind(&Connection::onSendBody, connection, networkMessage, onSend, boost::asio::placeholders::error)
);
}
void Connection::onSendBody(ConnectionPtr connection, NetworkMessagePtr networkMessage, ConnectionCallback onSend, const boost::system::error_code& error)
{
if(!connection->isConnected()){
return;
}
if(error){
connection->handleError(error);
}
onSend(error);
} }

View File

@ -28,33 +28,46 @@
#include <boost/asio.hpp> #include <boost/asio.hpp>
class TestState; #include "networkmessage.h"
class Connection class TestState;
class Protocol;
class Connections;
class Connection : public std::enable_shared_from_this<Connection>
{ {
public: public:
typedef std::function<void(const boost::system::error_code&)> ConnectionCallback;
typedef std::shared_ptr<Connection> ConnectionPtr;
private:
Connection(boost::asio::io_service& ioService); Connection(boost::asio::io_service& ioService);
void connect(const std::string& ip, uint16 port); bool connect(const std::string& ip, uint16 port, ConnectionCallback onConnect);
void stop(); void stop();
void send(NetworkMessagePtr networkMessage, ConnectionCallback onSend);
bool isConnecting() const { return m_connecting; } bool isConnecting() const { return m_connecting; }
bool isConnected() const { return m_connected; } bool isConnected() const { return m_connected; }
const boost::system::error_code& getLastError() const { return m_lastError; } boost::asio::ip::tcp::socket& getSocket() { return m_socket; }
void resetLastError() { m_lastError = boost::system::error_code(); } private:
static void onSendHeader(ConnectionPtr connection, NetworkMessagePtr networkMessage, ConnectionCallback onSend, const boost::system::error_code& error);
void setCallback(std::function<void()> f) { m_callback = f; } static void onSendBody(ConnectionPtr connection, NetworkMessagePtr networkMessage, ConnectionCallback onSend, const boost::system::error_code& error);
private: private:
void onResolveDns(const boost::system::error_code& error, boost::asio::ip::tcp::resolver::iterator endpointIt); void onResolveDns(const boost::system::error_code& error, boost::asio::ip::tcp::resolver::iterator endpointIt);
void onConnect(const boost::system::error_code& error); void onConnect(const boost::system::error_code& error);
private: private:
void closeSocket();
private:
void handleError(const boost::system::error_code& error);
boost::asio::ip::tcp::socket m_socket; boost::asio::ip::tcp::socket m_socket;
boost::asio::ip::tcp::resolver m_resolver; boost::asio::ip::tcp::resolver m_resolver;
boost::system::error_code m_lastError;
bool m_connecting; bool m_connecting;
bool m_connected; bool m_connected;
@ -62,10 +75,12 @@ private:
std::string m_ip; std::string m_ip;
uint16_t m_port; uint16_t m_port;
std::function<void()> m_callback; ConnectionCallback m_connectCallback;
friend class Protocol;
friend class Connections;
}; };
typedef std::shared_ptr<Connection> ConnectionPtr; typedef std::shared_ptr<Connection> ConnectionPtr;
#endif //CONNECTION_h #endif //CONNECTION_h

View File

@ -31,6 +31,7 @@ typedef unsigned char uchar;
typedef unsigned short ushort; typedef unsigned short ushort;
typedef unsigned int uint; typedef unsigned int uint;
typedef unsigned long ulong; typedef unsigned long ulong;
typedef uint64_t uint64;
typedef uint32_t uint32; typedef uint32_t uint32;
typedef uint16_t uint16; typedef uint16_t uint16;
typedef uint8_t uint8; typedef uint8_t uint8;

View File

@ -30,24 +30,12 @@
#include "framework/net/connections.h" #include "framework/net/connections.h"
#include "net/protocoltibia87.h"
void TestState::onEnter() void TestState::onEnter()
{ {
m_connection = g_connections.createConnection(); m_protocol = ProtocolTibia87Ptr(new ProtocolTibia87);
m_connection->setCallback([this]() { m_protocol->begin();
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() void TestState::onLeave()

View File

@ -26,8 +26,7 @@
#define TESTSTATE_H #define TESTSTATE_H
#include "framework/gamestate.h" #include "framework/gamestate.h"
#include "net/protocoltibia87.h"
#include "framework/net/connection.h"
class TestState : public GameState class TestState : public GameState
{ {
@ -44,10 +43,7 @@ public:
virtual void update(int ticks, int elapsedTicks); virtual void update(int ticks, int elapsedTicks);
private: private:
void onConnect(); ProtocolTibia87Ptr m_protocol;
private:
ConnectionPtr m_connection;
}; };
#endif // TESTSTATE_H #endif // TESTSTATE_H