Server development experience and development experience
During this time, I developed a game server in the company. I recorded some experiences in the development process, and I will also have some experiences in the future.
1. timed refresh mechanism. The server must have constant and timed refresh measures, such as the task system.
2. thread management and scheduling, generally using the thread pool Solution
3. In terms of memory, in order to improve performance, the memory pool is usually used instead of frequently allocating the memory dynamically.
4. data packet design, usually in the header + data packet body mode. When receiving data packets, first accept the packet header and then receive the remaining data according to the size described in the packet header;
Data packets are serialized (or serialized) in binary format to save space before being sent to the Buffer.
Google's protobuf is a good tool. Although it is not used in this project, it may be used in the future.
5. the queue is used. The server generally processes multiple threads. The queue logic can be clearly defined, which is often used in servers, such as data packet queues. In fact, in Windows, the idea of queue is also used to complete port IOCP.
6. Avoid competition. Place the processing of one Socket or connection in one thread. Do not allow multiple threads to process the same socket at the same time.
7. The log system and log system can also use the thread and queue ideas. You can consider using a cross-platform logstore, Log4cplus (Log4cxx), a powerful open-source C ++ logstore.
8. When asynchronous callback is used, sometimes the result to be processed on the server is more congested than it can be obtained immediately, such as database queries. In this case, asynchronous callback can be used to improve performance.
9. Timeout detection. Sometimes the client has been disconnected due to network reasons, but the server may not receive the signal for a long time. Therefore, timed timeout detection is necessary.
10. The application of state machines, such as user state machines and state machines, can make the logic clearer.
11. Use of design patterns, such as Singleton mode and observer mode.