refactor: 调整事件函数名称

This commit is contained in:
kercylan98 2023-08-22 15:19:38 +08:00
parent ab19bd6f6a
commit dc761964b9
4 changed files with 33 additions and 33 deletions

View File

@ -1,6 +1,6 @@
package client package client
type websocketPacket struct { type Packet struct {
websocketMessageType int // websocket 消息类型 websocketMessageType int // websocket 消息类型
packet []byte // 数据包 packet []byte // 数据包
callback func(err error) // 回调函数 callback func(err error) // 回调函数

View File

@ -26,8 +26,8 @@ type Websocket struct {
data map[string]any data map[string]any
mutex sync.Mutex mutex sync.Mutex
packetPool *concurrent.Pool[*websocketPacket] packetPool *concurrent.Pool[*Packet]
packets []*websocketPacket packets []*Packet
accumulate []server.Packet accumulate []server.Packet
} }
@ -47,16 +47,16 @@ func (slf *Websocket) Run() error {
defer func() { defer func() {
if err := recover(); err != nil { if err := recover(); err != nil {
slf.Close() slf.Close()
slf.OnConnectionClosedEvent(slf, err) slf.OnWebsocketConnectionClosedEvent(slf, err)
} }
}() }()
slf.OnConnectionOpenedEvent(slf) slf.OnWebsocketConnectionOpenedEvent(slf)
for slf.packetPool != nil { for slf.packetPool != nil {
messageType, packet, readErr := ws.ReadMessage() messageType, packet, readErr := ws.ReadMessage()
if readErr != nil { if readErr != nil {
panic(readErr) panic(readErr)
} }
slf.OnConnectionReceivePacketEvent(slf, server.NewWSPacket(messageType, packet)) slf.OnWebsocketConnectionReceivePacketEvent(slf, server.NewWSPacket(messageType, packet))
} }
}() }()
return nil return nil
@ -103,10 +103,10 @@ func (slf *Websocket) Write(packet server.Packet) {
// writeLoop 写循环 // writeLoop 写循环
func (slf *Websocket) writeLoop(wait *sync.WaitGroup) { func (slf *Websocket) writeLoop(wait *sync.WaitGroup) {
slf.packetPool = concurrent.NewPool[*websocketPacket](10*1024, slf.packetPool = concurrent.NewPool[*Packet](10*1024,
func() *websocketPacket { func() *Packet {
return &websocketPacket{} return &Packet{}
}, func(data *websocketPacket) { }, func(data *Packet) {
data.packet = nil data.packet = nil
data.websocketMessageType = 0 data.websocketMessageType = 0
data.callback = nil data.callback = nil

View File

@ -3,46 +3,46 @@ package client
import "github.com/kercylan98/minotaur/server" import "github.com/kercylan98/minotaur/server"
type ( type (
ConnectionClosedEventHandle func(conn *Websocket, err any) WebsocketConnectionClosedEventHandle func(conn *Websocket, err any)
ConnectionOpenedEventHandle func(conn *Websocket) WebsocketConnectionOpenedEventHandle func(conn *Websocket)
ConnectionReceivePacketEventHandle func(conn *Websocket, packet server.Packet) WebsocketConnectionReceivePacketEventHandle func(conn *Websocket, packet server.Packet)
) )
type websocketEvents struct { type websocketEvents struct {
connectionClosedEventHandles []ConnectionClosedEventHandle websocketConnectionClosedEventHandles []WebsocketConnectionClosedEventHandle
connectionOpenedEventHandles []ConnectionOpenedEventHandle websocketConnectionOpenedEventHandles []WebsocketConnectionOpenedEventHandle
connectionReceivePacketEventHandles []ConnectionReceivePacketEventHandle websocketConnectionReceivePacketEventHandles []WebsocketConnectionReceivePacketEventHandle
} }
// RegConnectionClosedEvent 注册连接关闭事件 // RegWebsocketConnectionClosedEvent 注册连接关闭事件
func (slf *websocketEvents) RegConnectionClosedEvent(handle ConnectionClosedEventHandle) { func (slf *websocketEvents) RegWebsocketConnectionClosedEvent(handle WebsocketConnectionClosedEventHandle) {
slf.connectionClosedEventHandles = append(slf.connectionClosedEventHandles, handle) slf.websocketConnectionClosedEventHandles = append(slf.websocketConnectionClosedEventHandles, handle)
} }
func (slf *websocketEvents) OnConnectionClosedEvent(conn *Websocket, err any) { func (slf *websocketEvents) OnWebsocketConnectionClosedEvent(conn *Websocket, err any) {
for _, handle := range slf.connectionClosedEventHandles { for _, handle := range slf.websocketConnectionClosedEventHandles {
handle(conn, err) handle(conn, err)
} }
} }
// RegConnectionOpenedEvent 注册连接打开事件 // RegWebsocketConnectionOpenedEvent 注册连接打开事件
func (slf *websocketEvents) RegConnectionOpenedEvent(handle ConnectionOpenedEventHandle) { func (slf *websocketEvents) RegWebsocketConnectionOpenedEvent(handle WebsocketConnectionOpenedEventHandle) {
slf.connectionOpenedEventHandles = append(slf.connectionOpenedEventHandles, handle) slf.websocketConnectionOpenedEventHandles = append(slf.websocketConnectionOpenedEventHandles, handle)
} }
func (slf *websocketEvents) OnConnectionOpenedEvent(conn *Websocket) { func (slf *websocketEvents) OnWebsocketConnectionOpenedEvent(conn *Websocket) {
for _, handle := range slf.connectionOpenedEventHandles { for _, handle := range slf.websocketConnectionOpenedEventHandles {
handle(conn) handle(conn)
} }
} }
// RegConnectionReceivePacketEvent 注册连接接收数据包事件 // RegWebsocketConnectionReceivePacketEvent 注册连接接收数据包事件
func (slf *websocketEvents) RegConnectionReceivePacketEvent(handle ConnectionReceivePacketEventHandle) { func (slf *websocketEvents) RegWebsocketConnectionReceivePacketEvent(handle WebsocketConnectionReceivePacketEventHandle) {
slf.connectionReceivePacketEventHandles = append(slf.connectionReceivePacketEventHandles, handle) slf.websocketConnectionReceivePacketEventHandles = append(slf.websocketConnectionReceivePacketEventHandles, handle)
} }
func (slf *websocketEvents) OnConnectionReceivePacketEvent(conn *Websocket, packet server.Packet) { func (slf *websocketEvents) OnWebsocketConnectionReceivePacketEvent(conn *Websocket, packet server.Packet) {
for _, handle := range slf.connectionReceivePacketEventHandles { for _, handle := range slf.websocketConnectionReceivePacketEventHandles {
handle(conn, packet) handle(conn, packet)
} }
} }

View File

@ -22,8 +22,8 @@ func NewEndpoint(name, address string, options ...EndpointOption) *Endpoint {
return 1 / (1 + 1.5*time.Duration(costUnixNano).Seconds()) return 1 / (1 + 1.5*time.Duration(costUnixNano).Seconds())
} }
} }
endpoint.client.RegConnectionClosedEvent(endpoint.onConnectionClosed) endpoint.client.RegWebsocketConnectionClosedEvent(endpoint.onConnectionClosed)
endpoint.client.RegConnectionReceivePacketEvent(endpoint.onConnectionReceivePacket) endpoint.client.RegWebsocketConnectionReceivePacketEvent(endpoint.onConnectionReceivePacket)
return endpoint return endpoint
} }