diff --git a/CrossSockets/Base.h b/CrossSockets/Base.h index 068301b..2958338 100644 --- a/CrossSockets/Base.h +++ b/CrossSockets/Base.h @@ -48,6 +48,11 @@ along with this program. If not, see . #include #include #include +#include + + +using namespace std::this_thread; // sleep_for, sleep_until +using namespace std::chrono; // nanoseconds, system_clock, seconds #ifdef _WIN32 @@ -59,10 +64,11 @@ along with this program. If not, see . #endif // _WIN32 #ifdef __linux - +#error "Linux is not suppored yet, we still use chrono for timing :(" #endif // __linux - +using std::cout; +using std::endl; #define EOR std::cout << "[DEBUGING USE ONLY] PASSED LINE: " << __LINE__ << std::endl; @@ -70,51 +76,6 @@ along with this program. If not, see . #define _STD ::std:: #endif // !_STD -// Defines the color output for the console -namespace ColorM { - enum Code { - FG_RED = 31, - FG_GREEN = 32, - FG_BLUE = 34, - FG_YELLOW = 33, - FG_DEFAULT = 39, - FG_MAGENTA = 35, - BG_RED = 41, - BG_GREEN = 42, - BG_BLUE = 44, - BG_DEFAULT = 49 - - }; - class Modifier { - Code code; - public: - Modifier(Code pCode) : code(pCode) {} - friend std::ostream& - operator<<(std::ostream& os, const Modifier& mod) { - return os << "\033[" << mod.code << "m"; - } - }; -} -ColorM::Modifier redM(ColorM::FG_RED); -ColorM::Modifier greenM(ColorM::FG_GREEN); -ColorM::Modifier blueM(ColorM::FG_BLUE); -ColorM::Modifier magentaM(ColorM::FG_MAGENTA); -ColorM::Modifier yellowM(ColorM::FG_YELLOW); -ColorM::Modifier defM(ColorM::FG_DEFAULT); - -// defines the log types for outputting -enum Debug -{ - D_LOG = 1, - D_FILE, - D_DEBUG, - D_ERROR, - D_INFO, - D_WARNING, -}; - -_STD string debugstring[] = { "NULL", "LOG", "FILE", "DEBUG", "ERROR", "INFO", "WARNING" }; -ColorM::Modifier debugcolor[] = { defM, blueM, greenM, redM, redM, magentaM, yellowM }; // displays out the log @@ -134,13 +95,12 @@ ColorM::Modifier debugcolor[] = { defM, blueM, greenM, redM, redM, magentaM, yel }*/ -// Defining Helpful macros -#define FOR(varname, iterations) for (int varname = 0; varname < iterations; varname++) -#define xstr(x) str(x) -#define str(x) #x -#define glue(x, y) x ## y - - +struct Server +{ + std::string IP = "127.0.0.1"; + int InBoundPort = 56010; + int OutBound = 56050; +}; #endif // !Template \ No newline at end of file diff --git a/CrossSockets/CPSocket.cpp b/CrossSockets/CPSocket.cpp index 26dbbe5..099235f 100644 --- a/CrossSockets/CPSocket.cpp +++ b/CrossSockets/CPSocket.cpp @@ -51,25 +51,25 @@ CPSocket::~CPSocket() { bool SocketThreadTxDone = false; - displayout(D_WARNING, "CPSocket Closing..."); + out(D_WARNING, "CPSocket Closing..."); while (!SocketThreadRxDone && !SocketThreadTxDone && AliveServers.size() != 0) { if (SocketThreadRx.joinable()) { SocketThreadRx.join(); SocketThreadRxDone = true; - displayout(D_INFO, "[RX] Joined Main"); + out(D_INFO, "[RX] Joined Main"); } if (SocketThreadTx.joinable()) { SocketThreadTx.join(); SocketThreadTxDone = true; - displayout(D_INFO, "[TX] Joined Main"); + out(D_INFO, "[TX] Joined Main"); } } - displayout(D_WARNING, "CPSocket Closed!"); + out(D_WARNING, "CPSocket Closed!"); } // starting servers @@ -78,8 +78,8 @@ void CPSocket::StartClient() { SocketThreadRx = std::thread(&CPSocket::ClientRx, this); SocketThreadTx = std::thread(&CPSocket::ClientTx, this); - displayout(D_INFO, "Starting Socket Threads for Client"); - displayout(D_INFO, "Auth Number Created: \\"); + out(D_INFO, "Starting Socket Threads for Client"); + out(D_INFO, "Auth Number Created: \\"); std::cout << AUTH_NUMBER << std::endl << std::endl; @@ -104,23 +104,24 @@ void CPSocket::SendString(std::string RecStr) { std::vector CPSocket::GetString() { std::vector SendQ = RxQue; RxQue.clear(); + return {}; } void CPSocket::ClientRx() { RxStart: Sleep(120); - displayout(D_INFO, "[RX] STARTING ..."); - displayout(D_INFO, "[RX] Starting Connection: %s::%d", ConnectionProp.IP, ConnectionProp.InBoundPort); + out(D_INFO, "[RX] STARTING ..."); + out(D_INFO, "[RX] Starting Connection: %s::%d"); - displayout(D_INFO, "[RX] Windows Platform - Startig WinSock"); + out(D_INFO, "[RX] Windows Platform - Startig WinSock"); WSAData Data; WORD ver = MAKEWORD(2, 2); int WS_START = WSAStartup(ver, &Data); if (WS_START != 0) { - displayout(D_ERROR, "[RX] Unable To Start WinSock. ERROR #%d", WS_START); + out(D_ERROR, "[RX] Unable To Start WinSock. ERROR #%d"); return; } @@ -129,13 +130,13 @@ void CPSocket::ClientRx() { SOCKET sock = socket(AF_INET, SOCK_STREAM, 0); if (sock == INVALID_SOCKET) { - displayout(D_ERROR, "[RX] Can't create socket, Err #%s", WSAGetLastError()); + out(D_ERROR, "[RX] Can't create socket, Err #%s"); WSACleanup(); return; } - displayout(D_INFO, "[RX] Winsock Started!"); - displayout(D_INFO, "[RX] Starting hint structure..."); + out(D_INFO, "[RX] Winsock Started!"); + out(D_INFO, "[RX] Starting hint structure..."); // Fill in a hint structure sockaddr_in hint; @@ -145,15 +146,15 @@ void CPSocket::ClientRx() { // Connect to server - displayout(D_INFO, "[RX] Setup complete!"); + out(D_INFO, "[RX] Setup complete!"); int TryCount = 0; do { - displayout(D_INFO, "[RX] Conneting..."); + out(D_INFO, "[RX] Conneting..."); int connResult = connect(sock, (sockaddr*)&hint, sizeof(hint)); if (connResult == SOCKET_ERROR) { - displayout(D_WARNING, "[RX] Connection Failed, Retrying..."); + out(D_WARNING, "[RX] Connection Failed, Retrying..."); TryCount++; } else { @@ -167,22 +168,22 @@ void CPSocket::ClientRx() { Sleep(1000); - displayout(D_INFO, "[RX] CONNECTED"); + out(D_INFO, "[RX] CONNECTED"); IsConnectedRx = true; #ifndef DISABLE_AUTH // Start Authing if (!ThreadShouldStop) { - displayout(D_INFO, "[RX] Begining Auth..."); + out(D_INFO, "[RX] Begining Auth..."); while (!ThreadShouldStop) { std::string GetRsc = RxV(sock); - displayout(D_INFO, GetRsc.c_str()); + out(D_INFO, GetRsc.c_str()); if (GetRsc == AuthString) { - displayout(D_INFO, "[RX] Got Init String "); + out(D_INFO, "[RX] Got Init String "); IsAuthSSC = true; break; } @@ -210,17 +211,17 @@ void CPSocket::ClientTx() { TxStart: Sleep(100); - displayout(D_INFO, "[TX] STARTING ..."); - displayout(D_INFO, "[TX] Starting Connection: %s::%d", ConnectionProp.IP, ConnectionProp.InBoundPort); + out(D_INFO, "[TX] STARTING ..."); + out(D_INFO, "[TX] Starting Connection: %s::%d"); - displayout(D_INFO, "[TX] Windows Platform - Using WinSock"); + out(D_INFO, "[TX] Windows Platform - Using WinSock"); WSAData Data; WORD ver = MAKEWORD(2, 2); int WS_START = WSAStartup(ver, &Data); if (WS_START != 0) { - displayout(D_ERROR, "[TX] Unable To Start WinSock. ERROR #%d", WS_START); + out(D_ERROR, "[TX] Unable To Start WinSock. ERROR #%d"); return; } @@ -229,13 +230,13 @@ void CPSocket::ClientTx() { SOCKET sock = socket(AF_INET, SOCK_STREAM, 0); if (sock == INVALID_SOCKET) { - displayout(D_ERROR, "[TX] Can't create socket, Err #%s", WSAGetLastError()); + out(D_ERROR, "[TX] Can't create socket, Err #%s"); WSACleanup(); return; } - displayout(D_INFO, "[TX] Winsock Started!"); - displayout(D_INFO, "[TX] Starting hint structure..."); + out(D_INFO, "[TX] Winsock Started!"); + out(D_INFO, "[TX] Starting hint structure..."); // Fill in a hint structure sockaddr_in hint; @@ -245,15 +246,15 @@ void CPSocket::ClientTx() { // Connect to server - displayout(D_INFO, "[TX] Setup complete!"); + out(D_INFO, "[TX] Setup complete!"); int TryCount = 0; do { - displayout(D_INFO, "[TX] Conneting..."); + out(D_INFO, "[TX] Conneting..."); int connResult = connect(sock, (sockaddr*)&hint, sizeof(hint)); if (connResult == SOCKET_ERROR) { - displayout(D_WARNING, "[TX] Connection Failed, Retrying..."); + out(D_WARNING, "[TX] Connection Failed, Retrying..."); TryCount++; } else { @@ -267,13 +268,13 @@ void CPSocket::ClientTx() { Sleep(1000); - displayout(D_INFO, "[TX] CONNECTED"); + out(D_INFO, "[TX] CONNECTED"); IsConnectedRx = true; #ifndef DISABLE_AUTH // Start Authing if (!ThreadShouldStop) { - displayout(D_INFO, "[TX] Sending Auth"); + out(D_INFO, "[TX] Sending Auth"); while (!ThreadShouldStop) { TxV(sock, AuthString); @@ -282,13 +283,13 @@ void CPSocket::ClientTx() { } } } - displayout(D_INFO, "[TX] Done Auth"); + out(D_INFO, "[TX] Done Auth"); #endif // !DISABLE_AUTH while (!ThreadShouldStop) { if (TxQue.size() > 0) { TxV(sock, AuthString); - displayout(D_LOG, TxQue[0].c_str()); + out(D_LOG, TxQue[0].c_str()); TxQue.erase(TxQue.begin()); } } @@ -304,10 +305,10 @@ void CPSocket::ClientTx() { } void CPSocket::ServerManager() { - displayout(D_INFO, "Server Manager Started!"); + out(D_INFO, "Server Manager Started!"); while (!ThreadShouldStop || ThreadShouldRestart) { if (ServersFull) { - displayout(D_INFO, "Spawning New Server"); + out(D_INFO, "Spawning New Server"); srand(rand() * time(0)); int ClientId = (rand() * rand() / rand() + (rand() * rand() + rand() + rand()) / rand()) * rand() / rand(); std::thread CopyThreadRX(&CPSocket::ServerRx, this, ClientId); @@ -319,14 +320,14 @@ void CPSocket::ServerManager() { ServerConnectionCount++; ServersFull = false; - displayout(D_INFO, ("Connection Count: " + std::to_string(ServerConnectionCount)).c_str()); + out(D_INFO, ("Connection Count: " + std::to_string(ServerConnectionCount)).c_str()); } if (ServerActivity) { if (ServerThreadsRx.size() == ServerThreadsTx.size()) { for (int i = 0; i < ServerThreadsRx.size(); i++) { if (AliveServers[i] == -1 && ServerThreadsRx[i].joinable() && ServerThreadsTx[i].joinable()) { - displayout(D_INFO, "Deleting Old Server..."); + out(D_INFO, "Deleting Old Server..."); ServerThreadsRx[i].join(); ServerThreadsTx[i].join(); @@ -338,7 +339,7 @@ void CPSocket::ServerManager() { HostNames.erase(HostNames.begin() + i); ServerConnectionCount--; - displayout(D_INFO, ("New Connection Count: " + std::to_string(ServerConnectionCount)).c_str()); + out(D_INFO, ("New Connection Count: " + std::to_string(ServerConnectionCount)).c_str()); ThreadShouldStop = false; ThreadShouldRestart = false; @@ -346,11 +347,11 @@ void CPSocket::ServerManager() { } } else { - displayout(D_ERROR, "Thread Mix, Thread Counts do not match"); - displayout(D_WARNING, "Restarting Threads..."); + out(D_ERROR, "Thread Mix, Thread Counts do not match"); + out(D_WARNING, "Restarting Threads..."); - AliveServers.empty(); - HostNames.empty(); + AliveServers.clear(); + HostNames.clear(); ServerConnectionCount = 0; ServersFull = true; @@ -369,11 +370,11 @@ void CPSocket::ServerManager() { } if (ServerConnectionCount > 0) { - displayout(D_WARNING, "Killing all Servers"); + out(D_WARNING, "Killing all Servers"); } - AliveServers.empty(); - HostNames.empty(); + AliveServers.clear(); + HostNames.clear(); ServerConnectionCount = 0; ServersFull = true; @@ -389,10 +390,10 @@ void CPSocket::ServerManager() { void CPSocket::ServerRx(int id) { Sleep(100); - displayout(D_INFO, "[RX] STARTING ..."); - displayout(D_INFO, "[RX] Starting Connection: %s::%d", ConnectionProp.IP, ConnectionProp.InBoundPort); + out(D_INFO, "[RX] STARTING ..."); + out(D_INFO, "[RX] Starting Connection: %s::%d"); - displayout(D_INFO, "[RX] Windows Platform - Using WinSock"); + out(D_INFO, "[RX] Windows Platform - Using WinSock"); // Initialze winsock WSADATA wsData; @@ -401,7 +402,7 @@ void CPSocket::ServerRx(int id) { int wsOk = WSAStartup(ver, &wsData); if (wsOk != 0) { - displayout(D_ERROR, "[RX] Unable To Start WinSock. ERROR #%d"); + out(D_ERROR, "[RX] Unable To Start WinSock. ERROR #%d"); } @@ -409,12 +410,12 @@ void CPSocket::ServerRx(int id) { SOCKET listening = socket(AF_INET, SOCK_STREAM, 0); if (listening == INVALID_SOCKET) { - displayout(D_ERROR, "[RX] Unable To Start WinSock. ERROR #%d"); + out(D_ERROR, "[RX] Unable To Start WinSock. ERROR #%d"); } - displayout(D_INFO, "[RX] Winsock Started!"); - displayout(D_INFO, "[RX] Starting hint structure..."); + out(D_INFO, "[RX] Winsock Started!"); + out(D_INFO, "[RX] Starting hint structure..."); // txtd the ip address and port to a socket sockaddr_in hint; @@ -431,9 +432,9 @@ void CPSocket::ServerRx(int id) { sockaddr_in client; int clientSize = sizeof(client); - displayout(D_INFO, "[TX] Setup complete!"); + out(D_INFO, "[TX] Setup complete!"); - displayout(D_INFO, "[TX] Conneting..."); + out(D_INFO, "[TX] Conneting..."); SOCKET clientSocket = accept(listening, (sockaddr*)&client, &clientSize); @@ -447,16 +448,16 @@ void CPSocket::ServerRx(int id) { if (getnameinfo((sockaddr*)&client, sizeof(client), host, NI_MAXHOST, service, NI_MAXSERV, 0) == 0) { - displayout(D_INFO, (host + std::string(" Connected on Port: ") + service).c_str()); + out(D_INFO, (host + std::string(" Connected on Port: ") + service).c_str()); HostNames[LookUpArrayId(id)] = host; } else { inet_ntop(AF_INET, &client.sin_addr, host, NI_MAXHOST); - displayout(D_INFO, (host + std::string(" Connected on Port: ") + std::to_string(ntohs(client.sin_port))).c_str()); + out(D_INFO, (host + std::string(" Connected on Port: ") + std::to_string(ntohs(client.sin_port))).c_str()); } - displayout(D_INFO, "[RX] CONNECTED"); + out(D_INFO, "[RX] CONNECTED"); // Close listening socket closesocket(listening); @@ -469,16 +470,16 @@ void CPSocket::ServerRx(int id) { #ifndef DISABLE_AUTH // Start Authing if (!ThreadShouldStop) { - displayout(D_INFO, "[RX] Begining Auth..."); + out(D_INFO, "[RX] Begining Auth..."); while (!ThreadShouldStop) { std::string GetRsc = RxV(clientSocket); - displayout(D_INFO, GetRsc.c_str()); + out(D_INFO, GetRsc.c_str()); if (GetRsc == AuthString) { - displayout(D_INFO, "[RX] Got Init String "); + out(D_INFO, "[RX] Got Init String "); IsAuthSSC = true; break; } @@ -490,7 +491,7 @@ void CPSocket::ServerRx(int id) { while (!ThreadShouldStop) { std::string GetString = RxV(clientSocket); - displayout(D_INFO, GetString.c_str()); + out(D_INFO, GetString.c_str()); if (GetString.size() > 0) { RxQue.push_back(GetString); @@ -513,10 +514,10 @@ void CPSocket::ServerRx(int id) { void CPSocket::ServerTx(int id) { Sleep(115); - displayout(D_INFO, "[TX] STARTING ..."); - displayout(D_INFO, "[TX] Starting Connection: %s::%d", ConnectionProp.IP, ConnectionProp.InBoundPort); + out(D_INFO, "[TX] STARTING ..."); + out(D_INFO, "[TX] Starting Connection: %s::%d"); - displayout(D_INFO, "[TX] Windows Platform - Using WinSock"); + out(D_INFO, "[TX] Windows Platform - Using WinSock"); // Initialze winsock WSADATA wsData; @@ -525,7 +526,7 @@ void CPSocket::ServerTx(int id) { int wsOk = WSAStartup(ver, &wsData); if (wsOk != 0) { - displayout(D_ERROR, "[TX] Unable To Start WinSock. ERROR #%d"); + out(D_ERROR, "[TX] Unable To Start WinSock. ERROR #%d"); } @@ -533,11 +534,11 @@ void CPSocket::ServerTx(int id) { SOCKET listening = socket(AF_INET, SOCK_STREAM, 0); if (listening == INVALID_SOCKET) { - displayout(D_ERROR, "[TX] Unable To Start WinSock. ERROR #%d"); + out(D_ERROR, "[TX] Unable To Start WinSock. ERROR #%d"); } - displayout(D_INFO, "[TX] Winsock Started!"); - displayout(D_INFO, "[TX] Starting hint structure..."); + out(D_INFO, "[TX] Winsock Started!"); + out(D_INFO, "[TX] Starting hint structure..."); // txtd the ip address and port to a socket @@ -556,9 +557,9 @@ void CPSocket::ServerTx(int id) { sockaddr_in client; int clientSize = sizeof(client); - displayout(D_INFO, "[TX] Setup complete!"); + out(D_INFO, "[TX] Setup complete!"); - displayout(D_INFO, "[TX] Conneting..."); + out(D_INFO, "[TX] Conneting..."); SOCKET clientSocket = accept(listening, (sockaddr*)&client, &clientSize); @@ -572,16 +573,16 @@ void CPSocket::ServerTx(int id) { if (getnameinfo((sockaddr*)&client, sizeof(client), host, NI_MAXHOST, service, NI_MAXSERV, 0) == 0) { - displayout(D_INFO, (host + std::string(" Connected on Port: ") + service).c_str()); + out(D_INFO, (host + std::string(" Connected on Port: ") + service).c_str()); HostNames[LookUpArrayId(id)] = host; } else { inet_ntop(AF_INET, &client.sin_addr, host, NI_MAXHOST); - displayout(D_INFO, (host + std::string(" Connected on Port: ") + std::to_string(ntohs(client.sin_port))).c_str()); + out(D_INFO, (host + std::string(" Connected on Port: ") + std::to_string(ntohs(client.sin_port))).c_str()); } - displayout(D_INFO, "[TX] CONNECTED"); + out(D_INFO, "[TX] CONNECTED"); // Close listening socket closesocket(listening); @@ -594,7 +595,7 @@ void CPSocket::ServerTx(int id) { #ifndef DISABLE_AUTH // Start Authing if (!ThreadShouldStop) { - displayout(D_INFO, "[TX] Begining Auth..."); + out(D_INFO, "[TX] Begining Auth..."); while (!ThreadShouldStop) { @@ -613,7 +614,7 @@ void CPSocket::ServerTx(int id) { { if (TxQue.size() > 0) { TxV(clientSocket, TxQue[0]); - displayout(D_LOG, TxQue[0].c_str()); + out(D_LOG, TxQue[0].c_str()); TxQue.erase(TxQue.begin()); } } @@ -632,17 +633,17 @@ std::string CPSocket::RxV(SOCKET sock) { int bytesReceived = recv(sock, buf, bufferSize + 10, 0); if (bytesReceived == SOCKET_ERROR) { - displayout(D_ERROR, "Error in recv, Quitting Thread..."); + out(D_ERROR, "Error in recv, Quitting Thread..."); ThreadShouldStop = true; } if (bytesReceived == 0) { - displayout(D_WARNING, "Disconnected..."); + out(D_WARNING, "Disconnected..."); ThreadShouldStop = true; } else { - //displayout(D_DEBUG, std::to_string(bytesReceived).c_str()); + //out(D_DEBUG, std::to_string(bytesReceived).c_str()); } ThreadShouldRestart = true; return std::string(buf); diff --git a/CrossSockets/CPSocket.h b/CrossSockets/CPSocket.h index 53f415c..7c62bfc 100644 --- a/CrossSockets/CPSocket.h +++ b/CrossSockets/CPSocket.h @@ -37,15 +37,11 @@ along with this program. If not, see . #define Template #include "Base.h" +#include "displayout.h" + -struct Server -{ - std::string IP = "127.0.0.1"; - int InBoundPort = 56010; - int OutBound = 56050; -}; -class CPSocket +class CPSocket : private displayout { public: CPSocket(); @@ -100,11 +96,7 @@ class CPSocket void ServerTx(int); void ServerRx(int); - void displayout(int msgType, const char* text, ...) - { - std::cout << debugcolor[msgType] << text << debugcolor[0] << std::endl; - - } + int LookUpArrayId(int id) { for (int i = 0; i < AliveServers.size(); i++) { diff --git a/CrossSockets/CS_COM.cpp b/CrossSockets/CS_COM.cpp new file mode 100644 index 0000000..cab33ec --- /dev/null +++ b/CrossSockets/CS_COM.cpp @@ -0,0 +1,187 @@ +/*_____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ + |_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____| + _ ____ ____ _ _ ___ _ _ + | | / ___|_ __ ___ ___ ___/ ___| ___ ___| | _____| |_ ___ __ __/ _ \ / | | | + | | | | | '__/ _ \/ __/ __\___ \ / _ \ / __| |/ / _ \ __/ __| \ \ / / | | || | | | + | | | |___| | | (_) \__ \__ \___) | (_) | (__| < __/ |_\__ \ \ V /| |_| || | | | + | | \____|_| \___/|___/___/____/ \___/ \___|_|\_\___|\__|___/ \_/ \___(_)_| | | + |_| |_| + _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ + |_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____| + + +========================================================= + -Copyright (C) 2019 Neoa Software + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +========================================================= + +*/ + +#pragma once + +// ---------------- CODE ---------------- \\ + +#include "Base.h" +#include "CS_COM.h" + +CS_COM::CS_COM(SOCKET *sock, bool NoOutput) { + SendingThr = std::thread(&CS_COM::SendThread, this); + RecvingThr = std::thread(&CS_COM::RecvThread, this); + this->sock = sock; + + NoOut = NoOutput; + + if (!NoOut) Displ.out(D_INFO, "Stack Size of block is " + std::to_string(IO_SIZE) + " Bytes"); +} + +CS_COM::~CS_COM() { + KillThreads = true; + + + + SendingThr.join(); + RecvingThr.join(); + + +} + +void CS_COM::Send(std::string Send) { + std::vector BrokenUp; + + //BrokenUp.push_back("\h"); + + std::string Built; + for (int i = 0; i < Send.size(); i++) { + if (Built.length() < IO_SIZE) { + Built += Send[i]; + } + else { + BrokenUp.push_back(Built); + Built = Send[i]; + } + } + BrokenUp.push_back(Built); //+"\g"); + + + + + + for (auto i : BrokenUp) { + if(!NoOut) Displ.out(D_INFO, i); + //if(!NoOut) Displ.out(D_INFO, std::to_string(Built.length()) + " Bytes"); + + SendingQue.push_back(i); + } + +} + +std::vector CS_COM::Recv() { + + + if (RecvingQue.size() > 0) { + auto Sendout = RecvingQue; + RecvingQue.clear(); + + return Sendout; + } + + std::vector non; + return non; + +} + +void CS_COM::SendThread() { + while (!KillThreads) { + sleep_for(16ms); + for (int i = 0; i < SendingQue.size(); i++) { + send((SOCKET)*sock, SendingQue[i].c_str(), SendingQue[i].length() + 1, 0); + } + + SendingQue.clear(); + + } + + +} + +void CS_COM::RecvThread() { + + while (!KillThreads) { + sleep_for(16ms); + char buf[IO_SIZE + 20]; + ZeroMemory(buf, IO_SIZE + 20); + + // Wait for client to send data + int bytesReceived = recv((SOCKET)*sock, buf, IO_SIZE + 20, 0); + if (bytesReceived == SOCKET_ERROR) + { + if(!NoOut) Displ.out(D_ERROR, "Error in recv, Quitting Thread..."); + ComError = true; + break; + } + if (bytesReceived == 0) + { + if(!NoOut) Displ.out(D_WARNING, "Disconnected..."); + ComError = true; + break; + } + + std::string str = std::string(buf); + + if (str.size() == 0) + continue; + + /*else if (str[0] == '\h' && str.back() == '\g') { + std::string Mstr = str; + + Mstr.erase(Mstr.begin()); + Mstr.erase(Mstr.end()); + + + RecvingQue.push_back(Mstr); + } + + else if (str[0] == '\n' && str.back() != '\g') { + std::string Mstr = str; + + Mstr.erase(Mstr.begin()); + + HoldingPos += Mstr; + } + + else if (str[0] != '\n' && str.back() == '\g') { + std::string Mstr = str; + + Mstr.erase(Mstr.end()); + + RecvingQue.push_back(HoldingPos + Mstr); + + HoldingPos = ""; + + } + + else { + HoldingPos += str; + }*/ + + RecvingQue.push_back(str); + + if(!NoOut) Displ.out(D_INFO, str); + + + } + + KillThreads = true; +} \ No newline at end of file diff --git a/CrossSockets/CS_COM.h b/CrossSockets/CS_COM.h new file mode 100644 index 0000000..392ec0b --- /dev/null +++ b/CrossSockets/CS_COM.h @@ -0,0 +1,110 @@ +/*_____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ + |_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____| + _ ____ ____ _ _ ___ _ _ + | | / ___|_ __ ___ ___ ___/ ___| ___ ___| | _____| |_ ___ __ __/ _ \ / | | | + | | | | | '__/ _ \/ __/ __\___ \ / _ \ / __| |/ / _ \ __/ __| \ \ / / | | || | | | + | | | |___| | | (_) \__ \__ \___) | (_) | (__| < __/ |_\__ \ \ V /| |_| || | | | + | | \____|_| \___/|___/___/____/ \___/ \___|_|\_\___|\__|___/ \_/ \___(_)_| | | + |_| |_| + _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ + |_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____| + + +========================================================= + -Copyright (C) 2019 Neoa Software + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +========================================================= + +*/ + +#pragma once + +// ---------------- CODE ---------------- \\ + +#include "Base.h" +#include "displayout.h" + +#define GB * 1000000000 +#define MB * 1000000 +#define KB * 1000 + + +#define IO_SIZE 4 KB + + +class CS_COM +{ +public: + CS_COM(SOCKET* sock, bool NoOutput = false); + ~CS_COM(/*Global*/); + +//protected: + void Send(std::string Send); + std::vector Recv(); + + bool Error() { + return ComError; + } + +private: + displayout Displ; + + std::vector SendingQue; + std::vector RecvingQue; + + std::string HoldingPos; + size_t BytesGot; + + std::thread SendingThr; + std::thread RecvingThr; + + SOCKET *sock; + + bool ComError = false; + bool NoOut = false; + bool KillThreads = false; + + void SendThread(); + void RecvThread(); +}; + +/* + char buf[bufferSize + 10]; + ZeroMemory(buf, bufferSize + 10); + + // Wait for client to send data + int bytesReceived = recv(sock, buf, bufferSize + 10, 0); + if (bytesReceived == SOCKET_ERROR) + { + out(D_ERROR, "Error in recv, Quitting Thread..."); + ThreadShouldStop = true; + } + + if (bytesReceived == 0) + { + out(D_WARNING, "Disconnected..."); + ThreadShouldStop = true; + } + else { + //out(D_DEBUG, std::to_string(bytesReceived).c_str()); + } + ThreadShouldRestart = true; + return std::string(buf); +} + +void CPSocket::TxV(SOCKET COM, std::string Data) { + send(COM, Data.c_str(), Data.length() + 1, 0); +} +*/ \ No newline at end of file diff --git a/CrossSockets/CS_Client.cpp b/CrossSockets/CS_Client.cpp new file mode 100644 index 0000000..98a6fe7 --- /dev/null +++ b/CrossSockets/CS_Client.cpp @@ -0,0 +1,167 @@ +/*_____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ + |_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____| + _ ____ ____ _ _ ___ _ _ + | | / ___|_ __ ___ ___ ___/ ___| ___ ___| | _____| |_ ___ __ __/ _ \ / | | | + | | | | | '__/ _ \/ __/ __\___ \ / _ \ / __| |/ / _ \ __/ __| \ \ / / | | || | | | + | | | |___| | | (_) \__ \__ \___) | (_) | (__| < __/ |_\__ \ \ V /| |_| || | | | + | | \____|_| \___/|___/___/____/ \___/ \___|_|\_\___|\__|___/ \_/ \___(_)_| | | + |_| |_| + _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ + |_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____| + + +========================================================= + -Copyright (C) 2019 Neoa Software + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +========================================================= + +*/ + +#pragma once + +// ---------------- CODE ---------------- \\ + +#include "Base.h" +#include "CS_Client.h" + +CS_Client::CS_Client(int port, std::string Ip, int TryConnectionCount, bool AddStringBuildBack) { + Port = port; + IP = Ip; + BuildBack = AddStringBuildBack; + TryCount = TryConnectionCount; + + StartClient(); +} + +CS_Client::~CS_Client() { + StopClient(); +} + + +void CS_Client::StopClient() { + while (true) { + sleep_for(66ms); + if (ClientsThread.joinable()) { + ClientsThread.join(); + break; + } + } + dis.out(D_WARNING, "Stopped Threads"); +} + +void CS_Client::StartClient() { + dis.out(D_INFO, "Started Threads"); + + ClientsThread = std::thread(&CS_Client::ClientThread, this); + +} + +std::vector CS_Client::GetRec() { + return Recv; +} + +void CS_Client::Send(std::string data) { + Sendv.push_back(data); +} + + +void CS_Client::ClientThread() { +__START: + + Sleep(100); + dis.out(D_INFO, "Client STARTING ..."); + dis.out(D_INFO, "Client Starting Connection: %s::%d"); + + dis.out(D_INFO, "Client Windows Platform - Using WinSock"); + + WSAData Data; + WORD ver = MAKEWORD(2, 2); + + int WS_START = WSAStartup(ver, &Data); + if (WS_START != 0) { + dis.out(D_ERROR, "Client Unable To Start WinSock. ERROR #%d"); + + return; + } + + // Create The Socket + SOCKET sock = socket(AF_INET, SOCK_STREAM, 0); + if (sock == INVALID_SOCKET) + { + dis.out(D_ERROR, "Client Can't create socket, Err #%s"); + WSACleanup(); + return; + } + + dis.out(D_INFO, "Client Winsock Started!"); + dis.out(D_INFO, "Client Starting hint structure..."); + + // Fill in a hint structure + sockaddr_in hint; + hint.sin_family = AF_INET; + hint.sin_port = htons(Port); + inet_pton(AF_INET, IP.c_str(), &hint.sin_addr); + + // Connect to server + + dis.out(D_INFO, "Client Setup complete!"); + + int TryCounting = 0; + do { + sleep_for(33ms); + dis.out(D_INFO, "Client Conneting..."); + int connResult = connect(sock, (sockaddr*)&hint, sizeof(hint)); + if (connResult == SOCKET_ERROR) + { + dis.out(D_WARNING, "Client Connection Failed, Retrying..."); + TryCounting++; + } + else { + break; + } + if (TryCounting >= TryCount) { + StopAll = true; + dis.out(D_ERROR, "Client Connection Failed! Server Not Found! Stopping Threads..."); + return; + } + } while (!StopAll); + + + CS_COM com(&sock, false); + dis.out(D_INFO, "Client CONNECTED"); + + while (!StopAll) { + sleep_for(33ms); + + + for (auto i : com.Recv()) { + Recv.push_back(i); + } + + for (auto i : Sendv) { + com.Send(i); + } + + Sendv.clear(); + + if (com.Error()) { + dis.out(D_ERROR, "Error, restarting Client..."); + goto __START; + } + + } + + +} \ No newline at end of file diff --git a/CrossSockets/CS_Client.h b/CrossSockets/CS_Client.h new file mode 100644 index 0000000..4ed150f --- /dev/null +++ b/CrossSockets/CS_Client.h @@ -0,0 +1,67 @@ +/*_____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ + |_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____| + _ ____ ____ _ _ ___ _ _ + | | / ___|_ __ ___ ___ ___/ ___| ___ ___| | _____| |_ ___ __ __/ _ \ / | | | + | | | | | '__/ _ \/ __/ __\___ \ / _ \ / __| |/ / _ \ __/ __| \ \ / / | | || | | | + | | | |___| | | (_) \__ \__ \___) | (_) | (__| < __/ |_\__ \ \ V /| |_| || | | | + | | \____|_| \___/|___/___/____/ \___/ \___|_|\_\___|\__|___/ \_/ \___(_)_| | | + |_| |_| + _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ + |_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____| + + +========================================================= + -Copyright (C) 2019 Neoa Software + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +========================================================= + +*/ + +#pragma once + +// ---------------- CODE ---------------- \\ + +#include "Base.h" +#include "displayout.h" +#include "CS_COM.h" + + +class CS_Client +{ +public: + CS_Client(int port, std::string Ip, int TryConnectionCount = 10, bool AddStringBuildBack = false); + ~CS_Client(); + + void StopClient(); + void StartClient(); + + std::vector GetRec(); + void Send(std::string data); + + +private: + int Port; + std::string IP; + bool BuildBack; + int TryCount; + displayout dis; + std::thread ClientsThread; + std::vector Recv; + std::vector Sendv; + + bool StopAll = false; + + void ClientThread(); +}; diff --git a/CrossSockets/CS_Server.cpp b/CrossSockets/CS_Server.cpp new file mode 100644 index 0000000..8eedea8 --- /dev/null +++ b/CrossSockets/CS_Server.cpp @@ -0,0 +1,310 @@ +/*_____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ + |_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____| + _ ____ ____ _ _ ___ _ _ + | | / ___|_ __ ___ ___ ___/ ___| ___ ___| | _____| |_ ___ __ __/ _ \ / | | | + | | | | | '__/ _ \/ __/ __\___ \ / _ \ / __| |/ / _ \ __/ __| \ \ / / | | || | | | + | | | |___| | | (_) \__ \__ \___) | (_) | (__| < __/ |_\__ \ \ V /| |_| || | | | + | | \____|_| \___/|___/___/____/ \___/ \___|_|\_\___|\__|___/ \_/ \___(_)_| | | + |_| |_| + _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ + |_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____| + + +========================================================= + -Copyright (C) 2019 Neoa Software + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +========================================================= + +*/ + +#pragma once + +// ---------------- CODE ---------------- \\ + +#include "Base.h" +#include "CS_Server.h" +#include "CS_COM.h" + +CS_Server::CS_Server(int port, bool AddBuildBack, bool htmlHost) { + //ConnectionProp.IP = ip; + stringBuildback = AddBuildBack; + HtmlHost = htmlHost; + ConnectionProp.OutBound = port; + + dis.out(D_INFO, "Server Starting..."); + dis.out(D_INFO, "PORT = " + std::to_string(port)); + + SMT = std::thread(&CS_Server::ServerManager, this); +} + +CS_Server::~CS_Server() { + StopAll = true; + + SMT.join(); +} + +std::vector CS_Server::GetAllClientNames() { + std::vector Ve; + for (auto i : ServerQue) { + if (i.Name != "NO CONNECTION") { + Ve.push_back(i.Name); + } + } + return Ve; +} + +std::vector* CS_Server::GetAllClientSerVect() { + std::vector Ve; + for (auto i : ServerQue) { + if (i.Name != "NO CONNECTION") { + Ve.push_back(i); + } + } + return &Ve; +} + +ServVect* CS_Server::GetClientSerVect(std::string Name) { + ServVect Ve; + for (auto i : ServerQue) { + if (i.Name == Name) { + Ve = i; + break; + } + } + return &Ve; +} + +void CS_Server::SendToClient(std::string Name, std::string Send) { + for (auto& i : ServerQue) { + if (i.Name == Name) + i.SendingQue.push_back(Send); + } +} + +void CS_Server::SendToAll(std::string Send) { + for (auto& i : ServerQue) { + if (i.Name != "NO CONNECTION") + i.SendingQue.push_back(Send); + } + +} + +void CS_Server::HostHtml(std::string Html) { + + HtmlF = Html; + +} + +void CS_Server::DisconnectClient(std::string Name) { + for (auto& i : ServerQue) { + if (i.Name == Name) { + i.shutdown = true; + } + } +} + +void CS_Server::DisconnectAll() { + for (auto& i : ServerQue) { + i.shutdown = true; + } +} + +void CS_Server::SetConnections(bool connect) { + Disconnect = connect; +} + + +void CS_Server::ServerManager() { + if(!HtmlHost) dis.out(D_INFO, "Server Manager has started!"); + + while (!StopAll) { + sleep_for(33ms); + + if (ServersFull && !Disconnect) { + if(!HtmlHost) dis.out(D_INFO, "Spawning New Thread Pool"); + srand(time(0)); + int ClientId = (rand() * rand() / rand() + (rand() * rand() + rand() + rand()) / rand()) * rand() / rand(); + std::thread CopyThread(&CS_Server::ServerThread, this, ClientId); + ThreadPool.push_back(std::move(CopyThread)); + SocketIds.push_back(ClientId); + ServerQue.push_back({ "NO CONNECTION" }); + + ConnectionCount++; + ServersFull = false; + if(!HtmlHost) dis.out(D_INFO, "New Connection Count = " + std::to_string(ConnectionCount - 1)); + } + if (ServerActivity) { + for (int i = 0; i < ThreadPool.size(); i++) { + if (SocketIds[i] == -1 && ThreadPool[i].joinable()) { + if(!HtmlHost) dis.out(D_INFO, "Deleting Old Server..."); + + ThreadPool[i].join(); + + ThreadPool.erase(ThreadPool.begin() + i); + SocketIds.erase(SocketIds.begin() + i); + ServerQue.erase(ServerQue.begin() + i); + + ConnectionCount--; + + if(!HtmlHost) dis.out(D_INFO, "New Connection Count = " + std::to_string(ConnectionCount - 1)); + } + } + ServerActivity = false; + } + if (HtmlHost) { + + std::string HtmlHeader = R"(HTTP/1.0 200 OK +Date: Thu, 06 Aug 1998 12:00:15 GMT +Server: CrossSockets/0.1.5 (Unix) +Last-Modified: Mon, 22 Jun 1998 +Content-Length: )" + std::to_string(HtmlF.size()) + R"( +Content-Type: text/html)"; + + for (int i = 0; i < ServerQue.size(); i++) { + if (ServerQue[i].Name != "NO CONNECTION") + ServerQue[i].SendingQue.push_back(HtmlHeader + "\n\n" + HtmlF); + } + + } + } +} + +void CS_Server::ServerThread(int id) { + Sleep(100); + + if(!HtmlHost) dis.out(D_INFO, std::to_string(id) + " STARTING ..."); + if(!HtmlHost) dis.out(D_INFO, std::to_string(id) + " Starting Connection: %s::%d"); + + if(!HtmlHost) dis.out(D_INFO, std::to_string(id) + " Windows Platform - Using WinSock"); + + // Initialze winsock + WSADATA wsData; + WORD ver = MAKEWORD(2, 2); + + int wsOk = WSAStartup(ver, &wsData); + if (wsOk != 0) + { + dis.out(D_ERROR, std::to_string(id) + " Unable To Start WinSock. ERROR #%d"); + + } + + // Create a socket + SOCKET listening = socket(AF_INET, SOCK_STREAM, 0); + if (listening == INVALID_SOCKET) + { + dis.out(D_ERROR, std::to_string(id) + " Unable To Start WinSock. ERROR #%d"); + + } + + if(!HtmlHost) dis.out(D_INFO, std::to_string(id) + " Winsock Started!"); + if(!HtmlHost) dis.out(D_INFO, std::to_string(id) + " Starting hint structure..."); + + // txtd the ip address and port to a socket + sockaddr_in hint; + hint.sin_family = AF_INET; + hint.sin_port = htons(ConnectionProp.OutBound); + hint.sin_addr.S_un.S_addr = INADDR_ANY; // Could also use inet_pton .... + + bind(listening, (sockaddr*)&hint, sizeof(hint)); + + // Tell Winsock the socket is for listening + listen(listening, SOMAXCONN); + + // Wait for a connection + sockaddr_in client; + int clientSize = sizeof(client); + + if(!HtmlHost) dis.out(D_INFO, std::to_string(id) + " Setup complete!"); + + if(!HtmlHost) dis.out(D_INFO, std::to_string(id) + " Conneting..."); + + SOCKET clientSocket = accept(listening, (sockaddr*)&client, &clientSize); + + char host[NI_MAXHOST]; // Client's remote name + char service[NI_MAXSERV]; // Service (i.e. port) the client is connect on + + ZeroMemory(host, NI_MAXHOST); // same as memset(host, 0, NI_MAXHOST); + ZeroMemory(service, NI_MAXSERV); + + + + if (getnameinfo((sockaddr*)&client, sizeof(client), host, NI_MAXHOST, service, NI_MAXSERV, 0) == 0) + { + if(!HtmlHost) dis.out(D_INFO, (host + std::string(" Connected on Port: ") + service).c_str()); + } + else + { + inet_ntop(AF_INET, &client.sin_addr, host, NI_MAXHOST); + if(!HtmlHost) dis.out(D_INFO, (host + std::string(" Connected on Port: ") + std::to_string(ntohs(client.sin_port))).c_str()); + } + + ServerQue[LookUpArrayId(id)].Name = std::to_string(id) + std::string(host); + + if(!HtmlHost) dis.out(D_INFO, std::to_string(id) + " CONNECTED"); + + // Close listening socket + closesocket(listening); + + // While loop: accept and echo message back to client + + ServersFull = true; + + CS_COM com(&clientSocket, HtmlHost); + + bool kill = false; + + while (!kill && !StopAll) { + sleep_for(33ms); + + if (com.Error()) { + if (!HtmlHost) dis.out(D_ERROR, std::to_string(id) + " COM ERROR"); + break; + } + + int index = LookUpArrayId(id); + for (auto &i : com.Recv()) { + ServerQue[index].RecvingQue.push_back(i); + } + + for (auto &i : ServerQue[index].SendingQue) { + com.Send(i); + } + + ServerQue[index].SendingQue.clear(); + + if (ServerQue[index].shutdown) { + kill = true; + break; + } + + } + + + + // Close the socket + closesocket(clientSocket); + + // Cleanup winsock + WSACleanup(); + + + + SocketIds[LookUpArrayId(id)] = -1; + ServerActivity = true; + + +} + + diff --git a/CrossSockets/CS_Server.h b/CrossSockets/CS_Server.h new file mode 100644 index 0000000..64b52d3 --- /dev/null +++ b/CrossSockets/CS_Server.h @@ -0,0 +1,99 @@ +/*_____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ + |_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____| + _ ____ ____ _ _ ___ _ _ + | | / ___|_ __ ___ ___ ___/ ___| ___ ___| | _____| |_ ___ __ __/ _ \ / | | | + | | | | | '__/ _ \/ __/ __\___ \ / _ \ / __| |/ / _ \ __/ __| \ \ / / | | || | | | + | | | |___| | | (_) \__ \__ \___) | (_) | (__| < __/ |_\__ \ \ V /| |_| || | | | + | | \____|_| \___/|___/___/____/ \___/ \___|_|\_\___|\__|___/ \_/ \___(_)_| | | + |_| |_| + _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ + |_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____| + + +========================================================= + -Copyright (C) 2019 Neoa Software + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +========================================================= + +*/ + +#pragma once + +// ---------------- CODE ---------------- \\ + +#include "Base.h" +#include "displayout.h" + +struct ServVect { + std::string Name; + std::vector RecvingQue; + std::vector SendingQue; + bool shutdown = false; +}; + + + +class CS_Server +{ +public: + CS_Server(int port, bool AddBuildBack = false, bool htmlHost = false); + ~CS_Server(); + + std::vector GetAllClientNames(); + std::vector* GetAllClientSerVect(); + ServVect* GetClientSerVect(std::string Name); + void HostHtml(std::string Html); + + void SendToClient(std::string Name, std::string Send); + void SendToAll(std::string Send); + + void DisconnectClient(std::string Name); + void SetConnections(bool connect); + void DisconnectAll(); + + + int LookUpArrayId(int id) { + for (int i = 0; i < SocketIds.size(); i++) { + if (SocketIds[i] == id) { + return i; + } + } + return -1; + } + + +private: + std::vector ThreadPool ; + std::thread SMT; // Server Manager Thread + std::vector ClientNames ; + std::vector SocketIds ; + std::vector ServerQue ; + int ConnectionCount = 0; + bool ServerActivity = false; + bool ServersFull = true; + bool StopAll = false; + bool stringBuildback = false; + bool HtmlHost = false; + bool Disconnect = false; + Server ConnectionProp ; + displayout dis ; + std::string HtmlF ; + + + + void ServerManager (); + void ServerThread (int id); +}; + diff --git a/CrossSockets/CS_Threads.cpp b/CrossSockets/CS_Threads.cpp new file mode 100644 index 0000000..e638944 --- /dev/null +++ b/CrossSockets/CS_Threads.cpp @@ -0,0 +1,37 @@ +/*_____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ + |_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____| + _ ____ ____ _ _ ___ _ _ + | | / ___|_ __ ___ ___ ___/ ___| ___ ___| | _____| |_ ___ __ __/ _ \ / | | | + | | | | | '__/ _ \/ __/ __\___ \ / _ \ / __| |/ / _ \ __/ __| \ \ / / | | || | | | + | | | |___| | | (_) \__ \__ \___) | (_) | (__| < __/ |_\__ \ \ V /| |_| || | | | + | | \____|_| \___/|___/___/____/ \___/ \___|_|\_\___|\__|___/ \_/ \___(_)_| | | + |_| |_| + _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ + |_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____| + + +========================================================= + -Copyright (C) 2019 Neoa Software + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +========================================================= + +*/ + +#pragma once + +// ---------------- CODE ---------------- \\ + +#include "Base.h" +#include "CS_Threads.h" diff --git a/CrossSockets/CS_Threads.h b/CrossSockets/CS_Threads.h new file mode 100644 index 0000000..979c1f4 --- /dev/null +++ b/CrossSockets/CS_Threads.h @@ -0,0 +1,41 @@ +/*_____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ + |_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____| + _ ____ ____ _ _ ___ _ _ + | | / ___|_ __ ___ ___ ___/ ___| ___ ___| | _____| |_ ___ __ __/ _ \ / | | | + | | | | | '__/ _ \/ __/ __\___ \ / _ \ / __| |/ / _ \ __/ __| \ \ / / | | || | | | + | | | |___| | | (_) \__ \__ \___) | (_) | (__| < __/ |_\__ \ \ V /| |_| || | | | + | | \____|_| \___/|___/___/____/ \___/ \___|_|\_\___|\__|___/ \_/ \___(_)_| | | + |_| |_| + _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ + |_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____| + + +========================================================= + -Copyright (C) 2019 Neoa Software + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +========================================================= + +*/ + +#pragma once + +// ---------------- CODE ---------------- \\ + +#include "Base.h" + +class CS_Threads +{ +}; + diff --git a/CrossSockets/CrossSockets.cpp b/CrossSockets/CrossSockets.cpp new file mode 100644 index 0000000..52736df --- /dev/null +++ b/CrossSockets/CrossSockets.cpp @@ -0,0 +1,37 @@ +/*_____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ + |_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____| + _ ____ ____ _ _ ___ _ _ + | | / ___|_ __ ___ ___ ___/ ___| ___ ___| | _____| |_ ___ __ __/ _ \ / | | | + | | | | | '__/ _ \/ __/ __\___ \ / _ \ / __| |/ / _ \ __/ __| \ \ / / | | || | | | + | | | |___| | | (_) \__ \__ \___) | (_) | (__| < __/ |_\__ \ \ V /| |_| || | | | + | | \____|_| \___/|___/___/____/ \___/ \___|_|\_\___|\__|___/ \_/ \___(_)_| | | + |_| |_| + _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ + |_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____| + + +========================================================= + -Copyright (C) 2019 Neoa Software + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +========================================================= + +*/ + +#pragma once + +// ---------------- CODE ---------------- \\ + +#include "Base.h" +#include "CrossSockets.h" diff --git a/CrossSockets/CrossSockets.h b/CrossSockets/CrossSockets.h new file mode 100644 index 0000000..2bb82d4 --- /dev/null +++ b/CrossSockets/CrossSockets.h @@ -0,0 +1,41 @@ +/*_____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ + |_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____| + _ ____ ____ _ _ ___ _ _ + | | / ___|_ __ ___ ___ ___/ ___| ___ ___| | _____| |_ ___ __ __/ _ \ / | | | + | | | | | '__/ _ \/ __/ __\___ \ / _ \ / __| |/ / _ \ __/ __| \ \ / / | | || | | | + | | | |___| | | (_) \__ \__ \___) | (_) | (__| < __/ |_\__ \ \ V /| |_| || | | | + | | \____|_| \___/|___/___/____/ \___/ \___|_|\_\___|\__|___/ \_/ \___(_)_| | | + |_| |_| + _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ + |_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____| + + +========================================================= + -Copyright (C) 2019 Neoa Software + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +========================================================= + +*/ + +#pragma once + +// ---------------- CODE ---------------- \\ + +#include "Base.h" + +class CrossSockets +{ +}; + diff --git a/CrossSockets/CrossSockets.vcxproj b/CrossSockets/CrossSockets.vcxproj index fd37e8a..900358f 100644 --- a/CrossSockets/CrossSockets.vcxproj +++ b/CrossSockets/CrossSockets.vcxproj @@ -87,10 +87,12 @@ true _DEBUG;_CONSOLE;%(PreprocessorDefinitions) true + stdcpplatest Console true + /FORCE:MULTIPLE %(AdditionalOptions) @@ -103,6 +105,7 @@ Console true + /FORCE:MULTIPLE %(AdditionalOptions) @@ -141,12 +144,20 @@ + + + + + + + + diff --git a/CrossSockets/CrossSockets.vcxproj.filters b/CrossSockets/CrossSockets.vcxproj.filters index 8923534..894094c 100644 --- a/CrossSockets/CrossSockets.vcxproj.filters +++ b/CrossSockets/CrossSockets.vcxproj.filters @@ -15,28 +15,52 @@ - - Source Files - Source Files Source Files + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + Source Files + Header Files - - Header Files - Header Files Header Files + + Header Files + + + Header Files + + + Header Files + + + Header Files + + + Header Files + \ No newline at end of file diff --git a/CrossSockets/Source.cpp b/CrossSockets/Source.cpp index 2cbbe6e..59ef820 100644 --- a/CrossSockets/Source.cpp +++ b/CrossSockets/Source.cpp @@ -1,20 +1,71 @@ -#include "CPSocket.h" +#include "CrossSockets.h" +#include "displayout.h" +#include "CS_COM.h" +#include "CS_Server.h" +#include "CS_Client.h" //#define DISABLE_AUTH 0 /*If you want to disable the SSC */ - +/* int main() { - CPSocket socket("127.0.0.1", 56040, 56000); - //socket.StartClient(); - socket.StartServer(); + + CS_Server ser(80, false, true); + CS_Server SSS(567); + + + + while (true) { + Sleep(1000); + + static int i = 0; + i++; + + std::string HoldingStr = ""; + + for (auto i : AllLog) { + HoldingStr += "

