From 097a324ab072a7bfcacd7f46271dd54aa3fa7969 Mon Sep 17 00:00:00 2001 From: flswld Date: Sun, 6 Aug 2023 01:34:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96kcp=E5=BA=93=E5=AE=A2?= =?UTF-8?q?=E6=88=B7=E7=AB=AF=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gate/kcp/session.go | 11 ++++++++++- gate/kcp/udp_socket.go | 9 ++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/gate/kcp/session.go b/gate/kcp/session.go index acfbf2a4..0cd62718 100644 --- a/gate/kcp/session.go +++ b/gate/kcp/session.go @@ -928,6 +928,10 @@ func DialWithOptions(raddr string) (*UDPSession, error) { return nil, err } buf := make([]byte, mtuLimit) + err = conn.SetReadDeadline(time.Now().Add(time.Second * 10)) + if err != nil { + return nil, err + } n, addr, err := conn.ReadFrom(buf) if err != nil { return nil, err @@ -936,11 +940,16 @@ func DialWithOptions(raddr string) (*UDPSession, error) { return nil, errors.New("recv packet remote addr not match") } udpPayload := buf[:n] - connType, enetType, _, _, rawConv, err := ParseEnet(udpPayload) + connType, enetType, sessionId, conv, _, err := ParseEnet(udpPayload) if err != nil || connType != ConnEnetEst || enetType != EnetClientConnectKey { return nil, errors.New("recv packet format error") } + rawConvData := make([]byte, 8) + binary.LittleEndian.PutUint32(rawConvData[0:4], sessionId) + binary.LittleEndian.PutUint32(rawConvData[4:8], conv) + rawConv := binary.LittleEndian.Uint64(rawConvData) + return newUDPSession(rawConv, nil, conn, true, udpaddr), nil } diff --git a/gate/kcp/udp_socket.go b/gate/kcp/udp_socket.go index ff71f3b7..e3b52833 100644 --- a/gate/kcp/udp_socket.go +++ b/gate/kcp/udp_socket.go @@ -125,7 +125,14 @@ func (s *UDPSession) defaultTx(txqueue []ipv4.Message) { nbytes := 0 npkts := 0 for k := range txqueue { - if n, err := s.conn.WriteTo(txqueue[k].Buffers[0], txqueue[k].Addr); err == nil { + var n = 0 + var err error = nil + if s.l != nil { + n, err = s.conn.WriteTo(txqueue[k].Buffers[0], txqueue[k].Addr) + } else { + n, err = s.conn.(*net.UDPConn).Write(txqueue[k].Buffers[0]) + } + if err == nil { nbytes += n npkts++ } else {