added socketConnect

This commit is contained in:
Syping 2020-07-03 18:57:05 +02:00
parent 809b5ff6aa
commit 188dd0c9cb
2 changed files with 27 additions and 8 deletions

View file

@ -16,12 +16,16 @@
*****************************************************************************/ *****************************************************************************/
#include "LuaEngineNetwork.h" #include "LuaEngineNetwork.h"
#include <QAbstractSocket>
#include <QLocalSocket> #include <QLocalSocket>
void LuaEngineNetwork::pushClass(lua_State *L_p) void LuaEngineNetwork::pushClass(lua_State *L_p)
{ {
// Local Socket // Local Socket
pushFunction(L_p, "createLocalSocket", createLocalSocket); pushFunction(L_p, "createLocalSocket", createLocalSocket);
// Socket
pushFunction(L_p, "socketConnect", socketConnect);
} }
void LuaEngineNetwork::pushClass(LuaEngine *luaEngine) void LuaEngineNetwork::pushClass(LuaEngine *luaEngine)
@ -32,23 +36,37 @@ void LuaEngineNetwork::pushClass(LuaEngine *luaEngine)
int LuaEngineNetwork::createLocalSocket(lua_State *L_p) int LuaEngineNetwork::createLocalSocket(lua_State *L_p)
{ {
QObject *parent = nullptr; QObject *parent = nullptr;
QString socketPath = "LuaEngine";
if (getArgumentCount(L_p) >= 1) { if (getArgumentCount(L_p) >= 1) {
socketPath = getVariant(L_p, 1).toString(); void *pointer = getPointer(L_p, 1);
if (getArgumentCount(L_p) >= 2) {
void *pointer = getPointer(L_p, 2);
if (pointer != NULL) { if (pointer != NULL) {
parent = (QObject*)pointer; parent = (QObject*)pointer;
} }
} }
}
QLocalSocket *localSocket = new QLocalSocket(parent); QLocalSocket *localSocket = new QLocalSocket(parent);
localSocket->setObjectName(nameForPointer(localSocket)); localSocket->setObjectName(nameForPointer(localSocket));
localSocket->setServerName(socketPath);
pushPointer(L_p, localSocket); pushPointer(L_p, localSocket);
return 1; 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 LuaEngineNetwork::nameForPointer(void *pointer)
{ {
QString nameStorage; QString nameStorage;

View file

@ -29,6 +29,7 @@ public:
static void pushClass(lua_State *L_p); static void pushClass(lua_State *L_p);
static void pushClass(LuaEngine *luaEngine); static void pushClass(LuaEngine *luaEngine);
static int createLocalSocket(lua_State *L_p); static int createLocalSocket(lua_State *L_p);
static int socketConnect(lua_State *L_p);
private: private:
lua_State *L; lua_State *L;