Xsocket as a well-known open source framework (see the code author as if it were just one person.) ), there are many places worth learning from.
1. Memory Management
The default is pre-allocated, each dispatcher has a Memorymanager object, Memorymanager manages a chunk of Bytebuffer by default is 16KB, which is allocated the first time the memory is requested. When a new connection is dispatcher the Memorymanager reference is passed to Iosockethandler, and when there is a new read event, Iosockethandler reads the data in a large bytebuffer, Then the size of the read from the large bytebuffer, the remainder of the recovery, the next time to continue to use, if the remainder is less than the minimum size to allocate a 16KB. Here Bytebuffer is managed by the dispatcher so the connection is shared, because the request memory, write, recycle and so on is in the dispatcher thread, so there is no need to synchronize.
2. Connection Management
The main is the maximum time to connect timeout, the connection idle time timeout management, by the server's ConnectionManager member management It maintains a connection list, server initialization is instantiated ConnectionManager, And initiates a timed task, periodically checks the connection in the list, and times out calls the connection function for processing.
3.tcp Sub-Package
Excerpted from the Official Handbook
As discussed above, data is segmented on the network level. If the OnData () method is called, it isn't predictable how many bytes would be received. By calling a read method such as ReadInt () or Readstringbydelimiter () a bufferunderflowexception can occur. To handle this, the read mark support can used.
That is, the starting place to mark the first read, in the process of reading such as Readint is not enough 4byte, then throw an exception reset to the mark, the OnData method exits, and so on when there is new data to call again.
4. Multi-threading, thread pool
is mainly a acceptor thread, a management connected timer, a dispatcher thread pool (default is two dispatcher), the underlying read and write operations are done by dispatcher, Each connection creates a new Ondataevent event queue and Runnabe object (note that it is not a thread), and the Runnable Run method takes an event from the queue to execute ondataevent, which is the handler function we define. The Runnable object is submitted to a global Workpool thread pool execution, with a core size of 2,max size of 100.
In general, there are six of threads.
Xsocket Analysis III, supplementary notes