diff --git a/.gitignore b/.gitignore index 3c4efe2..67d76f0 100644 --- a/.gitignore +++ b/.gitignore @@ -258,4 +258,7 @@ paket-files/ # Python Tools for Visual Studio (PTVS) __pycache__/ -*.pyc \ No newline at end of file +*.pyc + +# cmake folders +cmake-build-debug \ No newline at end of file diff --git a/01_DCS/Scripts/Hooks/Perun-hook.lua b/01_DCS/Scripts/Hooks/Perun-hook.lua index f16375f..75f6bee 100644 --- a/01_DCS/Scripts/Hooks/Perun-hook.lua +++ b/01_DCS/Scripts/Hooks/Perun-hook.lua @@ -10,7 +10,7 @@ package.cpath = package.cpath..";"..lfs.currentdir().."/LuaSocket/?.dll" -- Load Dlls package.cpath = package.cpath..';'.. lfs.writedir()..'/Mods/services/Perun/bin/' ..'?.dll;' -Perun.dll_main = require('main') +Perun.DLL = require('perun') -- Load config file local PerunConfig = require "perun_config" @@ -235,7 +235,7 @@ end Perun.ConnectToPerun = function () -- Connects to Perun server - Perun.dll_main.tcpConnect(Perun.TCPPerunHost, Perun.TCPTargetPort) + Perun.DLL.tcpConnect(Perun.TCPPerunHost, Perun.TCPTargetPort) Perun.AddLog(string.format("Connecting to TCP server %s:%i", Perun.TCPPerunHost, Perun.TCPTargetPort), 2) end @@ -256,7 +256,7 @@ Perun.SendToPerun = function(data_id, data_package) local _tcpMessage= table.concat( _TempData ) -- TCP Part - sending - _flagConnected, _flagReconnected = Perun.dll_main.tcpSend(_tcpMessage) + _flagConnected, _flagReconnected = Perun.DLL.tcpSend(_tcpMessage) if (_flagConnected < 1) and (_now > Perun.lastConnectionError + Perun.ReconnectTimeout) then -- Add information to log file and send chat message to all that Perun connection is broken @@ -868,7 +868,7 @@ end -- ########### Finalize and set callbacks ########### if DCS.isServer() then -- If this game instance is hosting multiplayer game, start Perun - Perun.dll_main.StartOfApp() -- Start the main Perun dll + Perun.DLL.StartOfApp() -- Start the main Perun dll Perun.MissionHash=Perun.GenerateMissionHash() -- Generate initial missionhash DCS.setUserCallbacks(Perun) -- Set user callbacs, map DCS event handlers with functions defined above Perun.AddLog("Loaded - Perun for DCS World - version: " .. Perun.Version,0) -- Display perun information in log diff --git a/03_Perun_Lua_Wrapper/CMakeLists.txt b/03_Perun_Lua_Wrapper/CMakeLists.txt index 6e708f3..1ef4ac9 100644 --- a/03_Perun_Lua_Wrapper/CMakeLists.txt +++ b/03_Perun_Lua_Wrapper/CMakeLists.txt @@ -3,7 +3,7 @@ # cmake_minimum_required (VERSION 3.8) -project ("perun") +project ("parent") set(CMAKE_CXX_STANDARD 20) set_property(GLOBAL PROPERTY USE_FOLDERS ON) diff --git a/03_Perun_Lua_Wrapper/perun/CmakeLists.txt b/03_Perun_Lua_Wrapper/perun/CmakeLists.txt index 0bebd22..bfc31d2 100644 --- a/03_Perun_Lua_Wrapper/perun/CmakeLists.txt +++ b/03_Perun_Lua_Wrapper/perun/CmakeLists.txt @@ -11,13 +11,13 @@ set (PERUN_DLL_SOURCES ) include (GenerateExportHeader) -add_library(main SHARED ${PERUN_DLL_SOURCES}) +add_library(perun SHARED ${PERUN_DLL_SOURCES}) target_link_libraries( - main + perun lua-5.1.5 ws2_32 ) -target_include_directories (main PUBLIC) +target_include_directories (perun PUBLIC) #set_target_properties(main PROPERTIES OUTPUT_NAME "perun") \ No newline at end of file diff --git a/03_Perun_Lua_Wrapper/perun/src/Connection.h b/03_Perun_Lua_Wrapper/perun/src/Connection.h index a414065..cd2480c 100644 --- a/03_Perun_Lua_Wrapper/perun/src/Connection.h +++ b/03_Perun_Lua_Wrapper/perun/src/Connection.h @@ -13,19 +13,18 @@ enum enumConnectionState { DISCONNECTED, CONNECTED, - FAILED, }; -class socketConnection { +class SocketWrapper { public: - socketConnection(); - ~socketConnection(); + SocketWrapper(); + ~SocketWrapper(); - void socketDisconnect(); - void socketCreateConnection(std::string* host, int* port); - void socketSendData(std::string* payload); + void disconnect(); + void createConnection(std::string* host, const int* port); + void enqueueForSending(std::string* payload); - int getFlagReconnected(); + int getAndResetReconnected(); int getFlagConnected(); private: @@ -34,15 +33,15 @@ private: int tcpPort; int flagReconnected = 0; - volatile enumConnectionState flagConnectionState = DISCONNECTED; + volatile enumConnectionState connectionState = DISCONNECTED; std::queue dataBuffer; std::deque sendQueue; std::mutex mutexLock; - void socketReconnect(); - void socketConnect(); + void reconnect(); + void tcpConnect(); }; diff --git a/03_Perun_Lua_Wrapper/perun/src/Connecton.cpp b/03_Perun_Lua_Wrapper/perun/src/Connecton.cpp index 77ffbd6..85c52e5 100644 --- a/03_Perun_Lua_Wrapper/perun/src/Connecton.cpp +++ b/03_Perun_Lua_Wrapper/perun/src/Connecton.cpp @@ -1,61 +1,53 @@ #include "Connection.h" -socketConnection::socketConnection() { - // Constructor +SocketWrapper::SocketWrapper() { tcpSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); } -socketConnection::~socketConnection() { - // Destructor - do nothing +SocketWrapper::~SocketWrapper() { } -int socketConnection::getFlagReconnected() { - // Return the reconnection flag and reset - int flagReconnected = this->flagReconnected; +int SocketWrapper::getAndResetReconnected() { + int result = this->flagReconnected; this->flagReconnected = 0; - return flagReconnected; + return result; } -int socketConnection::getFlagConnected() { - // Return the connection flag - int flagConnected = this->flagConnectionState; - - return flagConnected; +int SocketWrapper::getFlagConnected() { + return this->connectionState; } -void socketConnection::socketConnect() { - // Create socket adress object from TCP port and host +void SocketWrapper::tcpConnect() { + // Create socket address object from TCP port and host SOCKADDR_IN socketAddress; socketAddress.sin_family = AF_INET; socketAddress.sin_port = htons(u_short(this->tcpPort)); socketAddress.sin_addr.s_addr = *((unsigned long*)gethostbyname(this->tcpHost->c_str())->h_addr); if (connect(tcpSocket, (sockaddr*)&socketAddress, sizeof(SOCKADDR_IN)) == 0) { - this->flagConnectionState = CONNECTED; + this->connectionState = CONNECTED; this->flagReconnected = 1; } } -void socketConnection::socketCreateConnection(std::string* host, int* port) { +void SocketWrapper::createConnection(std::string* host, const int* port) { // TCP connection - ConnectTo this->tcpHost = host; this->tcpPort = *port; - socketConnect(); + tcpConnect(); // Create new thread std::thread thread_object([this]() { // TCP sending loop - bool isQueueEmpty = false; // Helper - + bool nothingToSend = false; while (true) { // Endless loop (will run after main dll thread is active) - if (flagConnectionState == CONNECTED && mutexLock.try_lock()) { + if (connectionState == CONNECTED && mutexLock.try_lock()) { if (sendQueue.empty()) { - // Empty queue - isQueueEmpty = true; + nothingToSend = true; } else { // Payload in queue auto payload = sendQueue.front(); @@ -77,21 +69,14 @@ void socketConnection::socketCreateConnection(std::string* host, int* port) { } else { // Payload was not sent - handle error switch (WSAGetLastError()) { + // Connection was reset case WSAECONNRESET: - // Connection was reset - flagConnectionState = DISCONNECTED; - socketReconnect(); - break; + // Connection aborted case WSAECONNABORTED: - // Connection aborted - flagConnectionState = DISCONNECTED; - socketReconnect(); - break; + // Connection was closed case WSAESHUTDOWN: - // Connection was closed - flagConnectionState = DISCONNECTED; - socketReconnect(); - break; + connectionState = DISCONNECTED; + reconnect(); } } } @@ -99,33 +84,31 @@ void socketConnection::socketCreateConnection(std::string* host, int* port) { mutexLock.unlock(); } else { // Not connected - socketReconnect(); + reconnect(); } - // Add delay - if (isQueueEmpty) { Sleep(100); } else { Sleep(10); } // Delay depending if there is something in Queue or not + // sleep longer if nothing to send + if (nothingToSend) { Sleep(100); } else { Sleep(10); } } }); thread_object.detach(); // Detach TCP thread from main thread } -void socketConnection::socketReconnect() { - // TCP connection - Reconnect - if (flagConnectionState == DISCONNECTED) { - socketDisconnect(); +void SocketWrapper::reconnect() { + if (connectionState == DISCONNECTED) { + disconnect(); tcpSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); // Reset socket } - socketConnect(); + tcpConnect(); } -void socketConnection::socketDisconnect() { +void SocketWrapper::disconnect() { // TCP connection - Disconnect - closesocket(tcpSocket); - flagConnectionState = DISCONNECTED; + closesocket(tcpSocket); + connectionState = DISCONNECTED; } -void socketConnection::socketSendData(std::string* payload) { - // Send data +void SocketWrapper::enqueueForSending(std::string* payload) { if (mutexLock.try_lock()) { while (!dataBuffer.empty()) { // Shift buffer to queue @@ -136,7 +119,6 @@ void socketConnection::socketSendData(std::string* payload) { mutexLock.unlock(); } else { - // Add to buffer - queue is busy dataBuffer.push(payload); } } \ No newline at end of file diff --git a/03_Perun_Lua_Wrapper/perun/src/library.cpp b/03_Perun_Lua_Wrapper/perun/src/library.cpp index 7eb2cb9..d3eba43 100644 --- a/03_Perun_Lua_Wrapper/perun/src/library.cpp +++ b/03_Perun_Lua_Wrapper/perun/src/library.cpp @@ -1,8 +1,8 @@ #include "library.h" -static socketConnection tcpConnection; +static SocketWrapper tcpConnection; -static int appStart(lua_State* luaState) { +static int appStartHook(lua_State* luaState) { // Starting the app - prepare lua_pushinteger(luaState, 1); // First return value: confirmation that app was started @@ -10,10 +10,10 @@ static int appStart(lua_State* luaState) { return (1); // Set return to number of arguments returned } -static int appEnd(lua_State* luaState) { +static int appEndHook(lua_State* luaState) { // Closing the app - clean up - tcpConnection.socketDisconnect(); + tcpConnection.disconnect(); return (0); // No return values } @@ -21,7 +21,9 @@ static int appEnd(lua_State* luaState) { static int tcpConnect(lua_State* luaState) { // Connect to the TCP socket - tcpConnection.socketCreateConnection(new std::string(lua_tolstring(luaState, 1, 0)), new int(lua_tointeger(luaState, 2))); + auto *host = new std::string(lua_tolstring(luaState, 1, 0)); + const int *port = new int(lua_tointeger(luaState, 2)); + tcpConnection.createConnection(host, port); return (0); // No return values } @@ -29,19 +31,19 @@ static int tcpConnect(lua_State* luaState) { static int tcpSend(lua_State* luaState) { // Send frame over TCP socket - tcpConnection.socketSendData(new std::string(lua_tolstring(luaState, 1, 0))); + tcpConnection.enqueueForSending(new std::string(lua_tolstring(luaState, 1, 0))); lua_pushinteger(luaState, tcpConnection.getFlagConnected()); // First return value: information if there is TCP connection - lua_pushinteger(luaState, tcpConnection.getFlagReconnected()); // Secound return value: information if there was recent reconnection to TCP server + lua_pushinteger(luaState, tcpConnection.getAndResetReconnected()); // Secound return value: information if there was recent reconnection to TCP server return (2); // Set return to number of arguments returned } -extern "C" int __declspec(dllexport) luaopen_main(lua_State * L) { +extern "C" int __declspec(dllexport) luaopen_perun(lua_State * L) { static const luaL_Reg Map[] = { - {"StartOfApp", appStart}, // Called at the begining of the session - {"EndOfApp", appEnd }, // Called at the end of the session out of LuaExportStop - {"tcpSend", tcpSend}, // Called to send data from lua over TCP + {"StartOfApp", appStartHook}, // Called at the begining of the session + {"EndOfApp", appEndHook }, // Called at the end of the session out of LuaExportStop + {"tcpSend", tcpSend}, // Called to send data from lua over TCP {"tcpConnect", tcpConnect}, // Create connection { NULL, NULL } };