From 5e4e584524022d043ac7440f6f18fcdca9bb3b71 Mon Sep 17 00:00:00 2001 From: turanszkij Date: Thu, 26 Nov 2015 23:04:35 +0100 Subject: [PATCH] created network lua binding --- WickedEngine/WickedEngine.vcxproj | 2 + WickedEngine/WickedEngine.vcxproj.filters | 6 ++ WickedEngine/wiClient.cpp | 28 +++--- WickedEngine/wiClient.h | 10 +- WickedEngine/wiHelper.cpp | 15 ++- WickedEngine/wiHelper.h | 2 + WickedEngine/wiLua.cpp | 3 + WickedEngine/wiNetwork.cpp | 8 +- WickedEngine/wiNetwork.h | 6 +- WickedEngine/wiNetwork_BindLua.cpp | 117 ++++++++++++++++++++++ WickedEngine/wiNetwork_BindLua.h | 42 ++++++++ WickedEngine/wiRenderer_BindLua.cpp | 1 - WickedEngine/wiServer.cpp | 30 +++--- WickedEngine/wiServer.h | 12 +-- 14 files changed, 230 insertions(+), 52 deletions(-) create mode 100644 WickedEngine/wiNetwork_BindLua.cpp create mode 100644 WickedEngine/wiNetwork_BindLua.h diff --git a/WickedEngine/WickedEngine.vcxproj b/WickedEngine/WickedEngine.vcxproj index b54987d16..a312d1ea0 100644 --- a/WickedEngine/WickedEngine.vcxproj +++ b/WickedEngine/WickedEngine.vcxproj @@ -468,6 +468,7 @@ + @@ -515,6 +516,7 @@ + diff --git a/WickedEngine/WickedEngine.vcxproj.filters b/WickedEngine/WickedEngine.vcxproj.filters index ca521063a..2cfd5c89b 100644 --- a/WickedEngine/WickedEngine.vcxproj.filters +++ b/WickedEngine/WickedEngine.vcxproj.filters @@ -794,6 +794,9 @@ Lua Bindings + + Lua Bindings + @@ -1765,6 +1768,9 @@ Lua Bindings + + Lua Bindings + diff --git a/WickedEngine/wiClient.cpp b/WickedEngine/wiClient.cpp index caa4b4c63..c3f5c406b 100644 --- a/WickedEngine/wiClient.cpp +++ b/WickedEngine/wiClient.cpp @@ -3,7 +3,7 @@ #ifndef WINSTORE_SUPPORT -Client::Client(const string& newName, const string& ipaddress, int port) +wiClient::wiClient(const string& newName, const string& ipaddress, int port) { if(ConnectToHost(port,ipaddress.length()<=1?"127.0.0.1":ipaddress.c_str())){ success=true; @@ -25,26 +25,26 @@ Client::Client(const string& newName, const string& ipaddress, int port) } -Client::~Client(void) +wiClient::~wiClient(void) { - Network::~Network(); + wiNetwork::~wiNetwork(); } -bool Client::sendText(const string& text){ - return Network::sendText(text,s); +bool wiClient::sendText(const string& text){ + return wiNetwork::sendText(text,s); } -bool Client::receiveText(string& text){ - return Network::receiveText(text,s); +bool wiClient::receiveText(string& text){ + return wiNetwork::receiveText(text,s); } -bool Client::changeName(const string& newName){ - Network::changeName(newName); - Network::sendData(Network::PACKET_TYPE_CHANGENAME,s); - return Network::sendText(newName,s); +bool wiClient::changeName(const string& newName){ + wiNetwork::changeName(newName); + wiNetwork::sendData(wiNetwork::PACKET_TYPE_CHANGENAME,s); + return wiNetwork::sendText(newName,s); } -bool Client::sendMessage(const string& text){ - Network::sendData(Network::PACKET_TYPE_TEXTMESSAGE,s); - return Network::sendText(text,s); +bool wiClient::sendMessage(const string& text){ + wiNetwork::sendData(wiNetwork::PACKET_TYPE_TEXTMESSAGE,s); + return wiNetwork::sendText(text,s); } #endif diff --git a/WickedEngine/wiClient.h b/WickedEngine/wiClient.h index cb82a7e82..1223cd7c0 100644 --- a/WickedEngine/wiClient.h +++ b/WickedEngine/wiClient.h @@ -5,11 +5,11 @@ #ifndef WINSTORE_SUPPORT -class Client : public Network +class wiClient : public wiNetwork { public: - Client(const string& newName = "CLIENT", const string& ipaddress = "127.0.0.1", int port = PORT); - ~Client(void); + wiClient(const string& newName = "CLIENT", const string& ipaddress = "127.0.0.1", int port = PORT); + ~wiClient(void); string serverName; @@ -24,7 +24,7 @@ public: } template bool receiveData(T& value){ - return Network::receiveData(value,s); + return wiNetwork::receiveData(value,s); } bool changeName(const string& newName); @@ -128,7 +128,7 @@ public: } case PACKET_TYPE_OTHER: { - Network::receiveData(data,s); + wiNetwork::receiveData(data,s); break; } default: diff --git a/WickedEngine/wiHelper.cpp b/WickedEngine/wiHelper.cpp index 2b5b16c7d..1305a52c1 100644 --- a/WickedEngine/wiHelper.cpp +++ b/WickedEngine/wiHelper.cpp @@ -47,12 +47,9 @@ namespace wiHelper { #ifndef WINSTORE_SUPPORT CreateDirectoryA("screenshots", 0); - time_t t = std::time(nullptr); - struct tm time_info; - localtime_s(&time_info, &t); stringstream ss(""); if (name.length() <= 0) - ss << "screenshots/sc_" << std::put_time(&time_info, "%d-%m-%Y %H-%M-%S") << ".png"; + ss << "screenshots/sc_" << getCurrentDateTimeAsString() << ".png"; else ss << name; wstringstream wss(L""); @@ -70,4 +67,14 @@ namespace wiHelper res->Release(); #endif } + + string getCurrentDateTimeAsString() + { + time_t t = std::time(nullptr); + struct tm time_info; + localtime_s(&time_info, &t); + stringstream ss(""); + ss << std::put_time(&time_info, "%d-%m-%Y %H-%M-%S"); + return ss.str(); + } } diff --git a/WickedEngine/wiHelper.h b/WickedEngine/wiHelper.h index 571c545f5..f9aa24a11 100644 --- a/WickedEngine/wiHelper.h +++ b/WickedEngine/wiHelper.h @@ -13,6 +13,8 @@ namespace wiHelper void messageBox(const string& msg, const string& caption = "Warning!", HWND hWnd = nullptr); void screenshot(const string& name = ""); + + string getCurrentDateTimeAsString(); }; #endif diff --git a/WickedEngine/wiLua.cpp b/WickedEngine/wiLua.cpp index 5366c7dbb..fac798609 100644 --- a/WickedEngine/wiLua.cpp +++ b/WickedEngine/wiLua.cpp @@ -21,6 +21,7 @@ #include "wiInputManager_BindLua.h" #include "wiFont_BindLua.h" #include "wiBackLog_BindLua.h" +#include "wiNetwork_BindLua.h" wiLua *wiLua::globalLua = nullptr; @@ -68,6 +69,8 @@ wiLua* wiLua::GetGlobal() wiInputManager_BindLua::Bind(); wiFont_BindLua::Bind(); wiBackLog_BindLua::Bind(); + wiClient_BindLua::Bind(); + wiServer_BindLua::Bind(); } return globalLua; diff --git a/WickedEngine/wiNetwork.cpp b/WickedEngine/wiNetwork.cpp index ba58c7103..ce1540889 100644 --- a/WickedEngine/wiNetwork.cpp +++ b/WickedEngine/wiNetwork.cpp @@ -3,19 +3,19 @@ #ifndef WINSTORE_SUPPORT -Network::Network(void) +wiNetwork::wiNetwork(void) { name="UNNAMED_NETWORK"; } -Network::~Network(void) +wiNetwork::~wiNetwork(void) { CloseConnection(); } -bool Network::sendText(const std::string& text, SOCKET socket){ +bool wiNetwork::sendText(const std::string& text, SOCKET socket){ if(sendData((int)text.length(),socket)){ int sent = send(socket, text.c_str(), text.length(), 0); @@ -31,7 +31,7 @@ bool Network::sendText(const std::string& text, SOCKET socket){ return false; } -bool Network::receiveText(std::string& text, SOCKET socket){ +bool wiNetwork::receiveText(std::string& text, SOCKET socket){ int textlen; if(receiveData(textlen,socket)){ char* puffer=new char[textlen]; diff --git a/WickedEngine/wiNetwork.h b/WickedEngine/wiNetwork.h index 341716ddd..c5785601b 100644 --- a/WickedEngine/wiNetwork.h +++ b/WickedEngine/wiNetwork.h @@ -11,7 +11,7 @@ #ifndef WINSTORE_SUPPORT -class Network +class wiNetwork { protected: @@ -32,8 +32,8 @@ public: static const int PACKET_TYPE_OTHER = 2; bool success; - Network(void); - ~Network(void); + wiNetwork(void); + ~wiNetwork(void); virtual bool changeName(const string& newName){ name=newName; diff --git a/WickedEngine/wiNetwork_BindLua.cpp b/WickedEngine/wiNetwork_BindLua.cpp new file mode 100644 index 000000000..e8876f11e --- /dev/null +++ b/WickedEngine/wiNetwork_BindLua.cpp @@ -0,0 +1,117 @@ +#include "wiNetwork_BindLua.h" +#include "wiClient.h" +#include "wiServer.h" +#include "wiHelper.h" + +const char wiClient_BindLua::className[] = "Client"; + +Luna::FunctionType wiClient_BindLua::methods[] = { + lunamethod(wiClient_BindLua,Poll), + { NULL, NULL } +}; +Luna::PropertyType wiClient_BindLua::properties[] = { + { NULL, NULL } +}; + + +wiClient_BindLua::wiClient_BindLua(lua_State* L) +{ + string name = "CLIENT-", ipaddress = "127.0.0.1"; + name += wiHelper::getCurrentDateTimeAsString(); + int port = 65000; + + int argc = wiLua::SGetArgCount(L); + if (argc > 0) + { + name = wiLua::SGetString(L, 1); + if (argc > 1) + { + ipaddress = wiLua::SGetString(L, 2); + if (argc > 2) + { + port = wiLua::SGetInt(L, 3); + } + } + } + + client = new wiClient(name, ipaddress, port); +} + + +wiClient_BindLua::~wiClient_BindLua() +{ + SAFE_DELETE(client); +} + +int wiClient_BindLua::Poll(lua_State* L) +{ + int i = 0; + client->Poll(i); + return 0; +} + +void wiClient_BindLua::Bind() +{ + static bool initialized = false; + if (!initialized) + { + initialized = true; + Luna::Register(wiLua::GetGlobal()->GetLuaState()); + } +} + + +const char wiServer_BindLua::className[] = "Server"; + +Luna::FunctionType wiServer_BindLua::methods[] = { + lunamethod(wiServer_BindLua,Poll), + { NULL, NULL } +}; +Luna::PropertyType wiServer_BindLua::properties[] = { + { NULL, NULL } +}; + +wiServer_BindLua::wiServer_BindLua(lua_State* L) +{ + string name = "SERVER-", ipaddress = "0.0.0.0"; + name+=wiHelper::getCurrentDateTimeAsString(); + int port = 65000; + + int argc = wiLua::SGetArgCount(L); + if (argc > 0) + { + name = wiLua::SGetString(L, 1); + if (argc > 1) + { + ipaddress = wiLua::SGetString(L, 2); + if (argc > 2) + { + port = wiLua::SGetInt(L, 3); + } + } + } + + server = new wiServer(name, ipaddress, port); +} + +wiServer_BindLua::~wiServer_BindLua() +{ + SAFE_DELETE(server); +} + +int wiServer_BindLua::Poll(lua_State* L) +{ + int i = 0; + server->Poll(i); + return 0; +} + +void wiServer_BindLua::Bind() +{ + static bool initialized = false; + if (!initialized) + { + initialized = true; + Luna::Register(wiLua::GetGlobal()->GetLuaState()); + } +} \ No newline at end of file diff --git a/WickedEngine/wiNetwork_BindLua.h b/WickedEngine/wiNetwork_BindLua.h new file mode 100644 index 000000000..4e1438fda --- /dev/null +++ b/WickedEngine/wiNetwork_BindLua.h @@ -0,0 +1,42 @@ +#pragma once +#include "wiLua.h" +#include "wiLuna.h" + +class wiClient; +class wiServer; + +class wiClient_BindLua +{ +public: + wiClient* client; + + static const char className[]; + static Luna::FunctionType methods[]; + static Luna::PropertyType properties[]; + + wiClient_BindLua(lua_State* L); + ~wiClient_BindLua(); + + int Poll(lua_State* L); + + static void Bind(); +}; + +class wiServer_BindLua +{ +public: + wiServer* server; + + static const char className[]; + static Luna::FunctionType methods[]; + static Luna::PropertyType properties[]; + + wiServer_BindLua(lua_State* L); + ~wiServer_BindLua(); + + + int Poll(lua_State* L); + + static void Bind(); +}; + diff --git a/WickedEngine/wiRenderer_BindLua.cpp b/WickedEngine/wiRenderer_BindLua.cpp index 9b9726938..536f2cede 100644 --- a/WickedEngine/wiRenderer_BindLua.cpp +++ b/WickedEngine/wiRenderer_BindLua.cpp @@ -475,7 +475,6 @@ namespace wiRenderer_BindLua wiLua::GetGlobal()->RegisterFunc("GetLights", GetLights); wiLua::GetGlobal()->RegisterFunc("GetMaterials", GetMaterials); wiLua::GetGlobal()->RegisterFunc("GetGameSpeed", GetGameSpeed); - wiLua::GetGlobal()->RegisterFunc("GetMaterials", GetMaterials); wiLua::GetGlobal()->RegisterFunc("GetScreenWidth", GetScreenWidth); wiLua::GetGlobal()->RegisterFunc("GetScreenHeight", GetScreenHeight); wiLua::GetGlobal()->RegisterFunc("GetRenderWidth", GetRenderWidth); diff --git a/WickedEngine/wiServer.cpp b/WickedEngine/wiServer.cpp index 8946dcb81..8ae6df67a 100644 --- a/WickedEngine/wiServer.cpp +++ b/WickedEngine/wiServer.cpp @@ -5,7 +5,7 @@ using namespace std; -Server::Server(const string& newName, int port, const string& ipaddress) +wiServer::wiServer(const string& newName, const string& ipaddress, int port) { name=newName; if(ListenOnPort(port,ipaddress.length()<=1?"0.0.0.0":ipaddress.c_str())){ @@ -23,15 +23,15 @@ Server::Server(const string& newName, int port, const string& ipaddress) } -Server::~Server(void) +wiServer::~wiServer(void) { for (map::iterator it = clients.begin(); it != clients.end(); ++it) { closesocket(it->first); } - Network::~Network(); + wiNetwork::~wiNetwork(); } -bool Server::ListenOnPort(int portno, const char* ipaddress) +bool wiServer::ListenOnPort(int portno, const char* ipaddress) { int error = WSAStartup(SCK_VERSION2,&w); @@ -75,7 +75,7 @@ bool Server::ListenOnPort(int portno, const char* ipaddress) return true; } -SOCKET Server::CreateAccepter(){ +SOCKET wiServer::CreateAccepter(){ struct sockaddr_in caller; int addrlen = sizeof(caller); SOCKET newsock = accept(s,(struct sockaddr *) &caller, &addrlen); @@ -93,7 +93,7 @@ SOCKET Server::CreateAccepter(){ -vector Server::listClients() +vector wiServer::listClients() { vector ret(0); for (map::iterator it = clients.begin(); it != clients.end(); ++it) { @@ -104,24 +104,24 @@ vector Server::listClients() return ret; } -bool Server::sendText(const string& text, int packettype, const string& clientName, int clientID){ +bool wiServer::sendText(const string& text, int packettype, const string& clientName, int clientID){ int sentTo=0; if(clientName.length()<=0){ //send to everyone for (map::iterator it = clients.begin(); it != clients.end(); ++it) { - sentTo += Network::sendData(packettype,it->first) && Network::sendText(text,it->first); + sentTo += wiNetwork::sendData(packettype,it->first) && wiNetwork::sendText(text,it->first); } } else if(clientID<0){ //send to all of same name for (map::iterator it = clients.begin(); it != clients.end(); ++it) { if(!clientName.compare(it->second)){ - sentTo += Network::sendData(packettype,it->first) && Network::sendText(text,it->first); + sentTo += wiNetwork::sendData(packettype,it->first) && wiNetwork::sendText(text,it->first); } } } else{ //send to specific client if(clients.find(clientID) != clients.end()){ - sentTo += Network::sendData(packettype,clientID) && Network::sendText(text,clientID); + sentTo += wiNetwork::sendData(packettype,clientID) && wiNetwork::sendText(text,clientID); } } @@ -131,12 +131,12 @@ bool Server::sendText(const string& text, int packettype, const string& clientNa return sentTo>0; } -bool Server::changeName(const string& newName){ - Network::changeName(newName); - return sendText(newName, Network::PACKET_TYPE_CHANGENAME); +bool wiServer::changeName(const string& newName){ + wiNetwork::changeName(newName); + return sendText(newName, wiNetwork::PACKET_TYPE_CHANGENAME); } -bool Server::sendMessage(const string& text, const string& clientName, int clientID){ - return sendText(text, Network::PACKET_TYPE_TEXTMESSAGE, clientName, clientID); +bool wiServer::sendMessage(const string& text, const string& clientName, int clientID){ + return sendText(text, wiNetwork::PACKET_TYPE_TEXTMESSAGE, clientName, clientID); } #endif diff --git a/WickedEngine/wiServer.h b/WickedEngine/wiServer.h index 593c624b3..3e830a0e2 100644 --- a/WickedEngine/wiServer.h +++ b/WickedEngine/wiServer.h @@ -9,13 +9,13 @@ #include -class Server : public Network +class wiServer : public wiNetwork { private: map clients; public: - Server(const string& newName = "SERVER", int port = PORT, const string& ipaddress = "0.0.0.0"); - ~Server(void); + wiServer(const string& newName = "SERVER", const string& ipaddress = "0.0.0.0", int port = PORT); + ~wiServer(void); bool active(){return !clients.empty();} @@ -78,7 +78,7 @@ public: SOCKET new_socket = CreateAccepter(); if(new_socket != SOCKET_ERROR){ - if(Network::sendText(name,new_socket)){ + if(wiNetwork::sendText(name,new_socket)){ stringstream ss(""); ss<<"Name sent, client ["<first , &readfds)) { int command; - bool receiveSuccess = Network::receiveData(command,it->first); + bool receiveSuccess = wiNetwork::receiveData(command,it->first); if(!receiveSuccess){ wiBackLog::post("Client disconnected."); @@ -134,7 +134,7 @@ public: } case PACKET_TYPE_OTHER: { - Network::receiveData(data,it->first); + wiNetwork::receiveData(data,it->first); break; } default: