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

97 lines
3.3 KiB
C
Raw Normal View History

2011-08-28 15:17:58 +02:00
/*
* Copyright (c) 2010-2013 OTClient <https://github.com/edubart/otclient>
2011-08-28 15:17:58 +02:00
*
* 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.
*/
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>
2012-07-30 21:40:03 +02:00
#include <framework/luaengine/luaobject.h>
2012-02-01 23:46:31 +01:00
#include <framework/core/timer.h>
#include <framework/core/declarations.h>
2011-04-20 08:40:31 +02:00
2012-07-30 21:40:03 +02:00
class Connection : public LuaObject
2011-04-20 08:40:31 +02:00
{
2012-01-09 19:45:13 +01:00
typedef std::function<void(const boost::system::error_code&)> ErrorCallback;
2011-07-28 01:01:33 +02:00
typedef std::function<void(uint8*, uint16)> RecvCallback;
2011-08-16 02:30:31 +02:00
enum {
2011-11-04 17:40:26 +01:00
READ_TIMEOUT = 30,
WRITE_TIMEOUT = 30,
SEND_INTERVAL = 1,
2012-02-01 23:46:31 +01:00
SEND_BUFFER_SIZE = 65536,
2011-11-04 17:40:26 +01:00
RECV_BUFFER_SIZE = 65536
2011-08-16 02:30:31 +02:00
};
2011-07-28 01:01:33 +02:00
2011-04-20 08:40:31 +02:00
public:
Connection();
2012-01-13 16:41:57 +01:00
~Connection();
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 std::function<void()>& connectCallback);
2011-08-16 02:30:31 +02:00
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);
2012-08-22 10:51:31 +02:00
void read_until(const std::string& what, const RecvCallback& callback);
2011-11-04 17:40:26 +01:00
void read_some(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
int getIp();
boost::system::error_code getError() { return m_error; }
bool isConnecting() { return m_connecting; }
bool isConnected() { return m_connected; }
2011-04-20 08:40:31 +02:00
ConnectionPtr asConnection() { return static_self_cast<Connection>(); }
2011-11-04 17:40:26 +01:00
protected:
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-11-04 17:40:26 +01:00
void onRecv(const boost::system::error_code& error, size_t recvSize);
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
std::function<void()> 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
2012-02-01 23:46:31 +01:00
uint8 m_sendBuffer[SEND_BUFFER_SIZE];
2012-08-22 10:51:31 +02:00
asio::streambuf m_streamBuffer;
2011-08-16 02:30:31 +02:00
bool m_connected;
2011-08-29 20:38:01 +02:00
bool m_connecting;
boost::system::error_code m_error;
2012-02-01 23:46:31 +01:00
int m_sendBufferSize;
Timer m_sendTimer;
ScheduledEventPtr m_sendEvent;
2011-11-04 17:40:26 +01:00
friend class Server;
2011-04-20 08:40:31 +02:00
};
#endif