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

View File

@@ -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)

View File

@@ -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")

View File

@@ -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<std::string*> dataBuffer;
std::deque<std::string*> sendQueue;
std::mutex mutexLock;
void socketReconnect();
void socketConnect();
void reconnect();
void tcpConnect();
};

View File

@@ -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);
}
}

View File

@@ -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 }
};