둘 다 동시 디버깅을 위해 push

This commit is contained in:
2025-05-03 17:58:46 +09:00
parent 4bbb6837e8
commit 6ad787eed7
11 changed files with 305 additions and 97 deletions

View File

@@ -140,7 +140,7 @@ public:
union {
struct {
PacketCategory packetType;
std::uint8_t padding;
RequestType requestType;
DataType dataType;
std::uint16_t packetLength;
std::uint16_t destId[4];
@@ -200,7 +200,33 @@ public:
}
};
class alignas(4) LoginResponsePacket : public ResponsePacket {};
class alignas(4) LoginResponsePacket : public ResponsePacket {
public:
union {
struct {
PacketCategory packetType;
RequestType requestType;
DataType dataType;
std::uint16_t packetLength;
ResponseStatusCode responseStatusCode;
std::uint16_t yourId[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.yourId[i] = ::htons(__data.yourId[i]);
return serialized;
}
std::uint8_t* convToH() {
__data.packetLength = ::ntohs(__data.packetLength);
for (int i = 0; i < 4; i++)
__data.yourId[i] = ::ntohs(__data.yourId[i]);
return serialized;
}
};
class alignas(4) RoomCreateResponsePacket : public ResponsePacket {
public:

View File

@@ -3,6 +3,7 @@
#include "Socket/WSAManager.hpp"
#include "Socket/TCPSocket.hpp"
#include "Socket/Log.hpp"
#include "Packet/Packet.hpp"
#include <functional>
#include <vector>
@@ -31,7 +32,7 @@ struct IOCPPASSINDATA {
char buf[1501];
std::uint32_t recvbytes;
std::uint32_t sendbytes;
std::uint32_t transfrredbytes;
std::uint32_t transferredbytes;
WSABUF wsabuf;
IOCP* IOCPInstance;
};
@@ -67,49 +68,95 @@ public:
return;
}
spdlog::trace("reading on tid: {} [{}]", tid, (std::string)iocpData->socket.remoteAddr);
std::lock_guard<std::mutex> lock(iocpData->socket->readMutex);
spdlog::trace("reading on tid: {} [{}]", tid, (std::string)iocpData->socket->remoteAddr);
int redSize = 0;
int packetSize = iocpData->wsabuf.len;
int headerSize = 8;
int totalRedSize = 0;
while (totalRedSize < packetSize) {
redSize = iocpData->socket.recv(iocpData->buf, packetSize - totalRedSize, 0);
while (totalRedSize < headerSize) {
redSize = iocpData->socket->recv(iocpData->buf, headerSize - totalRedSize, 0);
if (redSize <= 0) {
spdlog::debug("Client disconnected. [{}]", (std::string)iocpData->socket.remoteAddr);
::epoll_ctl(epollfd, EPOLL_CTL_DEL, iocpData->socket.sock, NULL);
delete iocpData;
return;
if (errno == EAGAIN || errno == EWOULDBLOCK) {
spdlog::trace("{}", strerror(errno));
break;
} else {
spdlog::debug("Client disconnected. [{}]", (std::string)iocpData->socket->remoteAddr);
::epoll_ctl(epollfd, EPOLL_CTL_DEL, iocpData->socket->sock, NULL);
delete iocpData;
return;
}
}
totalRedSize += redSize;
}
iocpData->sendbytes = packetSize - totalRedSize;
iocpData->recvbytes = totalRedSize;
Packet packet;
::memcpy(&packet.serialized, iocpData->buf, headerSize);
redSize = 0;
int dataLength = ntohs(packet.__data.packetLength);
while (totalRedSize < dataLength + headerSize) {
redSize = iocpData->socket->recv(iocpData->buf + totalRedSize, dataLength + headerSize - totalRedSize, 0);
if (redSize <= 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
spdlog::trace("{}", strerror(errno));
break;
} else {
spdlog::debug("Client disconnected. [{}]", (std::string)iocpData->socket->remoteAddr);
::epoll_ctl(epollfd, EPOLL_CTL_DEL, iocpData->socket->sock, NULL);
delete iocpData;
return;
}
}
totalRedSize += redSize;
}
iocpData->transferredbytes = totalRedSize;
threadPool->enqueueJob(callback, iocpData);
};
static void socketWriter(ThreadPool* threadPool, epoll_event event, int epollfd, std::function<void(ThreadPool*, IOCPPASSINDATA*)> callback) {
IOCPPASSINDATA* iocpData = (IOCPPASSINDATA*)event.data.ptr;
pthread_t tid = pthread_self();
int packetSize = iocpData->wsabuf.len;
if (iocpData == nullptr) {
spdlog::error("invalid call on {}", tid);
return;
}
std::lock_guard<std::mutex> lock(iocpData->socket->writeMutex);
spdlog::trace("Writing on tid: {} [{}]", tid, (std::string)iocpData->socket->remoteAddr);
int packetSize = iocpData->transferredbytes;
int totalSentSize = 0;
int sentSize = 0;
spdlog::trace("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);
sentSize = iocpData->socket->send(iocpData->buf, packetSize - totalSentSize, 0);
if (sentSize <= 0) {
spdlog::debug("Client disconnected. [{}]", (std::string)iocpData->socket.remoteAddr);
::epoll_ctl(epollfd, EPOLL_CTL_DEL, iocpData->socket.sock, NULL);
delete iocpData;
return;
if (errno == EAGAIN || errno == EWOULDBLOCK) {
spdlog::trace("{}", strerror(errno));
break;
} else {
spdlog::debug("Client disconnected. [{}]", (std::string)iocpData->socket->remoteAddr);
::epoll_ctl(epollfd, EPOLL_CTL_DEL, iocpData->socket->sock, NULL);
delete iocpData;
return;
}
}
totalSentSize += sentSize;
}
iocpData->recvbytes = packetSize - totalSentSize;
iocpData->sendbytes = totalSentSize;
iocpData->transferredbytes = totalSentSize;
threadPool->enqueueJob(callback, iocpData);
};
static void iocpWatcher(ThreadPool* threadPool, int epollfd, std::function<void(ThreadPool*, IOCPPASSINDATA*)> callback) {

View File

@@ -33,6 +33,9 @@ public:
int domain = 0;
SOCKET sock = INVALID_SOCKET;
std::mutex readMutex;
std::mutex writeMutex;
protected:
bool valid_ = false;
};

View File

@@ -18,6 +18,8 @@ struct Snowflake {
}
};
Snowflake GenerateID();
}
namespace std {