자잘한 수정, 이제 다른 소켓으로 쓰기 요청 할 수 있을 듯

This commit is contained in:
2025-05-03 13:59:04 +09:00
parent 181d30ff28
commit f765c163bf
12 changed files with 512 additions and 310 deletions

View File

@@ -5,7 +5,27 @@
namespace Chattr {
enum class PacketType : std::uint8_t {
enum class PacketSet {
LOGINREQUEST,
ROOMCREATEREQUEST,
ROOMLISTREQUEST,
ROOMJOINREQUEST,
ROOMEXITREQUEST,
USERSLISTREQUEST,
DATAPOSTTEXT,
DATAPOSTBINARY,
CONTINUE,
RESPONSE,
LOGINRESPONSE,
ROOMCREATERESPONSE,
ROOMLISTRESPONSE,
ROOMJOINRESPONSE,
ROOMEXITRESPONSE,
USERSLISTRESPONSE,
INVALID
};
enum class PacketCategory : std::uint8_t {
PACKET_POST,
PACKET_REQUEST,
PACKET_RESPONSE,
@@ -22,6 +42,8 @@ enum class RequestType : std::uint8_t {
DATA
};
#define MOREFRAG 0b00000001
enum class DataType : std::uint8_t {
TEXT,
BINARY
@@ -31,7 +53,7 @@ class alignas(4) Packet {
public:
union {
struct {
PacketType packetType;
PacketCategory packetType;
RequestType requestType;
DataType dataType;
std::uint16_t packetLength;
@@ -49,106 +71,48 @@ public:
}
};
class alignas(4) 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(4) LoginRequestPacket : public Packet {};
class alignas(4) 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(4) RoomCreateRequestPacket : public Packet {};
class alignas(4) RoomListRequestPacket : public Packet {
class alignas(4) RoomListRequestPacket : public Packet {};
class alignas(4) RoomJoinRequestPacket : public Packet {
public:
union {
struct {
PacketType packetType;
PacketCategory packetType;
RequestType requestType;
DataType dataType;
std::uint16_t packetLength;
std::uint32_t roomCount;
std::uint16_t destId[4];
std::uint8_t name[];
} __data;
std::uint8_t serialized[1500] = "";
};
std::uint8_t* convToN() {
__data.packetLength = ::htons(__data.packetLength);
__data.roomCount = ::htonl(__data.roomCount);
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);
__data.roomCount = ::ntohl(__data.roomCount);
for (int i = 0; i < 4; i++)
__data.destId[i] = ::ntohs(__data.destId[i]);
return serialized;
}
};
class alignas(4) 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(4) RoomExitRequestPacket : public Packet {};
class alignas(4) 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(4) UsersListRequestPacket : 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(4) UsersListRequestPacket : public Packet {};
class alignas(4) DataPostPacket : public Packet {
public:
union {
struct {
PacketType packetType;
PacketCategory packetType;
RequestType requestType;
DataType dataType;
std::uint16_t packetLength;
@@ -175,13 +139,27 @@ class alignas(4) ContinuePacket : public Packet {
public:
union {
struct {
PacketType packetType;
std::uint8_t padding[2];
PacketCategory packetType;
std::uint8_t padding;
DataType dataType;
std::uint16_t packetLength;
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;
}
};
enum class ResponseStatusCode : std::uint16_t {
@@ -201,7 +179,7 @@ class alignas(4) ResponsePacket : public Packet {
public:
union {
struct {
PacketType packetType;
PacketCategory packetType;
RequestType requestType;
DataType dataType;
std::uint16_t packetLength;
@@ -222,29 +200,98 @@ public:
}
};
//enum class RequestType : std::uint8_t {
// LOGIN,
// ROOM_CREATE,
// ROOM_LIST,
// ROOM_JOIN,
// ROOM_EXIT,
// USERS_LIST,
// DATA
//};
class alignas(4) LoginResponsePacket : public ResponsePacket {};
class alignas(4) LoginResponsePacket : public ResponsePacket {
class alignas(4) RoomCreateResponsePacket : public ResponsePacket {
public:
union {
struct {
PacketType packetType;
PacketCategory packetType;
RequestType requestType;
DataType dataType;
std::uint16_t packetLength;
ResponseStatusCode responseStatusCode;
std::uint16_t createdRoomId[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.createdRoomId[i] = ::htons(__data.createdRoomId[i]);
return serialized;
}
std::uint8_t* convToH() {
__data.packetLength = ::ntohs(__data.packetLength);
for (int i = 0; i < 4; i++)
__data.createdRoomId[i] = ::ntohs(__data.createdRoomId[i]);
return serialized;
}
};
class alignas(4) RoomListResponsePacket : public ResponsePacket {
public:
union {
struct {
PacketCategory packetType;
RequestType requestType;
DataType dataType;
std::uint16_t packetLength;
ResponseStatusCode responseStatusCode;
std::uint32_t roomCount;
std::uint16_t roomId[4];
std::uint8_t name[];
} __data;
std::uint8_t serialized[1500] = "";
};
std::uint8_t* convToN() {
__data.packetLength = ::htons(__data.packetLength);
__data.roomCount = ::htonl(__data.roomCount);
for (int i = 0; i < 4; i++)
__data.roomId[i] = ::htons(__data.roomId[i]);
return serialized;
}
std::uint8_t* convToH() {
__data.packetLength = ::ntohs(__data.packetLength);
__data.roomCount = ::ntohl(__data.roomCount);
for (int i = 0; i < 4; i++)
__data.roomId[i] = ::ntohs(__data.roomId[i]);
return serialized;
}
};
class alignas(4) RoomJoinResponsePacket : public ResponsePacket {};
class alignas(4) RoomExitResponsePacket : public ResponsePacket {};
class alignas(4) UsersListResponsePacket : public ResponsePacket {
public:
union {
struct {
PacketCategory packetType;
RequestType requestType;
DataType dataType;
std::uint16_t packetLength;
ResponseStatusCode responseStatusCode;
std::uint32_t usersCount;
std::uint16_t userId[4];
std::uint8_t name[];
} __data;
std::uint8_t serialized[1500] = "";
};
std::uint8_t* convToN() {
__data.packetLength = ::htons(__data.packetLength);
for (int i = 0; i < 4; i++)
__data.userId[i] = ::htons(__data.userId[i]);
return serialized;
}
std::uint8_t* convToH() {
__data.packetLength = ::ntohs(__data.packetLength);
for (int i = 0; i < 4; i++)
__data.userId[i] = ::ntohs(__data.userId[i]);
return serialized;
}
};
}

View File

@@ -4,6 +4,8 @@
#include "Socket/TCPSocket.hpp"
#include "Socket/Log.hpp"
#include <functional>
#include <vector>
#include "precomp.hpp"
#ifndef _WIN32
@@ -25,10 +27,11 @@ class IOCP;
struct IOCPPASSINDATA {
OVERLAPPED overlapped;
TCPSocket socket;
std::shared_ptr<TCPSocket> socket;
char buf[1501];
int recvbytes;
int sendbytes;
std::uint32_t recvbytes;
std::uint32_t sendbytes;
std::uint32_t transfrredbytes;
WSABUF wsabuf;
IOCP* IOCPInstance;
};
@@ -38,17 +41,18 @@ public:
#ifdef _WIN32
static void iocpWather(ThreadPool* threadPool, HANDLE completionPort_, std::function<void(ThreadPool*, IOCPPASSINDATA*)> callback) {
DWORD tid = GetCurrentThreadId();
spdlog::debug("Waiting IO to complete on TID: {}.", tid);
spdlog::trace("Waiting IO to complete on TID: {}.", tid);
IOCPPASSINDATA* data;
SOCKET sock;
DWORD cbTransfrred;
int retVal = GetQueuedCompletionStatus(completionPort_, &cbTransfrred, (PULONG_PTR)&sock, (LPOVERLAPPED*)&data, INFINITE);
if (retVal == 0 || cbTransfrred == 0) {
spdlog::info("Client disconnected. [{}]", (std::string)(data->socket.remoteAddr));
spdlog::debug("Client disconnected. [{}]", (std::string)(data->socket->remoteAddr));
delete data;
threadPool->enqueueJob(iocpWather, completionPort_, callback);
return;
}
data->transfrredbytes = cbTransfrred;
threadPool->enqueueJob(callback, data);
threadPool->enqueueJob(iocpWather, completionPort_, callback);
};
@@ -63,7 +67,7 @@ public:
return;
}
spdlog::info("reading on tid: {} [{}]", tid, (std::string)iocpData->socket.remoteAddr);
spdlog::trace("reading on tid: {} [{}]", tid, (std::string)iocpData->socket.remoteAddr);
int redSize = 0;
int packetSize = iocpData->wsabuf.len;
@@ -73,7 +77,7 @@ public:
redSize = iocpData->socket.recv(iocpData->buf, packetSize - totalRedSize, 0);
if (redSize <= 0) {
spdlog::info("Client disconnected. [{}]", (std::string)iocpData->socket.remoteAddr);
spdlog::debug("Client disconnected. [{}]", (std::string)iocpData->socket.remoteAddr);
::epoll_ctl(epollfd, EPOLL_CTL_DEL, iocpData->socket.sock, NULL);
delete iocpData;
return;
@@ -91,13 +95,13 @@ public:
int totalSentSize = 0;
int sentSize = 0;
spdlog::info("Sending to: [{}]", (std::string)iocpData->socket.remoteAddr);
spdlog::trace("Sending to: [{}]", (std::string)iocpData->socket.remoteAddr);
while (totalSentSize < packetSize) {
sentSize = iocpData->socket.send(iocpData->buf, packetSize - totalSentSize, 0);
if (sentSize <= 0) {
spdlog::info("Client disconnected. [{}]", (std::string)iocpData->socket.remoteAddr);
spdlog::debug("Client disconnected. [{}]", (std::string)iocpData->socket.remoteAddr);
::epoll_ctl(epollfd, EPOLL_CTL_DEL, iocpData->socket.sock, NULL);
delete iocpData;
return;
@@ -112,7 +116,7 @@ public:
struct epoll_event events[FD_SETSIZE];
pthread_t tid = pthread_self();
spdlog::debug("epoll waiting on {}", tid);
spdlog::trace("epoll waiting on {}", tid);
int nready = ::epoll_wait(epollfd, events, FD_SETSIZE, -1);
for (int i=0; i<nready; i++) {
@@ -164,8 +168,8 @@ public:
void registerSocket(Chattr::IOCPPASSINDATA* data);
int recv(Chattr::IOCPPASSINDATA* data);
int send(Chattr::IOCPPASSINDATA* data, int __flags);
int recv(Chattr::IOCPPASSINDATA* data, int bufferCount);
int send(Chattr::IOCPPASSINDATA* data, int bufferCount, int __flags);
private:
struct Chattr::WSAManager wsaManager;

View File

@@ -23,9 +23,9 @@ public:
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);
Socket(const Socket&) = delete;
Socket(const Socket&);
Socket(Socket&&) noexcept;
Socket& operator=(const Socket&) = delete;
Socket& operator=(const Socket&);
Socket& operator=(Socket&&) noexcept;
struct Address bindAddr = {};

View File

@@ -12,6 +12,19 @@ struct Snowflake {
};
std::uint64_t snowflake;
};
bool operator==(const Snowflake& other) const {
return snowflake == other.snowflake;
}
};
}
namespace std {
template <>
struct hash<Chattr::Snowflake> {
std::size_t operator()(const Chattr::Snowflake& k) const {
return std::hash<uint64_t>{}(k.snowflake);
}
};
}

View File

@@ -64,9 +64,9 @@ public:
handle_ = (HANDLE)_beginthreadex(nullptr, 0, thread_func, funcPtr, 0, nullptr);
#elif __linux__
int rc = pthread_create(&handle_, NULL, thread_func, funcPtr);
#endif
if (handle_ <= 0 || rc != 0)
log::critical("pthread_create()");
#endif
}
~Thread();

View File

@@ -24,13 +24,4 @@
#include <chrono>
#include <gsl/gsl>
#include "spdlog/spdlog.h"
namespace Chattr {
static struct _EPOQUE {
_EPOQUE() {
EPOQUE = std::chrono::system_clock::now();
}
std::chrono::system_clock::time_point EPOQUE;
} __EPOQUE__;
}
#include "spdlog/spdlog.h"