diff --git a/Server/src/server.cpp b/Server/src/server.cpp index f95a666..d98c0d4 100644 --- a/Server/src/server.cpp +++ b/Server/src/server.cpp @@ -5,13 +5,24 @@ #include "Utils/Thread.hpp" #include "Utils/ThreadPool.hpp" #include "Utils/StringTokenizer.hpp" +#include "Utils/Snowflake.hpp" #include "Packet/Packet.hpp" +#include +#include + #include "precomp.hpp" void _TCPClient(Chattr::TCPSocket sock, Chattr::Address addr); int main() { + Chattr::Snowflake flake; + flake.instance = 513; + flake.sequence = 2049; + flake.timestamp = 2199023255553; + spdlog::info("{}", sizeof(Chattr::Snowflake)); + std::cout << std::bitset<64>(flake.snowflake) << "(" << std::hex << flake.snowflake << ")" << std::endl; + return 0 ; struct Chattr::WSAManager wsaManager; auto config = Chattr::ConfigManager::load(); Chattr::log::setDefaultLogger(config.logLevel, config.logFileName, config.logfileSize, config.logfileCount); @@ -53,8 +64,6 @@ int main() { return 1; }, returnedIntager, std::ref(passvalue)); - sleep(10); - while (true) { spdlog::info("Waiting for connection..."); sock.accept(clientSock, clientAddr); diff --git a/impl/Session/Session.cpp b/impl/Session/Session.cpp index 2e65cb1..214d7ee 100644 --- a/impl/Session/Session.cpp +++ b/impl/Session/Session.cpp @@ -2,16 +2,16 @@ namespace Chattr { -Session::Session() { - init(); +Session::Session(Chattr::TCPSocket __sock) { + init(std::move(__sock)); } Session::~Session() { destruct(); } -bool Session::init() { - return false; +void Session::init(Chattr::TCPSocket __sock) { + } void Session::destruct() { diff --git a/impl/Socket/IOCP.cpp b/impl/Socket/IOCP.cpp new file mode 100644 index 0000000..c731702 --- /dev/null +++ b/impl/Socket/IOCP.cpp @@ -0,0 +1,27 @@ +#include "Socket/IOCP.hpp" +#include "Socket/WSAManager.hpp" +#include "Socket/Log.hpp" +#include "precomp.hpp" + +namespace Chattr { + +IOCP::IOCP(std::shared_ptr __IOCPThread) { + init(__IOCPThread); +} + +IOCP::~IOCP() { +} + +void IOCP::init(std::shared_ptr __IOCPThread) { + IOCPThread_ = __IOCPThread; +#ifdef _WIN32 + struct Chattr::WSAManager wsaManager; + completinPort_ = ::CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0); + if (completinPort_ == NULL) + log::critical("CreateIoCompletionPort()"); +#elif __linux__ + +#endif +} + +} \ No newline at end of file diff --git a/impl/Utils/ThreadPool.cpp b/impl/Utils/ThreadPool.cpp index 326fc0f..5033dcd 100644 --- a/impl/Utils/ThreadPool.cpp +++ b/impl/Utils/ThreadPool.cpp @@ -3,7 +3,22 @@ namespace Chattr { +ThreadPool::ThreadPool() { +} + ThreadPool::ThreadPool(std::uint32_t numThreads) { + init(numThreads); +} + +ThreadPool::~ThreadPool() { + terminate_ = true; + jobQueueCV_.notify_all(); + + for (auto& t : workers_) + t.join(); +} + +void ThreadPool::init(std::uint32_t numThreads) { int numCPU = numThreads; if (numThreads == 0) { #ifdef _WIN32 @@ -22,14 +37,6 @@ ThreadPool::ThreadPool(std::uint32_t numThreads) { workers_.push_back([this]() { this->Worker(); }); } -ThreadPool::~ThreadPool() { - terminate_ = true; - jobQueueCV_.notify_all(); - - for (auto& t : workers_) - t.join(); -} - void* ThreadPool::Worker() { #ifdef _WIN32 DWORD pid = GetCurrentThreadId(); diff --git a/include/Packet/Packet.hpp b/include/Packet/Packet.hpp index c6817c4..ca75eb2 100644 --- a/include/Packet/Packet.hpp +++ b/include/Packet/Packet.hpp @@ -27,7 +27,7 @@ enum class DataType : std::uint8_t { BINARY }; -class alignas(1) Packet { +class alignas(4) Packet { public: union { struct { @@ -35,7 +35,7 @@ public: RequestType requestType; DataType dataType; std::uint16_t packetLength; - std::uint8_t data[1495]; + std::uint8_t data[1484]; } __data; std::uint8_t serialized[1500]; }; @@ -49,7 +49,7 @@ public: } }; -class alignas(1) LoginRequestPacket : public Packet { +class alignas(4) LoginRequestPacket : public Packet { public: union { struct { @@ -63,7 +63,7 @@ public: }; }; -class alignas(1) RoomCreateRequestPacket : public Packet { +class alignas(4) RoomCreateRequestPacket : public Packet { public: union { struct { @@ -77,7 +77,7 @@ public: }; }; -class alignas(1) RoomListRequestPacket : public Packet { +class alignas(4) RoomListRequestPacket : public Packet { public: union { struct { @@ -92,7 +92,7 @@ public: }; }; -class alignas(1) RoomJoinRequestPacket : public Packet { +class alignas(4) RoomJoinRequestPacket : public Packet { public: union { struct { @@ -106,7 +106,7 @@ public: }; }; -class alignas(1) RoomExitRequestPacket : public Packet { +class alignas(4) RoomExitRequestPacket : public Packet { public: union { struct { @@ -120,7 +120,7 @@ public: }; }; -class alignas(1) UsersListRequestPacket : public Packet { +class alignas(4) UsersListRequestPacket : public Packet { public: union { struct { @@ -135,7 +135,7 @@ public: }; }; -class alignas(1) DataPostPacket : public Packet { +class alignas(4) DataPostPacket : public Packet { public: union { struct { @@ -149,7 +149,7 @@ public: }; }; -class alignas(1) ContinuePacket : public Packet { +class alignas(4) ContinuePacket : public Packet { public: union { struct { @@ -173,7 +173,7 @@ enum class ResponseStatusCode : std::uint16_t { INTERNAL_SERVER_ERROR = 500 }; -class alignas(1) ResponsePacket : public Packet { +class alignas(4) ResponsePacket : public Packet { public: union { struct { diff --git a/include/Session/Session.hpp b/include/Session/Session.hpp index a6beda0..2d65531 100644 --- a/include/Session/Session.hpp +++ b/include/Session/Session.hpp @@ -6,10 +6,10 @@ namespace Chattr { class Session { public: - Session(); + Session(Chattr::TCPSocket __sock); ~Session(); - bool init(); + void init(Chattr::TCPSocket __sock); void destruct(); int send(); diff --git a/include/Socket/IOCP.hpp b/include/Socket/IOCP.hpp index 73f5edc..fde4e84 100644 --- a/include/Socket/IOCP.hpp +++ b/include/Socket/IOCP.hpp @@ -1,10 +1,23 @@ #pragma once +#include "Utils/ThreadPool.hpp" namespace Chattr { class IOCP { public: - IOCP(); - + IOCP(std::shared_ptr __IOCPThread); ~IOCP(); + + void init(std::shared_ptr __IOCPThread); + + void recv(); + +private: + std::shared_ptr IOCPThread_; + +#ifdef _WIN32 + HANDLE completinPort_; +#elif __linux__ + +#endif }; } \ No newline at end of file diff --git a/include/Utils/ThreadPool.hpp b/include/Utils/ThreadPool.hpp index cfc5a58..bf6a62a 100644 --- a/include/Utils/ThreadPool.hpp +++ b/include/Utils/ThreadPool.hpp @@ -11,9 +11,12 @@ namespace Chattr { class ThreadPool { public: + ThreadPool(); ThreadPool(std::uint32_t numThreads); ~ThreadPool(); + void init(std::uint32_t numThreads); + template requires (!std::is_void_v>) int enqueueJob(_Callable&& __job, std::invoke_result_t<_Callable, _Args...>& retVal, _Args&&... __args) {