From 188dd0c9cbfa7e30f9dc63dcafeb9ee6d3c6b7d9 Mon Sep 17 00:00:00 2001 From: Syping Date: Fri, 3 Jul 2020 18:57:05 +0200 Subject: [PATCH] added socketConnect --- .../luaengine/LuaEngineNetwork.cpp | 34 ++++++++++++++----- .../luaengine/LuaEngineNetwork.h | 1 + 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/luaenginenetwork/luaengine/LuaEngineNetwork.cpp b/src/luaenginenetwork/luaengine/LuaEngineNetwork.cpp index a307163..878b7c9 100644 --- a/src/luaenginenetwork/luaengine/LuaEngineNetwork.cpp +++ b/src/luaenginenetwork/luaengine/LuaEngineNetwork.cpp @@ -16,12 +16,16 @@ *****************************************************************************/ #include "LuaEngineNetwork.h" +#include #include void LuaEngineNetwork::pushClass(lua_State *L_p) { // Local Socket pushFunction(L_p, "createLocalSocket", createLocalSocket); + + // Socket + pushFunction(L_p, "socketConnect", socketConnect); } void LuaEngineNetwork::pushClass(LuaEngine *luaEngine) @@ -32,23 +36,37 @@ void LuaEngineNetwork::pushClass(LuaEngine *luaEngine) int LuaEngineNetwork::createLocalSocket(lua_State *L_p) { QObject *parent = nullptr; - QString socketPath = "LuaEngine"; if (getArgumentCount(L_p) >= 1) { - socketPath = getVariant(L_p, 1).toString(); - if (getArgumentCount(L_p) >= 2) { - void *pointer = getPointer(L_p, 2); - if (pointer != NULL) { - parent = (QObject*)pointer; - } + void *pointer = getPointer(L_p, 1); + if (pointer != NULL) { + parent = (QObject*)pointer; } } QLocalSocket *localSocket = new QLocalSocket(parent); localSocket->setObjectName(nameForPointer(localSocket)); - localSocket->setServerName(socketPath); pushPointer(L_p, localSocket); return 1; } +int LuaEngineNetwork::socketConnect(lua_State *L_p) +{ + if (getArgumentCount(L_p) >= 2) { + void *pointer = getPointer(L_p, 1); + if (pointer != NULL) { + if (((QObject*)pointer)->inherits("QLocalSocket")) { + ((QLocalSocket*)pointer)->connectToServer(getVariant(L_p, 2).toString(), QIODevice::ReadWrite); + } + else if (((QObject*)pointer)->inherits("QAbstractSocket")) { + const QStringList remoteIPPort = getVariant(L_p, 2).toString().split(":"); + if (remoteIPPort.length() >= 2) { + ((QAbstractSocket*)pointer)->connectToHost(remoteIPPort.at(0), remoteIPPort.at(1).toUShort(), QIODevice::ReadWrite); + } + } + } + } + return 0; +} + QString LuaEngineNetwork::nameForPointer(void *pointer) { QString nameStorage; diff --git a/src/luaenginenetwork/luaengine/LuaEngineNetwork.h b/src/luaenginenetwork/luaengine/LuaEngineNetwork.h index 1637452..3ff154b 100644 --- a/src/luaenginenetwork/luaengine/LuaEngineNetwork.h +++ b/src/luaenginenetwork/luaengine/LuaEngineNetwork.h @@ -29,6 +29,7 @@ public: static void pushClass(lua_State *L_p); static void pushClass(LuaEngine *luaEngine); static int createLocalSocket(lua_State *L_p); + static int socketConnect(lua_State *L_p); private: lua_State *L;