tibia-client/src/framework/net/connection.h

57 lines
1.6 KiB
C
Raw Normal View History

2011-04-20 08:40:31 +02:00
#ifndef CONNECTION_H
#define CONNECTION_H
2011-08-15 16:06:15 +02:00
#include "declarations.h"
#include <boost/asio.hpp>
2011-04-20 08:40:31 +02:00
class Connection : public std::enable_shared_from_this<Connection>, boost::noncopyable
2011-04-20 08:40:31 +02:00
{
2011-07-28 01:01:33 +02:00
typedef std::function<void(boost::system::error_code&)> ErrorCallback;
typedef std::function<void(uint8*, uint16)> RecvCallback;
2011-08-16 02:30:31 +02:00
enum {
READ_TIMEOUT = 10,
WRITE_TIMEOUT = 10
};
2011-07-28 01:01:33 +02:00
2011-04-20 08:40:31 +02:00
public:
Connection();
2011-08-16 02:30:31 +02:00
static void init();
2011-04-20 08:40:31 +02:00
static void poll();
2011-08-16 02:30:31 +02:00
static void terminate();
void connect(const std::string& host, uint16 port, const SimpleCallback& connectCallback);
void close();
2011-04-20 08:40:31 +02:00
2011-08-16 02:30:31 +02:00
void write(uint8* buffer, uint16 size);
void read(uint16 bytes, const RecvCallback& callback);
2011-05-30 05:11:12 +02:00
2011-04-20 08:40:31 +02:00
void setErrorCallback(const ErrorCallback& errorCallback) { m_errorCallback = errorCallback; }
2011-08-16 02:30:31 +02:00
bool isConnected() const { return m_connected; }
2011-04-20 08:40:31 +02:00
2011-07-28 01:01:33 +02:00
private:
void onResolve(const boost::system::error_code& error, asio::ip::tcp::resolver::iterator endpointIterator);
2011-04-20 08:40:31 +02:00
void onConnect(const boost::system::error_code& error);
2011-08-16 02:30:31 +02:00
void onWrite(const boost::system::error_code& error, size_t);
2011-07-27 20:10:49 +02:00
void onRecv(const boost::system::error_code& error);
2011-08-16 02:30:31 +02:00
void onTimeout(const boost::system::error_code& error);
void handleError(const boost::system::error_code& error);
2011-04-20 08:40:31 +02:00
2011-08-16 02:30:31 +02:00
SimpleCallback m_connectCallback;
2011-04-20 08:40:31 +02:00
ErrorCallback m_errorCallback;
2011-08-16 02:30:31 +02:00
RecvCallback m_recvCallback;
2011-04-20 08:40:31 +02:00
2011-08-16 02:30:31 +02:00
asio::deadline_timer m_readTimer;
asio::deadline_timer m_writeTimer;
2011-07-28 01:01:33 +02:00
asio::ip::tcp::resolver m_resolver;
asio::ip::tcp::socket m_socket;
2011-04-20 08:40:31 +02:00
2011-08-01 06:28:41 +02:00
uint8 m_recvBuffer[65538];
2011-07-27 20:10:49 +02:00
uint16 m_recvSize;
2011-08-16 02:30:31 +02:00
bool m_connected;
2011-04-20 08:40:31 +02:00
};
#endif