diff --git a/01_DCS/Scripts/Hooks/Perun-hook.lua b/01_DCS/Scripts/Hooks/Perun-hook.lua index 7ce1501..ea750db 100644 --- a/01_DCS/Scripts/Hooks/Perun-hook.lua +++ b/01_DCS/Scripts/Hooks/Perun-hook.lua @@ -1,5 +1,5 @@ -- Perun for DCS World https://github.com/szporwolik/perun -> DCS Hook component -net.log("Loading - Perun") -- Display perun information in log +net.log("[Perun] Script started") -- Display perun information in log -- Initial init local Perun = {} @@ -11,7 +11,6 @@ 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_main.StartOfApp() -- Load config file local PerunConfig = require "perun_config" @@ -255,7 +254,7 @@ Perun.SendToPerun = function(data_id, data_package) local _now = DCS.getRealTime() local _delay_lua2json = 0 - local _TCPFrame="" .. net.lua2json(_TempData) .. "" + local _tcpMessage="" .. net.lua2json(_TempData) .. "" _delay_lua2json = (DCS.getRealTime() - _now) * 1000000 -- TCP Part - sending @@ -266,7 +265,7 @@ Perun.SendToPerun = function(data_id, data_package) local _delay = 0 -- Try to send a few times (defind in settings section) - _flagConnected, _flagReconnected = Perun.dll_main.tcpSendFrame(_TCPFrame) + _flagConnected, _flagReconnected = Perun.dll_main.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 @@ -879,10 +878,11 @@ end -- ########### Finalize and set callbacks ########### if DCS.isServer() then -- If this game instance is hosting multiplayer game, start Perun - Perun.MissionHash=Perun.GenerateMissionHash() -- Generate initial missionhash - DCS.setUserCallbacks(Perun) -- Set user callbacs, map DCS event handlers with functions defined above - net.log("Loaded - Perun - VladMordock: " .. Perun.Version ) -- Display perun information in log - Perun.ConnectToPerun() -- Connect to Perun server + Perun.dll_main.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 + Perun.ConnectToPerun() -- Connect to Perun server end diff --git a/03_Perun_Lua_Wrapper/perun/src/Connecton.cpp b/03_Perun_Lua_Wrapper/perun/src/Connecton.cpp index adfbd64..77ffbd6 100644 --- a/03_Perun_Lua_Wrapper/perun/src/Connecton.cpp +++ b/03_Perun_Lua_Wrapper/perun/src/Connecton.cpp @@ -6,14 +6,14 @@ socketConnection::socketConnection() { } socketConnection::~socketConnection() { - // Destrcutor - do nothing + // Destructor - do nothing } int socketConnection::getFlagReconnected() { // Return the reconnection flag and reset int flagReconnected = this->flagReconnected; - this->flagReconnected = 0; // Reset the flag + this->flagReconnected = 0; return flagReconnected; } @@ -32,7 +32,6 @@ void socketConnection::socketConnect() { socketAddress.sin_port = htons(u_short(this->tcpPort)); socketAddress.sin_addr.s_addr = *((unsigned long*)gethostbyname(this->tcpHost->c_str())->h_addr); - // Connect to socket if (connect(tcpSocket, (sockaddr*)&socketAddress, sizeof(SOCKADDR_IN)) == 0) { this->flagConnectionState = CONNECTED; this->flagReconnected = 1; @@ -41,10 +40,9 @@ void socketConnection::socketConnect() { void socketConnection::socketCreateConnection(std::string* host, int* port) { // TCP connection - ConnectTo - this->tcpHost = host; // Set target host - this->tcpPort = *port; // Set target port + this->tcpHost = host; + this->tcpPort = *port; - // Connect to socket socketConnect(); // Create new thread @@ -81,18 +79,18 @@ void socketConnection::socketCreateConnection(std::string* host, int* port) { switch (WSAGetLastError()) { case WSAECONNRESET: // Connection was reset - flagConnectionState = DISCONNECTED; // Set flag - socketReconnect(); // Try to reconnect + flagConnectionState = DISCONNECTED; + socketReconnect(); break; case WSAECONNABORTED: // Connection aborted - flagConnectionState = DISCONNECTED; // Set flag - socketReconnect(); // Try to reconnect + flagConnectionState = DISCONNECTED; + socketReconnect(); break; case WSAESHUTDOWN: // Connection was closed - flagConnectionState = DISCONNECTED; // Set flag - socketReconnect(); // Try to reconnect + flagConnectionState = DISCONNECTED; + socketReconnect(); break; } } @@ -100,7 +98,7 @@ void socketConnection::socketCreateConnection(std::string* host, int* port) { } mutexLock.unlock(); } else { - // Not connected - try to reconnect + // Not connected socketReconnect(); } // Add delay @@ -113,31 +111,29 @@ void socketConnection::socketCreateConnection(std::string* host, int* port) { void socketConnection::socketReconnect() { // TCP connection - Reconnect if (flagConnectionState == DISCONNECTED) { - socketDisconnect(); // Disconnect + socketDisconnect(); tcpSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); // Reset socket } - // Connect to socket socketConnect(); } void socketConnection::socketDisconnect() { // TCP connection - Disconnect - closesocket(tcpSocket); // Close socket - flagConnectionState = DISCONNECTED; // Set flag + closesocket(tcpSocket); + flagConnectionState = DISCONNECTED; } void socketConnection::socketSendData(std::string* payload) { // Send data if (mutexLock.try_lock()) { - // Add to queue (set mutex lock) while (!dataBuffer.empty()) { // Shift buffer to queue sendQueue.push_back(dataBuffer.front()); dataBuffer.pop(); } - sendQueue.push_back(payload); // Shift queue - mutexLock.unlock(); // Unlock sending thread + sendQueue.push_back(payload); + mutexLock.unlock(); } else { // Add to buffer - queue is busy diff --git a/03_Perun_Lua_Wrapper/perun/src/library.cpp b/03_Perun_Lua_Wrapper/perun/src/library.cpp index 57a1919..7eb2cb9 100644 --- a/03_Perun_Lua_Wrapper/perun/src/library.cpp +++ b/03_Perun_Lua_Wrapper/perun/src/library.cpp @@ -5,49 +5,43 @@ static socketConnection tcpConnection; static int appStart(lua_State* luaState) { // Starting the app - prepare - // No return values - return (0); + lua_pushinteger(luaState, 1); // First return value: confirmation that app was started + + return (1); // Set return to number of arguments returned } static int appEnd(lua_State* luaState) { // Closing the app - clean up - // Close the connection tcpConnection.socketDisconnect(); - // No return values - return (0); + return (0); // No return values } static int tcpConnect(lua_State* luaState) { // Connect to the TCP socket - // Connect tcpConnection.socketCreateConnection(new std::string(lua_tolstring(luaState, 1, 0)), new int(lua_tointeger(luaState, 2))); - // No return values - return (0); + return (0); // No return values } static int tcpSend(lua_State* luaState) { // Send frame over TCP socket - // Send over the socket tcpConnection.socketSendData(new std::string(lua_tolstring(luaState, 1, 0))); - // Return values 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 - // Set return to number of arguments returned - return (2); + return (2); // Set return to number of arguments returned } extern "C" int __declspec(dllexport) luaopen_main(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 - {"tcpSendFrame", tcpSend}, // Called to send data from lua over TCP + {"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 {"tcpConnect", tcpConnect}, // Create connection { NULL, NULL } };