优化kcp网络库
This commit is contained in:
1 parent
cc056b2d2c
commit
669a524153
19 files changed
+7283
-120
No files matched your search
@@ -1,6 +1,5 @@
|
||||
http_port = 8080
|
||||
|
||||
[hk4e]
|
||||
dispatch_http_port = 8080 # dispatch的http端口
|
||||
dispatch_url = "https://hk4e.flswld.com/query_cur_region" # 二级dispatch地址 将域名改为dispatch的外网地址
|
||||
login_sdk_account_key = "" # sdk服务器账号验证的签名密钥
|
||||
forward_mode_enable = false # 是否开启网关到机器人的转发功能
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
http_port = 9001
|
||||
|
||||
[hk4e]
|
||||
gm_http_port = 9001 # gm的http端口
|
||||
gm_auth_key = "flswld" # gm认证密钥
|
||||
|
||||
[logger]
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
http_port = 9999
|
||||
|
||||
[logger]
|
||||
level = "debug"
|
||||
track_line = true
|
||||
@@ -11,6 +9,7 @@ forward_mode_enable = false # 是否开启网关到机器人的转发功能
|
||||
register_all_proto_message = true # 注册全部pb消息
|
||||
|
||||
[hk4e_robot]
|
||||
robot_http_port = 9999 # robot的http端口
|
||||
region_list_url = "https://hk4e.flswld.com/query_region_list" # 一级dispatch地址
|
||||
region_list_param = "" # 一级dispatch的url参数
|
||||
select_region_index = 0 # 选择的二级dispatch索引
|
||||
|
||||
@@ -10,7 +10,6 @@ var CONF *Config = nil
|
||||
|
||||
// Config 配置
|
||||
type Config struct {
|
||||
HttpPort int32 `toml:"http_port"`
|
||||
Hk4e Hk4e `toml:"hk4e"`
|
||||
Hk4eRobot Hk4eRobot `toml:"hk4e_robot"`
|
||||
Logger Logger `toml:"logger"`
|
||||
@@ -21,6 +20,8 @@ type Config struct {
|
||||
|
||||
// Hk4e 原神服务器
|
||||
type Hk4e struct {
|
||||
DispatchHttpPort int32 `toml:"dispatch_http_port"` // dispatch的http端口
|
||||
GmHttpPort int32 `toml:"gm_http_port"` // gm的http端口
|
||||
KcpAddr string `toml:"kcp_addr"` // kcp地址 该地址只用来注册到节点服务器 填网关的外网地址 网关本地监听为0.0.0.0
|
||||
KcpPort int32 `toml:"kcp_port"` // kcp端口号
|
||||
TcpModeEnable bool `toml:"tcp_mode_enable"` // 是否开启tcp模式 需要hook客户端网络库才能支持 共用kcp端口号
|
||||
@@ -38,10 +39,12 @@ type Hk4e struct {
|
||||
ForwardDispatchUrl string `toml:"forward_dispatch_url"` // 转发的二级dispatch地址
|
||||
GmAuthKey string `toml:"gm_auth_key"` // gm认证密钥
|
||||
RegisterAllProtoMessage bool `toml:"register_all_proto_message"` // 注册全部pb消息
|
||||
ByteCheckEnable bool `toml:"byte_check_enable"`
|
||||
}
|
||||
|
||||
// Hk4eRobot 原神机器人
|
||||
type Hk4eRobot struct {
|
||||
RobotHttpPort int32 `toml:"robot_http_port"` // robot的http端口
|
||||
RegionListUrl string `toml:"region_list_url"` // 一级dispatch地址
|
||||
RegionListParam string `toml:"region_list_param"` // 一级dispatch的url参数
|
||||
SelectRegionIndex int32 `toml:"select_region_index"` // 选择的二级dispatch索引
|
||||
|
||||
@@ -162,7 +162,7 @@ func (c *Controller) registerRouter() {
|
||||
engine.StaticFS("/pictures", http.Dir("./static/geetest/pictures"))
|
||||
}
|
||||
engine.POST("/gate/token/verify", c.gateTokenVerify)
|
||||
port := config.GetConfig().HttpPort
|
||||
port := config.GetConfig().Hk4e.DispatchHttpPort
|
||||
addr := ":" + strconv.Itoa(int(port))
|
||||
err := engine.Run(addr)
|
||||
if err != nil {
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* xxHash - Extremely Fast Hash algorithm
|
||||
* Copyright (C) 2012-2023 Yann Collet
|
||||
*
|
||||
* BSD 2-Clause License (https://www.opensource.org/licenses/bsd-license.php)
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are
|
||||
* met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above
|
||||
* copyright notice, this list of conditions and the following disclaimer
|
||||
* in the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* You can contact the author at:
|
||||
* - xxHash homepage: https://www.xxhash.com
|
||||
* - xxHash source repository: https://github.com/Cyan4973/xxHash
|
||||
*/
|
||||
|
||||
/*
|
||||
* xxhash.c instantiates functions defined in xxhash.h
|
||||
*/
|
||||
|
||||
#define XXH_STATIC_LINKING_ONLY /* access advanced declarations */
|
||||
#define XXH_IMPLEMENTATION /* access definitions */
|
||||
|
||||
#include "xxhash.h"
|
||||
File diff suppressed because it is too large.
Load diff
+28
-13
@@ -25,19 +25,23 @@ type Enet struct {
|
||||
|
||||
// Enet连接状态类型
|
||||
const (
|
||||
ConnEnetSyn = "ConnEnetSyn"
|
||||
ConnEnetEst = "ConnEnetEst"
|
||||
ConnEnetFin = "ConnEnetFin"
|
||||
ConnEnetAddrChange = "ConnEnetAddrChange"
|
||||
ConnEnetSyn = "ConnEnetSyn" // 客户端前置握手获取conv
|
||||
ConnEnetEst = "ConnEnetEst" // 连接建立
|
||||
ConnEnetFin = "ConnEnetFin" // 连接断开
|
||||
ConnEnetPing = "ConnEnetPing" // 网络检查
|
||||
)
|
||||
|
||||
// Enet连接状态类型幻数
|
||||
var MagicEnetSynHead, _ = hex.DecodeString("000000ff")
|
||||
var MagicEnetSynTail, _ = hex.DecodeString("ffffffff")
|
||||
var MagicEnetEstHead, _ = hex.DecodeString("00000145")
|
||||
var MagicEnetEstTail, _ = hex.DecodeString("14514545")
|
||||
var MagicEnetFinHead, _ = hex.DecodeString("00000194")
|
||||
var MagicEnetFinTail, _ = hex.DecodeString("19419494")
|
||||
var (
|
||||
MagicEnetSynHead, _ = hex.DecodeString("000000ff")
|
||||
MagicEnetSynTail, _ = hex.DecodeString("ffffffff")
|
||||
MagicEnetEstHead, _ = hex.DecodeString("00000145")
|
||||
MagicEnetEstTail, _ = hex.DecodeString("14514545")
|
||||
MagicEnetFinHead, _ = hex.DecodeString("00000194")
|
||||
MagicEnetFinTail, _ = hex.DecodeString("19419494")
|
||||
MagicEnetPingHead, _ = hex.DecodeString("00000227")
|
||||
MagicEnetPingTail, _ = hex.DecodeString("22722727")
|
||||
)
|
||||
|
||||
// Enet事件类型
|
||||
const (
|
||||
@@ -77,6 +81,9 @@ func BuildEnet(connType string, enetType uint32, sessionId uint32, conv uint32)
|
||||
} else if connType == ConnEnetFin {
|
||||
copy(data[0:4], MagicEnetFinHead)
|
||||
copy(data[16:20], MagicEnetFinTail)
|
||||
} else if connType == ConnEnetPing {
|
||||
copy(data[0:4], MagicEnetPingHead)
|
||||
copy(data[16:20], MagicEnetPingTail)
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
@@ -98,26 +105,34 @@ func ParseEnet(data []byte) (connType string, enetType uint32, sessionId uint32,
|
||||
enetTypeDataBuffer := bytes.NewBuffer(enetTypeData)
|
||||
enetType = uint32(0)
|
||||
_ = binary.Read(enetTypeDataBuffer, binary.BigEndian, &enetType)
|
||||
|
||||
equalHead := bytes.Equal(udpPayloadEnetHead, MagicEnetSynHead)
|
||||
equalTail := bytes.Equal(udpPayloadEnetTail, MagicEnetSynTail)
|
||||
if equalHead && equalTail {
|
||||
// 客户端前置握手获取conv
|
||||
connType = ConnEnetSyn
|
||||
return connType, enetType, sessionId, conv, rawConv, nil
|
||||
}
|
||||
|
||||
equalHead = bytes.Equal(udpPayloadEnetHead, MagicEnetEstHead)
|
||||
equalTail = bytes.Equal(udpPayloadEnetTail, MagicEnetEstTail)
|
||||
if equalHead && equalTail {
|
||||
// 连接建立
|
||||
connType = ConnEnetEst
|
||||
return connType, enetType, sessionId, conv, rawConv, nil
|
||||
}
|
||||
|
||||
equalHead = bytes.Equal(udpPayloadEnetHead, MagicEnetFinHead)
|
||||
equalTail = bytes.Equal(udpPayloadEnetTail, MagicEnetFinTail)
|
||||
if equalHead && equalTail {
|
||||
// 连接断开
|
||||
connType = ConnEnetFin
|
||||
return connType, enetType, sessionId, conv, rawConv, nil
|
||||
}
|
||||
|
||||
equalHead = bytes.Equal(udpPayloadEnetHead, MagicEnetPingHead)
|
||||
equalTail = bytes.Equal(udpPayloadEnetTail, MagicEnetPingTail)
|
||||
if equalHead && equalTail {
|
||||
connType = ConnEnetPing
|
||||
return connType, enetType, sessionId, conv, rawConv, nil
|
||||
}
|
||||
|
||||
return "", 0, 0, 0, 0, errors.New("unknown conn type")
|
||||
}
|
||||
+29
-7
@@ -22,15 +22,27 @@ const (
|
||||
IKCP_MTU_DEF = 1400
|
||||
IKCP_ACK_FAST = 3
|
||||
IKCP_INTERVAL = 100
|
||||
IKCP_OVERHEAD = 24 + 4 // KCP组合会话id是8个字节
|
||||
IKCP_DEADLINK = 20
|
||||
IKCP_THRESH_INIT = 2
|
||||
IKCP_THRESH_MIN = 2
|
||||
IKCP_PROBE_INIT = 7000 // 7 secs to probe window size
|
||||
IKCP_PROBE_LIMIT = 120000 // up to 120 secs to probe window
|
||||
IKCP_SN_OFFSET = 12 + 4 // KCP组合会话id是8个字节
|
||||
IKCP_SN_OFFSET = 16
|
||||
)
|
||||
|
||||
var IKCP_OVERHEAD = 28
|
||||
|
||||
var (
|
||||
byteCheckModeEnable bool
|
||||
byteCheckMode int
|
||||
)
|
||||
|
||||
func enableByteCheckMode(mode int) {
|
||||
byteCheckModeEnable = true
|
||||
byteCheckMode = mode
|
||||
IKCP_OVERHEAD += 4
|
||||
}
|
||||
|
||||
// monotonic reference time point
|
||||
var refTime = time.Now()
|
||||
|
||||
@@ -137,6 +149,9 @@ func (seg *segment) encode(ptr []byte) []byte {
|
||||
ptr = ikcp_encode32u(ptr, seg.sn)
|
||||
ptr = ikcp_encode32u(ptr, seg.una)
|
||||
ptr = ikcp_encode32u(ptr, uint32(len(seg.data)))
|
||||
if byteCheckModeEnable {
|
||||
ptr = ikcp_encode32u(ptr, byte_check_hash(seg.data))
|
||||
}
|
||||
atomic.AddUint64(&DefaultSnmp.OutSegs, 1)
|
||||
return ptr
|
||||
}
|
||||
@@ -187,7 +202,7 @@ func NewKCP(conv uint64, output output_callback) *KCP {
|
||||
kcp.rcv_wnd = IKCP_WND_RCV
|
||||
kcp.rmt_wnd = IKCP_WND_RCV
|
||||
kcp.mtu = IKCP_MTU_DEF
|
||||
kcp.mss = kcp.mtu - IKCP_OVERHEAD
|
||||
kcp.mss = kcp.mtu - uint32(IKCP_OVERHEAD)
|
||||
kcp.buffer = make([]byte, kcp.mtu)
|
||||
kcp.rx_rto = IKCP_RTO_DEF
|
||||
kcp.rx_minrto = IKCP_RTO_MIN
|
||||
@@ -218,11 +233,11 @@ func (kcp *KCP) delSegment(seg *segment) {
|
||||
//
|
||||
// Return false if n >= mss
|
||||
func (kcp *KCP) ReserveBytes(n int) bool {
|
||||
if n >= int(kcp.mtu-IKCP_OVERHEAD) || n < 0 {
|
||||
if n >= int(kcp.mtu-uint32(IKCP_OVERHEAD)) || n < 0 {
|
||||
return false
|
||||
}
|
||||
kcp.reserved = n
|
||||
kcp.mss = kcp.mtu - IKCP_OVERHEAD - uint32(n)
|
||||
kcp.mss = kcp.mtu - uint32(IKCP_OVERHEAD) - uint32(n)
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -572,6 +587,13 @@ func (kcp *KCP) Input(data []byte, regular, ackNoDelay bool) int {
|
||||
data = ikcp_decode32u(data, &sn)
|
||||
data = ikcp_decode32u(data, &una)
|
||||
data = ikcp_decode32u(data, &length)
|
||||
if byteCheckModeEnable {
|
||||
var hash uint32
|
||||
data = ikcp_decode32u(data, &hash)
|
||||
if hash != byte_check_hash(data[:length]) {
|
||||
return -4
|
||||
}
|
||||
}
|
||||
if len(data) < int(length) {
|
||||
return -2
|
||||
}
|
||||
@@ -1003,7 +1025,7 @@ func (kcp *KCP) SetMtu(mtu int) int {
|
||||
if mtu < 50 || mtu < IKCP_OVERHEAD {
|
||||
return -1
|
||||
}
|
||||
if kcp.reserved >= int(kcp.mtu-IKCP_OVERHEAD) || kcp.reserved < 0 {
|
||||
if kcp.reserved >= int(kcp.mtu-uint32(IKCP_OVERHEAD)) || kcp.reserved < 0 {
|
||||
return -1
|
||||
}
|
||||
|
||||
@@ -1012,7 +1034,7 @@ func (kcp *KCP) SetMtu(mtu int) int {
|
||||
return -2
|
||||
}
|
||||
kcp.mtu = uint32(mtu)
|
||||
kcp.mss = kcp.mtu - IKCP_OVERHEAD - uint32(kcp.reserved)
|
||||
kcp.mss = kcp.mtu - uint32(IKCP_OVERHEAD) - uint32(kcp.reserved)
|
||||
kcp.buffer = buffer
|
||||
return 0
|
||||
}
|
||||
|
||||
+60
-3
@@ -355,6 +355,17 @@ func (s *UDPSession) Close() error {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *UDPSession) CloseConn(enetType uint32) error {
|
||||
s.SendEnetNotifyToPeer(&Enet{
|
||||
Addr: s.RemoteAddr().String(),
|
||||
SessionId: s.GetSessionId(),
|
||||
Conv: s.GetConv(),
|
||||
ConnType: ConnEnetFin,
|
||||
EnetType: enetType,
|
||||
})
|
||||
return s.Close()
|
||||
}
|
||||
|
||||
// LocalAddr returns the local network address. The Addr returned is shared by all invocations of LocalAddr, so do not modify it.
|
||||
func (s *UDPSession) LocalAddr() net.Addr { return s.conn.LocalAddr() }
|
||||
|
||||
@@ -679,12 +690,58 @@ type (
|
||||
xconn batchConn // for x/net
|
||||
xconnWriteError error
|
||||
|
||||
enetNotifyChan chan *Enet // Enet事件上报管道
|
||||
enetNotifyChan chan *Enet // Enet事件上报管道
|
||||
sessionIdCounter uint32
|
||||
}
|
||||
)
|
||||
|
||||
func (l *Listener) GetEnetNotifyChan() chan *Enet {
|
||||
return l.enetNotifyChan
|
||||
func (l *Listener) EnetHandle() {
|
||||
go func() {
|
||||
for {
|
||||
enetNotify := <-l.enetNotifyChan
|
||||
switch enetNotify.ConnType {
|
||||
case ConnEnetSyn:
|
||||
if enetNotify.EnetType != EnetClientConnectKey {
|
||||
continue
|
||||
}
|
||||
sessionId := atomic.AddUint32(&l.sessionIdCounter, 1)
|
||||
var conv uint32
|
||||
_ = binary.Read(rand.Reader, binary.LittleEndian, &conv)
|
||||
l.SendEnetNotifyToPeer(&Enet{
|
||||
Addr: enetNotify.Addr,
|
||||
SessionId: sessionId,
|
||||
Conv: conv,
|
||||
ConnType: ConnEnetEst,
|
||||
EnetType: enetNotify.EnetType,
|
||||
})
|
||||
case ConnEnetFin:
|
||||
rawConvData := make([]byte, 8)
|
||||
binary.LittleEndian.PutUint32(rawConvData[0:4], enetNotify.SessionId)
|
||||
binary.LittleEndian.PutUint32(rawConvData[4:8], enetNotify.Conv)
|
||||
rawConv := binary.LittleEndian.Uint64(rawConvData)
|
||||
l.sessionLock.RLock()
|
||||
conn, exist := l.sessions[rawConv]
|
||||
l.sessionLock.RUnlock()
|
||||
if !exist {
|
||||
continue
|
||||
}
|
||||
_ = conn.CloseConn(enetNotify.EnetType)
|
||||
case ConnEnetPing:
|
||||
l.SendEnetNotifyToPeer(&Enet{
|
||||
Addr: enetNotify.Addr,
|
||||
SessionId: 0,
|
||||
Conv: 0,
|
||||
ConnType: ConnEnetPing,
|
||||
EnetType: enetNotify.EnetType,
|
||||
})
|
||||
default:
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (l *Listener) EnableByteCheckMode(mode int) {
|
||||
enableByteCheckMode(mode)
|
||||
}
|
||||
|
||||
// packet input stage
|
||||
|
||||
+12
-14
@@ -31,14 +31,7 @@ func (s *UDPSession) defaultRx() {
|
||||
continue
|
||||
}
|
||||
if connType == ConnEnetFin {
|
||||
s.defaultSendEnetNotifyToPeer(&Enet{
|
||||
Addr: s.remote.String(),
|
||||
SessionId: sessionId,
|
||||
Conv: conv,
|
||||
ConnType: ConnEnetFin,
|
||||
EnetType: enetType,
|
||||
})
|
||||
_ = s.Close()
|
||||
_ = s.CloseConn(enetType)
|
||||
continue
|
||||
}
|
||||
}
|
||||
@@ -69,14 +62,13 @@ func (l *Listener) defaultRx() {
|
||||
}
|
||||
switch connType {
|
||||
case ConnEnetSyn:
|
||||
// 客户端前置握手获取conv
|
||||
l.enetNotifyChan <- &Enet{Addr: from.String(), SessionId: sessionId, Conv: conv, ConnType: ConnEnetSyn, EnetType: enetType}
|
||||
case ConnEnetEst:
|
||||
// 连接建立
|
||||
l.enetNotifyChan <- &Enet{Addr: from.String(), SessionId: sessionId, Conv: conv, ConnType: ConnEnetEst, EnetType: enetType}
|
||||
case ConnEnetFin:
|
||||
// 连接断开
|
||||
l.enetNotifyChan <- &Enet{Addr: from.String(), SessionId: sessionId, Conv: conv, ConnType: ConnEnetFin, EnetType: enetType}
|
||||
case ConnEnetPing:
|
||||
l.enetNotifyChan <- &Enet{Addr: from.String(), SessionId: sessionId, Conv: conv, ConnType: ConnEnetPing, EnetType: enetType}
|
||||
default:
|
||||
continue
|
||||
}
|
||||
@@ -92,8 +84,6 @@ func (l *Listener) defaultRx() {
|
||||
if exist {
|
||||
if conn.remote.String() != from.String() {
|
||||
conn.remote = from
|
||||
// 连接地址改变
|
||||
l.enetNotifyChan <- &Enet{Addr: conn.remote.String(), SessionId: sessionId, Conv: conv, ConnType: ConnEnetAddrChange}
|
||||
}
|
||||
}
|
||||
l.packetInput(udpPayload, from, rawConv)
|
||||
@@ -147,5 +137,13 @@ func (s *UDPSession) defaultSendEnetNotifyToPeer(enet *Enet) {
|
||||
if data == nil {
|
||||
return
|
||||
}
|
||||
_, _ = s.conn.(*net.UDPConn).Write(data)
|
||||
if s.l != nil {
|
||||
remoteAddr, err := net.ResolveUDPAddr("udp", enet.Addr)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
_, _ = s.conn.WriteTo(data, remoteAddr)
|
||||
} else {
|
||||
_, _ = s.conn.(*net.UDPConn).Write(data)
|
||||
}
|
||||
}
|
||||
@@ -46,14 +46,7 @@ func (s *UDPSession) rx() {
|
||||
continue
|
||||
}
|
||||
if connType == ConnEnetFin {
|
||||
s.SendEnetNotifyToPeer(&Enet{
|
||||
Addr: s.remote.String(),
|
||||
SessionId: sessionId,
|
||||
Conv: conv,
|
||||
ConnType: ConnEnetFin,
|
||||
EnetType: enetType,
|
||||
})
|
||||
_ = s.Close()
|
||||
_ = s.CloseConn(enetType)
|
||||
continue
|
||||
}
|
||||
}
|
||||
@@ -108,14 +101,13 @@ func (l *Listener) rx() {
|
||||
}
|
||||
switch connType {
|
||||
case ConnEnetSyn:
|
||||
// 客户端前置握手获取conv
|
||||
l.enetNotifyChan <- &Enet{Addr: msg.Addr.String(), SessionId: sessionId, Conv: conv, ConnType: ConnEnetSyn, EnetType: enetType}
|
||||
case ConnEnetEst:
|
||||
// 连接建立
|
||||
l.enetNotifyChan <- &Enet{Addr: msg.Addr.String(), SessionId: sessionId, Conv: conv, ConnType: ConnEnetEst, EnetType: enetType}
|
||||
case ConnEnetFin:
|
||||
// 连接断开
|
||||
l.enetNotifyChan <- &Enet{Addr: msg.Addr.String(), SessionId: sessionId, Conv: conv, ConnType: ConnEnetFin, EnetType: enetType}
|
||||
case ConnEnetPing:
|
||||
l.enetNotifyChan <- &Enet{Addr: msg.Addr.String(), SessionId: sessionId, Conv: conv, ConnType: ConnEnetPing, EnetType: enetType}
|
||||
default:
|
||||
continue
|
||||
}
|
||||
@@ -131,8 +123,6 @@ func (l *Listener) rx() {
|
||||
if exist {
|
||||
if conn.remote.String() != msg.Addr.String() {
|
||||
conn.remote = msg.Addr
|
||||
// 连接地址改变
|
||||
l.enetNotifyChan <- &Enet{Addr: conn.remote.String(), SessionId: sessionId, Conv: conv, ConnType: ConnEnetAddrChange}
|
||||
}
|
||||
}
|
||||
l.packetInput(udpPayload, msg.Addr, rawConv)
|
||||
@@ -224,6 +214,11 @@ func (l *Listener) SendEnetNotifyToPeer(enet *Enet) {
|
||||
}
|
||||
|
||||
func (s *UDPSession) SendEnetNotifyToPeer(enet *Enet) {
|
||||
// default version
|
||||
if s.xconn == nil || s.xconnWriteError != nil {
|
||||
s.defaultSendEnetNotifyToPeer(enet)
|
||||
return
|
||||
}
|
||||
data := BuildEnet(enet.ConnType, enet.EnetType, s.GetSessionId(), s.GetConv())
|
||||
if data == nil {
|
||||
return
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
//go:build !xxhash
|
||||
// +build !xxhash
|
||||
|
||||
package kcp
|
||||
|
||||
func byte_check_hash(data []byte) uint32 {
|
||||
switch byteCheckMode {
|
||||
case 1:
|
||||
return 0
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
//go:build xxhash
|
||||
// +build xxhash
|
||||
|
||||
package kcp
|
||||
|
||||
// #include "./cgo/xxhash.c"
|
||||
import "C"
|
||||
|
||||
func byte_check_hash(data []byte) uint32 {
|
||||
switch byteCheckMode {
|
||||
case 1:
|
||||
d := C.CBytes(data)
|
||||
h := C.XXH3_64bits(d, C.ulonglong(len(data)))
|
||||
C.free(d)
|
||||
return uint32(h)
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
+6
-2
@@ -49,9 +49,13 @@ func (c *Conn) GetConv() uint32 {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Conn) Close() {
|
||||
func (c *Conn) Close(enetType ...uint32) {
|
||||
if c.isKcp {
|
||||
_ = c.kcpConn.Close()
|
||||
if len(enetType) == 0 {
|
||||
_ = c.kcpConn.CloseConn(kcp.EnetTimeout)
|
||||
} else {
|
||||
_ = c.kcpConn.CloseConn(enetType[0])
|
||||
}
|
||||
} else {
|
||||
_ = c.tcpConn.Close()
|
||||
}
|
||||
|
||||
@@ -39,9 +39,8 @@ const (
|
||||
var CLIENT_CONN_NUM int32 = 0 // 当前客户端连接数
|
||||
|
||||
const (
|
||||
KcpConnEstNotify = "KcpConnEstNotify"
|
||||
KcpConnAddrChangeNotify = "KcpConnAddrChangeNotify"
|
||||
KcpConnCloseNotify = "KcpConnCloseNotify"
|
||||
KcpConnEstNotify = "KcpConnEstNotify"
|
||||
KcpConnCloseNotify = "KcpConnCloseNotify"
|
||||
)
|
||||
|
||||
type KcpEvent struct {
|
||||
@@ -133,10 +132,13 @@ func (k *KcpConnManager) run() error {
|
||||
logger.Error("listen kcp err: %v", err)
|
||||
return err
|
||||
}
|
||||
kcpListener.EnetHandle()
|
||||
if config.GetConfig().Hk4e.ByteCheckEnable {
|
||||
kcpListener.EnableByteCheckMode(1)
|
||||
}
|
||||
k.kcpListener = kcpListener
|
||||
logger.Info("listen kcp at addr: %v", addr)
|
||||
go k.kcpNetInfo()
|
||||
go k.kcpEnetHandle(kcpListener)
|
||||
go k.acceptHandle(false, kcpListener, nil)
|
||||
if config.GetConfig().Hk4e.TcpModeEnable {
|
||||
// tcp
|
||||
@@ -305,49 +307,6 @@ func (k *KcpConnManager) acceptHandle(tcpMode bool, kcpListener *kcp.Listener, t
|
||||
}
|
||||
}
|
||||
|
||||
// kcp连接事件处理函数
|
||||
func (k *KcpConnManager) kcpEnetHandle(listener *kcp.Listener) {
|
||||
logger.Info("kcp enet handle start")
|
||||
for {
|
||||
enetNotify := <-listener.GetEnetNotifyChan()
|
||||
logger.Info("[Kcp Enet] addr: %v, conv: %v, sessionId: %v, connType: %v, enetType: %v",
|
||||
enetNotify.Addr, enetNotify.Conv, enetNotify.SessionId, enetNotify.ConnType, enetNotify.EnetType)
|
||||
switch enetNotify.ConnType {
|
||||
case kcp.ConnEnetSyn:
|
||||
if enetNotify.EnetType != kcp.EnetClientConnectKey {
|
||||
logger.Error("enet type not match, sessionId: %v", enetNotify.SessionId)
|
||||
continue
|
||||
}
|
||||
sessionId := atomic.AddUint32(&k.sessionIdCounter, 1)
|
||||
listener.SendEnetNotifyToPeer(&kcp.Enet{
|
||||
Addr: enetNotify.Addr,
|
||||
SessionId: sessionId,
|
||||
Conv: binary.BigEndian.Uint32(random.GetRandomByte(4)),
|
||||
ConnType: kcp.ConnEnetEst,
|
||||
EnetType: enetNotify.EnetType,
|
||||
})
|
||||
case kcp.ConnEnetFin:
|
||||
session := k.GetSession(enetNotify.SessionId)
|
||||
if session == nil {
|
||||
continue
|
||||
}
|
||||
if session.conn.GetConv() != enetNotify.Conv {
|
||||
logger.Error("conv not match, sessionId: %v", enetNotify.SessionId)
|
||||
continue
|
||||
}
|
||||
k.closeKcpConn(session, enetNotify.EnetType)
|
||||
case kcp.ConnEnetAddrChange:
|
||||
// 连接地址改变通知
|
||||
k.kcpEventChan <- &KcpEvent{
|
||||
SessionId: enetNotify.SessionId,
|
||||
EventId: KcpConnAddrChangeNotify,
|
||||
EventMessage: enetNotify.Addr,
|
||||
}
|
||||
default:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Session 连接会话结构 只允许定义并发安全或者简单的基础数据结构
|
||||
type Session struct {
|
||||
sessionId uint32
|
||||
@@ -583,16 +542,7 @@ func (k *KcpConnManager) closeKcpConn(session *Session, enetType uint32) {
|
||||
// 清理数据
|
||||
k.DeleteSession(session.sessionId, session.userId)
|
||||
// 关闭连接
|
||||
if !session.conn.IsTcpMode() {
|
||||
k.kcpListener.SendEnetNotifyToPeer(&kcp.Enet{
|
||||
Addr: session.conn.RemoteAddr(),
|
||||
SessionId: session.conn.GetSessionId(),
|
||||
Conv: session.conn.GetConv(),
|
||||
ConnType: kcp.ConnEnetFin,
|
||||
EnetType: enetType,
|
||||
})
|
||||
}
|
||||
session.conn.Close()
|
||||
session.conn.Close(enetType)
|
||||
// 连接关闭通知
|
||||
k.kcpEventChan <- &KcpEvent{
|
||||
SessionId: session.sessionId,
|
||||
|
||||
@@ -107,7 +107,7 @@ func (c *Controller) registerRouter() {
|
||||
engine.POST("/server/white/add", c.serverWhiteAdd)
|
||||
engine.POST("/server/white/del", c.serverWhiteDel)
|
||||
engine.POST("/server/dispatch/cancel", c.serverDispatchCancel)
|
||||
port := config.GetConfig().HttpPort
|
||||
port := config.GetConfig().Hk4e.GmHttpPort
|
||||
addr := ":" + strconv.Itoa(int(port))
|
||||
err := engine.Run(addr)
|
||||
if err != nil {
|
||||
|
||||
+1
-1
@@ -114,7 +114,7 @@ func runPacketCaptureApi() {
|
||||
e.GET("/packet/capture/ws", packetCaptureWs)
|
||||
e.GET("/packet/capture/list", packetCaptureList)
|
||||
e.GET("/packet/capture/clear", packetCaptureClear)
|
||||
port := config.GetConfig().HttpPort
|
||||
port := config.GetConfig().Hk4eRobot.RobotHttpPort
|
||||
addr := ":" + strconv.Itoa(int(port))
|
||||
err := e.Run(addr)
|
||||
if err != nil {
|
||||
|
||||
@@ -111,7 +111,7 @@ func (s *Session) Close() {
|
||||
return
|
||||
}
|
||||
s.IsClose = true
|
||||
_ = s.Conn.Close()
|
||||
_ = s.Conn.CloseConn(kcp.EnetClientClose)
|
||||
s.DeadEvent <- true
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user