Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加了几个配置项用于设置kcp conn #299

Merged
merged 2 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions examples/zinx_kcp/conf/zinx.json

This file was deleted.

11 changes: 3 additions & 8 deletions examples/zinx_kcp/kcp_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion examples/zinx_kcp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"errors"
"fmt"
"github.com/aceld/zinx/zconf"
"time"

"github.com/aceld/zinx/ziface"
Expand Down Expand Up @@ -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()
}
22 changes: 22 additions & 0 deletions zconf/globalobj.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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.
Expand Down
37 changes: 37 additions & 0 deletions zconf/userconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

}
51 changes: 48 additions & 3 deletions znet/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -137,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
}

Expand Down Expand Up @@ -318,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 {
Expand All @@ -343,7 +381,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)
}
Expand Down
Loading