正确的KCP协议sessionId和conv解析

This commit is contained in:
flswld committed 2023-07-23 00:36:12 +08:00
1 parent 4f033f5501
commit 2b140361ca
9 files changed
+125 -33

No files matched your search

+4 -4
View File
@@ -8,17 +8,17 @@ import (
)
// Enet连接控制协议
// MM MM MM MM | CC CC CC CC | SS SS SS SS | EE EE EE EE | MM MM MM MM
// MM MM MM MM | SS SS SS SS | CC CC CC CC | EE EE EE EE | MM MM MM MM
// MM为表示连接状态的幻数 在开头的4字节和结尾的4字节
// CC为conv 4字节
// SS为sessionId 4字节
// CC为conv 4字节
// EE为Enet事件类型 4字节
// Enet Enet协议上报结构体
type Enet struct {
Addr string
Conv uint32
SessionId uint32
Conv uint32
ConnType uint8
EnetType uint32
}
@@ -89,7 +89,7 @@ func BuildEnet(connType uint8, enetType uint32, sessionId uint32, conv uint32) [
func ParseEnet(data []byte) (connType uint8, enetType uint32, sessionId uint32, conv uint32, rawConv uint64, err error) {
sessionId = binary.BigEndian.Uint32(data[4:8])
conv = binary.BigEndian.Uint32(data[8:12])
rawConv = binary.BigEndian.Uint64(data[4:12])
rawConv = binary.LittleEndian.Uint64(data[4:12])
// 提取Enet协议头部和尾部幻数
udpPayloadEnetHead := data[:4]
udpPayloadEnetTail := data[len(data)-4:]
+11 -7
View File
@@ -561,14 +561,18 @@ func (s *UDPSession) GetRawConv() uint64 {
return s.kcp.conv
}
// GetConv 获取KCP会话id
func (s *UDPSession) GetConv() uint32 {
return uint32(s.kcp.conv >> 32)
}
// GetSessionId 获取会话id
func (s *UDPSession) GetSessionId() uint32 {
return uint32(s.kcp.conv >> 0)
rawConvData := make([]byte, 8)
binary.LittleEndian.PutUint64(rawConvData, s.kcp.conv)
return binary.LittleEndian.Uint32(rawConvData[0:4])
}
// GetConv 获取KCP会话id
func (s *UDPSession) GetConv() uint32 {
rawConvData := make([]byte, 8)
binary.LittleEndian.PutUint64(rawConvData, s.kcp.conv)
return binary.LittleEndian.Uint32(rawConvData[4:8])
}
// GetRTO gets current rto of the session
@@ -913,8 +917,8 @@ func DialWithOptions(raddr string) (*UDPSession, error) {
}
enet := &Enet{
Addr: udpaddr.String(),
Conv: 0,
SessionId: 0,
Conv: 0,
ConnType: ConnEnetSyn,
EnetType: EnetClientConnectKey,
}
+7 -7
View File
@@ -67,8 +67,8 @@ func (l *Listener) defaultMonitor() {
// 客户端前置握手获取conv
l.EnetNotify <- &Enet{
Addr: from.String(),
Conv: conv,
SessionId: sessionId,
Conv: conv,
ConnType: ConnEnetSyn,
EnetType: enetType,
}
@@ -76,8 +76,8 @@ func (l *Listener) defaultMonitor() {
// 连接建立
l.EnetNotify <- &Enet{
Addr: from.String(),
Conv: conv,
SessionId: sessionId,
Conv: conv,
ConnType: ConnEnetEst,
EnetType: enetType,
}
@@ -85,8 +85,8 @@ func (l *Listener) defaultMonitor() {
// 连接断开
l.EnetNotify <- &Enet{
Addr: from.String(),
Conv: conv,
SessionId: sessionId,
Conv: conv,
ConnType: ConnEnetFin,
EnetType: enetType,
}
@@ -95,9 +95,9 @@ func (l *Listener) defaultMonitor() {
}
} else {
// 正常KCP包
conv = binary.BigEndian.Uint32(udpPayload[0:4])
sessionId = binary.BigEndian.Uint32(udpPayload[4:8])
rawConv = binary.BigEndian.Uint64(udpPayload[0:8])
sessionId = binary.LittleEndian.Uint32(udpPayload[0:4])
conv = binary.LittleEndian.Uint32(udpPayload[4:8])
rawConv = binary.LittleEndian.Uint64(udpPayload[0:8])
}
l.sessionLock.RLock()
conn, exist := l.sessions[rawConv]
@@ -108,8 +108,8 @@ func (l *Listener) defaultMonitor() {
// 连接地址改变
l.EnetNotify <- &Enet{
Addr: conn.remote.String(),
Conv: conv,
SessionId: sessionId,
Conv: conv,
ConnType: ConnEnetAddrChange,
}
}
+7 -7
View File
@@ -126,8 +126,8 @@ func (l *Listener) monitor() {
// 客户端前置握手获取conv
l.EnetNotify <- &Enet{
Addr: msg.Addr.String(),
Conv: conv,
SessionId: sessionId,
Conv: conv,
ConnType: ConnEnetSyn,
EnetType: enetType,
}
@@ -135,8 +135,8 @@ func (l *Listener) monitor() {
// 连接建立
l.EnetNotify <- &Enet{
Addr: msg.Addr.String(),
Conv: conv,
SessionId: sessionId,
Conv: conv,
ConnType: ConnEnetEst,
EnetType: enetType,
}
@@ -144,8 +144,8 @@ func (l *Listener) monitor() {
// 连接断开
l.EnetNotify <- &Enet{
Addr: msg.Addr.String(),
Conv: conv,
SessionId: sessionId,
Conv: conv,
ConnType: ConnEnetFin,
EnetType: enetType,
}
@@ -154,9 +154,9 @@ func (l *Listener) monitor() {
}
} else {
// 正常KCP包
conv = binary.BigEndian.Uint32(udpPayload[0:4])
sessionId = binary.BigEndian.Uint32(udpPayload[4:8])
rawConv = binary.BigEndian.Uint64(udpPayload[0:8])
sessionId = binary.LittleEndian.Uint32(udpPayload[0:4])
conv = binary.LittleEndian.Uint32(udpPayload[4:8])
rawConv = binary.LittleEndian.Uint64(udpPayload[0:8])
}
l.sessionLock.RLock()
conn, exist := l.sessions[rawConv]
@@ -167,8 +167,8 @@ func (l *Listener) monitor() {
// 连接地址改变
l.EnetNotify <- &Enet{
Addr: conn.remote.String(),
Conv: conv,
SessionId: sessionId,
Conv: conv,
ConnType: ConnEnetAddrChange,
}
}
+1 -4
View File
@@ -213,7 +213,6 @@ func (k *KcpConnectManager) acceptHandle(listener *kcp.Listener) {
}
// 连接建立成功通知
k.kcpEventOutput <- &KcpEvent{
Conv: conn.GetConv(),
SessionId: conn.GetSessionId(),
EventId: KcpConnEstNotify,
EventMessage: conn.RemoteAddr().String(),
@@ -239,8 +238,8 @@ func (k *KcpConnectManager) enetHandle(listener *kcp.Listener) {
sessionIdCounter++
listener.SendEnetNotifyToPeer(&kcp.Enet{
Addr: enetNotify.Addr,
Conv: binary.BigEndian.Uint32(random.GetRandomByte(4)),
SessionId: sessionIdCounter,
Conv: binary.BigEndian.Uint32(random.GetRandomByte(4)),
ConnType: kcp.ConnEnetEst,
EnetType: enetNotify.EnetType,
})
@@ -263,7 +262,6 @@ func (k *KcpConnectManager) enetHandle(listener *kcp.Listener) {
case kcp.ConnEnetAddrChange:
// 连接地址改变通知
k.kcpEventOutput <- &KcpEvent{
Conv: enetNotify.Conv,
SessionId: enetNotify.SessionId,
EventId: KcpConnAddrChangeNotify,
EventMessage: enetNotify.Addr,
@@ -422,7 +420,6 @@ func (k *KcpConnectManager) closeKcpConn(session *Session, enetType uint32) {
}
// 连接关闭通知
k.kcpEventOutput <- &KcpEvent{
Conv: conn.GetConv(),
SessionId: conn.GetSessionId(),
EventId: KcpConnCloseNotify,
}
+1 -1
View File
@@ -11,7 +11,7 @@ import (
原神KCP协议(带*为xor加密数据)
0 1 2 4 8(字节)
+---------------------------------------------------------------------------------------+
| conv |
| sessionId(le) | conv(le) |
+---------------------------------------------------------------------------------------+
| cmd | frg | wnd | ts |
+---------------------------------------------------------------------------------------+
+2 -3
View File
@@ -13,7 +13,6 @@ const (
)
type KcpEvent struct {
Conv uint32
SessionId uint32
EventId int
EventMessage any
@@ -32,8 +31,8 @@ func (k *KcpConnectManager) eventHandle() {
// 事件处理
for {
event := <-k.kcpEventInput
logger.Info("kcp manager recv event, Conv: %v, SessionId: %v, EventId: %v, EventMessage: %v",
event.Conv, event.SessionId, event.EventId, event.EventMessage)
logger.Info("kcp manager recv event, SessionId: %v, EventId: %v, EventMessage: %v",
event.SessionId, event.EventId, event.EventMessage)
switch event.EventId {
case KcpConnForceClose:
reason, ok := event.EventMessage.(uint32)