리눅스랑 코드 동기화

This commit is contained in:
2025-05-01 16:33:52 +09:00
parent d3161edb36
commit 4fb5bdd4cb
14 changed files with 279 additions and 144 deletions

View File

@@ -29,13 +29,16 @@ enum class DataType : std::uint8_t {
class alignas(4) Packet {
public:
Packet() {
memset(serialized, 0, 1500);
}
union {
struct {
PacketType packetType;
RequestType requestType;
DataType dataType;
std::uint16_t packetLength;
std::uint8_t data[1484];
std::uint8_t data[1495];
} __data;
std::uint8_t serialized[1500];
};
@@ -90,6 +93,16 @@ public:
} __data;
std::uint8_t serialized[1500];
};
std::uint8_t* convToN() {
__data.packetLength = ::htons(__data.packetLength);
__data.roomCount = ::htonl(__data.roomCount);
return serialized;
}
std::uint8_t* convToH() {
__data.packetLength = ::ntohs(__data.packetLength);
__data.roomCount = ::ntohl(__data.roomCount);
return serialized;
}
};
class alignas(4) RoomJoinRequestPacket : public Packet {
@@ -128,7 +141,6 @@ public:
RequestType requestType;
DataType dataType;
std::uint16_t packetLength;
std::uint32_t usersCount;
std::uint8_t name[];
} __data;
std::uint8_t serialized[1500];
@@ -143,16 +155,31 @@ public:
RequestType requestType;
DataType dataType;
std::uint16_t packetLength;
std::uint8_t name[];
std::uint16_t destId[4];
std::uint8_t data[];
} __data;
std::uint8_t serialized[1500];
};
std::uint8_t* convToN() {
__data.packetLength = ::htons(__data.packetLength);
for (int i = 0; i < 4; i++)
__data.destId[i] = ::htons(__data.destId[i]);
return serialized;
}
std::uint8_t* convToH() {
__data.packetLength = ::ntohs(__data.packetLength);
for (int i = 0; i < 4; i++)
__data.destId[i] = ::ntohs(__data.destId[i]);
return serialized;
}
};
class alignas(4) ContinuePacket : public Packet {
public:
union {
struct {
PacketType packetType;
std::uint8_t padding[2];
std::uint16_t packetLength;
std::uint8_t data[];
} __data;
@@ -174,6 +201,41 @@ enum class ResponseStatusCode : std::uint16_t {
};
class alignas(4) 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];
};
std::uint8_t* convToN() {
__data.packetLength = ::htons(__data.packetLength);
__data.responseStatusCode = (ResponseStatusCode)::htons((std::uint16_t)__data.responseStatusCode);
return serialized;
}
std::uint8_t* convToH() {
__data.packetLength = ::ntohs(__data.packetLength);
__data.responseStatusCode = (ResponseStatusCode)::ntohs((std::uint16_t)__data.responseStatusCode);
return serialized;
}
};
//enum class RequestType : std::uint8_t {
// LOGIN,
// ROOM_CREATE,
// ROOM_LIST,
// ROOM_JOIN,
// ROOM_EXIT,
// USERS_LIST,
// DATA
//};
class alignas(4) LoginResponsePacket : public ResponsePacket {
public:
union {
struct {

View File

@@ -1,25 +0,0 @@
#pragma once
#include "Socket/TCPSocket.hpp"
#include "Utils/Snowflake.hpp"
#include <vector>
namespace Chattr {
class Session {
public:
Session(Chattr::TCPSocket __sock);
~Session();
void init(Chattr::TCPSocket __sock);
void destruct();
int send();
int recv();
Session(const Session&) = delete;
Session& operator=(Session&) = delete;
private:
Chattr::TCPSocket sock_;
struct Snowflake sessId_;
};
}

View File

@@ -9,7 +9,8 @@ public:
void init(std::shared_ptr<ThreadPool> __IOCPThread);
void recv();
int recv(void* __restrict __buf, size_t __n, int __flags);
int send(const void* __buf, size_t __n, int __flags);
private:
std::shared_ptr<ThreadPool> IOCPThread_;

View File

@@ -11,22 +11,13 @@ public:
Socket(int domain, int type, int protocol);
~Socket();
void init(int domain, int type, int protocol);
int init(int domain, int type, int protocol);
void destruct();
operator SOCKET();
void set(const SOCKET);
void set(const SOCKET __sock, int __domain);
void bind(sockaddr *__addr);
void bind(sockaddr *__addr, socklen_t __len);
//IPV4
void bind(sockaddr_in *__addr);
void bind(sockaddr_in *__addr, socklen_t __len);
//IPV6
void bind(sockaddr_in6 *__addr);
void bind(sockaddr_in6 *__addr, socklen_t __len);
int bind(Address __addr);
int recvfrom(void *__restrict __buf, size_t __n, int __flags, struct Address& __addr);
int sendto(const void *__buf, size_t __n, int __flags, struct Address __addr);
@@ -37,6 +28,9 @@ public:
Socket& operator=(Socket&&);
struct Address bindAddr = {};
struct Address remoteAddr = {};
int domain = 0;
protected:
bool valid_ = false;
SOCKET sock_ = INVALID_SOCKET;

View File

@@ -7,10 +7,10 @@ class TCPSocket : public Socket {
public:
using Socket::Socket;
using Socket::init;
void init(int domain);
void listen(int __n);
int init(int domain);
int listen(int __n);
void accept(TCPSocket& newSock, Address& addr);
void connect(Chattr::Address& serveraddr);
int connect(Chattr::Address& serveraddr);
int recv(void *__restrict __buf, size_t __n, int __flags);
int send(const void *__buf, size_t __n, int __flags);
};

View File

@@ -18,7 +18,7 @@ public:
void init(std::uint32_t numThreads);
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, ThreadPool*, _Args...>>)
int enqueueJob(_Callable&& __job, std::invoke_result_t<_Callable, _Args...>& retVal, _Args&&... __args) {
if (terminate_) {
spdlog::error("Cannot run jobs on threads that terminating...");
@@ -26,8 +26,8 @@ public:
}
std::lock_guard<std::mutex> lock(jobQueueMutex);
auto boundFunc = [&retVal, __job = std::move(__job), ... __args = std::move(__args)]() mutable {
retVal = __job(std::move(__args)...);
auto boundFunc = [this, &retVal, __job = std::move(__job), ... __args = std::move(__args)]() mutable {
retVal = __job(this, std::move(__args)...);
};
auto task = std::packaged_task<void()>(std::move(boundFunc));
jobs_.push(std::move(task));
@@ -36,7 +36,7 @@ public:
return 0;
}
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, ThreadPool*, _Args...>>
int enqueueJob(_Callable&& __job, _Args&&... __args) {
if (terminate_) {
spdlog::error("Cannot run jobs on threads that terminating...");
@@ -44,8 +44,8 @@ public:
}
std::lock_guard<std::mutex> lock(jobQueueMutex);
auto boundFunc = [__job = std::move(__job), ... __args = std::move(__args)]() mutable {
__job(std::move(__args)...);
auto boundFunc = [this, __job = std::move(__job), ... __args = std::move(__args)]() mutable {
__job(this, std::move(__args)...);
};
auto task = std::packaged_task<void()>(std::move(boundFunc));
jobs_.push(std::move(task));

View File

@@ -19,5 +19,15 @@
#error "이 플랫폼은 지원되지 않습니다."
#endif
#include <chrono>
#include <gsl/gsl>
#include "spdlog/spdlog.h"
#include "spdlog/spdlog.h"
namespace Chattr {
struct _EPOQUE_ {
_EPOQUE_() {
EPOQUE = std::chrono::system_clock::now();
}
std::chrono::system_clock::time_point EPOQUE;
} _EPOQUE_;
}