mirror of
https://github.com/edubart/otclient.git
synced 2025-10-16 04:24:54 +02:00
Huge engine change, replace all std::shared_ptrs
Create a new shared pointer type stdext::shared_object_ptr and stdext::shared_obj using boost::intrusive_ptr Advantages: * half memory usage * faster and lightweight Disadvantages: * using weak_ptr is not supported anymore * compiling seems slower
This commit is contained in:
@@ -65,23 +65,23 @@ void Connection::connect(const std::string& host, uint16 port, const std::functi
|
||||
|
||||
asio::ip::tcp::resolver::query query(host, stdext::unsafe_cast<std::string>(port));
|
||||
|
||||
auto weakSelf = ConnectionWeakPtr(shared_from_this());
|
||||
auto self = asConnection();
|
||||
m_resolver.async_resolve(query, [=](const boost::system::error_code& error, asio::ip::tcp::resolver::iterator endpointIterator) {
|
||||
if(!weakSelf.lock())
|
||||
if(self->is_unique_ref())
|
||||
return;
|
||||
m_readTimer.cancel();
|
||||
|
||||
if(!error) {
|
||||
m_socket.async_connect(*endpointIterator, std::bind(&Connection::onConnect, shared_from_this(), std::placeholders::_1));
|
||||
m_socket.async_connect(*endpointIterator, std::bind(&Connection::onConnect, asConnection(), std::placeholders::_1));
|
||||
|
||||
m_readTimer.expires_from_now(boost::posix_time::seconds(READ_TIMEOUT));
|
||||
m_readTimer.async_wait(std::bind(&Connection::onTimeout, shared_from_this(), std::placeholders::_1));
|
||||
m_readTimer.async_wait(std::bind(&Connection::onTimeout, asConnection(), std::placeholders::_1));
|
||||
} else
|
||||
handleError(error);
|
||||
});
|
||||
|
||||
m_readTimer.expires_from_now(boost::posix_time::seconds(READ_TIMEOUT));
|
||||
m_readTimer.async_wait(std::bind(&Connection::onTimeout, shared_from_this(), std::placeholders::_1));
|
||||
m_readTimer.async_wait(std::bind(&Connection::onTimeout, asConnection(), std::placeholders::_1));
|
||||
}
|
||||
|
||||
void Connection::close()
|
||||
@@ -124,20 +124,20 @@ void Connection::write(uint8* buffer, uint16 size)
|
||||
m_sendBufferSize += size;
|
||||
|
||||
if(!m_sendEvent || m_sendEvent->isExecuted() || m_sendEvent->isCanceled()) {
|
||||
auto weakSelf = ConnectionWeakPtr(shared_from_this());
|
||||
auto self = asConnection();
|
||||
|
||||
// wait 1 ms to do the real send
|
||||
m_sendEvent = g_dispatcher.scheduleEvent([=] {
|
||||
if(!weakSelf.lock())
|
||||
if(self->is_unique_ref())
|
||||
return;
|
||||
//m_writeTimer.cancel();
|
||||
|
||||
asio::async_write(m_socket,
|
||||
asio::buffer(m_sendBuffer, m_sendBufferSize),
|
||||
std::bind(&Connection::onWrite, shared_from_this(), std::placeholders::_1, std::placeholders::_2));
|
||||
std::bind(&Connection::onWrite, asConnection(), std::placeholders::_1, std::placeholders::_2));
|
||||
|
||||
m_writeTimer.expires_from_now(boost::posix_time::seconds(WRITE_TIMEOUT));
|
||||
m_writeTimer.async_wait(std::bind(&Connection::onTimeout, shared_from_this(), std::placeholders::_1));
|
||||
m_writeTimer.async_wait(std::bind(&Connection::onTimeout, asConnection(), std::placeholders::_1));
|
||||
|
||||
m_sendBufferSize = 0;
|
||||
}, SEND_INTERVAL);
|
||||
@@ -155,10 +155,10 @@ void Connection::read(uint16 bytes, const RecvCallback& callback)
|
||||
|
||||
asio::async_read(m_socket,
|
||||
asio::buffer(m_recvBuffer, bytes),
|
||||
std::bind(&Connection::onRecv, shared_from_this(), std::placeholders::_1, bytes));
|
||||
std::bind(&Connection::onRecv, asConnection(), std::placeholders::_1, bytes));
|
||||
|
||||
m_readTimer.expires_from_now(boost::posix_time::seconds(READ_TIMEOUT));
|
||||
m_readTimer.async_wait(std::bind(&Connection::onTimeout, shared_from_this(), std::placeholders::_1));
|
||||
m_readTimer.async_wait(std::bind(&Connection::onTimeout, asConnection(), std::placeholders::_1));
|
||||
}
|
||||
|
||||
void Connection::read_some(const RecvCallback& callback)
|
||||
@@ -171,10 +171,10 @@ void Connection::read_some(const RecvCallback& callback)
|
||||
m_recvCallback = callback;
|
||||
|
||||
m_socket.async_read_some(asio::buffer(m_recvBuffer, RECV_BUFFER_SIZE),
|
||||
std::bind(&Connection::onRecv, shared_from_this(), std::placeholders::_1, std::placeholders::_2));
|
||||
std::bind(&Connection::onRecv, asConnection(), std::placeholders::_1, std::placeholders::_2));
|
||||
|
||||
m_readTimer.expires_from_now(boost::posix_time::seconds(READ_TIMEOUT));
|
||||
m_readTimer.async_wait(std::bind(&Connection::onTimeout, shared_from_this(), std::placeholders::_1));
|
||||
m_readTimer.async_wait(std::bind(&Connection::onTimeout, asConnection(), std::placeholders::_1));
|
||||
}
|
||||
|
||||
void Connection::onConnect(const boost::system::error_code& error)
|
||||
|
@@ -28,7 +28,7 @@
|
||||
#include <framework/core/timer.h>
|
||||
#include <framework/core/declarations.h>
|
||||
|
||||
class Connection : public std::enable_shared_from_this<Connection>, boost::noncopyable
|
||||
class Connection : public stdext::shared_object
|
||||
{
|
||||
typedef std::function<void(const boost::system::error_code&)> ErrorCallback;
|
||||
typedef std::function<void(uint8*, uint16)> RecvCallback;
|
||||
@@ -61,6 +61,7 @@ public:
|
||||
bool isConnecting() { return m_connecting; }
|
||||
bool isConnected() { return m_connected; }
|
||||
|
||||
ConnectionPtr asConnection() { return self_cast<Connection>(); }
|
||||
protected:
|
||||
void onConnect(const boost::system::error_code& error);
|
||||
void onWrite(const boost::system::error_code& error, size_t);
|
||||
|
@@ -34,11 +34,10 @@ class Connection;
|
||||
class Protocol;
|
||||
class Server;
|
||||
|
||||
typedef std::shared_ptr<InputMessage> InputMessagePtr;
|
||||
typedef std::shared_ptr<OutputMessage> OutputMessagePtr;
|
||||
typedef std::shared_ptr<Connection> ConnectionPtr;
|
||||
typedef std::weak_ptr<Connection> ConnectionWeakPtr;
|
||||
typedef std::shared_ptr<Protocol> ProtocolPtr;
|
||||
typedef std::shared_ptr<Server> ServerPtr;
|
||||
typedef stdext::shared_object_ptr<InputMessage> InputMessagePtr;
|
||||
typedef stdext::shared_object_ptr<OutputMessage> OutputMessagePtr;
|
||||
typedef stdext::shared_object_ptr<Connection> ConnectionPtr;
|
||||
typedef stdext::shared_object_ptr<Protocol> ProtocolPtr;
|
||||
typedef stdext::shared_object_ptr<Server> ServerPtr;
|
||||
|
||||
#endif
|
||||
|
@@ -51,7 +51,7 @@ public:
|
||||
virtual void send(const OutputMessagePtr& outputMessage);
|
||||
void recv();
|
||||
|
||||
ProtocolPtr asProtocol() { return std::static_pointer_cast<Protocol>(shared_from_this()); }
|
||||
ProtocolPtr asProtocol() { return self_cast<Protocol>(); }
|
||||
|
||||
protected:
|
||||
virtual void onConnect();
|
||||
|
Reference in New Issue
Block a user