优化kcp库多平台支持
This commit is contained in:
1 parent
9e0f5d9dd8
commit
9a655161b1
9 files changed
+176
-178
No files matched your search
+4
-1
@@ -81,7 +81,10 @@ func Run(ctx context.Context, configFile string) error {
|
||||
}
|
||||
defer db.CloseDao()
|
||||
|
||||
kcpConnManager := net.NewKcpConnManager(db, messageQueue, discoveryClient)
|
||||
kcpConnManager, err := net.NewKcpConnManager(db, messageQueue, discoveryClient)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer kcpConnManager.Close()
|
||||
|
||||
c := make(chan os.Signal, 1)
|
||||
|
||||
+27
-8
@@ -147,7 +147,7 @@ func newUDPSession(conv uint64, l *Listener, conn net.PacketConn, ownConn bool,
|
||||
sess.kcp.ReserveBytes(sess.headerSize)
|
||||
|
||||
if sess.l == nil { // it's a client connection
|
||||
go sess.readLoop()
|
||||
go sess.rx()
|
||||
atomic.AddUint64(&DefaultSnmp.ActiveOpens, 1)
|
||||
} else {
|
||||
atomic.AddUint64(&DefaultSnmp.PassiveOpens, 1)
|
||||
@@ -231,6 +231,10 @@ func (s *UDPSession) Read(b []byte) (n int, err error) {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *UDPSession) GetMaxPayloadLen() int {
|
||||
return 256 * int(s.kcp.mss)
|
||||
}
|
||||
|
||||
// Write implements net.Conn
|
||||
func (s *UDPSession) Write(b []byte) (n int, err error) {
|
||||
if len(b) > s.GetMaxPayloadLen() {
|
||||
@@ -239,10 +243,6 @@ func (s *UDPSession) Write(b []byte) (n int, err error) {
|
||||
return s.WriteBuffers([][]byte{b})
|
||||
}
|
||||
|
||||
func (s *UDPSession) GetMaxPayloadLen() int {
|
||||
return 256 * int(s.kcp.mss)
|
||||
}
|
||||
|
||||
// WriteBuffers write a vector of byte slices to the underlying connection
|
||||
func (s *UDPSession) WriteBuffers(v [][]byte) (n int, err error) {
|
||||
for {
|
||||
@@ -676,10 +676,17 @@ type (
|
||||
|
||||
rd atomic.Value // read deadline for Accept()
|
||||
|
||||
EnetNotify chan *Enet // Enet事件上报管道
|
||||
xconn batchConn // for x/net
|
||||
xconnWriteError error
|
||||
|
||||
enetNotifyChan chan *Enet // Enet事件上报管道
|
||||
}
|
||||
)
|
||||
|
||||
func (l *Listener) GetEnetNotifyChan() chan *Enet {
|
||||
return l.enetNotifyChan
|
||||
}
|
||||
|
||||
// packet input stage
|
||||
func (l *Listener) packetInput(data []byte, addr net.Addr, rawConv uint64) {
|
||||
if len(data) >= IKCP_OVERHEAD {
|
||||
@@ -885,8 +892,20 @@ func serveConn(conn net.PacketConn, ownConn bool) (*Listener, error) {
|
||||
l.chSessionClosed = make(chan net.Addr)
|
||||
l.die = make(chan struct{})
|
||||
l.chSocketReadError = make(chan struct{})
|
||||
l.EnetNotify = make(chan *Enet, 1000)
|
||||
go l.monitor()
|
||||
l.enetNotifyChan = make(chan *Enet, 1000)
|
||||
|
||||
if _, ok := l.conn.(*net.UDPConn); ok {
|
||||
addr, err := net.ResolveUDPAddr("udp", l.conn.LocalAddr().String())
|
||||
if err == nil {
|
||||
if addr.IP.To4() != nil {
|
||||
l.xconn = ipv4.NewPacketConn(l.conn)
|
||||
} else {
|
||||
l.xconn = ipv6.NewPacketConn(l.conn)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
go l.rx()
|
||||
return l, nil
|
||||
}
|
||||
|
||||
|
||||
+20
-37
@@ -8,13 +8,13 @@ import (
|
||||
"golang.org/x/net/ipv4"
|
||||
)
|
||||
|
||||
func (s *UDPSession) defaultReadLoop() {
|
||||
// 客户端收包循环
|
||||
func (s *UDPSession) defaultRx() {
|
||||
buf := make([]byte, mtuLimit)
|
||||
var src string
|
||||
for {
|
||||
if n, addr, err := s.conn.ReadFrom(buf); err == nil {
|
||||
udpPayload := buf[:n]
|
||||
|
||||
// make sure the packet is from the same source
|
||||
if src == "" { // set source address
|
||||
src = addr.String()
|
||||
@@ -22,9 +22,8 @@ func (s *UDPSession) defaultReadLoop() {
|
||||
s.remote = addr
|
||||
src = addr.String()
|
||||
}
|
||||
|
||||
if n == 20 {
|
||||
connType, _, sessionId, conv, _, err := ParseEnet(udpPayload)
|
||||
connType, enetType, sessionId, conv, _, err := ParseEnet(udpPayload)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
@@ -32,11 +31,17 @@ func (s *UDPSession) defaultReadLoop() {
|
||||
continue
|
||||
}
|
||||
if connType == ConnEnetFin {
|
||||
s.defaultSendEnetNotifyToPeer(&Enet{
|
||||
Addr: s.remote.String(),
|
||||
SessionId: sessionId,
|
||||
Conv: conv,
|
||||
ConnType: ConnEnetFin,
|
||||
EnetType: enetType,
|
||||
})
|
||||
_ = s.Close()
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
s.packetInput(udpPayload)
|
||||
} else {
|
||||
s.notifyReadError(err)
|
||||
@@ -45,7 +50,8 @@ func (s *UDPSession) defaultReadLoop() {
|
||||
}
|
||||
}
|
||||
|
||||
func (l *Listener) defaultMonitor() {
|
||||
// 服务器全局收包循环
|
||||
func (l *Listener) defaultRx() {
|
||||
buf := make([]byte, mtuLimit)
|
||||
for {
|
||||
if n, from, err := l.conn.ReadFrom(buf); err == nil {
|
||||
@@ -64,31 +70,13 @@ func (l *Listener) defaultMonitor() {
|
||||
switch connType {
|
||||
case ConnEnetSyn:
|
||||
// 客户端前置握手获取conv
|
||||
l.EnetNotify <- &Enet{
|
||||
Addr: from.String(),
|
||||
SessionId: sessionId,
|
||||
Conv: conv,
|
||||
ConnType: ConnEnetSyn,
|
||||
EnetType: enetType,
|
||||
}
|
||||
l.enetNotifyChan <- &Enet{Addr: from.String(), SessionId: sessionId, Conv: conv, ConnType: ConnEnetSyn, EnetType: enetType}
|
||||
case ConnEnetEst:
|
||||
// 连接建立
|
||||
l.EnetNotify <- &Enet{
|
||||
Addr: from.String(),
|
||||
SessionId: sessionId,
|
||||
Conv: conv,
|
||||
ConnType: ConnEnetEst,
|
||||
EnetType: enetType,
|
||||
}
|
||||
l.enetNotifyChan <- &Enet{Addr: from.String(), SessionId: sessionId, Conv: conv, ConnType: ConnEnetEst, EnetType: enetType}
|
||||
case ConnEnetFin:
|
||||
// 连接断开
|
||||
l.EnetNotify <- &Enet{
|
||||
Addr: from.String(),
|
||||
SessionId: sessionId,
|
||||
Conv: conv,
|
||||
ConnType: ConnEnetFin,
|
||||
EnetType: enetType,
|
||||
}
|
||||
l.enetNotifyChan <- &Enet{Addr: from.String(), SessionId: sessionId, Conv: conv, ConnType: ConnEnetFin, EnetType: enetType}
|
||||
default:
|
||||
continue
|
||||
}
|
||||
@@ -105,12 +93,7 @@ func (l *Listener) defaultMonitor() {
|
||||
if conn.remote.String() != from.String() {
|
||||
conn.remote = from
|
||||
// 连接地址改变
|
||||
l.EnetNotify <- &Enet{
|
||||
Addr: conn.remote.String(),
|
||||
SessionId: sessionId,
|
||||
Conv: conv,
|
||||
ConnType: ConnEnetAddrChange,
|
||||
}
|
||||
l.enetNotifyChan <- &Enet{Addr: conn.remote.String(), SessionId: sessionId, Conv: conv, ConnType: ConnEnetAddrChange}
|
||||
}
|
||||
}
|
||||
l.packetInput(udpPayload, from, rawConv)
|
||||
@@ -121,6 +104,7 @@ func (l *Listener) defaultMonitor() {
|
||||
}
|
||||
}
|
||||
|
||||
// 公共发包接口
|
||||
func (s *UDPSession) defaultTx(txqueue []ipv4.Message) {
|
||||
nbytes := 0
|
||||
npkts := 0
|
||||
@@ -144,6 +128,7 @@ func (s *UDPSession) defaultTx(txqueue []ipv4.Message) {
|
||||
atomic.AddUint64(&DefaultSnmp.OutBytes, uint64(nbytes))
|
||||
}
|
||||
|
||||
// 服务器Enet事件发送接口
|
||||
func (l *Listener) defaultSendEnetNotifyToPeer(enet *Enet) {
|
||||
remoteAddr, err := net.ResolveUDPAddr("udp", enet.Addr)
|
||||
if err != nil {
|
||||
@@ -156,13 +141,11 @@ func (l *Listener) defaultSendEnetNotifyToPeer(enet *Enet) {
|
||||
_, _ = l.conn.WriteTo(data, remoteAddr)
|
||||
}
|
||||
|
||||
// 客户端Enet事件发送接口
|
||||
func (s *UDPSession) defaultSendEnetNotifyToPeer(enet *Enet) {
|
||||
data := BuildEnet(enet.ConnType, enet.EnetType, s.GetSessionId(), s.GetConv())
|
||||
if data == nil {
|
||||
return
|
||||
}
|
||||
s.defaultTx([]ipv4.Message{{
|
||||
Buffers: [][]byte{data},
|
||||
Addr: s.remote,
|
||||
}})
|
||||
_, _ = s.conn.(*net.UDPConn).Write(data)
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
//go:build darwin
|
||||
// +build darwin
|
||||
|
||||
package kcp
|
||||
|
||||
import (
|
||||
"golang.org/x/net/ipv4"
|
||||
)
|
||||
|
||||
func (s *UDPSession) rx() {
|
||||
s.defaultRx()
|
||||
}
|
||||
|
||||
func (l *Listener) rx() {
|
||||
l.defaultRx()
|
||||
}
|
||||
|
||||
func (s *UDPSession) tx(txqueue []ipv4.Message) {
|
||||
s.defaultTx(txqueue)
|
||||
}
|
||||
|
||||
func (l *Listener) SendEnetNotifyToPeer(enet *Enet) {
|
||||
l.defaultSendEnetNotifyToPeer(enet)
|
||||
}
|
||||
|
||||
func (s *UDPSession) SendEnetNotifyToPeer(enet *Enet) {
|
||||
s.defaultSendEnetNotifyToPeer(enet)
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
//go:build linux || darwin
|
||||
// +build linux darwin
|
||||
//go:build linux
|
||||
// +build linux
|
||||
|
||||
package kcp
|
||||
|
||||
@@ -10,29 +10,25 @@ import (
|
||||
"sync/atomic"
|
||||
|
||||
"golang.org/x/net/ipv4"
|
||||
"golang.org/x/net/ipv6"
|
||||
)
|
||||
|
||||
// the read loop for a client session
|
||||
func (s *UDPSession) readLoop() {
|
||||
func (s *UDPSession) rx() {
|
||||
// default version
|
||||
if s.xconn == nil {
|
||||
s.defaultReadLoop()
|
||||
s.defaultRx()
|
||||
return
|
||||
}
|
||||
|
||||
// x/net version
|
||||
var src string
|
||||
msgs := make([]ipv4.Message, batchSize)
|
||||
for k := range msgs {
|
||||
msgs[k].Buffers = [][]byte{make([]byte, mtuLimit)}
|
||||
}
|
||||
|
||||
for {
|
||||
if count, err := s.xconn.ReadBatch(msgs, 0); err == nil {
|
||||
for i := 0; i < count; i++ {
|
||||
msg := &msgs[i]
|
||||
|
||||
// make sure the packet is from the same source
|
||||
if src == "" { // set source address if nil
|
||||
src = msg.Addr.String()
|
||||
@@ -40,11 +36,9 @@ func (s *UDPSession) readLoop() {
|
||||
s.remote = msg.Addr
|
||||
src = msg.Addr.String()
|
||||
}
|
||||
|
||||
udpPayload := msg.Buffers[0][:msg.N]
|
||||
|
||||
if msg.N == 20 {
|
||||
connType, _, sessionId, conv, _, err := ParseEnet(udpPayload)
|
||||
connType, enetType, sessionId, conv, _, err := ParseEnet(udpPayload)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
@@ -52,11 +46,17 @@ func (s *UDPSession) readLoop() {
|
||||
continue
|
||||
}
|
||||
if connType == ConnEnetFin {
|
||||
s.SendEnetNotifyToPeer(&Enet{
|
||||
Addr: s.remote.String(),
|
||||
SessionId: sessionId,
|
||||
Conv: conv,
|
||||
ConnType: ConnEnetFin,
|
||||
EnetType: enetType,
|
||||
})
|
||||
_ = s.Close()
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
// source and size has validated
|
||||
s.packetInput(udpPayload)
|
||||
}
|
||||
@@ -67,7 +67,7 @@ func (s *UDPSession) readLoop() {
|
||||
if operr, ok := err.(*net.OpError); ok {
|
||||
if se, ok := operr.Err.(*os.SyscallError); ok {
|
||||
if se.Syscall == "recvmmsg" {
|
||||
s.defaultReadLoop()
|
||||
s.defaultRx()
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -79,33 +79,19 @@ func (s *UDPSession) readLoop() {
|
||||
}
|
||||
|
||||
// monitor incoming data for all connections of server
|
||||
func (l *Listener) monitor() {
|
||||
var xconn batchConn
|
||||
if _, ok := l.conn.(*net.UDPConn); ok {
|
||||
addr, err := net.ResolveUDPAddr("udp", l.conn.LocalAddr().String())
|
||||
if err == nil {
|
||||
if addr.IP.To4() != nil {
|
||||
xconn = ipv4.NewPacketConn(l.conn)
|
||||
} else {
|
||||
xconn = ipv6.NewPacketConn(l.conn)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (l *Listener) rx() {
|
||||
// default version
|
||||
if xconn == nil {
|
||||
l.defaultMonitor()
|
||||
if l.xconn == nil {
|
||||
l.defaultRx()
|
||||
return
|
||||
}
|
||||
|
||||
// x/net version
|
||||
msgs := make([]ipv4.Message, batchSize)
|
||||
for k := range msgs {
|
||||
msgs[k].Buffers = [][]byte{make([]byte, mtuLimit)}
|
||||
}
|
||||
|
||||
for {
|
||||
if count, err := xconn.ReadBatch(msgs, 0); err == nil {
|
||||
if count, err := l.xconn.ReadBatch(msgs, 0); err == nil {
|
||||
for i := 0; i < count; i++ {
|
||||
msg := &msgs[i]
|
||||
udpPayload := msg.Buffers[0][:msg.N]
|
||||
@@ -123,31 +109,13 @@ func (l *Listener) monitor() {
|
||||
switch connType {
|
||||
case ConnEnetSyn:
|
||||
// 客户端前置握手获取conv
|
||||
l.EnetNotify <- &Enet{
|
||||
Addr: msg.Addr.String(),
|
||||
SessionId: sessionId,
|
||||
Conv: conv,
|
||||
ConnType: ConnEnetSyn,
|
||||
EnetType: enetType,
|
||||
}
|
||||
l.enetNotifyChan <- &Enet{Addr: msg.Addr.String(), SessionId: sessionId, Conv: conv, ConnType: ConnEnetSyn, EnetType: enetType}
|
||||
case ConnEnetEst:
|
||||
// 连接建立
|
||||
l.EnetNotify <- &Enet{
|
||||
Addr: msg.Addr.String(),
|
||||
SessionId: sessionId,
|
||||
Conv: conv,
|
||||
ConnType: ConnEnetEst,
|
||||
EnetType: enetType,
|
||||
}
|
||||
l.enetNotifyChan <- &Enet{Addr: msg.Addr.String(), SessionId: sessionId, Conv: conv, ConnType: ConnEnetEst, EnetType: enetType}
|
||||
case ConnEnetFin:
|
||||
// 连接断开
|
||||
l.EnetNotify <- &Enet{
|
||||
Addr: msg.Addr.String(),
|
||||
SessionId: sessionId,
|
||||
Conv: conv,
|
||||
ConnType: ConnEnetFin,
|
||||
EnetType: enetType,
|
||||
}
|
||||
l.enetNotifyChan <- &Enet{Addr: msg.Addr.String(), SessionId: sessionId, Conv: conv, ConnType: ConnEnetFin, EnetType: enetType}
|
||||
default:
|
||||
continue
|
||||
}
|
||||
@@ -164,12 +132,7 @@ func (l *Listener) monitor() {
|
||||
if conn.remote.String() != msg.Addr.String() {
|
||||
conn.remote = msg.Addr
|
||||
// 连接地址改变
|
||||
l.EnetNotify <- &Enet{
|
||||
Addr: conn.remote.String(),
|
||||
SessionId: sessionId,
|
||||
Conv: conv,
|
||||
ConnType: ConnEnetAddrChange,
|
||||
}
|
||||
l.enetNotifyChan <- &Enet{Addr: conn.remote.String(), SessionId: sessionId, Conv: conv, ConnType: ConnEnetAddrChange}
|
||||
}
|
||||
}
|
||||
l.packetInput(udpPayload, msg.Addr, rawConv)
|
||||
@@ -181,7 +144,7 @@ func (l *Listener) monitor() {
|
||||
if operr, ok := err.(*net.OpError); ok {
|
||||
if se, ok := operr.Err.(*os.SyscallError); ok {
|
||||
if se.Syscall == "recvmmsg" {
|
||||
l.defaultMonitor()
|
||||
l.defaultRx()
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -198,7 +161,6 @@ func (s *UDPSession) tx(txqueue []ipv4.Message) {
|
||||
s.defaultTx(txqueue)
|
||||
return
|
||||
}
|
||||
|
||||
// x/net version
|
||||
nbytes := 0
|
||||
npkts := 0
|
||||
@@ -226,47 +188,39 @@ func (s *UDPSession) tx(txqueue []ipv4.Message) {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
atomic.AddUint64(&DefaultSnmp.OutPkts, uint64(npkts))
|
||||
atomic.AddUint64(&DefaultSnmp.OutBytes, uint64(nbytes))
|
||||
}
|
||||
|
||||
func (l *Listener) SendEnetNotifyToPeer(enet *Enet) {
|
||||
var xconn batchConn
|
||||
_, ok := l.conn.(*net.UDPConn)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
localAddr, err := net.ResolveUDPAddr("udp", l.conn.LocalAddr().String())
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if localAddr.IP.To4() != nil {
|
||||
xconn = ipv4.NewPacketConn(l.conn)
|
||||
} else {
|
||||
xconn = ipv6.NewPacketConn(l.conn)
|
||||
}
|
||||
|
||||
// default version
|
||||
if xconn == nil {
|
||||
if l.xconn == nil || l.xconnWriteError != nil {
|
||||
l.defaultSendEnetNotifyToPeer(enet)
|
||||
return
|
||||
}
|
||||
|
||||
remoteAddr, err := net.ResolveUDPAddr("udp", enet.Addr)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
data := BuildEnet(enet.ConnType, enet.EnetType, enet.SessionId, enet.Conv)
|
||||
if data == nil {
|
||||
return
|
||||
}
|
||||
|
||||
_, _ = xconn.WriteBatch([]ipv4.Message{{
|
||||
Buffers: [][]byte{data},
|
||||
Addr: remoteAddr,
|
||||
}}, 0)
|
||||
remoteAddr, err := net.ResolveUDPAddr("udp", enet.Addr)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
_, err = l.xconn.WriteBatch([]ipv4.Message{{Buffers: [][]byte{data}, Addr: remoteAddr}}, 0)
|
||||
if err != nil {
|
||||
// compatibility issue:
|
||||
// for linux kernel<=2.6.32, support for sendmmsg is not available
|
||||
// an error of type os.SyscallError will be returned
|
||||
if operr, ok := err.(*net.OpError); ok {
|
||||
if se, ok := operr.Err.(*os.SyscallError); ok {
|
||||
if se.Syscall == "sendmmsg" {
|
||||
l.xconnWriteError = se
|
||||
l.defaultSendEnetNotifyToPeer(enet)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *UDPSession) SendEnetNotifyToPeer(enet *Enet) {
|
||||
@@ -274,8 +228,19 @@ func (s *UDPSession) SendEnetNotifyToPeer(enet *Enet) {
|
||||
if data == nil {
|
||||
return
|
||||
}
|
||||
s.tx([]ipv4.Message{{
|
||||
Buffers: [][]byte{data},
|
||||
Addr: s.remote,
|
||||
}})
|
||||
_, err := s.xconn.WriteBatch([]ipv4.Message{{Buffers: [][]byte{data}, Addr: s.remote}}, 0)
|
||||
if err != nil {
|
||||
// compatibility issue:
|
||||
// for linux kernel<=2.6.32, support for sendmmsg is not available
|
||||
// an error of type os.SyscallError will be returned
|
||||
if operr, ok := err.(*net.OpError); ok {
|
||||
if se, ok := operr.Err.(*os.SyscallError); ok {
|
||||
if se.Syscall == "sendmmsg" {
|
||||
s.xconnWriteError = se
|
||||
s.defaultSendEnetNotifyToPeer(enet)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
//go:build !linux
|
||||
// +build !linux
|
||||
//go:build windows
|
||||
// +build windows
|
||||
|
||||
package kcp
|
||||
|
||||
@@ -7,12 +7,16 @@ import (
|
||||
"golang.org/x/net/ipv4"
|
||||
)
|
||||
|
||||
func (s *UDPSession) readLoop() {
|
||||
s.defaultReadLoop()
|
||||
func (s *UDPSession) rx() {
|
||||
s.defaultRx()
|
||||
}
|
||||
|
||||
func (l *Listener) monitor() {
|
||||
l.defaultMonitor()
|
||||
func (l *Listener) rx() {
|
||||
l.defaultRx()
|
||||
}
|
||||
|
||||
func (s *UDPSession) tx(txqueue []ipv4.Message) {
|
||||
s.defaultTx(txqueue)
|
||||
}
|
||||
|
||||
func (l *Listener) SendEnetNotifyToPeer(enet *Enet) {
|
||||
@@ -22,7 +26,3 @@ func (l *Listener) SendEnetNotifyToPeer(enet *Enet) {
|
||||
func (s *UDPSession) SendEnetNotifyToPeer(enet *Enet) {
|
||||
s.defaultSendEnetNotifyToPeer(enet)
|
||||
}
|
||||
|
||||
func (s *UDPSession) tx(txqueue []ipv4.Message) {
|
||||
s.defaultTx(txqueue)
|
||||
}
|
||||
@@ -97,14 +97,6 @@ func (c *Conn) Write(b []byte) (int, error) {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Conn) SendEnetNotifyToPeer(enet *kcp.Enet) {
|
||||
if c.isKcp {
|
||||
c.kcpConn.SendEnetNotifyToPeer(enet)
|
||||
} else {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Conn) GetKcpRTO() uint32 {
|
||||
if c.isKcp {
|
||||
return c.kcpConn.GetRTO()
|
||||
|
||||
@@ -51,6 +51,7 @@ type KcpEvent struct {
|
||||
}
|
||||
|
||||
type KcpConnManager struct {
|
||||
kcpListener *kcp.Listener
|
||||
db *dao.Dao
|
||||
discovery *rpc.DiscoveryClient // 节点服务器rpc客户端
|
||||
messageQueue *mq.MessageQueue // 消息队列
|
||||
@@ -75,8 +76,9 @@ type KcpConnManager struct {
|
||||
dispatchKey []byte
|
||||
}
|
||||
|
||||
func NewKcpConnManager(db *dao.Dao, messageQueue *mq.MessageQueue, discovery *rpc.DiscoveryClient) (r *KcpConnManager) {
|
||||
r = new(KcpConnManager)
|
||||
func NewKcpConnManager(db *dao.Dao, messageQueue *mq.MessageQueue, discovery *rpc.DiscoveryClient) (*KcpConnManager, error) {
|
||||
r := new(KcpConnManager)
|
||||
r.kcpListener = nil
|
||||
r.db = db
|
||||
r.discovery = discovery
|
||||
r.messageQueue = messageQueue
|
||||
@@ -92,23 +94,26 @@ func NewKcpConnManager(db *dao.Dao, messageQueue *mq.MessageQueue, discovery *rp
|
||||
if config.GetConfig().Hk4e.ClientProtoProxyEnable {
|
||||
r.clientCmdProtoMap = client_proto.NewClientCmdProtoMap()
|
||||
}
|
||||
r.run()
|
||||
return r
|
||||
err := r.run()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return r, nil
|
||||
}
|
||||
|
||||
func (k *KcpConnManager) run() {
|
||||
func (k *KcpConnManager) run() error {
|
||||
// 读取密钥相关文件
|
||||
k.signRsaKey, k.encRsaKeyMap, _ = region.LoadRegionRsaKey()
|
||||
// key
|
||||
rsp, err := k.discovery.GetRegionEc2B(context.TODO(), &api.NullMsg{})
|
||||
if err != nil {
|
||||
logger.Error("get region ec2b error: %v", err)
|
||||
return
|
||||
return err
|
||||
}
|
||||
ec2b, err := random.LoadEc2bKey(rsp.Data)
|
||||
if err != nil {
|
||||
logger.Error("parse region ec2b error: %v", err)
|
||||
return
|
||||
return err
|
||||
}
|
||||
regionEc2b := random.NewEc2b()
|
||||
regionEc2b.SetSeed(ec2b.Seed())
|
||||
@@ -118,8 +123,9 @@ func (k *KcpConnManager) run() {
|
||||
kcpListener, err := kcp.ListenWithOptions(addr)
|
||||
if err != nil {
|
||||
logger.Error("listen kcp err: %v", err)
|
||||
return
|
||||
return err
|
||||
}
|
||||
k.kcpListener = kcpListener
|
||||
logger.Info("listen kcp at addr: %v", addr)
|
||||
go k.kcpNetInfo()
|
||||
go k.kcpEnetHandle(kcpListener)
|
||||
@@ -130,12 +136,12 @@ func (k *KcpConnManager) run() {
|
||||
tcpAddr, err := net.ResolveTCPAddr("tcp4", addr)
|
||||
if err != nil {
|
||||
logger.Error("parse tcp addr err: %v", err)
|
||||
return
|
||||
return err
|
||||
}
|
||||
tcpListener, err := net.ListenTCP("tcp4", tcpAddr)
|
||||
if err != nil {
|
||||
logger.Error("listen tcp err: %v", err)
|
||||
return
|
||||
return err
|
||||
}
|
||||
logger.Info("listen tcp at addr: %v", addr)
|
||||
go k.acceptHandle(true, nil, tcpListener)
|
||||
@@ -151,6 +157,7 @@ func (k *KcpConnManager) run() {
|
||||
logger.Info("[Kcp Event] kcpEvent: %+v", *kcpEvent)
|
||||
}
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (k *KcpConnManager) Close() {
|
||||
@@ -189,6 +196,7 @@ func (k *KcpConnManager) acceptHandle(tcpMode bool, kcpListener *kcp.Listener, t
|
||||
kcpConn.SetACKNoDelay(true)
|
||||
kcpConn.SetWriteDelay(false)
|
||||
kcpConn.SetWindowSize(256, 256)
|
||||
kcpConn.SetMtu(1200)
|
||||
conn = NewKcpConn(kcpConn)
|
||||
} else {
|
||||
tcpConn, err := tcpListener.AcceptTCP()
|
||||
@@ -288,7 +296,7 @@ func (k *KcpConnManager) acceptHandle(tcpMode bool, kcpListener *kcp.Listener, t
|
||||
func (k *KcpConnManager) kcpEnetHandle(listener *kcp.Listener) {
|
||||
logger.Info("kcp enet handle start")
|
||||
for {
|
||||
enetNotify := <-listener.EnetNotify
|
||||
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 {
|
||||
@@ -305,22 +313,16 @@ func (k *KcpConnManager) kcpEnetHandle(listener *kcp.Listener) {
|
||||
ConnType: kcp.ConnEnetEst,
|
||||
EnetType: enetNotify.EnetType,
|
||||
})
|
||||
case kcp.ConnEnetEst:
|
||||
case kcp.ConnEnetFin:
|
||||
session := k.GetSession(enetNotify.SessionId)
|
||||
if session == nil {
|
||||
logger.Error("session not exist, sessionId: %v", enetNotify.SessionId)
|
||||
continue
|
||||
}
|
||||
if session.conn.GetConv() != enetNotify.Conv {
|
||||
logger.Error("conv not match, sessionId: %v", enetNotify.SessionId)
|
||||
continue
|
||||
}
|
||||
session.conn.SendEnetNotifyToPeer(&kcp.Enet{
|
||||
ConnType: kcp.ConnEnetFin,
|
||||
EnetType: enetNotify.EnetType,
|
||||
})
|
||||
session.conn.Close()
|
||||
k.closeKcpConn(session, enetNotify.EnetType)
|
||||
case kcp.ConnEnetAddrChange:
|
||||
// 连接地址改变通知
|
||||
k.kcpEventChan <- &KcpEvent{
|
||||
@@ -566,11 +568,16 @@ 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.SendEnetNotifyToPeer(&kcp.Enet{
|
||||
ConnType: kcp.ConnEnetFin,
|
||||
EnetType: enetType,
|
||||
})
|
||||
// 连接关闭通知
|
||||
k.kcpEventChan <- &KcpEvent{
|
||||
SessionId: session.sessionId,
|
||||
|
||||
@@ -55,6 +55,7 @@ func NewSession(gateAddr string, dispatchKey []byte) (*Session, error) {
|
||||
conn.SetACKNoDelay(true)
|
||||
conn.SetWriteDelay(false)
|
||||
conn.SetWindowSize(256, 256)
|
||||
conn.SetMtu(1200)
|
||||
r := &Session{
|
||||
Conn: conn,
|
||||
XorKey: dispatchKey,
|
||||
|
||||
Reference in new issue
Block a user