Skip to content

Commit

Permalink
Merge branch 'main' into release-0.25
Browse files Browse the repository at this point in the history
  • Loading branch information
MihailRis committed Dec 4, 2024
2 parents f7903c1 + 5c40cf3 commit f0a07cb
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 25 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ If you use Wayland
sudo pacman -S glfw-wayland glew glm libpng libvorbis openal luajit libcurl
```

And you need entt. In yay you can use

```sh
yay -S entt
```

### Build engine with CMake

```sh
Expand Down
60 changes: 35 additions & 25 deletions src/network/Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,37 +293,46 @@ class SocketConnection : public Connection {
}
}

void startListen() {
while (state == ConnectionState::CONNECTED) {
int size = recvsocket(descriptor, buffer.data(), buffer.size());
if (size == 0) {
logger.info() << "closed connection with " << to_string(addr);
closesocket(descriptor);
state = ConnectionState::CLOSED;
break;
} else if (size < 0) {
logger.warning() << "an error ocurred while receiving from "
<< to_string(addr);
auto error = handle_socket_error("recv(...) error");
closesocket(descriptor);
state = ConnectionState::CLOSED;
logger.error() << error.what();
break;
}
{
std::lock_guard lock(mutex);
for (size_t i = 0; i < size; i++) {
readBatch.emplace_back(buffer[i]);
}
totalDownload += size;
}
logger.debug() << "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();
}
while (state == ConnectionState::CONNECTED) {
int size = recvsocket(descriptor, buffer.data(), buffer.size());
if (size == 0) {
logger.info() << "closed connection with " << to_string(addr);
closesocket(descriptor);
state = ConnectionState::CLOSED;
break;
} else if (size < 0) {
logger.info() << "an error ocurred while receiving from "
<< to_string(addr);
auto error = handle_socket_error("recv(...) error");
closesocket(descriptor);
state = ConnectionState::CLOSED;
logger.error() << error.what();
break;
}
{
std::lock_guard lock(mutex);
for (size_t i = 0; i < size; i++) {
readBatch.emplace_back(buffer[i]);
}
totalDownload += size;
}
logger.info() << "read " << size << " bytes from " << to_string(addr);
}
startListen();
});
}

Expand Down Expand Up @@ -459,6 +468,7 @@ class SocketTcpSServer : public TcpServer {
auto socket = std::make_shared<SocketConnection>(
clientDescriptor, address
);
socket->startClient();
u64id_t id = network->addConnection(socket);
{
std::lock_guard lock(clientsMutex);
Expand Down

0 comments on commit f0a07cb

Please sign in to comment.