mirror of
https://github.com/DaKerboul/perun.git
synced 2026-08-09 20:15:39 +02:00
Code cleanup
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
-- Perun for DCS World https://github.com/szporwolik/perun -> DCS Hook component
|
-- 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
|
-- Initial init
|
||||||
local Perun = {}
|
local Perun = {}
|
||||||
@@ -11,7 +11,6 @@ package.cpath = package.cpath..";"..lfs.currentdir().."/LuaSocket/?.dll"
|
|||||||
-- Load Dlls
|
-- Load Dlls
|
||||||
package.cpath = package.cpath..';'.. lfs.writedir()..'/Mods/services/Perun/bin/' ..'?.dll;'
|
package.cpath = package.cpath..';'.. lfs.writedir()..'/Mods/services/Perun/bin/' ..'?.dll;'
|
||||||
Perun.dll_main = require('main')
|
Perun.dll_main = require('main')
|
||||||
Perun.dll_main.StartOfApp()
|
|
||||||
|
|
||||||
-- Load config file
|
-- Load config file
|
||||||
local PerunConfig = require "perun_config"
|
local PerunConfig = require "perun_config"
|
||||||
@@ -255,7 +254,7 @@ Perun.SendToPerun = function(data_id, data_package)
|
|||||||
local _now = DCS.getRealTime()
|
local _now = DCS.getRealTime()
|
||||||
local _delay_lua2json = 0
|
local _delay_lua2json = 0
|
||||||
|
|
||||||
local _TCPFrame="<SOT>" .. net.lua2json(_TempData) .. "<EOT>"
|
local _tcpMessage="<SOT>" .. net.lua2json(_TempData) .. "<EOT>"
|
||||||
_delay_lua2json = (DCS.getRealTime() - _now) * 1000000
|
_delay_lua2json = (DCS.getRealTime() - _now) * 1000000
|
||||||
|
|
||||||
-- TCP Part - sending
|
-- TCP Part - sending
|
||||||
@@ -266,7 +265,7 @@ Perun.SendToPerun = function(data_id, data_package)
|
|||||||
local _delay = 0
|
local _delay = 0
|
||||||
|
|
||||||
-- Try to send a few times (defind in settings section)
|
-- 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
|
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
|
-- 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 ###########
|
-- ########### Finalize and set callbacks ###########
|
||||||
if DCS.isServer() then
|
if DCS.isServer() then
|
||||||
-- If this game instance is hosting multiplayer game, start Perun
|
-- If this game instance is hosting multiplayer game, start Perun
|
||||||
Perun.MissionHash=Perun.GenerateMissionHash() -- Generate initial missionhash
|
Perun.dll_main.StartOfApp() -- Start the main Perun dll
|
||||||
DCS.setUserCallbacks(Perun) -- Set user callbacs, map DCS event handlers with functions defined above
|
Perun.MissionHash=Perun.GenerateMissionHash() -- Generate initial missionhash
|
||||||
net.log("Loaded - Perun - VladMordock: " .. Perun.Version ) -- Display perun information in log
|
DCS.setUserCallbacks(Perun) -- Set user callbacs, map DCS event handlers with functions defined above
|
||||||
Perun.ConnectToPerun() -- Connect to Perun server
|
Perun.AddLog("Loaded - Perun for DCS World - version: " .. Perun.Version,0) -- Display perun information in log
|
||||||
|
Perun.ConnectToPerun() -- Connect to Perun server
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -6,14 +6,14 @@ socketConnection::socketConnection() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
socketConnection::~socketConnection() {
|
socketConnection::~socketConnection() {
|
||||||
// Destrcutor - do nothing
|
// Destructor - do nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
int socketConnection::getFlagReconnected() {
|
int socketConnection::getFlagReconnected() {
|
||||||
// Return the reconnection flag and reset
|
// Return the reconnection flag and reset
|
||||||
int flagReconnected = this->flagReconnected;
|
int flagReconnected = this->flagReconnected;
|
||||||
|
|
||||||
this->flagReconnected = 0; // Reset the flag
|
this->flagReconnected = 0;
|
||||||
|
|
||||||
return flagReconnected;
|
return flagReconnected;
|
||||||
}
|
}
|
||||||
@@ -32,7 +32,6 @@ void socketConnection::socketConnect() {
|
|||||||
socketAddress.sin_port = htons(u_short(this->tcpPort));
|
socketAddress.sin_port = htons(u_short(this->tcpPort));
|
||||||
socketAddress.sin_addr.s_addr = *((unsigned long*)gethostbyname(this->tcpHost->c_str())->h_addr);
|
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) {
|
if (connect(tcpSocket, (sockaddr*)&socketAddress, sizeof(SOCKADDR_IN)) == 0) {
|
||||||
this->flagConnectionState = CONNECTED;
|
this->flagConnectionState = CONNECTED;
|
||||||
this->flagReconnected = 1;
|
this->flagReconnected = 1;
|
||||||
@@ -41,10 +40,9 @@ void socketConnection::socketConnect() {
|
|||||||
|
|
||||||
void socketConnection::socketCreateConnection(std::string* host, int* port) {
|
void socketConnection::socketCreateConnection(std::string* host, int* port) {
|
||||||
// TCP connection - ConnectTo
|
// TCP connection - ConnectTo
|
||||||
this->tcpHost = host; // Set target host
|
this->tcpHost = host;
|
||||||
this->tcpPort = *port; // Set target port
|
this->tcpPort = *port;
|
||||||
|
|
||||||
// Connect to socket
|
|
||||||
socketConnect();
|
socketConnect();
|
||||||
|
|
||||||
// Create new thread
|
// Create new thread
|
||||||
@@ -81,18 +79,18 @@ void socketConnection::socketCreateConnection(std::string* host, int* port) {
|
|||||||
switch (WSAGetLastError()) {
|
switch (WSAGetLastError()) {
|
||||||
case WSAECONNRESET:
|
case WSAECONNRESET:
|
||||||
// Connection was reset
|
// Connection was reset
|
||||||
flagConnectionState = DISCONNECTED; // Set flag
|
flagConnectionState = DISCONNECTED;
|
||||||
socketReconnect(); // Try to reconnect
|
socketReconnect();
|
||||||
break;
|
break;
|
||||||
case WSAECONNABORTED:
|
case WSAECONNABORTED:
|
||||||
// Connection aborted
|
// Connection aborted
|
||||||
flagConnectionState = DISCONNECTED; // Set flag
|
flagConnectionState = DISCONNECTED;
|
||||||
socketReconnect(); // Try to reconnect
|
socketReconnect();
|
||||||
break;
|
break;
|
||||||
case WSAESHUTDOWN:
|
case WSAESHUTDOWN:
|
||||||
// Connection was closed
|
// Connection was closed
|
||||||
flagConnectionState = DISCONNECTED; // Set flag
|
flagConnectionState = DISCONNECTED;
|
||||||
socketReconnect(); // Try to reconnect
|
socketReconnect();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -100,7 +98,7 @@ void socketConnection::socketCreateConnection(std::string* host, int* port) {
|
|||||||
}
|
}
|
||||||
mutexLock.unlock();
|
mutexLock.unlock();
|
||||||
} else {
|
} else {
|
||||||
// Not connected - try to reconnect
|
// Not connected
|
||||||
socketReconnect();
|
socketReconnect();
|
||||||
}
|
}
|
||||||
// Add delay
|
// Add delay
|
||||||
@@ -113,31 +111,29 @@ void socketConnection::socketCreateConnection(std::string* host, int* port) {
|
|||||||
void socketConnection::socketReconnect() {
|
void socketConnection::socketReconnect() {
|
||||||
// TCP connection - Reconnect
|
// TCP connection - Reconnect
|
||||||
if (flagConnectionState == DISCONNECTED) {
|
if (flagConnectionState == DISCONNECTED) {
|
||||||
socketDisconnect(); // Disconnect
|
socketDisconnect();
|
||||||
tcpSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); // Reset socket
|
tcpSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); // Reset socket
|
||||||
}
|
}
|
||||||
|
|
||||||
// Connect to socket
|
|
||||||
socketConnect();
|
socketConnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
void socketConnection::socketDisconnect() {
|
void socketConnection::socketDisconnect() {
|
||||||
// TCP connection - Disconnect
|
// TCP connection - Disconnect
|
||||||
closesocket(tcpSocket); // Close socket
|
closesocket(tcpSocket);
|
||||||
flagConnectionState = DISCONNECTED; // Set flag
|
flagConnectionState = DISCONNECTED;
|
||||||
}
|
}
|
||||||
|
|
||||||
void socketConnection::socketSendData(std::string* payload) {
|
void socketConnection::socketSendData(std::string* payload) {
|
||||||
// Send data
|
// Send data
|
||||||
if (mutexLock.try_lock()) {
|
if (mutexLock.try_lock()) {
|
||||||
// Add to queue (set mutex lock)
|
|
||||||
while (!dataBuffer.empty()) {
|
while (!dataBuffer.empty()) {
|
||||||
// Shift buffer to queue
|
// Shift buffer to queue
|
||||||
sendQueue.push_back(dataBuffer.front());
|
sendQueue.push_back(dataBuffer.front());
|
||||||
dataBuffer.pop();
|
dataBuffer.pop();
|
||||||
}
|
}
|
||||||
sendQueue.push_back(payload); // Shift queue
|
sendQueue.push_back(payload);
|
||||||
mutexLock.unlock(); // Unlock sending thread
|
mutexLock.unlock();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// Add to buffer - queue is busy
|
// Add to buffer - queue is busy
|
||||||
|
|||||||
@@ -5,49 +5,43 @@ static socketConnection tcpConnection;
|
|||||||
static int appStart(lua_State* luaState) {
|
static int appStart(lua_State* luaState) {
|
||||||
// Starting the app - prepare
|
// Starting the app - prepare
|
||||||
|
|
||||||
// No return values
|
lua_pushinteger(luaState, 1); // First return value: confirmation that app was started
|
||||||
return (0);
|
|
||||||
|
return (1); // Set return to number of arguments returned
|
||||||
}
|
}
|
||||||
|
|
||||||
static int appEnd(lua_State* luaState) {
|
static int appEnd(lua_State* luaState) {
|
||||||
// Closing the app - clean up
|
// Closing the app - clean up
|
||||||
|
|
||||||
// Close the connection
|
|
||||||
tcpConnection.socketDisconnect();
|
tcpConnection.socketDisconnect();
|
||||||
|
|
||||||
// No return values
|
return (0); // No return values
|
||||||
return (0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int tcpConnect(lua_State* luaState) {
|
static int tcpConnect(lua_State* luaState) {
|
||||||
// Connect to the TCP socket
|
// Connect to the TCP socket
|
||||||
|
|
||||||
// Connect
|
|
||||||
tcpConnection.socketCreateConnection(new std::string(lua_tolstring(luaState, 1, 0)), new int(lua_tointeger(luaState, 2)));
|
tcpConnection.socketCreateConnection(new std::string(lua_tolstring(luaState, 1, 0)), new int(lua_tointeger(luaState, 2)));
|
||||||
|
|
||||||
// No return values
|
return (0); // No return values
|
||||||
return (0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int tcpSend(lua_State* luaState) {
|
static int tcpSend(lua_State* luaState) {
|
||||||
// Send frame over TCP socket
|
// Send frame over TCP socket
|
||||||
|
|
||||||
// Send over the socket
|
|
||||||
tcpConnection.socketSendData(new std::string(lua_tolstring(luaState, 1, 0)));
|
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.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.getFlagReconnected()); // Secound return value: information if there was recent reconnection to TCP server
|
||||||
|
|
||||||
// Set return to number of arguments returned
|
return (2); // Set return to number of arguments returned
|
||||||
return (2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" int __declspec(dllexport) luaopen_main(lua_State * L) {
|
extern "C" int __declspec(dllexport) luaopen_main(lua_State * L) {
|
||||||
static const luaL_Reg Map[] = {
|
static const luaL_Reg Map[] = {
|
||||||
{"StartOfApp", appStart}, // Called at the begining of the session
|
{"StartOfApp", appStart}, // Called at the begining of the session
|
||||||
{"EndOfApp", appEnd }, // Called at the end of the session out of LuaExportStop
|
{"EndOfApp", appEnd }, // Called at the end of the session out of LuaExportStop
|
||||||
{"tcpSendFrame", tcpSend}, // Called to send data from lua over TCP
|
{"tcpSend", tcpSend}, // Called to send data from lua over TCP
|
||||||
{"tcpConnect", tcpConnect}, // Create connection
|
{"tcpConnect", tcpConnect}, // Create connection
|
||||||
{ NULL, NULL }
|
{ NULL, NULL }
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user