created network lua binding
This commit is contained in:
@@ -468,6 +468,7 @@
|
||||
<ClCompile Include="wiLua.cpp" />
|
||||
<ClCompile Include="wiNetwork.cpp" />
|
||||
<ClCompile Include="Renderable3DComponent.cpp" />
|
||||
<ClCompile Include="wiNetwork_BindLua.cpp" />
|
||||
<ClCompile Include="wiRawInput.cpp" />
|
||||
<ClCompile Include="wiRenderer_BindLua.cpp" />
|
||||
<ClCompile Include="wiResourceManager_BindLua.cpp" />
|
||||
@@ -515,6 +516,7 @@
|
||||
<ClInclude Include="wiLoader_BindLua.h" />
|
||||
<ClInclude Include="wiLua_Globals.h" />
|
||||
<ClInclude Include="wiLuna.h" />
|
||||
<ClInclude Include="wiNetwork_BindLua.h" />
|
||||
<ClInclude Include="wiRawInput.h" />
|
||||
<ClInclude Include="wiRenderer_BindLua.h" />
|
||||
<ClInclude Include="LUA\lapi.h" />
|
||||
|
||||
@@ -794,6 +794,9 @@
|
||||
<ClCompile Include="wiBackLog_BindLua.cpp">
|
||||
<Filter>Lua Bindings</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="wiNetwork_BindLua.cpp">
|
||||
<Filter>Lua Bindings</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="typelib.h">
|
||||
@@ -1765,6 +1768,9 @@
|
||||
<ClInclude Include="wiBackLog_BindLua.h">
|
||||
<Filter>Lua Bindings</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="wiNetwork_BindLua.h">
|
||||
<Filter>Lua Bindings</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="..\io_export_wicked_wi_bin.py" />
|
||||
|
||||
+14
-14
@@ -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
|
||||
|
||||
@@ -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 <typename T>
|
||||
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:
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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];
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
#include "wiNetwork_BindLua.h"
|
||||
#include "wiClient.h"
|
||||
#include "wiServer.h"
|
||||
#include "wiHelper.h"
|
||||
|
||||
const char wiClient_BindLua::className[] = "Client";
|
||||
|
||||
Luna<wiClient_BindLua>::FunctionType wiClient_BindLua::methods[] = {
|
||||
lunamethod(wiClient_BindLua,Poll),
|
||||
{ NULL, NULL }
|
||||
};
|
||||
Luna<wiClient_BindLua>::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<wiClient_BindLua>::Register(wiLua::GetGlobal()->GetLuaState());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const char wiServer_BindLua::className[] = "Server";
|
||||
|
||||
Luna<wiServer_BindLua>::FunctionType wiServer_BindLua::methods[] = {
|
||||
lunamethod(wiServer_BindLua,Poll),
|
||||
{ NULL, NULL }
|
||||
};
|
||||
Luna<wiServer_BindLua>::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<wiServer_BindLua>::Register(wiLua::GetGlobal()->GetLuaState());
|
||||
}
|
||||
}
|
||||
@@ -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<wiClient_BindLua>::FunctionType methods[];
|
||||
static Luna<wiClient_BindLua>::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<wiServer_BindLua>::FunctionType methods[];
|
||||
static Luna<wiServer_BindLua>::PropertyType properties[];
|
||||
|
||||
wiServer_BindLua(lua_State* L);
|
||||
~wiServer_BindLua();
|
||||
|
||||
|
||||
int Poll(lua_State* L);
|
||||
|
||||
static void Bind();
|
||||
};
|
||||
|
||||
@@ -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);
|
||||
|
||||
+15
-15
@@ -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<SOCKET,string>::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<string> Server::listClients()
|
||||
vector<string> wiServer::listClients()
|
||||
{
|
||||
vector<string> ret(0);
|
||||
for (map<SOCKET,string>::iterator it = clients.begin(); it != clients.end(); ++it) {
|
||||
@@ -104,24 +104,24 @@ vector<string> 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<SOCKET,string>::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<SOCKET,string>::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
|
||||
|
||||
@@ -9,13 +9,13 @@
|
||||
#include <map>
|
||||
|
||||
|
||||
class Server : public Network
|
||||
class wiServer : public wiNetwork
|
||||
{
|
||||
private:
|
||||
map<SOCKET,string> 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 ["<<new_socket<<"] connected";
|
||||
wiBackLog::post(ss.str().c_str());
|
||||
@@ -100,7 +100,7 @@ public:
|
||||
if (FD_ISSET( it->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:
|
||||
|
||||
Reference in New Issue
Block a user