去除同步发数据,不合理
This commit is contained in:
parent
f997332639
commit
ba252155ff
|
@ -24,11 +24,6 @@ func (slf *Player[ID]) Send(packet []byte, messageType ...int) {
|
||||||
slf.conn.Write(packet, messageType...)
|
slf.conn.Write(packet, messageType...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SyncSend 向该玩家同步发送数据
|
|
||||||
func (slf *Player[ID]) SyncSend(packet []byte, messageType ...int) error {
|
|
||||||
return slf.conn.SyncWrite(packet, messageType...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close 关闭玩家
|
// Close 关闭玩家
|
||||||
func (slf *Player[ID]) Close() {
|
func (slf *Player[ID]) Close() {
|
||||||
slf.conn.Close()
|
slf.conn.Close()
|
||||||
|
|
|
@ -7,8 +7,6 @@ type Player[ID comparable] interface {
|
||||||
// Send 发送数据包
|
// Send 发送数据包
|
||||||
// - messageType: websocket模式中指定消息类型
|
// - messageType: websocket模式中指定消息类型
|
||||||
Send(packet []byte, messageType ...int)
|
Send(packet []byte, messageType ...int)
|
||||||
// SyncSend 同步发送数据包
|
|
||||||
SyncSend(packet []byte, messageType ...int) error
|
|
||||||
// Close 关闭玩家并且释放其资源
|
// Close 关闭玩家并且释放其资源
|
||||||
Close()
|
Close()
|
||||||
}
|
}
|
||||||
|
|
|
@ -93,42 +93,6 @@ func (slf *Conn) GetIP() string {
|
||||||
return slf.ip
|
return slf.ip
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write 向连接中写入数据
|
|
||||||
// - messageType: websocket模式中指定消息类型
|
|
||||||
func (slf *Conn) Write(data []byte, messageType ...int) {
|
|
||||||
cp := slf.packetPool.Get()
|
|
||||||
if len(messageType) > 0 {
|
|
||||||
cp.websocketMessageType = messageType[0]
|
|
||||||
}
|
|
||||||
cp.packet = data
|
|
||||||
slf.mutex.Lock()
|
|
||||||
slf.packets = append(slf.packets, cp)
|
|
||||||
slf.mutex.Unlock()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (slf *Conn) SyncWrite(data []byte, messageType ...int) error {
|
|
||||||
if slf.server.network == NetworkWebsocket {
|
|
||||||
if len(messageType) > 0 {
|
|
||||||
return slf.ws.WriteMessage(messageType[0], data)
|
|
||||||
} else {
|
|
||||||
return slf.ws.WriteMessage(slf.server.websocketWriteMessageType, data)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if slf.gn != nil {
|
|
||||||
switch slf.server.network {
|
|
||||||
case NetworkUdp, NetworkUdp4, NetworkUdp6:
|
|
||||||
return slf.gn.SendTo(data)
|
|
||||||
default:
|
|
||||||
return slf.gn.AsyncWrite(data)
|
|
||||||
}
|
|
||||||
} else if slf.kcp != nil {
|
|
||||||
_, err := slf.kcp.Write(data)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close 关闭连接
|
// Close 关闭连接
|
||||||
func (slf *Conn) Close() {
|
func (slf *Conn) Close() {
|
||||||
slf.mutex.Lock()
|
slf.mutex.Lock()
|
||||||
|
@ -170,6 +134,19 @@ func (slf *Conn) IsWebsocket() bool {
|
||||||
return slf.server.network == NetworkWebsocket
|
return slf.server.network == NetworkWebsocket
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Write 向连接中写入数据
|
||||||
|
// - messageType: websocket模式中指定消息类型
|
||||||
|
func (slf *Conn) Write(data []byte, messageType ...int) {
|
||||||
|
cp := slf.packetPool.Get()
|
||||||
|
if len(messageType) > 0 {
|
||||||
|
cp.websocketMessageType = messageType[0]
|
||||||
|
}
|
||||||
|
cp.packet = data
|
||||||
|
slf.mutex.Lock()
|
||||||
|
slf.packets = append(slf.packets, cp)
|
||||||
|
slf.mutex.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
// writeLoop 写循环
|
// writeLoop 写循环
|
||||||
func (slf *Conn) writeLoop() {
|
func (slf *Conn) writeLoop() {
|
||||||
slf.packetPool = synchronization.NewPool[*connPacket](64,
|
slf.packetPool = synchronization.NewPool[*connPacket](64,
|
||||||
|
@ -196,10 +173,10 @@ func (slf *Conn) writeLoop() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
packets := slf.packets[0:]
|
packets := slf.packets[0:]
|
||||||
slf.packets = slf.packets[:0]
|
slf.packets = slf.packets[0:0]
|
||||||
slf.mutex.Unlock()
|
slf.mutex.Unlock()
|
||||||
for _, data := range packets {
|
for i := 0; i < len(packets); i++ {
|
||||||
data := data
|
data := packets[i]
|
||||||
if len(data.packet) == 0 {
|
if len(data.packet) == 0 {
|
||||||
for _, packet := range packets {
|
for _, packet := range packets {
|
||||||
slf.packetPool.Release(packet)
|
slf.packetPool.Release(packet)
|
||||||
|
|
Loading…
Reference in New Issue