mirror of
https://github.com/edubart/otclient.git
synced 2025-10-14 11:34:54 +02:00
Stats module
This commit is contained in:
@@ -432,6 +432,8 @@ if(FRAMEWORK_NET)
|
||||
${CMAKE_CURRENT_LIST_DIR}/net/outputmessage.h
|
||||
${CMAKE_CURRENT_LIST_DIR}/net/protocol.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/net/protocol.h
|
||||
${CMAKE_CURRENT_LIST_DIR}/net/protocolhttp.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/net/protocolhttp.h
|
||||
${CMAKE_CURRENT_LIST_DIR}/net/server.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/net/server.h
|
||||
)
|
||||
|
@@ -22,7 +22,6 @@
|
||||
|
||||
#include <framework/core/application.h>
|
||||
#include <framework/luaengine/luainterface.h>
|
||||
#include <framework/net/protocol.h>
|
||||
#include <framework/core/eventdispatcher.h>
|
||||
#include <framework/core/configmanager.h>
|
||||
#include <framework/otml/otml.h>
|
||||
@@ -45,6 +44,8 @@
|
||||
|
||||
#ifdef FW_NET
|
||||
#include <framework/net/server.h>
|
||||
#include <framework/net/protocol.h>
|
||||
#include <framework/net/protocolhttp.h>
|
||||
#endif
|
||||
|
||||
#ifdef FW_SQL
|
||||
@@ -682,6 +683,14 @@ void Application::registerLuaFunctions()
|
||||
g_lua.bindClassMemberFunction<Protocol>("enableXteaEncryption", &Protocol::enableXteaEncryption);
|
||||
g_lua.bindClassMemberFunction<Protocol>("enableChecksum", &Protocol::enableChecksum);
|
||||
|
||||
// ProtocolHttp
|
||||
g_lua.registerClass<ProtocolHttp>();
|
||||
g_lua.bindClassStaticFunction<ProtocolHttp>("create", []{ return ProtocolHttpPtr(new ProtocolHttp); });
|
||||
g_lua.bindClassMemberFunction<ProtocolHttp>("connect", &ProtocolHttp::connect);
|
||||
g_lua.bindClassMemberFunction<ProtocolHttp>("disconnect", &ProtocolHttp::disconnect);
|
||||
g_lua.bindClassMemberFunction<ProtocolHttp>("send", &ProtocolHttp::send);
|
||||
g_lua.bindClassMemberFunction<ProtocolHttp>("recv", &ProtocolHttp::recv);
|
||||
|
||||
// InputMessage
|
||||
g_lua.registerClass<InputMessage>();
|
||||
g_lua.bindClassStaticFunction<InputMessage>("create", []{ return InputMessagePtr(new InputMessage); });
|
||||
@@ -744,7 +753,7 @@ void Application::registerLuaFunctions()
|
||||
g_lua.bindClassMemberFunction<DBResult>("next", &DBResult::next);
|
||||
|
||||
// Mysql
|
||||
g_lua.registerClass<DatabaseMySQL>();
|
||||
g_lua.registerClass<DatabaseMySQL, Database>();
|
||||
g_lua.bindClassStaticFunction<DatabaseMySQL>("create", []{ return DatabaseMySQLPtr(new DatabaseMySQL); });
|
||||
g_lua.bindClassMemberFunction<DatabaseMySQL>("connect", &DatabaseMySQL::connect);
|
||||
g_lua.bindClassMemberFunction<DatabaseMySQL>("executeQuery", &DatabaseMySQL::executeQuery);
|
||||
|
@@ -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
|
@@ -193,4 +193,3 @@ std::string OTMLNode::emit()
|
||||
{
|
||||
return OTMLEmitter::emitNode(asOTMLNode(), 0);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user