From c7dc7c7f0ea269f81f4c7284153c11c8889b856b Mon Sep 17 00:00:00 2001 From: josh Date: Sat, 18 Jan 2025 14:21:14 -0500 Subject: [PATCH] Fix Small Error --- include/Sockets/UdpSocket.hpp | 4 +- main.cpp | 274 ++++------------------------------ 2 files changed, 33 insertions(+), 245 deletions(-) diff --git a/include/Sockets/UdpSocket.hpp b/include/Sockets/UdpSocket.hpp index 7ef9cb2..d122f0d 100644 --- a/include/Sockets/UdpSocket.hpp +++ b/include/Sockets/UdpSocket.hpp @@ -142,10 +142,10 @@ private: { tempBuffer[messageLength] = '\0'; if (udpSocket->onMessageReceived) - udpSocket->onMessageReceived(std::string(tempBuffer, messageLength), ipToString(udpSocket->address), ntohs(udpSocket->address.sin_port)); + udpSocket->onMessageReceived(std::string(tempBuffer, messageLength), IPToString(udpSocket->address), ntohs(udpSocket->address.sin_port)); if (udpSocket->onRawMessageReceived) - udpSocket->onRawMessageReceived(tempBuffer, messageLength, ipToString(udpSocket->address), ntohs(udpSocket->address.sin_port)); + udpSocket->onRawMessageReceived(tempBuffer, messageLength, IPToString(udpSocket->address), ntohs(udpSocket->address.sin_port)); } } diff --git a/main.cpp b/main.cpp index b59212f..8e84217 100644 --- a/main.cpp +++ b/main.cpp @@ -1,257 +1,45 @@ -#include - +#include "Sockets/UdpServer.hpp" #include -#include -#include -#include -#include "Sockets/UdpSocket.hpp" -#include -#include -#include -#include -class ServerLogger { -private: - class TransmissionLogger : protected jlog::ConsoleLogger { - public: - TransmissionLogger(std::string ctx, Color4 transcolor) : ConsoleLogger("Server::" + ctx) { - this->timestampColor = Colors::Gray; - this->contextColor = transcolor; - this->locationColor = Colors::Gray; - this->pointerColor = transcolor; - this->messageColor = Colors::White; - //Colors::Green, Colors::Gray, Colors::Gray, Colors::Green, Colors::White - } - void operator()(const std::string &message, const std::source_location &location = std::source_location::current(), const jlog::Timestamp &ts = jlog::Timestamp()) - { - std::string msg = message; - if (!msg.empty() && msg[msg.length()-1] == '\n') { - msg.erase(msg.length()-1); - } - ConsoleLogger::Log(msg, location, ts); - }; - }; -public: - ServerLogger() : RX("RX", Colors::Oranges::Coral), TX("TX", Colors::Green) {}; -public: - TransmissionLogger RX; - TransmissionLogger TX; -}; +using namespace std; -using namespace Socket; +int main() +{ + // Initialize server socket.. + UDPServer<> udpServer; -class TCPServer { -public: - ServerLogger logger; - using Socket_p = std::shared_ptr; - //Event OnServe; - Event OnAccept; - Event OnRX; - Event OnTX; - Socket_p sock {new TcpSocket}; - Socket_p client; -public: - TCPServer() {}; -public: - virtual void Serve() { - try { - sock->Bind(42069); - sock->Listen(5); - client = sock->Accept(); - Accept(); - } catch(std::exception &e) { - std::cout << e.what() << std::endl; - return; - } + // onMessageReceived will run when a message received with information of ip & port of sender: + /*udpServer.onMessageReceived = [&](string message, string ipv4, uint16_t port) { + //cout << ipv4 << ":" << port << " => " << message << endl; - // Welcoming the new users - //std::string payload = "Welcome!\n\f"; + // Echo to client: + udpServer.SendTo("A!", ipv4, port); + };*/ - //client->Send(reinterpret_cast(payload.data()), payload.size(), 0); - // Closing the listening socket, we want nobody else. - //sock->Close(); + // If you want to use raw byte arrays: + udpServer.onRawMessageReceived = [&](const char* message, int length, string ipv4, uint16_t port) { + cout << ipv4 << ":" << port << " => " << message << "(" << length << ")" << endl; - /* - u8 data[512]; - memset(&data, 0, 512); - while (client->Receive(data, sizeof data, 0)) - { - logger.RX(reinterpret_cast(data)); - client->Send(data, sizeof data, 0); - logger.TX(reinterpret_cast(data)); - memset(&data, 0, 512); - } - */ - //OnRX(client); - //OnTX(client); - //client->Close(); - }; - virtual void Accept() { OnAccept(client); } - virtual void RX() { OnRX(client); }; - virtual void TX() { OnTX(client); }; -}; - -class MProtoServer : public TCPServer { -public: - enum MsgID : uint8_t { - THandShake = 100, - RHandShake = 101, + // Echo to client: + udpServer.SendTo(message, length, ipv4, port); }; - MProtoServer() : TCPServer() {}; - void Accept() { - sock->Close(); - u8 data[7]; - memset(&data, 0, 1); - while (client->Receive(data, sizeof data, 0)) { - //logger.RX(reinterpret_cast(data)); - //client->Send(data, sizeof data, 0); - //logger.TX(reinterpret_cast(data)); - std::cout << std::to_string(data[7]) << std::endl; - if (uint8_t(data[1]) == THandShake) { - logger.RX("THandShake"); - } - memset(&data, 0, 1); + // Bind the server to a port. + udpServer.Bind(8888, [](int errorCode, string errorMessage) { + // BINDING FAILED: + cout << errorCode << " : " << errorMessage << endl; + }); - } - - - client->Close(); - - //uint16_t wd = (data[2] << 8) | data[1]; - - }; - - /// Spins up a TCP server, listens for connections, and echoes input directly back to the client. - /// While running this function, use the terminal program netcat to connect and send messages: - /// nc localhost 40269 - void TCP_ReceiveTest() { - ServerLogger TCPLogger; - using Socket_p = std::shared_ptr; - - Socket_p sock(new TcpSocket); - Socket_p client; - - try { - sock->Bind(42069); - sock->Listen(5); - client = sock->Accept(); - } catch (std::exception &e) { - std::cout << e.what() << std::endl; - return; - } - // Welcoming the new users - std::string payload = "Welcome!\n\f"; - - client->Send(reinterpret_cast(payload.data()), payload.size(), 0); - // Closing the listening socket, we want nobody else. - sock->Close(); - - u8 data[512]; - memset(&data, 0, 512); - while (client->Receive(data, sizeof data, 0)) { - //std::cout << "[Recv] " << data << std::endl; - - //std::string datastr = reinterpret_cast(data); - - TCPLogger.RX(reinterpret_cast(data)); - client->Send(data, sizeof data, 0); - TCPLogger.TX(reinterpret_cast(data)); - memset(&data, 0, 512); - } - client->Close(); - } - - void UDP_ReceiveTest() { - using UdpSocket_p = std::shared_ptr; - - UdpSocket_p sock(new UdpSocket); - - try { - sock->Open(); - sock->Bind(42575); - - } catch (const std::exception &e) { - std::cout << e.what() << std::endl; - return; - } - - - UdpSocket_p client(new UdpSocket); - - try { - client->Open(); - client->Connect(42575); - std::string payload = "bruh"; - client->Send(reinterpret_cast(payload.data()), payload.size()); - //client->SendTo(IPAddress::Loopback(42575), reinterpret_cast(payload.data()), payload.size(), 0); - //client->BindAny(); - //client->Open(); - //client->Connect(sock->GetSelfIP()); - - } catch (const std::exception &e) { - std::cout << e.what() << std::endl; - return; - } - client->Close(); - - - sock->Close(); - } -}; - - int main(int argc, char *argv[]) + // You should do an input loop, so the program won't terminate immediately + string input; + getline(cin, input); + while (input != "exit") { + getline(cin, input); + } - if (Socket::Endianness::IsBigEndian()) - std::cout << "Big Endian" << std::endl; + udpServer.Close(); - - if (Socket::Endianness::IsLittleEndian()) - std::cout << "Little Endian" << std::endl; - - - float val = 420.5f; - float a = Socket::Endianness::HostToNetworkOrder(val); - a = Socket::Endianness::NetworkToHostOrder(a); - - std::cout << a << std::endl; - - - //TCP_ReceiveTest(); - //UDP_ReceiveTest(); - TCPServer server; - - server.OnAccept += [&] (TCPServer::Socket_p s) { - server.logger.RX("Accepted client"); - server.RX(); - }; - - server.OnRX += [&] (TCPServer::Socket_p s) { - u8 data[512]; - memset(&data, 0, 512); - while (s->Receive(data, sizeof data, 0)) - { - server.logger.RX(reinterpret_cast(data)); - //s->Send(data, sizeof data, 0); - //server.logger.TX(reinterpret_cast(data)); - server.TX(); - memset(&data, 0, 512); - } - }; - - server.OnTX += [&] (TCPServer::Socket_p s) { - std::string pong = "Message recieved"; - u8 data[512]; - memset(&data, 0, 512); - std::copy(pong.begin(), pong.end(), std::begin(data)); - s->Send(data, sizeof(data), 0); - server.logger.TX(pong); - memset(&data, 0, 512); - }; - server.Serve(); - //MProtoServer ms; - //ms.Serve(); - return 0; - } \ No newline at end of file + return 0; +}