From 7ac540e26996c4cba1e37110df40f304c46d94cf Mon Sep 17 00:00:00 2001 From: "zheng.ma" Date: Wed, 31 Jan 2024 16:08:23 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=A2=9E=E5=8A=A0kcpconn=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- zconf/globalobj.go | 22 +++++++++++++++++++++ znet/server.go | 48 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/zconf/globalobj.go b/zconf/globalobj.go index 86816794..4c212d55 100644 --- a/zconf/globalobj.go +++ b/zconf/globalobj.go @@ -45,6 +45,18 @@ type Config struct { Name string // The name of the current server.(当前服务器名称) KcpPort int // he port number on which the server listens for KCP connections.(当前服务器主机监听端口号) + /* + ServerConfig + */ + KcpACKNoDelay bool // changes ack flush option, set true to flush ack immediately, + KcpStreamMode bool // toggles the stream mode on/off + KcpNoDelay int // Whether nodelay mode is enabled, 0 is not enabled; 1 enabled. + KcpInterval int // Protocol internal work interval, in milliseconds, such as 10 ms or 20 ms. + KcpResend int // Fast retransmission mode, 0 represents off by default, 2 can be set (2 ACK spans will result in direct retransmission) + KcpNc int // Whether to turn off flow control, 0 represents “Do not turn off” by default, 1 represents “Turn off”. + KcpSendWindow int // SND_BUF, this unit is the packet, default 32. + KcpRecvWindow int // RCV_BUF, this unit is the packet, default 32. + /* Zinx */ @@ -225,6 +237,16 @@ func init() { PrivateKeyFile: "", Mode: ServerModeTcp, RouterSlicesMode: false, + KcpACKNoDelay: false, + KcpStreamMode: true, + //Normal Mode: ikcp_nodelay(kcp, 0, 40, 0, 0); + //Turbo Mode: ikcp_nodelay(kcp, 1, 10, 2, 1); + KcpNoDelay: 1, + KcpInterval: 10, + KcpResend: 2, + KcpNc: 1, + KcpRecvWindow: 32, + KcpSendWindow: 32, } // Note: Load some user-configured parameters from the configuration file. diff --git a/znet/server.go b/znet/server.go index c1429e07..794b10e7 100644 --- a/znet/server.go +++ b/znet/server.go @@ -80,10 +80,39 @@ type Server struct { // websocket connection authentication websocketAuth func(r *http.Request) error + kcpConfig *KcpConfig + // connection id cID uint64 } +type KcpConfig struct { + // changes ack flush option, set true to flush ack immediately, + // (改变ack刷新选项,设置为true立即刷新ack) + KcpACKNoDelay bool + // toggles the stream mode on/off + // (切换流模式开/关) + KcpStreamMode bool + // Whether nodelay mode is enabled, 0 is not enabled; 1 enabled. + // (是否启用nodelay模式,0不启用;1启用) + KcpNoDelay int + // Protocol internal work interval, in milliseconds, such as 10 ms or 20 ms. + // (协议内部工作的间隔,单位毫秒,比如10ms或者20ms) + KcpInterval int + // Fast retransmission mode, 0 represents off by default, 2 can be set (2 ACK spans will result in direct retransmission) + // (快速重传模式,默认为0关闭,可以设置2(2次ACK跨越将会直接重传) + KcpResend int + // Whether to turn off flow control, 0 represents “Do not turn off” by default, 1 represents “Turn off”. + // (是否关闭流控,默认是0代表不关闭,1代表关闭) + KcpNc int + // SND_BUF, this unit is the packet, default 32. + // (SND_BUF发送缓冲区大小,单位是包,默认是32) + KcpSendWindow int + // RCV_BUF, this unit is the packet, default 32. + // (RCV_BUF接收缓冲区大小,单位是包,默认是32) + KcpRecvWindow int +} + // newServerWithConfig creates a server handle based on config // (根据config创建一个服务器句柄) func newServerWithConfig(config *zconf.Config, ipVersion string, opts ...Option) ziface.IServer { @@ -110,6 +139,16 @@ func newServerWithConfig(config *zconf.Config, ipVersion string, opts ...Option) return true }, }, + kcpConfig: &KcpConfig{ + KcpACKNoDelay: config.KcpACKNoDelay, + KcpStreamMode: config.KcpStreamMode, + KcpNoDelay: config.KcpNoDelay, + KcpInterval: config.KcpInterval, + KcpResend: config.KcpResend, + KcpNc: config.KcpNc, + KcpSendWindow: config.KcpSendWindow, + KcpRecvWindow: config.KcpRecvWindow, + }, } for _, opt := range opts { @@ -343,7 +382,14 @@ func (s *Server) ListenKcpConn() { // 3.4 Handle the business method for this new connection request. At this time, the handler and conn should be bound. // (处理该新连接请求的 业务 方法, 此时应该有 handler 和 conn 是绑定的) newCid := atomic.AddUint64(&s.cID, 1) - dealConn := newKcpServerConn(s, conn.(*kcp.UDPSession), newCid) + + kcpConn := conn.(*kcp.UDPSession) + kcpConn.SetACKNoDelay(s.kcpConfig.KcpACKNoDelay) + kcpConn.SetStreamMode(s.kcpConfig.KcpStreamMode) + kcpConn.SetNoDelay(s.kcpConfig.KcpNoDelay, s.kcpConfig.KcpInterval, s.kcpConfig.KcpResend, s.kcpConfig.KcpNc) + kcpConn.SetWindowSize(s.kcpConfig.KcpSendWindow, s.kcpConfig.KcpRecvWindow) + + dealConn := newKcpServerConn(s, kcpConn, newCid) go s.StartConn(dealConn) } From fab55ddd5839e43a7cb58de3ebd433820e7243d7 Mon Sep 17 00:00:00 2001 From: "zheng.ma" Date: Wed, 31 Jan 2024 16:39:52 +0800 Subject: [PATCH 2/2] =?UTF-8?q?kcp=E5=8F=82=E6=95=B0=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E5=80=BC=20=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/zinx_kcp/conf/zinx.json | 10 --------- examples/zinx_kcp/kcp_client.go | 11 +++------- examples/zinx_kcp/server.go | 12 ++++++++++- zconf/userconf.go | 37 ++++++++++++++++++++++++++++++++ znet/server.go | 3 +-- 5 files changed, 52 insertions(+), 21 deletions(-) delete mode 100644 examples/zinx_kcp/conf/zinx.json diff --git a/examples/zinx_kcp/conf/zinx.json b/examples/zinx_kcp/conf/zinx.json deleted file mode 100644 index f8b28b3a..00000000 --- a/examples/zinx_kcp/conf/zinx.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "Name":"zinx v-0.10 demoApp", - "Host":"127.0.0.1", - "KcpPort":7777, - "MaxConn":3, - "WorkerPoolSize":10, - "LogDir": "./mylog", - "LogFile":"zinx.log", - "Mode":"kcp" -} diff --git a/examples/zinx_kcp/kcp_client.go b/examples/zinx_kcp/kcp_client.go index b05c6082..0c856052 100644 --- a/examples/zinx_kcp/kcp_client.go +++ b/examples/zinx_kcp/kcp_client.go @@ -2,22 +2,17 @@ package main import ( "fmt" - "io" - "time" - "github.com/aceld/zinx/ziface" "github.com/aceld/zinx/zpack" "github.com/xtaci/kcp-go" + "io" ) -//模拟客户端 +// 模拟客户端 func main() { fmt.Println("Client Test ... start") - // Send a test request after 3 seconds to give the server a chance to start the service. (3秒之后发起测试请求,给服务端开启服务的机会) - time.Sleep(3 * time.Second) - // Replace net.Dial with kcp.DialWithOptions - conn, err := kcp.DialWithOptions("127.0.0.1:7777", nil, 0, 0) + conn, err := kcp.Dial("127.0.0.1:7777") if err != nil { fmt.Println("client start err, exit!") return diff --git a/examples/zinx_kcp/server.go b/examples/zinx_kcp/server.go index 2bee75cd..03213ae4 100644 --- a/examples/zinx_kcp/server.go +++ b/examples/zinx_kcp/server.go @@ -3,6 +3,7 @@ package main import ( "errors" "fmt" + "github.com/aceld/zinx/zconf" "time" "github.com/aceld/zinx/ziface" @@ -67,7 +68,16 @@ func Err() error { } func main() { - s := znet.NewServer() + s := znet.NewUserConfServer(&zconf.Config{ + Mode: "kcp", + KcpPort: 7777, + KcpRecvWindow: 128, + KcpSendWindow: 128, + KcpStreamMode: true, + KcpACKNoDelay: false, + LogDir: "./", + LogFile: "test.log", + }) s.AddRouter(1, &TestRouter{}) s.Serve() } diff --git a/zconf/userconf.go b/zconf/userconf.go index 8779feb3..fe47eaa9 100644 --- a/zconf/userconf.go +++ b/zconf/userconf.go @@ -88,4 +88,41 @@ func UserConfToGlobal(config *Config) { if config.RouterSlicesMode { GlobalObject.RouterSlicesMode = config.RouterSlicesMode } + + if config.KcpPort != 0 { + GlobalObject.KcpPort = config.KcpPort + } + + if config.KcpACKNoDelay { + GlobalObject.KcpACKNoDelay = config.KcpACKNoDelay + } + + if !config.KcpStreamMode { + GlobalObject.KcpStreamMode = config.KcpStreamMode + } + + if config.KcpNoDelay != 0 { + GlobalObject.KcpNoDelay = config.KcpNoDelay + } + + if config.KcpInterval != 0 { + GlobalObject.KcpInterval = config.KcpInterval + } + + if config.KcpResend != 0 { + GlobalObject.KcpResend = config.KcpResend + } + + if config.KcpNc != 0 { + GlobalObject.KcpNc = config.KcpNc + } + + if config.KcpSendWindow != 0 { + GlobalObject.KcpSendWindow = config.KcpSendWindow + } + + if config.KcpRecvWindow != 0 { + GlobalObject.KcpRecvWindow = config.KcpRecvWindow + } + } diff --git a/znet/server.go b/znet/server.go index 794b10e7..3d777c6c 100644 --- a/znet/server.go +++ b/znet/server.go @@ -176,7 +176,7 @@ func NewUserConfServer(config *zconf.Config, opts ...Option) ziface.IServer { // (刷新用户配置到全局配置变量) zconf.UserConfToGlobal(config) - s := newServerWithConfig(config, "tcp4", opts...) + s := newServerWithConfig(zconf.GlobalObject, "tcp4", opts...) return s } @@ -357,7 +357,6 @@ func (s *Server) ListenKcpConn() { } zlog.Ins().InfoF("[START] KCP server listening at IP: %s, Port %d, Addr %s", s.IP, s.KcpPort, listener.Addr().String()) - // 2. Start server network connection business go func() { for {