mirror of
https://github.com/edubart/otclient.git
synced 2025-10-16 20:43:26 +02:00
init protocol integ
This commit is contained in:
@@ -43,8 +43,11 @@ void Connection::stop()
|
||||
}
|
||||
}
|
||||
|
||||
bool Connection::connect(const std::string& ip, uint16 port, ConnectionCallback onConnect)
|
||||
bool Connection::connect(const std::string& ip, uint16 port, const Callback& callback)
|
||||
{
|
||||
|
||||
logInfo("[Connection::connect]: Ip: %s - Port: %d", ip.c_str(), port);
|
||||
|
||||
if(m_connecting){
|
||||
logError("Already is connecting.");
|
||||
return false;
|
||||
@@ -55,7 +58,7 @@ bool Connection::connect(const std::string& ip, uint16 port, ConnectionCallback
|
||||
return false;
|
||||
}
|
||||
|
||||
m_connectCallback = onConnect;
|
||||
m_connectCallback = callback;
|
||||
m_connecting = true;
|
||||
m_ip = ip;
|
||||
m_port = port;
|
||||
@@ -63,14 +66,15 @@ bool Connection::connect(const std::string& ip, uint16 port, ConnectionCallback
|
||||
//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));
|
||||
|
||||
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__);
|
||||
return;
|
||||
}
|
||||
@@ -82,6 +86,7 @@ void Connection::onResolveDns(const boost::system::error_code& error, boost::asi
|
||||
void Connection::onConnect(const boost::system::error_code& error)
|
||||
{
|
||||
if(error){
|
||||
logInfo("Error");
|
||||
m_connecting = false;
|
||||
m_errorCallback(error, __FUNCTION__);
|
||||
return;
|
||||
|
@@ -47,10 +47,10 @@ public:
|
||||
private:
|
||||
Connection(boost::asio::io_service& ioService);
|
||||
|
||||
bool connect(const std::string& ip, uint16 port, ConnectionCallback onConnect);
|
||||
bool connect(const std::string& ip, uint16 port, const Callback& callback);
|
||||
void stop();
|
||||
|
||||
void setErrorCallback(ErrorCallback c) { m_errorCallback = c; }
|
||||
void setErrorCallback(const ErrorCallback& callback) { m_errorCallback = callback; }
|
||||
|
||||
void recv(RecvCallback onSend);
|
||||
void send(NetworkMessagePtr networkMessage, ConnectionCallback onRecv);
|
||||
@@ -88,7 +88,7 @@ private:
|
||||
std::string m_ip;
|
||||
uint16_t m_port;
|
||||
|
||||
ConnectionCallback m_connectCallback;
|
||||
Callback m_connectCallback;
|
||||
ErrorCallback m_errorCallback;
|
||||
|
||||
friend class Protocol;
|
||||
|
@@ -26,12 +26,14 @@
|
||||
|
||||
Protocol::Protocol()
|
||||
{
|
||||
logInfo("Protocol()");
|
||||
m_connection = g_connections.createConnection();
|
||||
/*m_connection->setErrorCallback(
|
||||
[this](const boost::system::error_code& error, const std::string& msg){
|
||||
this->onError(error, msg);
|
||||
}
|
||||
);*/
|
||||
m_connection->setErrorCallback(boost::bind(&Protocol::onError, this, boost::asio::placeholders::error, _2));
|
||||
}
|
||||
|
||||
Protocol::~Protocol()
|
||||
{
|
||||
logInfo("~Protocol()");
|
||||
}
|
||||
|
||||
void Protocol::send(NetworkMessagePtr networkMessage, Connection::ConnectionCallback onSend)
|
||||
@@ -39,9 +41,9 @@ void Protocol::send(NetworkMessagePtr networkMessage, Connection::ConnectionCall
|
||||
m_connection->send(networkMessage, onSend);
|
||||
}
|
||||
|
||||
bool Protocol::connect(const std::string& ip, uint16 port, Connection::ConnectionCallback onConnect)
|
||||
bool Protocol::connect(const std::string& ip, uint16 port, const Callback& callback)
|
||||
{
|
||||
return m_connection->connect(ip, port, onConnect);
|
||||
return m_connection->connect(ip, port, callback);
|
||||
}
|
||||
|
||||
void Protocol::recv(Connection::RecvCallback onRecv)
|
||||
|
@@ -31,6 +31,7 @@ class Protocol
|
||||
{
|
||||
public:
|
||||
Protocol();
|
||||
~Protocol();
|
||||
|
||||
virtual void begin() = 0;
|
||||
|
||||
@@ -38,7 +39,7 @@ protected:
|
||||
void send(NetworkMessagePtr networkMessage, Connection::ConnectionCallback onSend);
|
||||
void recv(Connection::RecvCallback onRecv);
|
||||
|
||||
bool connect(const std::string& ip, uint16 port, Connection::ConnectionCallback onConnect);
|
||||
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;
|
||||
|
||||
|
Reference in New Issue
Block a user