mirror of
https://github.com/DaKerboul/perun.git
synced 2026-08-09 19:05:39 +02:00
lighter perun event listener
This commit is contained in:
@@ -91,7 +91,7 @@
|
||||
".\\?.lua;" LUA_LDIR"?.lua;" LUA_LDIR"?\\init.lua;" \
|
||||
LUA_CDIR"?.lua;" LUA_CDIR"?\\init.lua"
|
||||
#define LUA_CPATH_DEFAULT \
|
||||
".\\?.dll;" LUA_CDIR"?.dll;" LUA_CDIR"loadall.dll"
|
||||
".\\?.bin;" LUA_CDIR"?.bin;" LUA_CDIR"loadall.bin"
|
||||
|
||||
#else
|
||||
#define LUA_ROOT "/usr/local/"
|
||||
|
||||
@@ -7,7 +7,7 @@ set (PERUN_DLL_SOURCES
|
||||
"src/library.h"
|
||||
"src/library.cpp"
|
||||
"src/Connection.h"
|
||||
"src/Connecton.cpp"
|
||||
"src/Connection.cpp"
|
||||
)
|
||||
|
||||
include (GenerateExportHeader)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "Connection.h"
|
||||
|
||||
SocketWrapper::SocketWrapper() {
|
||||
SocketWrapper::SocketWrapper(std::string name): outputFile(name, std::ofstream::app) {
|
||||
tcpSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||
}
|
||||
|
||||
@@ -44,14 +44,15 @@ void SocketWrapper::createConnection(std::string* host, const int* port) {
|
||||
// TCP sending loop
|
||||
bool nothingToSend = false;
|
||||
while (true) {
|
||||
// Endless loop (will run after main dll thread is active)
|
||||
if (connectionState == CONNECTED && mutexLock.try_lock()) {
|
||||
if (sendQueue.empty()) {
|
||||
nothingToSend = true;
|
||||
} else {
|
||||
// Payload in queue
|
||||
auto payload = sendQueue.front();
|
||||
int length = payload->length();
|
||||
|
||||
outputFile.write(payload->c_str(), payload->length());
|
||||
outputFile.flush();
|
||||
int bytesSent = send(tcpSocket, payload->c_str(), payload->length(), 0);
|
||||
|
||||
if (bytesSent == payload->length()) {
|
||||
@@ -62,7 +63,7 @@ void SocketWrapper::createConnection(std::string* host, const int* port) {
|
||||
// Remaining paylad
|
||||
if (bytesSent > 0) {
|
||||
// Send remaining bytes
|
||||
auto shortened = payload->substr(bytesSent, length - bytesSent);
|
||||
auto shortened = payload->substr(bytesSent, payload->length() - bytesSent);
|
||||
sendQueue.pop_front();
|
||||
sendQueue.push_front(&shortened);
|
||||
delete payload;
|
||||
@@ -17,7 +17,7 @@ enum enumConnectionState {
|
||||
|
||||
class SocketWrapper {
|
||||
public:
|
||||
SocketWrapper();
|
||||
SocketWrapper(std::string name);
|
||||
~SocketWrapper();
|
||||
|
||||
void disconnect();
|
||||
@@ -28,6 +28,9 @@ public:
|
||||
int getFlagConnected();
|
||||
|
||||
private:
|
||||
// SocketWrapper(const SocketWrapper&);
|
||||
// SocketWrapper& operator=(const SocketWrapper&);
|
||||
|
||||
SOCKET tcpSocket;
|
||||
std::string* tcpHost;
|
||||
int tcpPort;
|
||||
@@ -39,6 +42,7 @@ private:
|
||||
std::deque<std::string*> sendQueue;
|
||||
|
||||
std::mutex mutexLock;
|
||||
std::ofstream outputFile;
|
||||
|
||||
void reconnect();
|
||||
void tcpConnect();
|
||||
|
||||
@@ -1,42 +1,68 @@
|
||||
#include "library.h"
|
||||
|
||||
static SocketWrapper tcpConnection;
|
||||
static SocketWrapper * tcpConnection = nullptr;
|
||||
|
||||
/* this method exists for comments */
|
||||
static int valuesToReturn(int input) {
|
||||
return input;
|
||||
}
|
||||
|
||||
static int appStartHook(lua_State* luaState) {
|
||||
// Starting the app - prepare
|
||||
if(tcpConnection == nullptr) {
|
||||
auto nowMillis = std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now());
|
||||
auto millis = nowMillis.time_since_epoch().count();
|
||||
|
||||
char buffer[50];
|
||||
sprintf_s(buffer, "c:/temp/perun.%llu.log", millis);
|
||||
|
||||
tcpConnection = new SocketWrapper(std::string(buffer));
|
||||
}
|
||||
|
||||
lua_pushinteger(luaState, 1); // First return value: confirmation that app was started
|
||||
|
||||
return (1); // Set return to number of arguments returned
|
||||
return valuesToReturn(1);
|
||||
}
|
||||
|
||||
static int appEndHook(lua_State* luaState) {
|
||||
// Closing the app - clean up
|
||||
|
||||
tcpConnection.disconnect();
|
||||
if(tcpConnection != nullptr) {
|
||||
tcpConnection->disconnect();
|
||||
}
|
||||
|
||||
return (0); // No return values
|
||||
return valuesToReturn(0);
|
||||
}
|
||||
|
||||
static int tcpConnect(lua_State* luaState) {
|
||||
// Connect to the TCP socket
|
||||
|
||||
auto *host = new std::string(lua_tolstring(luaState, 1, 0));
|
||||
const int *port = new int(lua_tointeger(luaState, 2));
|
||||
tcpConnection.createConnection(host, port);
|
||||
if(tcpConnection != nullptr) {
|
||||
auto *host = new std::string(lua_tolstring(luaState, 1, 0));
|
||||
const int *port = new int(lua_tointeger(luaState, 2));
|
||||
|
||||
return (0); // No return values
|
||||
tcpConnection->createConnection(host, port);
|
||||
}
|
||||
|
||||
return valuesToReturn(0);
|
||||
}
|
||||
|
||||
static int tcpSend(lua_State* luaState) {
|
||||
// Send frame over TCP socket
|
||||
|
||||
tcpConnection.enqueueForSending(new std::string(lua_tolstring(luaState, 1, 0)));
|
||||
if(tcpConnection != nullptr) {
|
||||
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.getAndResetReconnected()); // Secound return value: information if there was recent reconnection to TCP server
|
||||
lua_pushinteger(luaState,
|
||||
tcpConnection->getFlagConnected()); // First return value: information if there is TCP connection
|
||||
lua_pushinteger(luaState,
|
||||
tcpConnection->getAndResetReconnected()); // Second return value: information if there was recent reconnection to TCP server
|
||||
} else {
|
||||
lua_pushinteger(luaState, 0);
|
||||
lua_pushinteger(luaState, 0);
|
||||
}
|
||||
|
||||
return (2); // Set return to number of arguments returned
|
||||
return valuesToReturn(2);
|
||||
}
|
||||
|
||||
extern "C" int __declspec(dllexport) luaopen_perun(lua_State * L) {
|
||||
|
||||
Reference in New Issue
Block a user