change dll name

This commit is contained in:
ags
2021-02-23 23:30:16 +00:00
parent 8aa0a8e6e0
commit feec035260
7 changed files with 66 additions and 80 deletions

5
.gitignore vendored
View File

@@ -258,4 +258,7 @@ paket-files/
# Python Tools for Visual Studio (PTVS) # Python Tools for Visual Studio (PTVS)
__pycache__/ __pycache__/
*.pyc *.pyc
# cmake folders
cmake-build-debug

View File

@@ -10,7 +10,7 @@ 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 = require('perun')
-- Load config file -- Load config file
local PerunConfig = require "perun_config" local PerunConfig = require "perun_config"
@@ -235,7 +235,7 @@ end
Perun.ConnectToPerun = function () Perun.ConnectToPerun = function ()
-- Connects to Perun server -- 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) Perun.AddLog(string.format("Connecting to TCP server %s:%i", Perun.TCPPerunHost, Perun.TCPTargetPort), 2)
end end
@@ -256,7 +256,7 @@ Perun.SendToPerun = function(data_id, data_package)
local _tcpMessage= table.concat( _TempData ) local _tcpMessage= table.concat( _TempData )
-- TCP Part - sending -- 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 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
@@ -868,7 +868,7 @@ 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.dll_main.StartOfApp() -- Start the main Perun dll Perun.DLL.StartOfApp() -- Start the main Perun dll
Perun.MissionHash=Perun.GenerateMissionHash() -- Generate initial missionhash Perun.MissionHash=Perun.GenerateMissionHash() -- Generate initial missionhash
DCS.setUserCallbacks(Perun) -- Set user callbacs, map DCS event handlers with functions defined above 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.AddLog("Loaded - Perun for DCS World - version: " .. Perun.Version,0) -- Display perun information in log

View File

@@ -3,7 +3,7 @@
# #
cmake_minimum_required (VERSION 3.8) cmake_minimum_required (VERSION 3.8)
project ("perun") project ("parent")
set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD 20)
set_property(GLOBAL PROPERTY USE_FOLDERS ON) set_property(GLOBAL PROPERTY USE_FOLDERS ON)

View File

@@ -11,13 +11,13 @@ set (PERUN_DLL_SOURCES
) )
include (GenerateExportHeader) include (GenerateExportHeader)
add_library(main SHARED ${PERUN_DLL_SOURCES}) add_library(perun SHARED ${PERUN_DLL_SOURCES})
target_link_libraries( target_link_libraries(
main perun
lua-5.1.5 lua-5.1.5
ws2_32 ws2_32
) )
target_include_directories (main PUBLIC) target_include_directories (perun PUBLIC)
#set_target_properties(main PROPERTIES OUTPUT_NAME "perun") #set_target_properties(main PROPERTIES OUTPUT_NAME "perun")

View File

@@ -13,19 +13,18 @@
enum enumConnectionState { enum enumConnectionState {
DISCONNECTED, DISCONNECTED,
CONNECTED, CONNECTED,
FAILED,
}; };
class socketConnection { class SocketWrapper {
public: public:
socketConnection(); SocketWrapper();
~socketConnection(); ~SocketWrapper();
void socketDisconnect(); void disconnect();
void socketCreateConnection(std::string* host, int* port); void createConnection(std::string* host, const int* port);
void socketSendData(std::string* payload); void enqueueForSending(std::string* payload);
int getFlagReconnected(); int getAndResetReconnected();
int getFlagConnected(); int getFlagConnected();
private: private:
@@ -34,15 +33,15 @@ private:
int tcpPort; int tcpPort;
int flagReconnected = 0; int flagReconnected = 0;
volatile enumConnectionState flagConnectionState = DISCONNECTED; volatile enumConnectionState connectionState = DISCONNECTED;
std::queue<std::string*> dataBuffer; std::queue<std::string*> dataBuffer;
std::deque<std::string*> sendQueue; std::deque<std::string*> sendQueue;
std::mutex mutexLock; std::mutex mutexLock;
void socketReconnect(); void reconnect();
void socketConnect(); void tcpConnect();
}; };

View File

