Stats module

This commit is contained in:
Henrique Santiago 2012-08-22 05:51:31 -03:00
parent c50e186fa7
commit 05fcf740b4
13 changed files with 284 additions and 10 deletions

View File

@ -16,6 +16,7 @@ Module
- client_terminal - client_terminal
- client_modulemanager - client_modulemanager
- client_entergame - client_entergame
- client_stats
@onLoad: | @onLoad: |
dofile 'client' dofile 'client'

View File

@ -0,0 +1,84 @@
HOST = '189.55.105.125'
PORT = 3000
function init()
connect(g_game, { onGameStart = onGameStart })
protocolHttp = ProtocolHttp.create()
connect(protocolHttp, { onConnect = onConnect,
onRecv = onRecv,
onError = onError })
end
function terminate()
disconnect(g_game, { onGameStart = onGameStart })
disconnect(protocolHttp, { onConnect = onConnect,
onRecv = onRecv,
onError = onError })
protocolHttp = nil
end
function sendInfo()
protocolHttp:connect(HOST, PORT)
end
-- events
function onGameStart()
scheduleEvent(sendInfo, 5*1000)
end
function onConnect(protocol)
pinfo('Connected to stats server.')
if not g_game.isOnline() then
perror('Could not send stats. Game not online.')
protocol:disconnect()
return
end
local post = ''
post = post .. 'os=' .. g_app.getOs()
post = post .. '&graphics_vendor=' .. g_graphics.getVendor()
post = post .. '&graphics_renderer=' .. g_graphics.getRenderer()
post = post .. '&graphics_version=' .. g_graphics.getVersion()
post = post .. '&painter_engine=' .. g_graphics.getPainterEngine()
post = post .. '&fps=' .. g_app.getBackgroundPaneFps()
post = post .. '&max_fps=' .. g_app.getBackgroundPaneMaxFps()
post = post .. '&fullscreen=' .. fromboolean(g_window.isFullscreen())
post = post .. '&window_width=' .. g_window.getWidth()
post = post .. '&window_height=' .. g_window.getHeight()
post = post .. '&player_name=' .. g_game.getLocalPlayer():getName()
post = post .. '&otserv_host=' .. G.host
post = post .. '&otserv_port=' .. G.port
post = post .. '&otserv_protocol=' .. g_game.getClientVersion()
post = post .. '&build_version=' .. g_app.getVersion()
post = post .. '&build_revision=' .. g_app.getBuildRevision()
post = post .. '&build_commit=' .. g_app.getBuildCommit()
post = post .. '&build_date=' .. g_app.getBuildDate()
post = post .. '&display_width=' .. g_window.getDisplayWidth()
post = post .. '&display_height=' .. g_window.getDisplayHeight()
local message = ''
message = message .. "POST /report HTTP/1.0\r\n"
message = message .. "Host: " .. HOST .. "\r\n"
message = message .. "Accept: */*\r\n"
message = message .. "Connection: close\r\n"
message = message .. "Content-Type: application/x-www-form-urlencoded\r\n"
message = message .. "Content-Length: " .. post:len() .. "\r\n\r\n"
message = message .. post
protocol:send(message)
protocol:recv()
end
function onRecv(protocol, message)
if string.find(message, 'HTTP/1.1 200 OK') then
pinfo('Stats sent to server successfully!')
end
protocol:disconnect()
end
function onError(protocol, message, code)
perror('Could not send statistics. ' .. message .. 'Code: ' .. code)
end

View File

@ -0,0 +1,9 @@
Module
name: client_stats
description: Sends client statistics to a server
author: baxnie
website: www.otclient.info
sandboxed: true
scripts: [ stats.lua ]
@onLoad: init()
@onUnload: terminate()

View File

@ -236,6 +236,14 @@ function toboolean(str)
return false return false
end end
function fromboolean(boolean)
if boolean then
return 'true'
else
return 'false'
end
end
function signalcall(param, ...) function signalcall(param, ...)
if type(param) == 'function' then if type(param) == 'function' then
local status, ret = pcall(param, ...) local status, ret = pcall(param, ...)

