From af414e58a16d485aa9e9fd0e73b6d3efacd56785 Mon Sep 17 00:00:00 2001 From: HappyTanuki Date: Wed, 30 Apr 2025 20:13:20 +0900 Subject: [PATCH] =?UTF-8?q?=ED=8C=A8=ED=82=B7=20=EC=A0=95=EC=9D=98,=20?= =?UTF-8?q?=EB=A1=9C=EA=B7=B8=EA=B0=80=20=EB=8D=94=20=EB=A7=8E=EC=9D=B4=20?= =?UTF-8?q?=EC=84=A4=EB=AA=85=ED=95=98=EB=8F=84=EB=A1=9D=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Client/src/client.cpp | 2 +- Server/src/server.cpp | 5 +- impl/Session/Session.cpp | 2 + impl/Socket/Socket.cpp | 6 +- impl/Utils/ThreadPool.cpp | 10 +- include/Packet/Packet.hpp | 191 ++++++++++++++++++++++++++++++++++++ include/Session/Session.hpp | 6 +- include/Socket/IOCP.hpp | 10 ++ include/Socket/Socket.hpp | 2 +- 9 files changed, 222 insertions(+), 12 deletions(-) create mode 100644 include/Packet/Packet.hpp create mode 100644 include/Socket/IOCP.hpp diff --git a/Client/src/client.cpp b/Client/src/client.cpp index 586b3ab..8a04a56 100644 --- a/Client/src/client.cpp +++ b/Client/src/client.cpp @@ -10,6 +10,6 @@ int main() { Chattr::Address serveraddr(AF_INET, "localhost", 9011); sock.connect(serveraddr); - spdlog::info("Connection established from {}, {}", sock, (std::string)serveraddr); + spdlog::info("Connection established from {}", (std::string)serveraddr); sock.send("Hello, World!", 14, 0); } \ No newline at end of file diff --git a/Server/src/server.cpp b/Server/src/server.cpp index fb27a9a..f95a666 100644 --- a/Server/src/server.cpp +++ b/Server/src/server.cpp @@ -5,6 +5,7 @@ #include "Utils/Thread.hpp" #include "Utils/ThreadPool.hpp" #include "Utils/StringTokenizer.hpp" +#include "Packet/Packet.hpp" #include "precomp.hpp" @@ -63,7 +64,7 @@ int main() { } void _TCPClient(Chattr::TCPSocket sock, Chattr::Address addr) { - spdlog::info("Connection accepted. [{}:{}]", sock, (std::string)addr); + spdlog::info("Connection accepted. [{}]", (std::string)addr); char buf[1024]; @@ -74,5 +75,5 @@ void _TCPClient(Chattr::TCPSocket sock, Chattr::Address addr) { std::string recvData(buf, redSize); spdlog::info("Red size : {}, {}", redSize, recvData); } - spdlog::info("Client disconnected. [{}:{}]", sock, (std::string)addr); + spdlog::info("Client disconnected. [{}]", (std::string)addr); } \ No newline at end of file diff --git a/impl/Session/Session.cpp b/impl/Session/Session.cpp index 3b0ee2b..2e65cb1 100644 --- a/impl/Session/Session.cpp +++ b/impl/Session/Session.cpp @@ -3,9 +3,11 @@ namespace Chattr { Session::Session() { + init(); } Session::~Session() { + destruct(); } bool Session::init() { diff --git a/impl/Socket/Socket.cpp b/impl/Socket/Socket.cpp index 651dbae..d3ae205 100644 --- a/impl/Socket/Socket.cpp +++ b/impl/Socket/Socket.cpp @@ -35,9 +35,11 @@ void Socket::destruct() { #endif } -Socket::operator SOCKET() const { - if (valid_) +Socket::operator SOCKET() { + if (valid_) { + valid_ = false; return sock_; + } spdlog::critical("No valid socket created."); return INVALID_SOCKET; } diff --git a/impl/Utils/ThreadPool.cpp b/impl/Utils/ThreadPool.cpp index 1e0ed41..c0452f0 100644 --- a/impl/Utils/ThreadPool.cpp +++ b/impl/Utils/ThreadPool.cpp @@ -7,12 +7,14 @@ ThreadPool::ThreadPool(std::uint32_t numThreads) { int numCPU = numThreads; if (numThreads == 0) { #ifdef _WIN32 - SYSTEM_INFO sysinfo; - GetSystemInfo(&sysinfo); - numCPU = sysinfo.dwNumberOfProcessors; + SYSTEM_INFO sysinfo; + GetSystemInfo(&sysinfo); + numCPU = sysinfo.dwNumberOfProcessors; #elif __linux__ - numCPU = sysconf(_SC_NPROCESSORS_ONLN); + numCPU = sysconf(_SC_NPROCESSORS_ONLN); #endif + spdlog::info("Auto-detected cpu count: {}", numCPU); + spdlog::info("Setting ThreadPool Worker count to: {}", numCPU); } workers_.reserve(numCPU); diff --git a/include/Packet/Packet.hpp b/include/Packet/Packet.hpp new file mode 100644 index 0000000..c6817c4 --- /dev/null +++ b/include/Packet/Packet.hpp @@ -0,0 +1,191 @@ +#pragma once +#include "Utils/Snowflake.hpp" + +#include "precomp.hpp" + +namespace Chattr { + +enum class PacketType : std::uint8_t { + PACKET_POST, + PACKET_REQUEST, + PACKET_RESPONSE, + PACKET_CONTINUE, +}; + +enum class RequestType : std::uint8_t { + LOGIN, + ROOM_CREATE, + ROOM_LIST, + ROOM_JOIN, + ROOM_EXIT, + USERS_LIST, + DATA +}; + +enum class DataType : std::uint8_t { + TEXT, + BINARY +}; + +class alignas(1) Packet { +public: + union { + struct { + PacketType packetType; + RequestType requestType; + DataType dataType; + std::uint16_t packetLength; + std::uint8_t data[1495]; + } __data; + std::uint8_t serialized[1500]; + }; + std::uint8_t* convToN() { + __data.packetLength = ::htons(__data.packetLength); + return serialized; + } + std::uint8_t* convToH() { + __data.packetLength = ::ntohs(__data.packetLength); + return serialized; + } +}; + +class alignas(1) LoginRequestPacket : public Packet { +public: + union { + struct { + PacketType packetType; + RequestType requestType; + DataType dataType; + std::uint16_t packetLength; + std::uint8_t name[]; + } __data; + std::uint8_t serialized[1500]; + }; +}; + +class alignas(1) RoomCreateRequestPacket : public Packet { +public: + union { + struct { + PacketType packetType; + RequestType requestType; + DataType dataType; + std::uint16_t packetLength; + std::uint8_t name[]; + } __data; + std::uint8_t serialized[1500]; + }; +}; + +class alignas(1) RoomListRequestPacket : public Packet { +public: + union { + struct { + PacketType packetType; + RequestType requestType; + DataType dataType; + std::uint16_t packetLength; + std::uint32_t roomCount; + std::uint8_t name[]; + } __data; + std::uint8_t serialized[1500]; + }; +}; + +class alignas(1) RoomJoinRequestPacket : public Packet { +public: + union { + struct { + PacketType packetType; + RequestType requestType; + DataType dataType; + std::uint16_t packetLength; + std::uint8_t name[]; + } __data; + std::uint8_t serialized[1500]; + }; +}; + +class alignas(1) RoomExitRequestPacket : public Packet { +public: + union { + struct { + PacketType packetType; + RequestType requestType; + DataType dataType; + std::uint16_t packetLength; + std::uint8_t data[]; + } __data; + std::uint8_t serialized[1500]; + }; +}; + +class alignas(1) UsersListRequestPacket : public Packet { +public: + union { + struct { + PacketType packetType; + RequestType requestType; + DataType dataType; + std::uint16_t packetLength; + std::uint32_t usersCount; + std::uint8_t name[]; + } __data; + std::uint8_t serialized[1500]; + }; +}; + +class alignas(1) DataPostPacket : public Packet { +public: + union { + struct { + PacketType packetType; + RequestType requestType; + DataType dataType; + std::uint16_t packetLength; + std::uint8_t name[]; + } __data; + std::uint8_t serialized[1500]; + }; +}; + +class alignas(1) ContinuePacket : public Packet { +public: + union { + struct { + std::uint16_t packetLength; + std::uint8_t data[]; + } __data; + std::uint8_t serialized[1500]; + }; +}; + +enum class ResponseStatusCode : std::uint16_t { + OK = 200, + CREATED = 201, + ACCEPTED = 202, + BAD_REQUEST = 400, + FORBIDDEN = 403, + NOT_FOUND = 404, + LENGTH_REQUIRED = 411, + IAM_A_TEAPOT = 418, + TOO_MANY_REQUESTS = 429, + INTERNAL_SERVER_ERROR = 500 +}; + +class alignas(1) ResponsePacket : public Packet { +public: + union { + struct { + PacketType packetType; + RequestType requestType; + DataType dataType; + std::uint16_t packetLength; + ResponseStatusCode responseStatusCode; + std::uint8_t data[]; + } __data; + std::uint8_t serialized[1500]; + }; +}; + +} \ No newline at end of file diff --git a/include/Session/Session.hpp b/include/Session/Session.hpp index 05455cb..a6beda0 100644 --- a/include/Session/Session.hpp +++ b/include/Session/Session.hpp @@ -15,9 +15,11 @@ public: int send(); int recv(); + Session(const Session&) = delete; + Session& operator=(Session&) = delete; + private: - std::vector tcpSock_; - std::vector udpSock_; + Chattr::TCPSocket sock_; struct Snowflake sessId_; }; } \ No newline at end of file diff --git a/include/Socket/IOCP.hpp b/include/Socket/IOCP.hpp new file mode 100644 index 0000000..73f5edc --- /dev/null +++ b/include/Socket/IOCP.hpp @@ -0,0 +1,10 @@ +#pragma once + +namespace Chattr { +class IOCP { +public: + IOCP(); + + ~IOCP(); +}; +} \ No newline at end of file diff --git a/include/Socket/Socket.hpp b/include/Socket/Socket.hpp index 75606a6..fad4bb9 100644 --- a/include/Socket/Socket.hpp +++ b/include/Socket/Socket.hpp @@ -14,7 +14,7 @@ public: void init(int domain, int type, int protocol); void destruct(); - operator SOCKET() const; + operator SOCKET(); void set(const SOCKET); void bind(sockaddr *__addr);