리눅스랑 코드 동기화
This commit is contained in:
@@ -13,16 +13,10 @@
|
||||
|
||||
#include "precomp.hpp"
|
||||
|
||||
void _TCPClient(Chattr::TCPSocket sock, Chattr::Address addr);
|
||||
void _TCPRecvClient(Chattr::ThreadPool* thread, Chattr::TCPSocket sock, Chattr::Address addr);
|
||||
void _TCPSendClient(Chattr::ThreadPool* thread, Chattr::TCPSocket sock, Chattr::Address addr, std::queue<Chattr::ResponsePacket> packets);
|
||||
|
||||
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);
|
||||
@@ -41,7 +35,7 @@ int main() {
|
||||
serveraddr.set(AF_INET6, in6addr_any, config.listenPort);
|
||||
clientAddr.setType(AF_INET6);
|
||||
}
|
||||
sock.bind(&serveraddr.addr, serveraddr.length);
|
||||
sock.bind(serveraddr);
|
||||
sock.listen(SOMAXCONN);
|
||||
|
||||
#ifdef _WIN32
|
||||
@@ -53,7 +47,7 @@ int main() {
|
||||
|
||||
|
||||
Chattr::ThreadPool threadPool(0);
|
||||
int returnedIntager = 0;
|
||||
/*int returnedIntager = 0;
|
||||
int passvalue = 2;
|
||||
threadPool.enqueueJob([](int& i){
|
||||
spdlog::info("JobTest");
|
||||
@@ -62,27 +56,110 @@ int main() {
|
||||
return 2;
|
||||
}
|
||||
return 1;
|
||||
}, returnedIntager, std::ref(passvalue));
|
||||
}, returnedIntager, std::ref(passvalue));*/
|
||||
|
||||
while (true) {
|
||||
spdlog::info("Waiting for connection...");
|
||||
sock.accept(clientSock, clientAddr);
|
||||
|
||||
threadPool.enqueueJob(_TCPClient, std::move(clientSock), clientAddr);
|
||||
threadPool.enqueueJob(_TCPRecvClient, std::move(clientSock), clientAddr);
|
||||
}
|
||||
}
|
||||
|
||||
void _TCPClient(Chattr::TCPSocket sock, Chattr::Address addr) {
|
||||
spdlog::info("Connection accepted. [{}]", (std::string)addr);
|
||||
void _TCPRecvClient(Chattr::ThreadPool* thread, Chattr::TCPSocket sock, Chattr::Address addr) {
|
||||
Chattr::Packet pack;
|
||||
int packetSize = 1500;
|
||||
int totalRedSize = 0;
|
||||
int redSize = 0;
|
||||
|
||||
char buf[1024];
|
||||
|
||||
while (true) {
|
||||
int redSize = sock.recv(buf, 1024, 0);
|
||||
if (redSize == 0)
|
||||
break;
|
||||
std::string recvData(buf, redSize);
|
||||
spdlog::info("Red size : {}, {}", redSize, recvData);
|
||||
while (totalRedSize < packetSize) {
|
||||
redSize = sock.recv(&pack.serialized, 1500 - totalRedSize, 0);
|
||||
|
||||
if (redSize <= 0) {
|
||||
spdlog::info("Client disconnected. [{}]", (std::string)addr);
|
||||
return;
|
||||
}
|
||||
totalRedSize += redSize;
|
||||
}
|
||||
spdlog::info("Client disconnected. [{}]", (std::string)addr);
|
||||
|
||||
std::string recvString;
|
||||
bool packetError = false;
|
||||
Chattr::DataPostPacket dataPostPacket;
|
||||
|
||||
switch (pack.__data.packetType) {
|
||||
case Chattr::PacketType::PACKET_POST:
|
||||
switch (pack.__data.dataType) {
|
||||
case Chattr::DataType::TEXT:
|
||||
std::memcpy(&dataPostPacket.serialized, &pack, 1500);
|
||||
dataPostPacket.convToH();
|
||||
recvString = std::string((char *)dataPostPacket.__data.data, dataPostPacket.__data.packetLength - sizeof(std::uint16_t));
|
||||
spdlog::info("Red size : {}, {} from : [{}]", redSize, recvString, (std::string)addr);
|
||||
break;
|
||||
case Chattr::DataType::BINARY:
|
||||
break;
|
||||
default:
|
||||
packetError = true;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case Chattr::PacketType::PACKET_REQUEST:
|
||||
switch (pack.__data.requestType) {
|
||||
case Chattr::RequestType::LOGIN:
|
||||
break;
|
||||
case Chattr::RequestType::ROOM_CREATE:
|
||||
break;
|
||||
case Chattr::RequestType::ROOM_LIST:
|
||||
break;
|
||||
case Chattr::RequestType::ROOM_JOIN:
|
||||
break;
|
||||
case Chattr::RequestType::ROOM_EXIT:
|
||||
break;
|
||||
case Chattr::RequestType::USERS_LIST:
|
||||
break;
|
||||
default:
|
||||
packetError = true;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case Chattr::PacketType::PACKET_CONTINUE:
|
||||
break;
|
||||
default:
|
||||
packetError = true;
|
||||
break;
|
||||
}
|
||||
|
||||
if (packetError) {
|
||||
Chattr::ResponsePacket packet;
|
||||
packet.__data.responseStatusCode = Chattr::ResponseStatusCode::BAD_REQUEST;
|
||||
std::queue<Chattr::ResponsePacket> packs;
|
||||
packet.convToN();
|
||||
packs.push(packet);
|
||||
thread->enqueueJob(_TCPSendClient, std::move(sock), addr, packs);
|
||||
}
|
||||
else
|
||||
thread->enqueueJob(_TCPRecvClient, std::move(sock), addr);
|
||||
}
|
||||
|
||||
void _TCPSendClient(Chattr::ThreadPool* thread, Chattr::TCPSocket sock, Chattr::Address addr, std::queue<Chattr::ResponsePacket> packets) {
|
||||
Chattr::ResponsePacket pack = packets.front();
|
||||
packets.pop();
|
||||
int packetSize = 1500;
|
||||
int totalSentSize = 0;
|
||||
int sentSize = 0;
|
||||
|
||||
while (totalSentSize < packetSize) {
|
||||
|
||||
sentSize = sock.send(&pack.serialized, 1500 - totalSentSize, 0);
|
||||
|
||||
if (sentSize <= 0) {
|
||||
spdlog::info("Client disconnected. [{}]", (std::string)addr);
|
||||
return;
|
||||
}
|
||||
totalSentSize += sentSize;
|
||||
}
|
||||
|
||||
if (packets.empty())
|
||||
thread->enqueueJob(_TCPRecvClient, std::move(sock), addr);
|
||||
else
|
||||
thread->enqueueJob(_TCPSendClient, std::move(sock), addr, packets);
|
||||
}
|
||||
Reference in New Issue
Block a user