mirror of
https://github.com/edubart/otclient.git
synced 2025-10-16 04:24:54 +02:00
Stats module
This commit is contained in:
@@ -156,13 +156,31 @@ void Connection::read(uint16 bytes, const RecvCallback& callback)
|
||||
m_recvCallback = callback;
|
||||
|
||||
asio::async_read(m_socket,
|
||||
asio::buffer(m_recvBuffer, bytes),
|
||||
asio::buffer(m_streamBuffer.prepare(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, asConnection(), std::placeholders::_1));
|
||||
}
|
||||
|
||||
void Connection::read_until(const std::string& what, const RecvCallback& callback)
|
||||
{
|
||||
m_readTimer.cancel();
|
||||
|
||||
if(!m_connected)
|
||||
return;
|
||||
|
||||
m_recvCallback = callback;
|
||||
|
||||
asio::async_read_until(m_socket,
|
||||
m_streamBuffer,
|
||||
what.c_str(),
|
||||
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, asConnection(), std::placeholders::_1));
|
||||
}
|
||||
|
||||
void Connection::read_some(const RecvCallback& callback)
|
||||
{
|
||||
m_readTimer.cancel();
|
||||
@@ -172,7 +190,7 @@ void Connection::read_some(const RecvCallback& callback)
|
||||
|
||||
m_recvCallback = callback;
|
||||
|
||||
m_socket.async_read_some(asio::buffer(m_recvBuffer, RECV_BUFFER_SIZE),
|
||||
m_socket.async_read_some(asio::buffer(m_streamBuffer.prepare(RECV_BUFFER_SIZE)),
|
||||
std::bind(&Connection::onRecv, asConnection(), std::placeholders::_1, std::placeholders::_2));
|
||||
|
||||
m_readTimer.expires_from_now(boost::posix_time::seconds(READ_TIMEOUT));
|
||||
@@ -217,10 +235,15 @@ void Connection::onRecv(const boost::system::error_code& error, size_t recvSize)
|
||||
return;
|
||||
|
||||
if(!error) {
|
||||
if(m_recvCallback)
|
||||
m_recvCallback(m_recvBuffer, recvSize);
|
||||
} else
|
||||
if(m_recvCallback) {
|
||||
const char* header = boost::asio::buffer_cast<const char*>(m_streamBuffer.data());
|
||||
m_recvCallback((uint8*)header, recvSize);
|
||||
}
|
||||
}
|
||||
else
|
||||
handleError(error);
|
||||
|
||||
m_streamBuffer.consume(recvSize);
|
||||
}
|
||||
|
||||
void Connection::onTimeout(const boost::system::error_code& error)
|
||||
|
@@ -54,6 +54,7 @@ public:
|
||||
|
||||
void write(uint8* buffer, uint16 size);
|
||||
void read(uint16 bytes, const RecvCallback& callback);
|
||||
void read_until(const std::string& what, const RecvCallback& callback);
|
||||
void read_some(const RecvCallback& callback);
|
||||
|
||||
void setErrorCallback(const ErrorCallback& errorCallback) { m_errorCallback = errorCallback; }
|
||||
@@ -81,7 +82,7 @@ protected:
|
||||
asio::ip::tcp::socket m_socket;
|
||||
|
||||
uint8 m_sendBuffer[SEND_BUFFER_SIZE];
|
||||
uint8 m_recvBuffer[RECV_BUFFER_SIZE];
|
||||
asio::streambuf m_streamBuffer;
|
||||
bool m_connected;
|
||||
bool m_connecting;
|
||||
boost::system::error_code m_error;
|
||||
|
@@ -32,12 +32,14 @@ class InputMessage;
|
||||
class OutputMessage;
|
||||
class Connection;
|
||||
class Protocol;
|
||||
class ProtocolHttp;
|
||||
class Server;
|
||||
|
||||
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<ProtocolHttp> ProtocolHttpPtr;
|
||||
typedef stdext::shared_object_ptr<Server> ServerPtr;
|
||||
|
||||
#endif
|
||||
|
@@ -53,7 +53,7 @@ public:
|
||||
void enableChecksum() { m_checksumEnabled = true; }
|
||||
|
||||
virtual void send(const OutputMessagePtr& outputMessage);
|
||||
void recv();
|
||||
virtual void recv();
|
||||
|
||||
ProtocolPtr asProtocol() { return static_self_cast<Protocol>(); }
|
||||
|
||||
|
81
src/framework/net/protocolhttp.cpp
Normal file
81
src/framework/net/protocolhttp.cpp
Normal file
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include "protocolhttp.h"
|
||||
#include "connection.h"
|
||||
#include <framework/core/application.h>
|
||||
|
||||
ProtocolHttp::ProtocolHttp()
|
||||
{
|
||||
}
|
||||
|
||||
ProtocolHttp::~ProtocolHttp()
|
||||
{
|
||||
#ifndef NDEBUG
|
||||
assert(!g_app.isTerminated());
|
||||
#endif
|
||||
disconnect();
|
||||
}
|
||||
|
||||
void ProtocolHttp::connect(const std::string& host, uint16 port)
|
||||
{
|
||||
m_connection = ConnectionPtr(new Connection);
|
||||
m_connection->setErrorCallback(std::bind(&ProtocolHttp::onError, asProtocolHttp(), std::placeholders::_1));
|
||||
m_connection->connect(host, port, std::bind(&ProtocolHttp::onConnect, asProtocolHttp()));
|
||||
}
|
||||
|
||||
void ProtocolHttp::disconnect()
|
||||
{
|
||||
if(m_connection) {
|
||||
m_connection->close();
|
||||
m_connection.reset();
|
||||
}
|
||||
}
|
||||
|
||||
void ProtocolHttp::send(const std::string& message)
|
||||
{
|
||||
if(m_connection)
|
||||
m_connection->write((uint8*)message.c_str(), message.length());
|
||||
}
|
||||
|
||||
void ProtocolHttp::recv()
|
||||
{
|
||||
if(m_connection)
|
||||
m_connection->read_until("\r\n\r\n", std::bind(&ProtocolHttp::onRecv, asProtocolHttp(), std::placeholders::_1, std::placeholders::_2));
|
||||
}
|
||||
|
||||
void ProtocolHttp::onConnect()
|
||||
{
|
||||
callLuaField("onConnect");
|
||||
}
|
||||
|
||||
void ProtocolHttp::onRecv(uint8* buffer, uint16 size)
|
||||
{
|
||||
std::string string = std::string((char*)buffer, (size_t)size);
|
||||
callLuaField("onRecv", string);
|
||||
}
|
||||
|
||||
void ProtocolHttp::onError(const boost::system::error_code& err)
|
||||
{
|
||||
callLuaField("onError", err.message(), err.value());
|
||||
disconnect();
|
||||
}
|
55
src/framework/net/protocolhttp.h
Normal file
55
src/framework/net/protocolhttp.h
Normal file
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef PROTOCOLHTTP_H
|
||||
#define PROTOCOLHTTP_H
|
||||
|
||||
#include "declarations.h"
|
||||
#include "connection.h"
|
||||
|
||||
#include <framework/luaengine/luaobject.h>
|
||||
|
||||
// @bindclass
|
||||
class ProtocolHttp : public LuaObject
|
||||
{
|
||||
public:
|
||||
ProtocolHttp();
|
||||
virtual ~ProtocolHttp();
|
||||
|
||||
void connect(const std::string& host, uint16 port);
|
||||
void disconnect();
|
||||
|
||||
void send(const std::string &message);
|
||||
void recv();
|
||||
|
||||
ProtocolHttpPtr asProtocolHttp() { return static_self_cast<ProtocolHttp>(); }
|
||||
|
||||
protected:
|
||||
void onConnect();
|
||||
void onRecv(uint8* buffer, uint16 size);
|
||||
void onError(const boost::system::error_code& err);
|
||||
|
||||
private:
|
||||
ConnectionPtr m_connection;
|
||||
};
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user