@@ -1,61 +1,53 @@
#include "Connection.h" #include "Connection.h"
socketConnection::socketConnection() { SocketWrapper::SocketWrapper() {
// Constructor
tcpSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); tcpSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
} }
socketConnection::~socketConnection() { SocketWrapper::~SocketWrapper() {
// Destructor - do nothing
} }
int socketConnection::getFlagReconnected() { int SocketWrapper::getAndResetReconnected() {
// Return the reconnection flag and reset int result = this->flagReconnected;
int flagReconnected = this->flagReconnected;
this->flagReconnected = 0; this->flagReconnected = 0;
return flagReconnected; return result;
} }
int socketConnection::getFlagConnected() { int SocketWrapper::getFlagConnected() {
// Return the connection flag return this->connectionState;
int flagConnected = this->flagConnectionState;
return flagConnected;
} }
void socketConnection::socketConnect() { void SocketWrapper::tcpConnect() {
// Create socket adress object from TCP port and host // Create socket address object from TCP port and host
SOCKADDR_IN socketAddress; SOCKADDR_IN socketAddress;
socketAddress.sin_family = AF_INET; socketAddress.sin_family = AF_INET;
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);
if (connect(tcpSocket, (sockaddr*)&socketAddress, sizeof(SOCKADDR_IN)) == 0) { if (connect(tcpSocket, (sockaddr*)&socketAddress, sizeof(SOCKADDR_IN)) == 0) {
this->flagConnectionState = CONNECTED; this->connectionState = CONNECTED;
this->flagReconnected = 1; this->flagReconnected = 1;
} }
} }
void socketConnection::socketCreateConnection(std::string* host, int* port) { void SocketWrapper::createConnection(std::string* host, const int* port) {
// TCP connection - ConnectTo // TCP connection - ConnectTo
this->tcpHost = host; this->tcpHost = host;
this->tcpPort = *port; this->tcpPort = *port;
socketConnect(); tcpConnect();
// Create new thread // Create new thread
std::thread thread_object([this]() { std::thread thread_object([this]() {
// TCP sending loop // TCP sending loop
bool isQueueEmpty = false; // Helper bool nothingToSend = false;
while (true) { while (true) {
// Endless loop (will run after main dll thread is active) // 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()) { if (sendQueue.empty()) {
// Empty queue nothingToSend = true;
isQueueEmpty = true;
} else { } else {
// Payload in queue // Payload in queue
auto payload = sendQueue.front(); auto payload = sendQueue.front();
@@ -77,21 +69,14 @@ void socketConnection::socketCreateConnection(std::string* host, int* port) {
} else { } else {
// Payload was not sent - handle error // Payload was not sent - handle error
switch (WSAGetLastError()) { switch (WSAGetLastError()) {
// Connection was reset
case WSAECONNRESET: case WSAECONNRESET:
// Connection was reset // Connection aborted
flagConnectionState = DISCONNECTED;
socketReconnect();
break;
case WSAECONNABORTED: case WSAECONNABORTED:
// Connection aborted // Connection was closed
flagConnectionState = DISCONNECTED;
socketReconnect();
break;
case WSAESHUTDOWN: case WSAESHUTDOWN:
// Connection was closed connectionState = DISCONNECTED;
flagConnectionState = DISCONNECTED; reconnect();
socketReconnect();
break;
} }
} }
} }
@@ -99,33 +84,31 @@ void socketConnection::socketCreateConnection(std::string* host, int* port) {
mutexLock.unlock(); mutexLock.unlock();
} else { } else {
// Not connected // Not connected
socketReconnect(); reconnect();
} }
// Add delay // sleep longer if nothing to send
if (isQueueEmpty) { Sleep(100); } else { Sleep(10); } // Delay depending if there is something in Queue or not if (nothingToSend) { Sleep(100); } else { Sleep(10); }
} }
}); });
thread_object.detach(); // Detach TCP thread from main thread thread_object.detach(); // Detach TCP thread from main thread
} }
void socketConnection::socketReconnect() { void SocketWrapper::reconnect() {
// TCP connection - Reconnect if (connectionState == DISCONNECTED) {
if (flagConnectionState == DISCONNECTED) { disconnect();
socketDisconnect();
tcpSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); // Reset socket tcpSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); // Reset socket
} }
socketConnect(); tcpConnect();
} }
void socketConnection::socketDisconnect() { void SocketWrapper::disconnect() {
// TCP connection - Disconnect // TCP connection - Disconnect
closesocket(tcpSocket); closesocket(tcpSocket);
flagConnectionState = DISCONNECTED; connectionState = DISCONNECTED;
} }
void socketConnection::socketSendData(std::string* payload) { void SocketWrapper::enqueueForSending(std::string* payload) {
// Send data
if (mutexLock.try_lock()) { if (mutexLock.try_lock()) {
while (!dataBuffer.empty()) { while (!dataBuffer.empty()) {
// Shift buffer to queue // Shift buffer to queue
@@ -136,7 +119,6 @@ void socketConnection::socketSendData(std::string* payload) {
mutexLock.unlock(); mutexLock.unlock();
} }
else { else {
// Add to buffer - queue is busy
dataBuffer.push(payload); dataBuffer.push(payload);
} }
} }

View File

@@ -1,8 +1,8 @@
#include "library.h" #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 // Starting the app - prepare
lua_pushinteger(luaState, 1); // First return value: confirmation that app was started 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 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 // Closing the app - clean up
tcpConnection.socketDisconnect(); tcpConnection.disconnect();
return (0); // No return values return (0); // No return values
} }
@@ -21,7 +21,9 @@ static int appEnd(lua_State* luaState) {
static int tcpConnect(lua_State* luaState) { static int tcpConnect(lua_State* luaState) {
// Connect to the TCP socket // 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 return (0); // No return values
} }
@@ -29,19 +31,19 @@ static int tcpConnect(lua_State* luaState) {
static int tcpSend(lua_State* luaState) { static int tcpSend(lua_State* luaState) {
// Send frame over TCP socket // 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.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 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[] = { static const luaL_Reg Map[] = {
{"StartOfApp", appStart}, // Called at the begining of the session {"StartOfApp", appStartHook}, // Called at the begining of the session
{"EndOfApp", appEnd }, // Called at the end of the session out of LuaExportStop {"EndOfApp", appEndHook }, // Called at the end of the session out of LuaExportStop
{"tcpSend", 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 }
}; };