Some changes to server compatibility

This commit is contained in:
Henrique 2012-07-30 16:40:03 -03:00
parent 4c369bc823
commit c969f5209f
5 changed files with 29 additions and 7 deletions

View File

@ -41,6 +41,10 @@
#include <framework/ui/ui.h> #include <framework/ui/ui.h>
#endif #endif
#ifdef FW_NET
#include <framework/net/server.h>
#endif
void Application::registerLuaFunctions() void Application::registerLuaFunctions()
{ {
// conversion globals // conversion globals
@ -622,6 +626,11 @@ void Application::registerLuaFunctions()
#endif #endif
#ifdef FW_NET #ifdef FW_NET
// Server
g_lua.registerClass<Server>();
g_lua.bindClassStaticFunction<Server>("create", &Server::create);
g_lua.bindClassMemberFunction<Server>("acceptNext", &Server::acceptNext);
// Protocol // Protocol
g_lua.registerClass<Protocol>(); g_lua.registerClass<Protocol>();
g_lua.bindClassStaticFunction<Protocol>("create", []{ return ProtocolPtr(new Protocol); }); g_lua.bindClassStaticFunction<Protocol>("create", []{ return ProtocolPtr(new Protocol); });
@ -629,6 +638,8 @@ void Application::registerLuaFunctions()
g_lua.bindClassMemberFunction<Protocol>("disconnect", &Protocol::disconnect); g_lua.bindClassMemberFunction<Protocol>("disconnect", &Protocol::disconnect);
g_lua.bindClassMemberFunction<Protocol>("isConnected", &Protocol::isConnected); g_lua.bindClassMemberFunction<Protocol>("isConnected", &Protocol::isConnected);
g_lua.bindClassMemberFunction<Protocol>("isConnecting", &Protocol::isConnecting); g_lua.bindClassMemberFunction<Protocol>("isConnecting", &Protocol::isConnecting);
g_lua.bindClassMemberFunction<Protocol>("getConnection", &Protocol::getConnection);
g_lua.bindClassMemberFunction<Protocol>("setConnection", &Protocol::setConnection);
g_lua.bindClassMemberFunction<Protocol>("send", &Protocol::send); g_lua.bindClassMemberFunction<Protocol>("send", &Protocol::send);
g_lua.bindClassMemberFunction<Protocol>("recv", &Protocol::recv); g_lua.bindClassMemberFunction<Protocol>("recv", &Protocol::recv);
g_lua.bindClassMemberFunction<Protocol>("getXteaKey", &Protocol::getXteaKey); g_lua.bindClassMemberFunction<Protocol>("getXteaKey", &Protocol::getXteaKey);

View File

@ -25,10 +25,11 @@
#include "declarations.h" #include "declarations.h"
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include <framework/luaengine/luaobject.h>
#include <framework/core/timer.h> #include <framework/core/timer.h>
#include <framework/core/declarations.h> #include <framework/core/declarations.h>
class Connection : public stdext::shared_object class Connection : public LuaObject
{ {
typedef std::function<void(const boost::system::error_code&)> ErrorCallback; typedef std::function<void(const boost::system::error_code&)> ErrorCallback;
typedef std::function<void(uint8*, uint16)> RecvCallback; typedef std::function<void(uint8*, uint16)> RecvCallback;

View File

@ -26,6 +26,7 @@
#include "declarations.h" #include "declarations.h"
#include "inputmessage.h" #include "inputmessage.h"
#include "outputmessage.h" #include "outputmessage.h"
#include "connection.h"
#include <framework/luaengine/luaobject.h> #include <framework/luaengine/luaobject.h>
@ -41,6 +42,8 @@ public:
bool isConnected(); bool isConnected();
bool isConnecting(); bool isConnecting();
ConnectionPtr getConnection() { return m_connection; }
void setConnection(const ConnectionPtr& connection) { m_connection = connection; }
void generateXteaKey(); void generateXteaKey();
std::vector<int> getXteaKey(); std::vector<int> getXteaKey();

View File

@ -25,12 +25,17 @@
extern asio::io_service g_ioService; extern asio::io_service g_ioService;
Server::Server(uint16 port) Server::Server(int port)
: m_acceptor(g_ioService, asio::ip::tcp::endpoint(asio::ip::tcp::v4(), port)) : m_acceptor(g_ioService, asio::ip::tcp::endpoint(asio::ip::tcp::v4(), port))
{ {
} }
void Server::acceptNext(const AcceptCallback& acceptCallback) ServerPtr Server::create(int port)
{
return ServerPtr(new Server(port));
}
void Server::acceptNext()
{ {
ConnectionPtr connection = ConnectionPtr(new Connection); ConnectionPtr connection = ConnectionPtr(new Connection);
connection->m_connecting = true; connection->m_connecting = true;
@ -39,6 +44,6 @@ void Server::acceptNext(const AcceptCallback& acceptCallback)
connection->m_connected = true; connection->m_connected = true;
connection->m_connecting = false; connection->m_connecting = false;
} }
acceptCallback(connection, error); callLuaField("onAccept", connection, error.message(), error.value());
}); });
} }

View File

@ -24,15 +24,17 @@
#define SERVER_H #define SERVER_H
#include "declarations.h" #include "declarations.h"
#include <framework/luaengine/luaobject.h>
class Server class Server : public LuaObject
{ {
typedef std::function<void(ConnectionPtr, const boost::system::error_code&)> AcceptCallback; typedef std::function<void(ConnectionPtr, const boost::system::error_code&)> AcceptCallback;
public: public:
Server(uint16 port); Server(int port);
static ServerPtr create(int port);
void acceptNext(const AcceptCallback& acceptCallback); void acceptNext();
private: private:
asio::ip::tcp::acceptor m_acceptor; asio::ip::tcp::acceptor m_acceptor;