Code cleanup

This commit is contained in:
VladMordock
2021-02-22 16:57:49 +01:00
parent 1546ed2894
commit 585293ecf3
3 changed files with 33 additions and 43 deletions

View File

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

View File

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