diff --git a/src/framework/luafunctions.cpp b/src/framework/luafunctions.cpp index e29c6218..ddaeee95 100644 --- a/src/framework/luafunctions.cpp +++ b/src/framework/luafunctions.cpp @@ -41,6 +41,10 @@ #include #endif +#ifdef FW_NET +#include +#endif + void Application::registerLuaFunctions() { // conversion globals @@ -622,6 +626,11 @@ void Application::registerLuaFunctions() #endif #ifdef FW_NET + // Server + g_lua.registerClass(); + g_lua.bindClassStaticFunction("create", &Server::create); + g_lua.bindClassMemberFunction("acceptNext", &Server::acceptNext); + // Protocol g_lua.registerClass(); g_lua.bindClassStaticFunction("create", []{ return ProtocolPtr(new Protocol); }); @@ -629,6 +638,8 @@ void Application::registerLuaFunctions() g_lua.bindClassMemberFunction("disconnect", &Protocol::disconnect); g_lua.bindClassMemberFunction("isConnected", &Protocol::isConnected); g_lua.bindClassMemberFunction("isConnecting", &Protocol::isConnecting); + g_lua.bindClassMemberFunction("getConnection", &Protocol::getConnection); + g_lua.bindClassMemberFunction("setConnection", &Protocol::setConnection); g_lua.bindClassMemberFunction("send", &Protocol::send); g_lua.bindClassMemberFunction("recv", &Protocol::recv); g_lua.bindClassMemberFunction("getXteaKey", &Protocol::getXteaKey); diff --git a/src/framework/net/connection.h b/src/framework/net/connection.h index b91af883..8ebfbabf 100644 --- a/src/framework/net/connection.h +++ b/src/framework/net/connection.h @@ -25,10 +25,11 @@ #include "declarations.h" #include +#include #include #include -class Connection : public stdext::shared_object +class Connection : public LuaObject { typedef std::function ErrorCallback; typedef std::function RecvCallback; diff --git a/src/framework/net/protocol.h b/src/framework/net/protocol.h index 1622acd6..7bc75c04 100644 --- a/src/framework/net/protocol.h +++ b/src/framework/net/protocol.h @@ -26,6 +26,7 @@ #include "declarations.h" #include "inputmessage.h" #include "outputmessage.h" +#include "connection.h" #include @@ -41,6 +42,8 @@ public: bool isConnected(); bool isConnecting(); + ConnectionPtr getConnection() { return m_connection; } + void setConnection(const ConnectionPtr& connection) { m_connection = connection; } void generateXteaKey(); std::vector getXteaKey(); diff --git a/src/framework/net/server.cpp b/src/framework/net/server.cpp index 243f1957..e7bd8879 100644 --- a/src/framework/net/server.cpp +++ b/src/framework/net/server.cpp @@ -25,12 +25,17 @@ 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)) { } -void Server::acceptNext(const AcceptCallback& acceptCallback) +ServerPtr Server::create(int port) +{ + return ServerPtr(new Server(port)); +} + +void Server::acceptNext() { ConnectionPtr connection = ConnectionPtr(new Connection); connection->m_connecting = true; @@ -39,6 +44,6 @@ void Server::acceptNext(const AcceptCallback& acceptCallback) connection->m_connected = true; connection->m_connecting = false; } - acceptCallback(connection, error); + callLuaField("onAccept", connection, error.message(), error.value()); }); } diff --git a/src/framework/net/server.h b/src/framework/net/server.h index 6f5c29df..48ca347e 100644 --- a/src/framework/net/server.h +++ b/src/framework/net/server.h @@ -24,15 +24,17 @@ #define SERVER_H #include "declarations.h" +#include -class Server +class Server : public LuaObject { typedef std::function AcceptCallback; public: - Server(uint16 port); + Server(int port); + static ServerPtr create(int port); - void acceptNext(const AcceptCallback& acceptCallback); + void acceptNext(); private: asio::ip::tcp::acceptor m_acceptor;