fix: tcp server not listening to clients

This commit is contained in:
MihailRis
2024-12-04 11:58:37 +03:00
parent c668cdafd1
commit 33a64915ad
+16 -6
View File
@@ -293,12 +293,7 @@ public:
} }
} }
void connect(runnable callback) override { void startListen() {
thread = std::make_unique<std::thread>([this, callback]() {
connectSocket();
if (state == ConnectionState::CONNECTED) {
callback();
}
while (state == ConnectionState::CONNECTED) { while (state == ConnectionState::CONNECTED) {
int size = recvsocket(descriptor, buffer.data(), buffer.size()); int size = recvsocket(descriptor, buffer.data(), buffer.size());
if (size == 0) { if (size == 0) {
@@ -324,6 +319,20 @@ public:
} }
logger.info() << "read " << size << " bytes from " << to_string(addr); logger.info() << "read " << size << " bytes from " << to_string(addr);
} }
}
void startClient() {
state = ConnectionState::CONNECTED;
thread = std::make_unique<std::thread>([this]() { startListen();});
}
void connect(runnable callback) override {
thread = std::make_unique<std::thread>([this, callback]() {
connectSocket();
if (state == ConnectionState::CONNECTED) {
callback();
}
startListen();
}); });
} }
@@ -459,6 +468,7 @@ public:
auto socket = std::make_shared<SocketConnection>( auto socket = std::make_shared<SocketConnection>(
clientDescriptor, address clientDescriptor, address
); );
socket->startClient();
u64id_t id = network->addConnection(socket); u64id_t id = network->addConnection(socket);
{ {
std::lock_guard lock(clientsMutex); std::lock_guard lock(clientsMutex);