English | 简体中文
The TCP server framework is a TCP based lightweight server framework that can help developers quickly build network communication applications, supporting high concurrency.
TLV
message format.TLV
protocol is used for communication between client and server, andTCP
byte 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
Goroutine
Pool. TheGoroutine
Pool is used to process messages and reuseGoroutines
. The server distributes the message load evenly toGoroutine
processing, avoiding the overhead of massiveGoroutine
scheduling- Sequential processing. Messages from the same
TCP
connection are assigned to a fixedGoroutine
inGoroutine
Pool 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.git
for https
or
git clone [email protected]:lim-yoona/TCP-server-framework.git
for 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.