feat: server 新增 NewOfflineConn 函数,兼容部分特殊操作
This commit is contained in:
parent
9c596a8a9d
commit
147f0a31a0
|
@ -14,6 +14,7 @@ import (
|
||||||
"github.com/panjf2000/gnet"
|
"github.com/panjf2000/gnet"
|
||||||
"github.com/xtaci/kcp-go/v5"
|
"github.com/xtaci/kcp-go/v5"
|
||||||
"io"
|
"io"
|
||||||
|
"math"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
@ -83,6 +84,26 @@ func newWebsocketConn(server *Server, ws *websocket.Conn, ip string) *Conn {
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewOfflineConn 创建一个离线连接
|
||||||
|
func NewOfflineConn(server *Server) *Conn {
|
||||||
|
addr := &net.TCPAddr{
|
||||||
|
IP: net.ParseIP(random.IPv4()),
|
||||||
|
Port: random.Int(65536, math.MaxInt),
|
||||||
|
}
|
||||||
|
c := &Conn{
|
||||||
|
ctx: server.ctx,
|
||||||
|
connection: &connection{
|
||||||
|
server: server,
|
||||||
|
remoteAddr: addr,
|
||||||
|
ip: addr.String(),
|
||||||
|
data: map[any]any{},
|
||||||
|
openTime: time.Now(),
|
||||||
|
offline: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
// newBotConn 创建一个适用于测试等情况的机器人连接
|
// newBotConn 创建一个适用于测试等情况的机器人连接
|
||||||
func newBotConn(server *Server) *Conn {
|
func newBotConn(server *Server) *Conn {
|
||||||
ip, port := random.NetIP(), random.Port()
|
ip, port := random.NetIP(), random.Port()
|
||||||
|
@ -132,6 +153,7 @@ type connection struct {
|
||||||
delay time.Duration
|
delay time.Duration
|
||||||
fluctuation time.Duration
|
fluctuation time.Duration
|
||||||
botWriter atomic.Pointer[io.Writer]
|
botWriter atomic.Pointer[io.Writer]
|
||||||
|
offline bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ticker 获取定时器
|
// Ticker 获取定时器
|
||||||
|
@ -257,6 +279,9 @@ func (slf *Conn) PushUniqueAsyncMessage(name string, caller func() error, callba
|
||||||
|
|
||||||
// Write 向连接中写入数据
|
// Write 向连接中写入数据
|
||||||
func (slf *Conn) Write(packet []byte, callback ...func(err error)) {
|
func (slf *Conn) Write(packet []byte, callback ...func(err error)) {
|
||||||
|
if slf.offline {
|
||||||
|
return
|
||||||
|
}
|
||||||
if slf.gw != nil {
|
if slf.gw != nil {
|
||||||
slf.gw(packet)
|
slf.gw(packet)
|
||||||
return
|
return
|
||||||
|
@ -333,6 +358,10 @@ func (slf *Conn) init() {
|
||||||
|
|
||||||
// Close 关闭连接
|
// Close 关闭连接
|
||||||
func (slf *Conn) Close(err ...error) {
|
func (slf *Conn) Close(err ...error) {
|
||||||
|
if slf.offline {
|
||||||
|
slf.server.dispatcherMgr.UnBindProducer(slf.GetID())
|
||||||
|
return
|
||||||
|
}
|
||||||
slf.mu.Lock()
|
slf.mu.Lock()
|
||||||
if slf.closed {
|
if slf.closed {
|
||||||
if slf.ticker != nil {
|
if slf.ticker != nil {
|
||||||
|
|
Loading…
Reference in New Issue