Skip to content

03 Zinx Server Client

刘丹冰 edited this page May 12, 2023 · 3 revisions

1. Creating instances

1.1 Creating a Server instance

A. Default parameters

You can create a Zinx Server instance with the following statement:

znet.NewServer()

If no default parameters are passed, the Zinx Server instance will use the following default parameters:

Name:             "ZinxServerApp",
Version:          "V1.0",
TCPPort:          8999,
Host:             "0.0.0.0",
MaxConn:          12000,
MaxPacketSize:    4096,
ConfFilePath:     "./conf",
WorkerPoolSize:   10,
MaxWorkerTaskLen: 1024,
MaxMsgChanLen:    1024,
LogDir:           $PWD + "/log",
LogFile:          "",
LogDebugClose:    false,
HeartbeatMax:     10,          //The default maximum interval for heartbeat detection is 10 seconds

B. Server configuration file

The Zinx server will load the $PWD/conf/zinx.json configuration file by default, and the Zinx configuration file json is as follows:

{
  "Name":"zinx server Demo",
  "Host":"127.0.0.1",
  "TcpPort":8999,
  "MaxConn":3,
  "WorkerPoolSize":10,
  "LogDir": "./mylog",
  "LogFile":"zinx.log",
  "LogDebugClose":true
}

The meanings of the fields are as follows:

Host              string   //Current server host IP
TCPPort           int      //Current server host listening port number
Name              string   //Current server name

MaxPacketSize     int      //Maximum size of data packet
MaxConn           int      //Maximum number of connections allowed by the current server host
WorkerPoolSize    int      //Number of business work worker pools
MaxWorkerTaskLen  int      //Maximum task storage capacity of the task queue responsible for the business work worker
MaxMsgChanLen     int      //Maximum length of the SendBuffMsg send message buffer

LogDir            string   //Folder where logs are located, default is "./log"
LogFile           string   //Log file name, default is "", if no log file is set, print information will be printed to stderr
LogDebugClose     bool     //Whether to close the debug log level debugging information, default is false -- debug information is turned on by default

HeartbeatMax      int      //Maximum interval for heartbeat detection (in seconds). If the interval exceeds this time, it will be considered timeout and read from the configuration file

1.2 Creating a Client instance

You can create a Zinx Client instance with the following statement:

znet.NewClient("127.0.0.1", 8999)

Of course, you can also set some basic information parameters through $PWD/conf/zinx.json.

2. Configure Server/Client Startup and Shutdown Hook Functions

2.1 Server Hook Configuration

image

The following method can be used to configure server hooks:

//...
s := znet.NewServer()
//...
s.SetOnConnStart(DoConnectionBegin)
s.SetOnConnStop(DoConnectionLost)
//...

The prototypes for DoConnectionBegin and DoConnectionLost are as follows:

func DoConnectionBegin(conn ziface.IConnection) {
	//...
}

func DoConnectionLost(conn ziface.IConnection) {
	//...
}
image

The following method can be used to configure server hooks:

//...
client := znet.NewClient("127.0.0.1", 8999)
//...
client.SetOnConnStart(DoClientConnectedBegin)
client.SetOnConnStop(DoClientConnectedLost)
//...

The prototypes for DoClientConnectionBegin and DoClientConnectionLost are as follows:

func DoClientConnectedBegin(conn ziface.IConnection) {
	//...
}

func DoClientConnectedLost(conn ziface.IConnection) {
	//...
}

3. Server/Client interface接口

3.1 Server methods

// Server interface
type IServer interface {
	Start()                                                   // start server
	Stop()                                                    // stop server
	Serve()                                                   // start the business service
	AddRouter(msgID uint32, router IRouter)                   // add router for handling client request
	GetConnMgr() IConnManager                                 // get connection manager
	SetOnConnStart(func(IConnection))                         // set hook function for connection creation
	SetOnConnStop(func(IConnection))                          // set hook function for connection close
	GetOnConnStart() func(IConnection)                        // get hook function for connection creation
	GetOnConnStop() func(IConnection)                         // get hook function for connection close
	GetPacket() IDataPack                                     // get data protocol packer
	GetMsgHandler() IMsgHandle                                // get message handler
	SetPacket(IDataPack)                                      // set data protocol packer
	StartHeartBeat(time.Duration)                             // start heartbeat check
	StartHeartBeatWithOption(time.Duration, *HeartBeatOption) // start heartbeat check with customized callback function
}

3.2 Client methods

type IClient interface {
	Start()                                                   // start client
	Stop() 													  // stop client
	AddRouter(msgID uint32, router IRouter)                   // add router for handling server response
	Conn() IConnection                                        // get client connection
	SetOnConnStart(func(IConnection))                         // set hook function for connection creation
	SetOnConnStop(func(IConnection))                          // set hook function for connection close
	GetOnConnStart() func(IConnection)                        // get hook function for connection creation
	GetOnConnStop() func(IConnection)                         // get hook function for connection close
	GetPacket() IDataPack                                     // get data protocol packer
	SetPacket(IDataPack)                                      // set data protocol packer
	GetMsgHandler() IMsgHandle                                // get message handler
	StartHeartBeat(time.Duration)                             // start heartbeat check
	StartHeartBeatWithOption(time.Duration, *HeartBeatOption) // start heartbeat check with customized callback function
}