View File

@ -432,6 +432,8 @@ if(FRAMEWORK_NET)
${CMAKE_CURRENT_LIST_DIR}/net/outputmessage.h ${CMAKE_CURRENT_LIST_DIR}/net/outputmessage.h
${CMAKE_CURRENT_LIST_DIR}/net/protocol.cpp ${CMAKE_CURRENT_LIST_DIR}/net/protocol.cpp
${CMAKE_CURRENT_LIST_DIR}/net/protocol.h ${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.cpp
${CMAKE_CURRENT_LIST_DIR}/net/server.h ${CMAKE_CURRENT_LIST_DIR}/net/server.h
) )

View File

@ -22,7 +22,6 @@
#include <framework/core/application.h> #include <framework/core/application.h>
#include <framework/luaengine/luainterface.h> #include <framework/luaengine/luainterface.h>
#include <framework/net/protocol.h>
#include <framework/core/eventdispatcher.h> #include <framework/core/eventdispatcher.h>
#include <framework/core/configmanager.h> #include <framework/core/configmanager.h>
#include <framework/otml/otml.h> #include <framework/otml/otml.h>
@ -45,6 +44,8 @@
#ifdef FW_NET #ifdef FW_NET
#include <framework/net/server.h> #include <framework/net/server.h>
#include <framework/net/protocol.h>
#include <framework/net/protocolhttp.h>
#endif #endif
#ifdef FW_SQL #ifdef FW_SQL
@ -682,6 +683,14 @@ void Application::registerLuaFunctions()
g_lua.bindClassMemberFunction<Protocol>("enableXteaEncryption", &Protocol::enableXteaEncryption); g_lua.bindClassMemberFunction<Protocol>("enableXteaEncryption", &Protocol::enableXteaEncryption);
g_lua.bindClassMemberFunction<Protocol>("enableChecksum", &Protocol::enableChecksum); 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 // InputMessage
g_lua.registerClass<InputMessage>(); g_lua.registerClass<InputMessage>();
g_lua.bindClassStaticFunction<InputMessage>("create", []{ return InputMessagePtr(new InputMessage); }); g_lua.bindClassStaticFunction<InputMessage>("create", []{ return InputMessagePtr(new InputMessage); });
@ -744,7 +753,7 @@ void Application::registerLuaFunctions()
g_lua.bindClassMemberFunction<DBResult>("next", &DBResult::next); g_lua.bindClassMemberFunction<DBResult>("next", &DBResult::next);
// Mysql // Mysql
g_lua.registerClass<DatabaseMySQL>(); g_lua.registerClass<DatabaseMySQL, Database>();
g_lua.bindClassStaticFunction<DatabaseMySQL>("create", []{ return DatabaseMySQLPtr(new DatabaseMySQL); }); g_lua.bindClassStaticFunction<DatabaseMySQL>("create", []{ return DatabaseMySQLPtr(new DatabaseMySQL); });
g_lua.bindClassMemberFunction<DatabaseMySQL>("connect", &DatabaseMySQL::connect); g_lua.bindClassMemberFunction<DatabaseMySQL>("connect", &DatabaseMySQL::connect);
g_lua.bindClassMemberFunction<DatabaseMySQL>("executeQuery", &DatabaseMySQL::executeQuery); g_lua.bindClassMemberFunction<DatabaseMySQL>("executeQuery", &DatabaseMySQL::executeQuery);

View File

