From 36feb5bc5784e4fc2219a289d4edc8b81d0d5f4a Mon Sep 17 00:00:00 2001 From: Main Menu Date: Mon, 16 Mar 2020 16:59:22 -0500 Subject: [PATCH 1/4] Fixed Displayout --- CrossSockets/Base.h | 45 --------- CrossSockets/CPSocket.cpp | 151 +++++++++++++++--------------- CrossSockets/CPSocket.h | 9 +- CrossSockets/CrossSockets.vcxproj | 2 + CrossSockets/Source.cpp | 5 + CrossSockets/displayout.cpp | 121 ++++++++++++++++++++++++ CrossSockets/displayout.h | 68 +++++++++++++- 7 files changed, 272 insertions(+), 129 deletions(-) diff --git a/CrossSockets/Base.h b/CrossSockets/Base.h index 068301b..a1e9db9 100644 --- a/CrossSockets/Base.h +++ b/CrossSockets/Base.h @@ -70,51 +70,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 diff --git a/CrossSockets/CPSocket.cpp b/CrossSockets/CPSocket.cpp index 26dbbe5..fae1a8b 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,8 +347,8 @@ 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(); @@ -369,7 +370,7 @@ void CPSocket::ServerManager() { } if (ServerConnectionCount > 0) { - displayout(D_WARNING, "Killing all Servers"); + out(D_WARNING, "Killing all Servers"); } AliveServers.empty(); @@ -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..10cccf3 100644 --- a/CrossSockets/CPSocket.h +++ b/CrossSockets/CPSocket.h @@ -37,6 +37,7 @@ along with this program. If not, see . #define Template #include "Base.h" +#include "displayout.h" struct Server { @@ -45,7 +46,7 @@ struct Server int OutBound = 56050; }; -class CPSocket +class CPSocket : private displayout { public: CPSocket(); @@ -100,11 +101,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/CrossSockets.vcxproj b/CrossSockets/CrossSockets.vcxproj index fd37e8a..27736ff 100644 --- a/CrossSockets/CrossSockets.vcxproj +++ b/CrossSockets/CrossSockets.vcxproj @@ -87,10 +87,12 @@ true _DEBUG;_CONSOLE;%(PreprocessorDefinitions) true + stdcpplatest Console true + /FORCE:MULTIPLE %(AdditionalOptions) diff --git a/CrossSockets/Source.cpp b/CrossSockets/Source.cpp index 2cbbe6e..d949476 100644 --- a/CrossSockets/Source.cpp +++ b/CrossSockets/Source.cpp @@ -1,9 +1,11 @@ #include "CPSocket.h" +#include "displayout.h" //#define DISABLE_AUTH 0 /*If you want to disable the SSC */ int main() { + CPSocket socket("127.0.0.1", 56040, 56000); //socket.StartClient(); @@ -16,5 +18,8 @@ int main() { std::cout << "Sure" << std::endl; socket.~CPSocket(); + + + } \ No newline at end of file diff --git a/CrossSockets/displayout.cpp b/CrossSockets/displayout.cpp index 57f51ff..4cbb27b 100644 --- a/CrossSockets/displayout.cpp +++ b/CrossSockets/displayout.cpp @@ -1 +1,122 @@ +/*_____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ + |_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____| + _ ____ ____ _ _ ___ _ _ + | | / ___|_ __ ___ ___ ___/ ___| ___ ___| | _____| |_ ___ __ __/ _ \ / | | | + | | | | | '__/ _ \/ __/ __\___ \ / _ \ / __| |/ / _ \ __/ __| \ \ / / | | || | | | + | | | |___| | | (_) \__ \__ \___) | (_) | (__| < __/ |_\__ \ \ 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" + +/* Use as template for building of the cpp class def +std::atomic __IsDisplaying = false; + +class displayout +{ +public: + + displayout(); + ~displayout(); + + // Check if we have color enabled +#if D_DISABLE_COLOR == 0 + void out(int enumTypeColor, std::string ToDisplay); +#else + void out(std::string ToDisplay); +#endif + +protected: + + +private: + std::atomic> PreDisplayControl; + + std::thread ThreadDisplay; + bool SetToQuit = false; + + void Threaded_Display(); + +}; +*/ + +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) { + + auto ThreadPart = PreDisplayControl; + PreDisplayControl.empty(); + + + if (ThreadPart.size() > 0 && !__IsDisplaying) { + __IsDisplaying = true; + + for (auto i : ThreadPart) { + std::cout << i.DebugType << "[" << i.Prompt << "]: " << i.Message << D_COLOR::defM << std::endl; + } + + __IsDisplaying = false; + } + + } + +} \ No newline at end of file diff --git a/CrossSockets/displayout.h b/CrossSockets/displayout.h index 2615e9a..6d782c0 100644 --- a/CrossSockets/displayout.h +++ b/CrossSockets/displayout.h @@ -70,6 +70,64 @@ along with this program. If not, see . std::atomic __IsDisplaying = false; +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 + ColorM::Modifier debugcolor[] = { D_COLOR::defM, D_COLOR::blueM, D_COLOR::greenM, D_COLOR::redM, D_COLOR::redM, D_COLOR::magentaM, D_COLOR::yellowM }; // Convert the Color to a color name + + typedef struct __ObjectDisplayColorString { + std::string Message; + std::string Prompt; + + ColorM::Modifier DebugType; + + } __OBJSTRING; + + + +} class displayout { @@ -86,10 +144,14 @@ class displayout #endif protected: - - + private: - std::atomic> PreDisplayControl; + std::vector PreDisplayControl; + std::thread ThreadDisplay; + + bool SetToQuit = false; + + void Threaded_Display(); }; From 8c11a4246727d16559031130e7754c65d6338ef9 Mon Sep 17 00:00:00 2001 From: Main Menu Date: Mon, 23 Mar 2020 20:38:25 -0500 Subject: [PATCH 2/4] Added Some Files :p --- CrossSockets/CS_COM.cpp | 42 ++++++++++++ CrossSockets/CS_COM.h | 81 +++++++++++++++++++++++ CrossSockets/CS_Client.cpp | 37 +++++++++++ CrossSockets/CS_Client.h | 42 ++++++++++++ CrossSockets/CS_Server.cpp | 37 +++++++++++ CrossSockets/CS_Server.h | 41 ++++++++++++ CrossSockets/CS_Threads.cpp | 37 +++++++++++ CrossSockets/CS_Threads.h | 41 ++++++++++++ CrossSockets/CrossSockets.cpp | 37 +++++++++++ CrossSockets/CrossSockets.h | 41 ++++++++++++ CrossSockets/CrossSockets.vcxproj | 10 +++ CrossSockets/CrossSockets.vcxproj.filters | 42 ++++++++++-- CrossSockets/Source.cpp | 15 ++--- CrossSockets/Template.h | 2 + CrossSockets/displayout.cpp | 30 --------- CrossSockets/displayout.h | 4 -- 16 files changed, 488 insertions(+), 51 deletions(-) create mode 100644 CrossSockets/CS_COM.cpp create mode 100644 CrossSockets/CS_COM.h create mode 100644 CrossSockets/CS_Client.cpp create mode 100644 CrossSockets/CS_Client.h create mode 100644 CrossSockets/CS_Server.cpp create mode 100644 CrossSockets/CS_Server.h create mode 100644 CrossSockets/CS_Threads.cpp create mode 100644 CrossSockets/CS_Threads.h create mode 100644 CrossSockets/CrossSockets.cpp create mode 100644 CrossSockets/CrossSockets.h diff --git a/CrossSockets/CS_COM.cpp b/CrossSockets/CS_COM.cpp new file mode 100644 index 0000000..39a28fb --- /dev/null +++ b/CrossSockets/CS_COM.cpp @@ -0,0 +1,42 @@ +/*_____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ + |_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____| + _ ____ ____ _ _ ___ _ _ + | | / ___|_ __ ___ ___ ___/ ___| ___ ___| | _____| |_ ___ __ __/ _ \ / | | | + | | | | | '__/ _ \/ __/ __\___ \ / _ \ / __| |/ / _ \ __/ __| \ \ / / | | || | | | + | | | |___| | | (_) \__ \__ \___) | (_) | (__| < __/ |_\__ \ \ 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" + +void Send() +{ + +} \ No newline at end of file diff --git a/CrossSockets/CS_COM.h b/CrossSockets/CS_COM.h new file mode 100644 index 0000000..56f8169 --- /dev/null +++ b/CrossSockets/CS_COM.h @@ -0,0 +1,81 @@ +/*_____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ + |_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____| + _ ____ ____ _ _ ___ _ _ + | | / ___|_ __ ___ ___ ___/ ___| ___ ___| | _____| |_ ___ __ __/ _ \ / | | | + | | | | | '__/ _ \/ __/ __\___ \ / _ \ / __| |/ / _ \ __/ __| \ \ / / | | || | | | + | | | |___| | | (_) \__ \__ \___) | (_) | (__| < __/ |_\__ \ \ 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_COM +{ +public: + CS_COM(SOCKET Socket); + ~CS_COM(/*Global*/); + +protected: + void Send(); + + + +}; + +/* + 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..413b1ec --- /dev/null +++ b/CrossSockets/CS_Client.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_Client.h" diff --git a/CrossSockets/CS_Client.h b/CrossSockets/CS_Client.h new file mode 100644 index 0000000..ebe26f0 --- /dev/null +++ b/CrossSockets/CS_Client.h @@ -0,0 +1,42 @@ +/*_____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ + |_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____|_____| + _ ____ ____ _ _ ___ _ _ + | | / ___|_ __ ___ ___ ___/ ___| ___ ___| | _____| |_ ___ __ __/ _ \ / | | | + | | | | | '__/ _ \/ __/ __\___ \ / _ \ / __| |/ / _ \ __/ __| \ \ / / | | || | | | + | | | |___| | | (_) \__ \__ \___) | (_) | (__| < __/ |_\__ \ \ 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_Client +{ +}; + diff --git a/CrossSockets/CS_Server.cpp b/CrossSockets/CS_Server.cpp new file mode 100644 index 0000000..5cd1449 --- /dev/null +++ b/CrossSockets/CS_Server.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_Server.h" diff --git a/CrossSockets/CS_Server.h b/CrossSockets/CS_Server.h new file mode 100644 index 0000000..9ae7989 --- /dev/null +++ b/CrossSockets/CS_Server.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_Server +{ +}; + 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 27736ff..0ada1fd 100644 --- a/CrossSockets/CrossSockets.vcxproj +++ b/CrossSockets/CrossSockets.vcxproj @@ -143,12 +143,22 @@ + + + + + + + + + + diff --git a/CrossSockets/CrossSockets.vcxproj.filters b/CrossSockets/CrossSockets.vcxproj.filters index 8923534..c362d86 100644 --- a/CrossSockets/CrossSockets.vcxproj.filters +++ b/CrossSockets/CrossSockets.vcxproj.filters @@ -15,28 +15,58 @@ - - Source Files - 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 + + + Header Files + \ No newline at end of file diff --git a/CrossSockets/Source.cpp b/CrossSockets/Source.cpp index d949476..acfd676 100644 --- a/CrossSockets/Source.cpp +++ b/CrossSockets/Source.cpp @@ -1,25 +1,18 @@ -#include "CPSocket.h" +#include "CrossSockets.h" #include "displayout.h" //#define DISABLE_AUTH 0 /*If you want to disable the SSC */ int main() { + // so look at the CPSockets.cpp file - CPSocket socket("127.0.0.1", 56040, 56000); + std::string input = "HONK HONK THE DUCK"; + - //socket.StartClient(); - socket.StartServer(); - while (true) { - } - - 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 4cbb27b..5fba06a 100644 --- a/CrossSockets/displayout.cpp +++ b/CrossSockets/displayout.cpp @@ -35,36 +35,6 @@ along with this program. If not, see . #include "displayout.h" -/* Use as template for building of the cpp class def -std::atomic __IsDisplaying = false; - -class displayout -{ -public: - - displayout(); - ~displayout(); - - // Check if we have color enabled -#if D_DISABLE_COLOR == 0 - void out(int enumTypeColor, std::string ToDisplay); -#else - void out(std::string ToDisplay); -#endif - -protected: - - -private: - std::atomic> PreDisplayControl; - - std::thread ThreadDisplay; - bool SetToQuit = false; - - void Threaded_Display(); - -}; -*/ displayout::displayout() { // Init the display thread diff --git a/CrossSockets/displayout.h b/CrossSockets/displayout.h index 6d782c0..06b5e6e 100644 --- a/CrossSockets/displayout.h +++ b/CrossSockets/displayout.h @@ -115,7 +115,6 @@ namespace D_COLOR { ColorM::Modifier defM(ColorM::FG_DEFAULT); std::string debugstring[] = { "NULL", "LOG", "FILE", "DEBUG", "ERROR", "INFO", "WARNING" }; // Convert the Color to a readable displayname - ColorM::Modifier debugcolor[] = { D_COLOR::defM, D_COLOR::blueM, D_COLOR::greenM, D_COLOR::redM, D_COLOR::redM, D_COLOR::magentaM, D_COLOR::yellowM }; // Convert the Color to a color name typedef struct __ObjectDisplayColorString { std::string Message; @@ -124,9 +123,6 @@ namespace D_COLOR { ColorM::Modifier DebugType; } __OBJSTRING; - - - } class displayout From 94c869080c07807d141dfdedcbeea5dbd60f02c7 Mon Sep 17 00:00:00 2001 From: Main Menu Date: Fri, 17 Apr 2020 15:25:07 -0500 Subject: [PATCH 3/4] Server Working Also I got Chrome to connect to it and display a HTML file --- CrossSockets/Base.h | 14 +- CrossSockets/CPSocket.cpp | 8 +- CrossSockets/CPSocket.h | 7 +- CrossSockets/CS_COM.cpp | 144 +++++++++++- CrossSockets/CS_COM.h | 37 ++- CrossSockets/CS_Server.cpp | 272 ++++++++++++++++++++++ CrossSockets/CS_Server.h | 52 +++++ CrossSockets/CrossSockets.vcxproj | 2 - CrossSockets/CrossSockets.vcxproj.filters | 6 - CrossSockets/Source.cpp | 25 +- CrossSockets/displayout.cpp | 21 +- 11 files changed, 550 insertions(+), 38 deletions(-) diff --git a/CrossSockets/Base.h b/CrossSockets/Base.h index a1e9db9..8b7133c 100644 --- a/CrossSockets/Base.h +++ b/CrossSockets/Base.h @@ -50,6 +50,10 @@ along with this program. If not, see . #include +using namespace std::this_thread; // sleep_for, sleep_until +using namespace std::chrono; // nanoseconds, system_clock, seconds + + #ifdef _WIN32 #include #include @@ -62,7 +66,8 @@ along with this program. If not, see . #endif // __linux - +using std::cout; +using std::endl; #define EOR std::cout << "[DEBUGING USE ONLY] PASSED LINE: " << __LINE__ << std::endl; @@ -95,7 +100,12 @@ along with this program. If not, see . #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 fae1a8b..099235f 100644 --- a/CrossSockets/CPSocket.cpp +++ b/CrossSockets/CPSocket.cpp @@ -350,8 +350,8 @@ void CPSocket::ServerManager() { 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; @@ -373,8 +373,8 @@ void CPSocket::ServerManager() { out(D_WARNING, "Killing all Servers"); } - AliveServers.empty(); - HostNames.empty(); + AliveServers.clear(); + HostNames.clear(); ServerConnectionCount = 0; ServersFull = true; diff --git a/CrossSockets/CPSocket.h b/CrossSockets/CPSocket.h index 10cccf3..7c62bfc 100644 --- a/CrossSockets/CPSocket.h +++ b/CrossSockets/CPSocket.h @@ -39,12 +39,7 @@ along with this program. If not, see . #include "Base.h" #include "displayout.h" -struct Server -{ - std::string IP = "127.0.0.1"; - int InBoundPort = 56010; - int OutBound = 56050; -}; + class CPSocket : private displayout { diff --git a/CrossSockets/CS_COM.cpp b/CrossSockets/CS_COM.cpp index 39a28fb..aba7c16 100644 --- a/CrossSockets/CS_COM.cpp +++ b/CrossSockets/CS_COM.cpp @@ -36,7 +36,147 @@ along with this program. If not, see . #include "Base.h" #include "CS_COM.h" -void Send() -{ +CS_COM::CS_COM(SOCKET *sock) { + Displ.out(D_INFO, "Stack Size of block is " + std::to_string(IO_SIZE) + " Bytes"); + SendingThr = std::thread(&CS_COM::SendThread, this); + RecvingThr = std::thread(&CS_COM::RecvThread, this); + this->sock = sock; +} + +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) { + //Displ.out(D_INFO, i); + //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 (auto i : SendingQue) { + send((SOCKET)*sock, i.c_str(), 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) + { + Displ.out(D_ERROR, "Error in recv, Quitting Thread..."); + ComError = true; + break; + } + if (bytesReceived == 0) + { + 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); + + 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 index 56f8169..b71a396 100644 --- a/CrossSockets/CS_COM.h +++ b/CrossSockets/CS_COM.h @@ -34,21 +34,50 @@ along with this program. If not, see . // ---------------- 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 Socket); + CS_COM(SOCKET *sock); ~CS_COM(/*Global*/); -protected: - void Send(); +//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 KillThreads = false; + void SendThread(); + void RecvThread(); }; /* @@ -78,4 +107,4 @@ class CS_COM void CPSocket::TxV(SOCKET COM, std::string Data) { send(COM, Data.c_str(), Data.length() + 1, 0); } -*/ \ No newline at end of file +*/ \ No newline at end of file diff --git a/CrossSockets/CS_Server.cpp b/CrossSockets/CS_Server.cpp index 5cd1449..bdcadd0 100644 --- a/CrossSockets/CS_Server.cpp +++ b/CrossSockets/CS_Server.cpp @@ -35,3 +35,275 @@ along with this program. If not, see . #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::ServerManager() { + dis.out(D_INFO, "Server Manager has started!"); + + while (!StopAll) { + sleep_for(33ms); + + if (ServersFull) { + 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; + 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()) { + dis.out(D_INFO, "Deleting Old Server..."); + + ThreadPool[i].join(); + try + { + ThreadPool.erase(ThreadPool.begin() + i); + SocketIds.erase(SocketIds.begin() + i); + ServerQue.erase(ServerQue.begin() + i); + } + catch (const std::exception&) + { + dis.out(D_ERROR, "CAN NOT ERASE VECTORS!"); + } + + + ConnectionCount--; + + 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: Gavin/1.3.0 (Unix) +Last-Modified: Mon, 22 Jun 1998 +Content-Length: )" + std::to_string(HtmlF.size()) + R"( +Content-Type: text/html)"; + try + { + for (int i = 0; i < ServerQue.size(); i++) { + if (ServerQue[i].Name != "NO CONNECTION") + ServerQue[i].SendingQue.push_back(HtmlHeader + "\n\n" + HtmlF); + } + } + catch (const std::exception&) + { + dis.out(D_ERROR, "CAN NOT SEND DATA"); + } + + + } + } +} + +void CS_Server::ServerThread(int id) { + Sleep(100); + + dis.out(D_INFO, std::to_string(id) + " STARTING ..."); + dis.out(D_INFO, std::to_string(id) + " Starting Connection: %s::%d"); + + 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"); + + } + + dis.out(D_INFO, std::to_string(id) + " Winsock Started!"); + 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); + + dis.out(D_INFO, std::to_string(id) + " Setup complete!"); + + 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) + { + dis.out(D_INFO, (host + std::string(" Connected on Port: ") + service).c_str()); + } + else + { + inet_ntop(AF_INET, &client.sin_addr, host, NI_MAXHOST); + 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); + + 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); + + bool kill = false; + + while (!kill && !StopAll) { + sleep_for(33ms); + + int index = LookUpArrayId(id); + for (auto i : com.Recv()) { + ServerQue[index].RecvingQue.push_back(i); + } + + for (auto i : ServerQue[index].SendingQue) { + com.Send(i); + } + + try + { + ServerQue[index].SendingQue.clear(); + } + catch (const std::exception&) + { + dis.out(D_ERROR, "CAN NOT ERASE VECTORS!"); + } + + + if (com.Error()) { + dis.out(D_ERROR, std::to_string(id) + " COM ERROR"); + 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 index 9ae7989..3e729d0 100644 --- a/CrossSockets/CS_Server.h +++ b/CrossSockets/CS_Server.h @@ -34,8 +34,60 @@ along with this program. If not, see . // ---------------- 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); + + 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; + Server ConnectionProp ; + displayout dis ; + std::string HtmlF ; + + + + void ServerManager (); + void ServerThread (int id); }; diff --git a/CrossSockets/CrossSockets.vcxproj b/CrossSockets/CrossSockets.vcxproj index 0ada1fd..9975966 100644 --- a/CrossSockets/CrossSockets.vcxproj +++ b/CrossSockets/CrossSockets.vcxproj @@ -147,7 +147,6 @@ - @@ -158,7 +157,6 @@ - diff --git a/CrossSockets/CrossSockets.vcxproj.filters b/CrossSockets/CrossSockets.vcxproj.filters index c362d86..894094c 100644 --- a/CrossSockets/CrossSockets.vcxproj.filters +++ b/CrossSockets/CrossSockets.vcxproj.filters @@ -27,9 +27,6 @@ Source Files - - Source Files - Source Files @@ -56,9 +53,6 @@ Header Files - - Header Files - Header Files diff --git a/CrossSockets/Source.cpp b/CrossSockets/Source.cpp index acfd676..a8a5450 100644 --- a/CrossSockets/Source.cpp +++ b/CrossSockets/Source.cpp @@ -1,18 +1,39 @@ #include "CrossSockets.h" #include "displayout.h" +#include "CS_COM.h" +#include "CS_Server.h" //#define DISABLE_AUTH 0 /*If you want to disable the SSC */ int main() { - // so look at the CPSockets.cpp file + - std::string input = "HONK HONK THE DUCK"; + CS_Server ser(80, false, true); + CS_Server SSS(567); + + + + + while (true) { + Sleep(1000); + static int i = 0; + i++; + std::string NewHtml = R"( + +

