A new TCP service and client crate, the entire client and server design is as follows. I would like to hear everyone's opinions on what needs to be improved, what are the shortcomings, and what further improvements are needed? I am glad to receive your letters. Thank you very much
Links
Design
-
First, use the
TcpListener
onTokio
to listen on port (0.0.0.0:9177), then start an asynchronous thread and use the createdTcpListener
to start listening for newTcpStream
entries
â‘ . When a newTcpStream
is entered, first query the value ofServer_max_Connections (usize)
in theapp
configuration and compare it with the len of theclients (Arc<RwLock<HashMap<SocketAddr, LynnUser>>>)
in theapp
. If the clients have reached their maximum value, then disconnect the currentTcpStream
. Otherwise, create a new asynchronous thread -
Split
theTcpStream
in a new thread to obtain theread_calf
andwritable variables
â‘ . Create achannel
in the current thread for asynchronous multi-threaded communication At the same time, create aLynnUser
containing asender
for thechannel
, and insertSocketAddr
andLynnUser
as keys and values into the app's clients, respectively
â‘¡. Create a new asynchronous thread in the current thread, use theReceiver
of thechannel
to read it, and when theReceiver
receives the message data, send the message out through writable
â‘¢. In the current thread, useread_malf
to listen. When data enters, receive the data and pass it to a custom message parser -
Validate message data in a custom message parser
â‘ . Custom start and endmarks
were used. If the data meets themark
criteria, it indicates that the data is a complete message. The data enclosed between the start and endmarks
is encapsulated in astruct (InputBufVo)
. If themark
criteria are not met, the currently received byte data is discarded (concurrent message from the agreed client)
â‘¡. Pass the encapsulatedstruct (InputBufVo)
to the task scheduler -
The task scheduler contains n created asynchronous threads, each thread containing a
channel
â‘ .Thestruct (InputBufVo)
contains: received byte data, Future
â‘¡. After receiving thestruct (InputBufVo)
, the task scheduler performs a simple load balancing by evenly sending thestruct (InputBufVo)
through thesender
in the asynchronous threadchannel
â‘¢. Receive through theReceiver
of thechannel
in the thread. When astruct (InputBufVo)
enters, use the Future in thestruct(InputBufVo)
to pass in byte data and wait for the execution to complete -
After the asynchronous thread completes the task, create a
struct (Result)
â‘ Thestruct (Result)
contains the fieldis_dend (boolean)
, which represents whether it needs to be sent to the client
â‘¡. If the data needs to be sent to the client, the correspondingchannel (Sender)
inLynnUser
in the app's clients should be sent out, and a complete communication is completed until then