English | 简体中文
The TCP server framework is a TCP based lightweight server framework that can help developers quickly build network communication applications, supporting high concurrency.
TLVmessage format.TLVprotocol is used for communication between client and server, andTCPbyte stream is processed by a message packager- Multi-Route. Through the message ID to implement the use of multi-route to process messages, support the developer custom multi-route to process messages
GoroutinePool. TheGoroutinePool is used to process messages and reuseGoroutines. The server distributes the message load evenly toGoroutineprocessing, avoiding the overhead of massiveGoroutinescheduling- Sequential processing. Messages from the same
TCPconnection are assigned to a fixedGoroutineinGoroutinePool to implement sequential processing of messages - read-write separation. For each connection, a read-write separation model is adopted to achieve high cohesion and low coupling
First, clone this project, using
git clone https://github.com/lim-yoona/TCP-server-framework.gitfor https or
git clone [email protected]:lim-yoona/TCP-server-framework.gitfor ssh.
Some use cases are in the demo folder.
Specifically, you can customize the route by inheriting the BaseRouter class and rewriting its methods.
type MyRouter struct {
seNet.BaseRouter
}
func (this *MyRouter) Handle(request seInterface.IRequest) {
···
err := request.GetConnection().SendMsg(200, []byte(···))
···
}You can then register the route using the Server's AddRouter method, with the first parameter being the message ID, processing the message corresponding to that ID, and the second parameter being your custom route.
Finally, start the server by calling the Serve method of Server .
func main() {
s := seNet.NewServer()
s.AddRouter(0, &MyRouter{})
s.Serve()
}You can also add hook functions through the SetOnConnStart and SetOnConnStop methods of the Server , as shown in the example.
