Otclient  14/8/2020
connection.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2020 OTClient <https://github.com/edubart/otclient>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 
23 #ifndef CONNECTION_H
24 #define CONNECTION_H
25 
26 #include "declarations.h"
28 #include <framework/core/timer.h>
30 
31 class Connection : public LuaObject
32 {
33  typedef std::function<void(const boost::system::error_code&)> ErrorCallback;
34  typedef std::function<void(uint8*, uint16)> RecvCallback;
35 
36  enum {
37  READ_TIMEOUT = 30,
38  WRITE_TIMEOUT = 30,
39  SEND_BUFFER_SIZE = 65536,
40  RECV_BUFFER_SIZE = 65536
41  };
42 
43 public:
44  Connection();
45  ~Connection();
46 
47  static void poll();
48  static void terminate();
49 
50  void connect(const std::string& host, uint16 port, const std::function<void()>& connectCallback);
51  void close();
52 
53  void write(uint8* buffer, size_t size);
54  void read(uint16 bytes, const RecvCallback& callback);
55  void read_until(const std::string& what, const RecvCallback& callback);
56  void read_some(const RecvCallback& callback);
57 
58  void setErrorCallback(const ErrorCallback& errorCallback) { m_errorCallback = errorCallback; }
59 
60  int getIp();
61  boost::system::error_code getError() { return m_error; }
62  bool isConnecting() { return m_connecting; }
63  bool isConnected() { return m_connected; }
65 
66  ConnectionPtr asConnection() { return static_self_cast<Connection>(); }
67 
68 protected:
69  void internal_connect(asio::ip::basic_resolver<asio::ip::tcp>::iterator endpointIterator);
70  void internal_write();
71  void onResolve(const boost::system::error_code& error, asio::ip::tcp::resolver::iterator endpointIterator);
72  void onConnect(const boost::system::error_code& error);
73  void onCanWrite(const boost::system::error_code& error);
74  void onWrite(const boost::system::error_code& error, size_t writeSize, std::shared_ptr<asio::streambuf> outputStream);
75  void onRecv(const boost::system::error_code& error, size_t recvSize);
76  void onTimeout(const boost::system::error_code& error);
77  void handleError(const boost::system::error_code& error);
78 
79  std::function<void()> m_connectCallback;
80  ErrorCallback m_errorCallback;
81  RecvCallback m_recvCallback;
82 
83  asio::deadline_timer m_readTimer;
84  asio::deadline_timer m_writeTimer;
85  asio::deadline_timer m_delayedWriteTimer;
86  asio::ip::tcp::resolver m_resolver;
87  asio::ip::tcp::socket m_socket;
88 
89  static std::list<std::shared_ptr<asio::streambuf>> m_outputStreams;
90  std::shared_ptr<asio::streambuf> m_outputStream;
91  asio::streambuf m_inputStream;
94  boost::system::error_code m_error;
96 
97  friend class Server;
98 };
99 
100 #endif
Connection::onResolve
void onResolve(const boost::system::error_code &error, asio::ip::tcp::resolver::iterator endpointIterator)
Definition: connection.cpp:205
Connection::m_error
boost::system::error_code m_error
Definition: connection.h:94
stdext::timer
Definition: time.h:36
Connection::m_connectCallback
std::function< void()> m_connectCallback
Definition: connection.h:79
Connection::m_readTimer
asio::deadline_timer m_readTimer
Definition: connection.h:83
Connection::handleError
void handleError(const boost::system::error_code &error)
Definition: connection.cpp:297
Connection::read
void read(uint16 bytes, const RecvCallback &callback)
Definition: connection.cpp:157
Server
Definition: server.h:29
Connection::poll
static void poll()
Definition: connection.cpp:53
Connection::m_inputStream
asio::streambuf m_inputStream
Definition: connection.h:91
Connection::getIp
int getIp()
Definition: connection.cpp:309
ticks_t
int64 ticks_t
Definition: types.h:43
Connection::getElapsedTicksSinceLastRead
ticks_t getElapsedTicksSinceLastRead()
Definition: connection.h:64
Connection::m_outputStreams
static std::list< std::shared_ptr< asio::streambuf > > m_outputStreams
Definition: connection.h:89
Connection::onCanWrite
void onCanWrite(const boost::system::error_code &error)
Definition: connection.cpp:241
luaobject.h
uint16
uint16_t uint16
Definition: types.h:36
Connection::m_delayedWriteTimer
asio::deadline_timer m_delayedWriteTimer
Definition: connection.h:85
Connection::Connection
Connection()
Definition: connection.cpp:34
declarations.h
Connection::isConnected
bool isConnected()
Definition: connection.h:63
Connection::onConnect
void onConnect(const boost::system::error_code &error)
Definition: connection.cpp:218
Connection
Definition: connection.h:31
Connection::onWrite
void onWrite(const boost::system::error_code &error, size_t writeSize, std::shared_ptr< asio::streambuf > outputStream)
Definition: connection.cpp:252
timer.h
Connection::m_recvCallback
RecvCallback m_recvCallback
Definition: connection.h:81
Connection::close
void close()
Definition: connection.cpp:66
Connection::m_connected
bool m_connected
Definition: connection.h:92
Connection::m_socket
asio::ip::tcp::socket m_socket
Definition: connection.h:87
Connection::internal_connect
void internal_connect(asio::ip::basic_resolver< asio::ip::tcp >::iterator endpointIterator)
Definition: connection.cpp:108
Connection::getError
boost::system::error_code getError()
Definition: connection.h:61
Connection::write
void write(uint8 *buffer, size_t size)
Definition: connection.cpp:117
Connection::connect
void connect(const std::string &host, uint16 port, const std::function< void()> &connectCallback)
Definition: connection.cpp:93
Connection::read_until
void read_until(const std::string &what, const RecvCallback &callback)
Definition: connection.cpp:173
Connection::~Connection
~Connection()
Definition: connection.cpp:45
Connection::asConnection
ConnectionPtr asConnection()
Definition: connection.h:66
Connection::m_resolver
asio::ip::tcp::resolver m_resolver
Definition: connection.h:86
Connection::m_errorCallback
ErrorCallback m_errorCallback
Definition: connection.h:80
stdext::shared_object_ptr< Connection >
stdext::timer::elapsed_millis
ticks_t elapsed_millis()
Definition: time.h:40
Connection::m_writeTimer
asio::deadline_timer m_writeTimer
Definition: connection.h:84
Connection::m_outputStream
std::shared_ptr< asio::streambuf > m_outputStream
Definition: connection.h:90
Connection::isConnecting
bool isConnecting()
Definition: connection.h:62
Connection::m_activityTimer
stdext::timer m_activityTimer
Definition: connection.h:95
Connection::m_connecting
bool m_connecting
Definition: connection.h:93
Connection::onTimeout
void onTimeout(const boost::system::error_code &error)
Definition: connection.cpp:289
Connection::read_some
void read_some(const RecvCallback &callback)
Definition: connection.cpp:190
Connection::terminate
static void terminate()
Definition: connection.cpp:60
LuaObject
LuaObject, all script-able classes have it as base.
Definition: luaobject.h:30
Connection::setErrorCallback
void setErrorCallback(const ErrorCallback &errorCallback)
Definition: connection.h:58
uint8
uint8_t uint8
Definition: types.h:37
Connection::internal_write
void internal_write()
Definition: connection.cpp:140
Connection::onRecv
void onRecv(const boost::system::error_code &error, size_t recvSize)
Definition: connection.cpp:267