" + i + "

\n"; + } + + std::string NewHtml = R"( + + + +

CrossSockets v0.1

+

Time in seconds : )" + std::to_string(i) + R"(

+)" + HoldingStr + R"( + +)"; + + ser.HostHtml(NewHtml); + + + //ser.SendToAll(Html); } +}*/ + +int main() { + CS_Client cl(56012, "127.0.0.1"); + + + + while (true) { + sleep_for(1s); + + std::string str = "VER::"; + + + cl.Send(str); - std::cout << "Sure" << std::endl; - socket.~CPSocket(); + + + } + + + } \ No newline at end of file diff --git a/CrossSockets/Template.h b/CrossSockets/Template.h index 74d79b7..ff49b74 100644 --- a/CrossSockets/Template.h +++ b/CrossSockets/Template.h @@ -33,6 +33,8 @@ along with this program. If not, see . // ---------------- CODE ---------------- \\ +#include "Base.h" + #ifndef Template #define Template diff --git a/CrossSockets/displayout.cpp b/CrossSockets/displayout.cpp index 57f51ff..353134d 100644 --- a/CrossSockets/displayout.cpp +++ b/CrossSockets/displayout.cpp @@ -1 +1,94 @@ +/*_____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ + |_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____| + _ ____ ____ _ _ ___ _ _ + | | / ___|_ __ ___ ___ ___/ ___| ___ ___| | _____| |_ ___ __ __/ _ \ / | | | + | | | | | '__/ _ \/ __/ __\___ \ / _ \ / __| |/ / _ \ __/ __| \ \ / / | | || | | | + | | | |___| | | (_) \__ \__ \___) | (_) | (__| < __/ |_\__ \ \ V /| |_| || | | | + | | \____|_| \___/|___/___/____/ \___/ \___|_|\_\___|\__|___/ \_/ \___(_)_| | | + |_| |_| + _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ + |_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____| + + +========================================================= + -Copyright (C) 2019 Neoa Software + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +========================================================= + +*/ + +#pragma once + +// ---------------- CODE ---------------- \\ + #include "displayout.h" + + +displayout::displayout() { + // Init the display thread + + ThreadDisplay = std::thread(&displayout::Threaded_Display, this); + + +} + +displayout::~displayout() { + // Join the main thread from here + SetToQuit = true; + + // if it takes too long for the thread to join then we can hang here + /// TODO fix this hang + while (ThreadDisplay.joinable()) { + ThreadDisplay.join(); + } + +} + +void displayout::out(int enumTypeColor, std::string ToDisplay) { + auto ThreadPart = PreDisplayControl; + + D_COLOR::ColorM::Modifier ModColor[] = { D_COLOR::defM, D_COLOR::blueM, D_COLOR::greenM, D_COLOR::redM, D_COLOR::redM, D_COLOR::magentaM, D_COLOR::yellowM }; + + D_COLOR::__OBJSTRING ColorBack = { ToDisplay, D_COLOR::debugstring[enumTypeColor], ModColor[enumTypeColor]}; + + ThreadPart.push_back(ColorBack); + + PreDisplayControl = ThreadPart; +} + +void displayout::Threaded_Display() { + // Start the init for the display thread that will be displaying all content from a thread(s) + while (!SetToQuit) { + sleep_for(66ms); + + if (!__IsDisplaying) { + auto ThreadPart = PreDisplayControl; + PreDisplayControl.clear(); + + if (ThreadPart.size() > 0) { + __IsDisplaying = true; + + for (auto i : ThreadPart) { + if (i.Message.size() > 0) { + std::cout << i.DebugType << "[" << i.Prompt << "]: " << i.Message << D_COLOR::defM << std::endl; + AllLog.push_back(i.Message); + } + } + __IsDisplaying = false; + } + } + } + +} \ No newline at end of file diff --git a/CrossSockets/displayout.h b/CrossSockets/displayout.h index 2615e9a..9e0003e 100644 --- a/CrossSockets/displayout.h +++ b/CrossSockets/displayout.h @@ -69,7 +69,62 @@ along with this program. If not, see . #endif std::atomic __IsDisplaying = false; +std::vector AllLog; +enum Debug_D +{ + D_DEF = 0, + D_LOG, + D_FILE, + D_DEBUG, + D_ERROR, + D_INFO, + D_WARNING, +}; + +namespace D_COLOR { + // Defines the color output for the console + namespace ColorM { + enum Code { + FG_RED = 31, + FG_GREEN = 32, + FG_BLUE = 34, + FG_YELLOW = 33, + FG_DEFAULT = 39, + FG_MAGENTA = 35, + BG_RED = 41, + BG_GREEN = 42, + BG_BLUE = 44, + BG_DEFAULT = 49 + + }; + class Modifier { + Code code; + public: + Modifier(Code pCode) : code(pCode) {} + friend std::ostream& + operator<<(std::ostream& os, const Modifier& mod) { + return os << "\033[" << mod.code << "m"; + } + }; + } + ColorM::Modifier redM(ColorM::FG_RED); + ColorM::Modifier greenM(ColorM::FG_GREEN); + ColorM::Modifier blueM(ColorM::FG_BLUE); + ColorM::Modifier magentaM(ColorM::FG_MAGENTA); + ColorM::Modifier yellowM(ColorM::FG_YELLOW); + ColorM::Modifier defM(ColorM::FG_DEFAULT); + + std::string debugstring[] = { "NULL", "LOG", "FILE", "DEBUG", "ERROR", "INFO", "WARNING" }; // Convert the Color to a readable displayname + + typedef struct __ObjectDisplayColorString { + std::string Message; + std::string Prompt; + + ColorM::Modifier DebugType; + + } __OBJSTRING; +} class displayout { @@ -86,10 +141,14 @@ class displayout #endif protected: - - + private: - std::atomic> PreDisplayControl; + std::vector PreDisplayControl; + std::thread ThreadDisplay; + + bool SetToQuit = false; + + void Threaded_Display(); };