O
O
Ockonal2014-10-05 13:03:21
C++ / C#
Ockonal, 2014-10-05 13:03:21

How to properly implement a server for the game?

Hello, I am writing a server that will serve many client connections. The game does not take place in real time, the client needs to synchronize / send data every 10-15 seconds.
The server is written in libevent with multithreading. The base was taken here .
The main question - how to keep a lot of connections? Whether to answer correctly and close the connection so that other clients can continue to connect.
Tried to check the load:
ab -c 1000 -r -t 10 http://127.0.0.1:5555/
And got

Complete requests: 50000
Failed requests: 101997
(Connect: 0, Receive: 50998, Length: 0, Exceptions: 50999)

Increasing the allowed number of descriptors did not improve the situation much.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2014-10-05
@Ockonal

Is it correct to answer and close the connection

well, if you no longer need this connection, then yes. If people connect every 10 seconds and there are a lot of them, your server will simply crash (well, or just the load will be unrealistically large). The overhead of creating a connection is higher than that of maintaining it.
Once a connection is created, it only occupies one socket descriptor. And that's all. You may simply run out of sockets free, but this is a solvable problem. And that's all. It does not consume any more resources. And with the help of epoll, you can select connections from a common pack that contain data for reading or writing, or that have fallen off ... though I'm not sure about the context of libevent, it seemed to me that it resolves this itself.
You can also make one thread that accepts connections and scatters them across processes to workers. And they are already serving them. For example, in nginx, each worker process serves its own connection packs and resolves everything inside the process through the same epoll. That is, not a connection = a process, but many connections = a process.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question