Hello, world!

+

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

+ +)"; + ser.HostHtml(NewHtml); + + //ser.SendToAll(Html); + } } \ No newline at end of file diff --git a/CrossSockets/displayout.cpp b/CrossSockets/displayout.cpp index 5fba06a..6910aaf 100644 --- a/CrossSockets/displayout.cpp +++ b/CrossSockets/displayout.cpp @@ -70,23 +70,24 @@ void displayout::out(int enumTypeColor, std::string ToDisplay) { 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.empty(); + PreDisplayControl.clear(); + if (ThreadPart.size() > 0) { + __IsDisplaying = true; - if (ThreadPart.size() > 0 && !__IsDisplaying) { - __IsDisplaying = true; - - for (auto i : ThreadPart) { - std::cout << i.DebugType << "[" << i.Prompt << "]: " << i.Message << D_COLOR::defM << std::endl; + for (auto i : ThreadPart) { + if (i.Message.size() > 0) { + std::cout << i.DebugType << "[" << i.Prompt << "]: " << i.Message << D_COLOR::defM << std::endl; + } + } + __IsDisplaying = false; } - - __IsDisplaying = false; } - } } \ No newline at end of file From 4c666933b4de62fb4f3a6466da54e59968fcb23e Mon Sep 17 00:00:00 2001 From: Main Menu Date: Wed, 29 Apr 2020 12:16:30 -0500 Subject: [PATCH 4/4] All is working --- CrossSockets/Base.h | 9 +-- CrossSockets/CS_COM.cpp | 29 ++++--- CrossSockets/CS_COM.h | 4 +- CrossSockets/CS_Client.cpp | 130 ++++++++++++++++++++++++++++++ CrossSockets/CS_Client.h | 27 ++++++- CrossSockets/CS_Server.cpp | 113 +++++++++++++------------- CrossSockets/CS_Server.h | 6 ++ CrossSockets/CrossSockets.vcxproj | 1 + CrossSockets/Source.cpp | 42 ++++++++-- CrossSockets/displayout.cpp | 1 + CrossSockets/displayout.h | 1 + 11 files changed, 280 insertions(+), 83 deletions(-) diff --git a/CrossSockets/Base.h b/CrossSockets/Base.h index 8b7133c..2958338 100644 --- a/CrossSockets/Base.h +++ b/CrossSockets/Base.h @@ -48,6 +48,7 @@ along with this program. If not, see . #include #include #include +#include using namespace std::this_thread; // sleep_for, sleep_until @@ -63,7 +64,7 @@ using namespace std::chrono; // nanoseconds, system_clock, seconds #endif // _WIN32 #ifdef __linux - +#error "Linux is not suppored yet, we still use chrono for timing :(" #endif // __linux using std::cout; @@ -94,12 +95,6 @@ using std::endl; }*/ -// 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"; diff --git a/CrossSockets/CS_COM.cpp b/CrossSockets/CS_COM.cpp index aba7c16..cab33ec 100644 --- a/CrossSockets/CS_COM.cpp +++ b/CrossSockets/CS_COM.cpp @@ -36,17 +36,21 @@ along with this program. If not, see . #include "Base.h" #include "CS_COM.h" -CS_COM::CS_COM(SOCKET *sock) { - Displ.out(D_INFO, "Stack Size of block is " + std::to_string(IO_SIZE) + " Bytes"); - +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(); @@ -75,8 +79,8 @@ void CS_COM::Send(std::string Send) { for (auto i : BrokenUp) { - //Displ.out(D_INFO, i); - //Displ.out(D_INFO, std::to_string(Built.length()) + " Bytes"); + if(!NoOut) Displ.out(D_INFO, i); + //if(!NoOut) Displ.out(D_INFO, std::to_string(Built.length()) + " Bytes"); SendingQue.push_back(i); } @@ -89,7 +93,7 @@ std::vector CS_COM::Recv() { if (RecvingQue.size() > 0) { auto Sendout = RecvingQue; RecvingQue.clear(); - + return Sendout; } @@ -101,11 +105,12 @@ std::vector CS_COM::Recv() { void CS_COM::SendThread() { while (!KillThreads) { sleep_for(16ms); - for (auto i : SendingQue) { - send((SOCKET)*sock, i.c_str(), i.length() + 1, 0); - + for (int i = 0; i < SendingQue.size(); i++) { + send((SOCKET)*sock, SendingQue[i].c_str(), SendingQue[i].length() + 1, 0); } + SendingQue.clear(); + } @@ -122,13 +127,13 @@ void CS_COM::RecvThread() { int bytesReceived = recv((SOCKET)*sock, buf, IO_SIZE + 20, 0); if (bytesReceived == SOCKET_ERROR) { - Displ.out(D_ERROR, "Error in recv, Quitting Thread..."); + if(!NoOut) Displ.out(D_ERROR, "Error in recv, Quitting Thread..."); ComError = true; break; } if (bytesReceived == 0) { - Displ.out(D_WARNING, "Disconnected..."); + if(!NoOut) Displ.out(D_WARNING, "Disconnected..."); ComError = true; break; } @@ -173,7 +178,7 @@ void CS_COM::RecvThread() { RecvingQue.push_back(str); - Displ.out(D_INFO, str); + if(!NoOut) Displ.out(D_INFO, str); } diff --git a/CrossSockets/CS_COM.h b/CrossSockets/CS_COM.h index b71a396..392ec0b 100644 --- a/CrossSockets/CS_COM.h +++ b/CrossSockets/CS_COM.h @@ -47,7 +47,7 @@ along with this program. If not, see . class CS_COM { public: - CS_COM(SOCKET *sock); + CS_COM(SOCKET* sock, bool NoOutput = false); ~CS_COM(/*Global*/); //protected: @@ -73,7 +73,7 @@ class CS_COM SOCKET *sock; bool ComError = false; - + bool NoOut = false; bool KillThreads = false; void SendThread(); diff --git a/CrossSockets/CS_Client.cpp b/CrossSockets/CS_Client.cpp index 413b1ec..98a6fe7 100644 --- a/CrossSockets/CS_Client.cpp +++ b/CrossSockets/CS_Client.cpp @@ -35,3 +35,133 @@ along with this program. If not, see . #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 index ebe26f0..4ed150f 100644 --- a/CrossSockets/CS_Client.h +++ b/CrossSockets/CS_Client.h @@ -34,9 +34,34 @@ along with this program. If not, see . // ---------------- 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 index bdcadd0..8eedea8 100644 --- a/CrossSockets/CS_Server.cpp +++ b/CrossSockets/CS_Server.cpp @@ -107,15 +107,33 @@ void CS_Server::HostHtml(std::string 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() { - dis.out(D_INFO, "Server Manager has started!"); + if(!HtmlHost) dis.out(D_INFO, "Server Manager has started!"); while (!StopAll) { sleep_for(33ms); - if (ServersFull) { - dis.out(D_INFO, "Spawning New Thread Pool"); + 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); @@ -125,29 +143,22 @@ void CS_Server::ServerManager() { ConnectionCount++; ServersFull = false; - dis.out(D_INFO, "New Connection Count = " + std::to_string(ConnectionCount - 1)); + 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()) { - dis.out(D_INFO, "Deleting Old Server..."); + if(!HtmlHost) dis.out(D_INFO, "Deleting Old Server..."); ThreadPool[i].join(); - try - { - ThreadPool.erase(ThreadPool.begin() + i); - SocketIds.erase(SocketIds.begin() + i); - ServerQue.erase(ServerQue.begin() + i); - } - catch (const std::exception&) - { - dis.out(D_ERROR, "CAN NOT ERASE VECTORS!"); - } - + ThreadPool.erase(ThreadPool.begin() + i); + SocketIds.erase(SocketIds.begin() + i); + ServerQue.erase(ServerQue.begin() + i); + ConnectionCount--; - dis.out(D_INFO, "New Connection Count = " + std::to_string(ConnectionCount - 1)); + if(!HtmlHost) dis.out(D_INFO, "New Connection Count = " + std::to_string(ConnectionCount - 1)); } } ServerActivity = false; @@ -156,23 +167,16 @@ void CS_Server::ServerManager() { std::string HtmlHeader = R"(HTTP/1.0 200 OK Date: Thu, 06 Aug 1998 12:00:15 GMT -Server: Gavin/1.3.0 (Unix) +Server: CrossSockets/0.1.5 (Unix) Last-Modified: Mon, 22 Jun 1998 Content-Length: )" + std::to_string(HtmlF.size()) + R"( Content-Type: text/html)"; - try - { - for (int i = 0; i < ServerQue.size(); i++) { - if (ServerQue[i].Name != "NO CONNECTION") - ServerQue[i].SendingQue.push_back(HtmlHeader + "\n\n" + HtmlF); - } - } - catch (const std::exception&) - { - dis.out(D_ERROR, "CAN NOT SEND DATA"); + + for (int i = 0; i < ServerQue.size(); i++) { + if (ServerQue[i].Name != "NO CONNECTION") + ServerQue[i].SendingQue.push_back(HtmlHeader + "\n\n" + HtmlF); } - } } } @@ -180,10 +184,10 @@ Content-Type: text/html)"; void CS_Server::ServerThread(int id) { Sleep(100); - dis.out(D_INFO, std::to_string(id) + " STARTING ..."); - dis.out(D_INFO, std::to_string(id) + " Starting Connection: %s::%d"); + 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"); - dis.out(D_INFO, std::to_string(id) + " Windows Platform - Using WinSock"); + if(!HtmlHost) dis.out(D_INFO, std::to_string(id) + " Windows Platform - Using WinSock"); // Initialze winsock WSADATA wsData; @@ -204,8 +208,8 @@ void CS_Server::ServerThread(int id) { } - dis.out(D_INFO, std::to_string(id) + " Winsock Started!"); - dis.out(D_INFO, std::to_string(id) + " Starting hint structure..."); + 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; @@ -222,9 +226,9 @@ void CS_Server::ServerThread(int id) { sockaddr_in client; int clientSize = sizeof(client); - dis.out(D_INFO, std::to_string(id) + " Setup complete!"); + if(!HtmlHost) dis.out(D_INFO, std::to_string(id) + " Setup complete!"); - dis.out(D_INFO, std::to_string(id) + " Conneting..."); + if(!HtmlHost) dis.out(D_INFO, std::to_string(id) + " Conneting..."); SOCKET clientSocket = accept(listening, (sockaddr*)&client, &clientSize); @@ -238,17 +242,17 @@ void CS_Server::ServerThread(int id) { if (getnameinfo((sockaddr*)&client, sizeof(client), host, NI_MAXHOST, service, NI_MAXSERV, 0) == 0) { - dis.out(D_INFO, (host + std::string(" Connected on Port: ") + service).c_str()); + 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); - dis.out(D_INFO, (host + std::string(" Connected on Port: ") + std::to_string(ntohs(client.sin_port))).c_str()); + 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); - dis.out(D_INFO, std::to_string(id) + " CONNECTED"); + if(!HtmlHost) dis.out(D_INFO, std::to_string(id) + " CONNECTED"); // Close listening socket closesocket(listening); @@ -257,41 +261,38 @@ void CS_Server::ServerThread(int id) { ServersFull = true; - CS_COM com(&clientSocket); + 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()) { + for (auto &i : com.Recv()) { ServerQue[index].RecvingQue.push_back(i); } - - for (auto i : ServerQue[index].SendingQue) { + + for (auto &i : ServerQue[index].SendingQue) { com.Send(i); } - try - { - ServerQue[index].SendingQue.clear(); - } - catch (const std::exception&) - { - dis.out(D_ERROR, "CAN NOT ERASE VECTORS!"); - } - + ServerQue[index].SendingQue.clear(); - if (com.Error()) { - dis.out(D_ERROR, std::to_string(id) + " COM ERROR"); + if (ServerQue[index].shutdown) { + kill = true; break; } - - } + + // Close the socket closesocket(clientSocket); diff --git a/CrossSockets/CS_Server.h b/CrossSockets/CS_Server.h index 3e729d0..64b52d3 100644 --- a/CrossSockets/CS_Server.h +++ b/CrossSockets/CS_Server.h @@ -59,6 +59,11 @@ class CS_Server 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) { @@ -81,6 +86,7 @@ class CS_Server bool StopAll = false; bool stringBuildback = false; bool HtmlHost = false; + bool Disconnect = false; Server ConnectionProp ; displayout dis ; std::string HtmlF ; diff --git a/CrossSockets/CrossSockets.vcxproj b/CrossSockets/CrossSockets.vcxproj index 9975966..900358f 100644 --- a/CrossSockets/CrossSockets.vcxproj +++ b/CrossSockets/CrossSockets.vcxproj @@ -105,6 +105,7 @@ Console true + /FORCE:MULTIPLE %(AdditionalOptions) diff --git a/CrossSockets/Source.cpp b/CrossSockets/Source.cpp index a8a5450..59ef820 100644 --- a/CrossSockets/Source.cpp +++ b/CrossSockets/Source.cpp @@ -2,10 +2,11 @@ #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() { @@ -20,13 +21,21 @@ int main() { Sleep(1000); static int i = 0; - i++; + std::string HoldingStr = ""; + + for (auto i : AllLog) { + HoldingStr += "

" + i + "

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

Hello, world!

-

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

+ + + +

CrossSockets v0.1

+

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

+)" + HoldingStr + R"( )"; @@ -36,4 +45,27 @@ int main() { //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); + + + + + } + + + + } \ No newline at end of file diff --git a/CrossSockets/displayout.cpp b/CrossSockets/displayout.cpp index 6910aaf..353134d 100644 --- a/CrossSockets/displayout.cpp +++ b/CrossSockets/displayout.cpp @@ -83,6 +83,7 @@ void displayout::Threaded_Display() { 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; diff --git a/CrossSockets/displayout.h b/CrossSockets/displayout.h index 06b5e6e..9e0003e 100644 --- a/CrossSockets/displayout.h +++ b/CrossSockets/displayout.h @@ -69,6 +69,7 @@ along with this program. If not, see . #endif std::atomic __IsDisplaying = false; +std::vector AllLog; enum Debug_D {