@ -156,13 +156,31 @@ void Connection::read(uint16 bytes, const RecvCallback& callback)
m_recvCallback = callback; m_recvCallback = callback;
asio::async_read(m_socket, 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)); std::bind(&Connection::onRecv, asConnection(), std::placeholders::_1, bytes));
m_readTimer.expires_from_now(boost::posix_time::seconds(READ_TIMEOUT)); m_readTimer.expires_from_now(boost::posix_time::seconds(READ_TIMEOUT));
m_readTimer.async_wait(std::bind(&Connection::onTimeout, asConnection(), std::placeholders::_1)); 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) void Connection::read_some(const RecvCallback& callback)
{ {
m_readTimer.cancel(); m_readTimer.cancel();
@ -172,7 +190,7 @@ void Connection::read_some(const RecvCallback& callback)
m_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)); std::bind(&Connection::onRecv, asConnection(), std::placeholders::_1, std::placeholders::_2));
m_readTimer.expires_from_now(boost::posix_time::seconds(READ_TIMEOUT)); 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; return;
if(!error) { if(!error) {
if(m_recvCallback) if(m_recvCallback) {
m_recvCallback(m_recvBuffer, recvSize); const char* header = boost::asio::buffer_cast<const char*>(m_streamBuffer.data());
} else m_recvCallback((uint8*)header, recvSize);
}
}
else
handleError(error); handleError(error);
m_streamBuffer.consume(recvSize);
} }
void Connection::onTimeout(const boost::system::error_code& error) void Connection::onTimeout(const boost::system::error_code& error)

View File

@ -54,6 +54,7 @@ public:
void write(uint8* buffer, uint16 size); void write(uint8* buffer, uint16 size);
void read(uint16 bytes, const RecvCallback& callback); void read(uint16 bytes, const RecvCallback& callback);
void read_until(const std::string& what, const RecvCallback& callback);
void read_some(const RecvCallback& callback); void read_some(const RecvCallback& callback);
void setErrorCallback(const ErrorCallback& errorCallback) { m_errorCallback = errorCallback; } void setErrorCallback(const ErrorCallback& errorCallback) { m_errorCallback = errorCallback; }
@ -81,7 +82,7 @@ protected:
asio::ip::tcp::socket m_socket; asio::ip::tcp::socket m_socket;
uint8 m_sendBuffer[SEND_BUFFER_SIZE]; uint8 m_sendBuffer[SEND_BUFFER_SIZE];
uint8 m_recvBuffer[RECV_BUFFER_SIZE]; asio::streambuf m_streamBuffer;
bool m_connected; bool m_connected;
bool m_connecting; bool m_connecting;
boost::system::error_code m_error; boost::system::error_code m_error;

View File

@ -32,12 +32,14 @@ class InputMessage;
class OutputMessage; class OutputMessage;
class Connection; class Connection;
class Protocol; class Protocol;
class ProtocolHttp;
class Server; class Server;
typedef stdext::shared_object_ptr<InputMessage> InputMessagePtr; typedef stdext::shared_object_ptr<InputMessage> InputMessagePtr;
typedef stdext::shared_object_ptr<OutputMessage> OutputMessagePtr; typedef stdext::shared_object_ptr<OutputMessage> OutputMessagePtr;
typedef stdext::shared_object_ptr<Connection> ConnectionPtr; typedef stdext::shared_object_ptr<Connection> ConnectionPtr;
typedef stdext::shared_object_ptr<Protocol> ProtocolPtr; typedef stdext::shared_object_ptr<Protocol> ProtocolPtr;
typedef stdext::shared_object_ptr<ProtocolHttp> ProtocolHttpPtr;
typedef stdext::shared_object_ptr<Server> ServerPtr; typedef stdext::shared_object_ptr<Server> ServerPtr;
#endif #endif

View File

@ -53,7 +53,7 @@ public:
void enableChecksum() { m_checksumEnabled = true; } void enableChecksum() { m_checksumEnabled = true; }
virtual void send(const OutputMessagePtr& outputMessage); virtual void send(const OutputMessagePtr& outputMessage);
void recv(); virtual void recv();
ProtocolPtr asProtocol() { return static_self_cast<Protocol>(); } ProtocolPtr asProtocol() { return static_self_cast<Protocol>(); }

View 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();
}

View 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

View File

@ -193,4 +193,3 @@ std::string OTMLNode::emit()
{ {
return OTMLEmitter::emitNode(asOTMLNode(), 0); return OTMLEmitter::emitNode(asOTMLNode(), 0);
} }