This commit is contained in:
Eduardo Bart
2011-04-16 23:48:13 -03:00
parent 2070b94661
commit da57770f88
16 changed files with 163 additions and 335 deletions

View File

@@ -28,9 +28,9 @@
#include "graphics/graphics.h"
#include "configs.h"
#include "dispatcher.h"
#include "net/connections.h"
#include "ui/uicontainer.h"
#include "graphics/fonts.h"
#include "net/connection.h"
Engine g_engine;
@@ -70,7 +70,7 @@ void Engine::run()
Platform::poll();
// poll network events
g_connections.poll();
Connection::poll();
// poll diaptcher tasks
g_dispatcher.poll();

View File

@@ -23,38 +23,52 @@
#include "connection.h"
#include <boost/bind.hpp>
static boost::asio::io_service ioService;
Connection::Connection(boost::asio::io_service& ioService)
: m_socket(ioService), m_resolver(ioService)
Connection::Connection() :
m_socket(ioService),
m_resolver(ioService),
m_connecting(false),
m_connected(false),
m_port(0)
{
m_connected = false;
m_connecting = false;
m_port = 0;
logTrace();
}
void Connection::stop()
Connection::~Connection()
{
if(m_connecting){
logTrace();
}
void Connection::poll()
{
ioService.poll();
ioService.reset();
}
void Connection::close()
{
logTrace();
if(m_connecting) {
m_resolver.cancel();
m_socket.cancel();
m_connecting = false;
m_connected = false;
closeSocket();
}
}
bool Connection::connect(const std::string& ip, uint16 port, const Callback& callback)
{
logTrace();
logInfo("[Connection::connect]: Ip: %s - Port: %d", ip.c_str(), port);
if(m_connecting){
logError("Already is connecting.");
if(m_connecting) {
logTraceError("already connecting.");
return false;
}
if(m_connected){
logError("Already is connected.");
if(m_connected) {
logTraceError("already connected.");
return false;
}
@@ -63,123 +77,116 @@ bool Connection::connect(const std::string& ip, uint16 port, const Callback& cal
m_ip = ip;
m_port = port;
//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));
m_resolver.async_resolve(query, boost::bind(&Connection::onResolveDns, shared_from_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__);
logTrace();
if(error) {
handleError(error);
return;
}
//lets connect
m_socket.async_connect(*endpointIt, boost::bind(&Connection::onConnect, this, boost::asio::placeholders::error));
m_socket.async_connect(*endpointIt, boost::bind(&Connection::onConnect, shared_from_this(), boost::asio::placeholders::error));
}
void Connection::onConnect(const boost::system::error_code& error)
{
if(error){
logInfo("Error");
m_connecting = false;
m_errorCallback(error, __FUNCTION__);
logTrace();
if(error) {
handleError(error);
return;
}
m_connected = true;
m_connectCallback();
}
void Connection::handleError(const boost::system::error_code& error)
{
stop();
logTrace();
if(isConnected()){
closeSocket();
}
close();
m_errorCallback(error);
}
void Connection::closeSocket()
{
logTrace();
boost::system::error_code error;
m_socket.shutdown(boost::asio::ip::tcp::socket::shutdown_both, error);
if(error) {
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)
void Connection::send(const NetworkMessage& networkMessage, const ConnectionCallback& onSend)
{
logTrace();
boost::asio::async_write(m_socket,
boost::asio::buffer(networkMessage->getBuffer(), NetworkMessage::header_length),
boost::asio::buffer(networkMessage.getBuffer(), NetworkMessage::header_length),
boost::bind(&Connection::onSendHeader, shared_from_this(), networkMessage, onSend, boost::asio::placeholders::error));
}
void Connection::recv(RecvCallback onRecv)
void Connection::recv(const RecvCallback& onRecv)
{
NetworkMessagePtr networkMessage(new NetworkMessage);
logTrace();
static NetworkMessage networkMessage;
boost::asio::async_read(m_socket,
boost::asio::buffer(networkMessage->getBuffer(), NetworkMessage::header_length),
boost::asio::buffer(networkMessage.getBuffer(), NetworkMessage::header_length),
boost::bind(&Connection::onRecvHeader, shared_from_this(), networkMessage, onRecv, boost::asio::placeholders::error));
}
void Connection::onRecvHeader(ConnectionPtr connection, NetworkMessagePtr networkMessage, RecvCallback onRecv, const boost::system::error_code& error)
void Connection::onRecvHeader(const NetworkMessage& networkMessage, const RecvCallback& onRecv, const boost::system::error_code& error)
{
if(error){
connection->handleError(error);
connection->onError(error, __FUNCTION__);
logTrace();
if(error) {
handleError(error);
return;
}
boost::asio::async_read(connection->getSocket(),
boost::asio::buffer(networkMessage->getBodyBuffer(), networkMessage->getMessageLength()),
boost::bind(&Connection::onRecvBody, connection, networkMessage, onRecv, boost::asio::placeholders::error));
boost::asio::async_read(m_socket,
boost::asio::buffer(networkMessage.getBodyBuffer(), networkMessage.getMessageLength()),
boost::bind(&Connection::onRecvBody, shared_from_this(), networkMessage, onRecv, boost::asio::placeholders::error));
}
void Connection::onRecvBody(ConnectionPtr connection, NetworkMessagePtr networkMessage, RecvCallback onRecv, const boost::system::error_code& error)
void Connection::onRecvBody(const NetworkMessage& networkMessage, const RecvCallback& onRecv, const boost::system::error_code& error)
{
logTrace();
if(error){
connection->handleError(error);
connection->onError(error, __FUNCTION__);
handleError(error);
return;
}
onRecv(networkMessage);
}
void Connection::onSendHeader(ConnectionPtr connection, NetworkMessagePtr networkMessage, ConnectionCallback onSend, const boost::system::error_code& error)
void Connection::onSendHeader(const NetworkMessage& networkMessage, const ConnectionCallback& onSend, const boost::system::error_code& error)
{
logTrace();
if(error){
connection->handleError(error);
connection->onError(error, __FUNCTION__);
handleError(error);
return;
}
boost::asio::async_write(connection->getSocket(),
boost::asio::buffer(networkMessage->getBodyBuffer(), networkMessage->getMessageLength()),
boost::bind(&Connection::onSendBody, connection, networkMessage, onSend, boost::asio::placeholders::error));
boost::asio::async_write(m_socket,
boost::asio::buffer(networkMessage.getBodyBuffer(), networkMessage.getMessageLength()),
boost::bind(&Connection::onSendBody, shared_from_this(), networkMessage, onSend, boost::asio::placeholders::error));
}
void Connection::onSendBody(ConnectionPtr connection, NetworkMessagePtr networkMessage, ConnectionCallback onSend, const boost::system::error_code& error)
void Connection::onSendBody(const NetworkMessage& networkMessage, const ConnectionCallback& onSend, const boost::system::error_code& error)
{
if(error){
connection->handleError(error);
connection->onError(error, __FUNCTION__);
logTrace();
if(error) {
handleError(error);
return;
}

View File

@@ -25,58 +25,47 @@
#define CONNECTION_H
#include "prerequisites.h"
#include "networkmessage.h"
#include <boost/asio.hpp>
#include "networkmessage.h"
class TestState;
class Protocol;
class Connections;
class Connection;
typedef boost::shared_ptr<Connection> ConnectionPtr;
typedef boost::function<void()> ConnectionCallback;
typedef boost::function<void(const NetworkMessage&)> RecvCallback;
typedef boost::function<void(const boost::system::error_code&)> ErrorCallback;
class Connection : public boost::enable_shared_from_this<Connection>
{
public:
typedef boost::function<void()> ConnectionCallback;
typedef boost::function<void(NetworkMessagePtr)> RecvCallback;
typedef boost::function<void(const boost::system::error_code&, const std::string&)> ErrorCallback;
typedef boost::shared_ptr<Connection> ConnectionPtr;
private:
Connection(boost::asio::io_service& ioService);
Connection();
~Connection();
bool connect(const std::string& ip, uint16 port, const Callback& callback);
void stop();
void close();
void setErrorCallback(const ErrorCallback& callback) { m_errorCallback = callback; }
void setOnError(const ErrorCallback& callback) { m_errorCallback = callback; }
void recv(RecvCallback onSend);
void send(NetworkMessagePtr networkMessage, ConnectionCallback onRecv);
void recv(const RecvCallback& onSend);
void send(const NetworkMessage& networkMessage, const ConnectionCallback& onRecv);
bool isConnecting() const { return m_connecting; }
bool isConnected() const { return m_connected; }
boost::asio::ip::tcp::socket& getSocket() { return m_socket; }
void onError(const boost::system::error_code& error, const std::string& msg) { m_errorCallback(error, msg); }
private:
static void onSendHeader(ConnectionPtr connection, NetworkMessagePtr networkMessage, ConnectionCallback onSend, const boost::system::error_code& error);
static void onSendBody(ConnectionPtr connection, NetworkMessagePtr networkMessage, ConnectionCallback onSend, const boost::system::error_code& error);
static void onRecvHeader(ConnectionPtr connection, NetworkMessagePtr networkMessage, RecvCallback onRecv, const boost::system::error_code& error);
static void onRecvBody(ConnectionPtr connection, NetworkMessagePtr networkMessage, RecvCallback onRecv, const boost::system::error_code& error);
static void poll();
private:
void onSendHeader(const NetworkMessage& networkMessage, const ConnectionCallback& onSend, const boost::system::error_code& error);
void onSendBody(const NetworkMessage& networkMessage, const ConnectionCallback& onSend, const boost::system::error_code& error);
void onRecvHeader(const NetworkMessage& networkMessage, const RecvCallback& onRecv, const boost::system::error_code& error);
void onRecvBody(const NetworkMessage& networkMessage, const RecvCallback& onRecv, const boost::system::error_code& error);
void onResolveDns(const boost::system::error_code& error, boost::asio::ip::tcp::resolver::iterator endpointIt);
void onConnect(const boost::system::error_code& error);
private:
void closeSocket();
private:
void handleError(const boost::system::error_code& error);
boost::asio::ip::tcp::socket m_socket;
@@ -90,11 +79,6 @@ private:
Callback m_connectCallback;
ErrorCallback m_errorCallback;
friend class Protocol;
friend class Connections;
};
typedef boost::shared_ptr<Connection> ConnectionPtr;
#endif //CONNECTION_h

View File

@@ -1,39 +0,0 @@
/* The MIT License
*
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "connections.h"
Connections g_connections;
size_t Connections::poll()
{
return m_ioService.poll();
}
ConnectionPtr Connections::createConnection()
{
ConnectionPtr connection(new Connection(m_ioService));
m_connections.push_back(connection);
return connection;
}

View File

@@ -1,47 +0,0 @@
/* The MIT License
*
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef CONNECTIONS_H
#define CONNECTIONS_H
#include "prerequisites.h"
#include "connection.h"
class Connections
{
public:
size_t poll();
ConnectionPtr createConnection();
private:
boost::asio::io_service m_ioService;
typedef std::vector<ConnectionPtr> ConnectionVector;
ConnectionVector m_connections;
};
extern Connections g_connections;
#endif //CONNECTIONS_H

View File

@@ -168,11 +168,10 @@ uint64 NetworkMessage::getU64()
return v;
}
char* NetworkMessage::getBuffer() {
char* NetworkMessage::getBuffer() const {
return (char*)&m_msgBuf[0];
}
char* NetworkMessage::getBodyBuffer() {
m_readPos = 2;
char* NetworkMessage::getBodyBuffer() const {
return (char*)&m_msgBuf[header_length];
}

View File

@@ -78,8 +78,8 @@ public:
void setMessageLength(int32 newSize);
int32 getReadPos() const;
int32 getHeaderSize();
char* getBuffer();
char* getBodyBuffer();
char* getBuffer() const;
char* getBodyBuffer() const;
void updateHeaderLength();
@@ -92,6 +92,4 @@ protected:
uint8 m_msgBuf[NETWORKMESSAGE_MAXSIZE];
};
typedef boost::shared_ptr<NetworkMessage> NetworkMessagePtr;
#endif //NETWORKMESSAGE_H

View File

@@ -22,21 +22,20 @@
*/
#include "protocol.h"
#include "connections.h"
Protocol::Protocol()
Protocol::Protocol() :
m_connection(new Connection)
{
logInfo("Protocol()");
m_connection = g_connections.createConnection();
m_connection->setErrorCallback(boost::bind(&Protocol::onError, this, boost::asio::placeholders::error, _2));
logTrace();
m_connection->setOnError(boost::bind(&Protocol::onError, this, boost::asio::placeholders::error));
}
Protocol::~Protocol()
{
logInfo("~Protocol()");
logTrace();
}
void Protocol::send(NetworkMessagePtr networkMessage, Connection::ConnectionCallback onSend)
void Protocol::send(const NetworkMessage& networkMessage, const ConnectionCallback& onSend)
{
m_connection->send(networkMessage, onSend);
}
@@ -46,7 +45,7 @@ bool Protocol::connect(const std::string& ip, uint16 port, const Callback& callb
return m_connection->connect(ip, port, callback);
}
void Protocol::recv(Connection::RecvCallback onRecv)
void Protocol::recv(const RecvCallback& onRecv)
{
m_connection->recv(onRecv);
}

View File

@@ -31,17 +31,13 @@ class Protocol
{
public:
Protocol();
~Protocol();
virtual void begin() = 0;
virtual ~Protocol();
protected:
void send(NetworkMessagePtr networkMessage, Connection::ConnectionCallback onSend);
void recv(Connection::RecvCallback onRecv);
void send(const NetworkMessage& networkMessage, const ConnectionCallback& onSend);
void recv(const RecvCallback& onRecv);
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;
virtual void onError(const boost::system::error_code& error) = 0;
ConnectionPtr m_connection;
};