비트 딱 맞는지 리눅스에서도 확인할 것

This commit is contained in:
2025-05-01 10:44:45 +09:00
parent 9451b4da40
commit d3161edb36
8 changed files with 88 additions and 29 deletions

View File

@@ -5,13 +5,24 @@
#include "Utils/Thread.hpp" #include "Utils/Thread.hpp"
#include "Utils/ThreadPool.hpp" #include "Utils/ThreadPool.hpp"
#include "Utils/StringTokenizer.hpp" #include "Utils/StringTokenizer.hpp"
#include "Utils/Snowflake.hpp"
#include "Packet/Packet.hpp" #include "Packet/Packet.hpp"
#include <iostream>
#include <bitset>
#include "precomp.hpp" #include "precomp.hpp"
void _TCPClient(Chattr::TCPSocket sock, Chattr::Address addr); void _TCPClient(Chattr::TCPSocket sock, Chattr::Address addr);
int main() { 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; struct Chattr::WSAManager wsaManager;
auto config = Chattr::ConfigManager::load(); auto config = Chattr::ConfigManager::load();
Chattr::log::setDefaultLogger(config.logLevel, config.logFileName, config.logfileSize, config.logfileCount); Chattr::log::setDefaultLogger(config.logLevel, config.logFileName, config.logfileSize, config.logfileCount);
@@ -53,8 +64,6 @@ int main() {
return 1; return 1;
}, returnedIntager, std::ref(passvalue)); }, returnedIntager, std::ref(passvalue));
sleep(10);
while (true) { while (true) {
spdlog::info("Waiting for connection..."); spdlog::info("Waiting for connection...");
sock.accept(clientSock, clientAddr); sock.accept(clientSock, clientAddr);

View File

@@ -2,16 +2,16 @@
namespace Chattr { namespace Chattr {
Session::Session() { Session::Session(Chattr::TCPSocket __sock) {
init(); init(std::move(__sock));
} }
Session::~Session() { Session::~Session() {
destruct(); destruct();
} }
bool Session::init() { void Session::init(Chattr::TCPSocket __sock) {
return false;
} }
void Session::destruct() { void Session::destruct() {

27
impl/Socket/IOCP.cpp Normal file
View File

@@ -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<ThreadPool> __IOCPThread) {
init(__IOCPThread);
}
IOCP::~IOCP() {
}
void IOCP::init(std::shared_ptr<ThreadPool> __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
}
}

View File

@@ -3,7 +3,22 @@
namespace Chattr { namespace Chattr {
ThreadPool::ThreadPool() {
}
ThreadPool::ThreadPool(std::uint32_t numThreads) { 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; int numCPU = numThreads;
if (numThreads == 0) { if (numThreads == 0) {
#ifdef _WIN32 #ifdef _WIN32
@@ -22,14 +37,6 @@ ThreadPool::ThreadPool(std::uint32_t numThreads) {
workers_.push_back([this]() { this->Worker(); }); workers_.push_back([this]() { this->Worker(); });
} }
ThreadPool::~ThreadPool() {
terminate_ = true;
jobQueueCV_.notify_all();
for (auto& t : workers_)
t.join();
}
void* ThreadPool::Worker() { void* ThreadPool::Worker() {
#ifdef _WIN32 #ifdef _WIN32
DWORD pid = GetCurrentThreadId(); DWORD pid = GetCurrentThreadId();

View File

@@ -27,7 +27,7 @@ enum class DataType : std::uint8_t {
BINARY BINARY
}; };
class alignas(1) Packet { class alignas(4) Packet {
public: public:
union { union {
struct { struct {
@@ -35,7 +35,7 @@ public:
RequestType requestType; RequestType requestType;
DataType dataType; DataType dataType;
std::uint16_t packetLength; std::uint16_t packetLength;
std::uint8_t data[1495]; std::uint8_t data[1484];
} __data; } __data;
std::uint8_t serialized[1500]; std::uint8_t serialized[1500];
}; };
@@ -49,7 +49,7 @@ public:
} }
}; };
class alignas(1) LoginRequestPacket : public Packet { class alignas(4) LoginRequestPacket : public Packet {
public: public:
union { union {
struct { struct {
@@ -63,7 +63,7 @@ public:
}; };
}; };
class alignas(1) RoomCreateRequestPacket : public Packet { class alignas(4) RoomCreateRequestPacket : public Packet {
public: public:
union { union {
struct { struct {
@@ -77,7 +77,7 @@ public:
}; };
}; };
class alignas(1) RoomListRequestPacket : public Packet { class alignas(4) RoomListRequestPacket : public Packet {
public: public:
union { union {
struct { struct {
@@ -92,7 +92,7 @@ public:
}; };
}; };
class alignas(1) RoomJoinRequestPacket : public Packet { class alignas(4) RoomJoinRequestPacket : public Packet {
public: public:
union { union {
struct { struct {
@@ -106,7 +106,7 @@ public:
}; };
}; };
class alignas(1) RoomExitRequestPacket : public Packet { class alignas(4) RoomExitRequestPacket : public Packet {
public: public:
union { union {
struct { struct {
@@ -120,7 +120,7 @@ public:
}; };
}; };
class alignas(1) UsersListRequestPacket : public Packet { class alignas(4) UsersListRequestPacket : public Packet {
public: public:
union { union {
struct { struct {
@@ -135,7 +135,7 @@ public:
}; };
}; };
class alignas(1) DataPostPacket : public Packet { class alignas(4) DataPostPacket : public Packet {
public: public:
union { union {
struct { struct {
@@ -149,7 +149,7 @@ public:
}; };
}; };
class alignas(1) ContinuePacket : public Packet { class alignas(4) ContinuePacket : public Packet {
public: public:
union { union {
struct { struct {
@@ -173,7 +173,7 @@ enum class ResponseStatusCode : std::uint16_t {
INTERNAL_SERVER_ERROR = 500 INTERNAL_SERVER_ERROR = 500
}; };
class alignas(1) ResponsePacket : public Packet { class alignas(4) ResponsePacket : public Packet {
public: public:
union { union {
struct { struct {

View File

@@ -6,10 +6,10 @@
namespace Chattr { namespace Chattr {
class Session { class Session {
public: public:
Session(); Session(Chattr::TCPSocket __sock);
~Session(); ~Session();
bool init(); void init(Chattr::TCPSocket __sock);
void destruct(); void destruct();
int send(); int send();

View File

@@ -1,10 +1,23 @@
#pragma once #pragma once
#include "Utils/ThreadPool.hpp"
namespace Chattr { namespace Chattr {
class IOCP { class IOCP {
public: public:
IOCP(); IOCP(std::shared_ptr<ThreadPool> __IOCPThread);
~IOCP(); ~IOCP();
void init(std::shared_ptr<ThreadPool> __IOCPThread);
void recv();
private:
std::shared_ptr<ThreadPool> IOCPThread_;
#ifdef _WIN32
HANDLE completinPort_;
#elif __linux__
#endif
}; };
} }

View File

@@ -11,9 +11,12 @@ namespace Chattr {
class ThreadPool { class ThreadPool {
public: public:
ThreadPool();
ThreadPool(std::uint32_t numThreads); ThreadPool(std::uint32_t numThreads);
~ThreadPool(); ~ThreadPool();
void init(std::uint32_t numThreads);
template<typename _Callable, typename... _Args> template<typename _Callable, typename... _Args>
requires (!std::is_void_v<std::invoke_result_t<_Callable, _Args...>>) requires (!std::is_void_v<std::invoke_result_t<_Callable, _Args...>>)
int enqueueJob(_Callable&& __job, std::invoke_result_t<_Callable, _Args...>& retVal, _Args&&... __args) { int enqueueJob(_Callable&& __job, std::invoke_result_t<_Callable, _Args...>& retVal, _Args&&... __args) {