refactor: 调整事件函数名称
This commit is contained in:
parent
ab19bd6f6a
commit
dc761964b9
|
@ -1,6 +1,6 @@
|
|||
package client
|
||||
|
||||
type websocketPacket struct {
|
||||
type Packet struct {
|
||||
websocketMessageType int // websocket 消息类型
|
||||
packet []byte // 数据包
|
||||
callback func(err error) // 回调函数
|
|
@ -26,8 +26,8 @@ type Websocket struct {
|
|||
data map[string]any
|
||||
|
||||
mutex sync.Mutex
|
||||
packetPool *concurrent.Pool[*websocketPacket]
|
||||
packets []*websocketPacket
|
||||
packetPool *concurrent.Pool[*Packet]
|
||||
packets []*Packet
|
||||
|
||||
accumulate []server.Packet
|
||||
}
|
||||
|
@ -47,16 +47,16 @@ func (slf *Websocket) Run() error {
|
|||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
slf.Close()
|
||||
slf.OnConnectionClosedEvent(slf, err)
|
||||
slf.OnWebsocketConnectionClosedEvent(slf, err)
|
||||
}
|
||||
}()
|
||||
slf.OnConnectionOpenedEvent(slf)
|
||||
slf.OnWebsocketConnectionOpenedEvent(slf)
|
||||
for slf.packetPool != nil {
|
||||
messageType, packet, readErr := ws.ReadMessage()
|
||||
if readErr != nil {
|
||||
panic(readErr)
|
||||
}
|
||||
slf.OnConnectionReceivePacketEvent(slf, server.NewWSPacket(messageType, packet))
|
||||
slf.OnWebsocketConnectionReceivePacketEvent(slf, server.NewWSPacket(messageType, packet))
|
||||
}
|
||||
}()
|
||||
return nil
|
||||
|
@ -103,10 +103,10 @@ func (slf *Websocket) Write(packet server.Packet) {
|
|||
|
||||
// writeLoop 写循环
|
||||
func (slf *Websocket) writeLoop(wait *sync.WaitGroup) {
|
||||
slf.packetPool = concurrent.NewPool[*websocketPacket](10*1024,
|
||||
func() *websocketPacket {
|
||||
return &websocketPacket{}
|
||||
}, func(data *websocketPacket) {
|
||||
slf.packetPool = concurrent.NewPool[*Packet](10*1024,
|
||||
func() *Packet {
|
||||
return &Packet{}
|
||||
}, func(data *Packet) {
|
||||
data.packet = nil
|
||||
data.websocketMessageType = 0
|
||||
data.callback = nil
|
||||
|
|
|
@ -3,46 +3,46 @@ package client
|
|||
import "github.com/kercylan98/minotaur/server"
|
||||
|
||||
type (
|
||||
ConnectionClosedEventHandle func(conn *Websocket, err any)
|
||||
ConnectionOpenedEventHandle func(conn *Websocket)
|
||||
ConnectionReceivePacketEventHandle func(conn *Websocket, packet server.Packet)
|
||||
WebsocketConnectionClosedEventHandle func(conn *Websocket, err any)
|
||||
WebsocketConnectionOpenedEventHandle func(conn *Websocket)
|
||||
WebsocketConnectionReceivePacketEventHandle func(conn *Websocket, packet server.Packet)
|
||||
)
|
||||
|
||||
type websocketEvents struct {
|
||||
connectionClosedEventHandles []ConnectionClosedEventHandle
|
||||
connectionOpenedEventHandles []ConnectionOpenedEventHandle
|
||||
connectionReceivePacketEventHandles []ConnectionReceivePacketEventHandle
|
||||
websocketConnectionClosedEventHandles []WebsocketConnectionClosedEventHandle
|
||||
websocketConnectionOpenedEventHandles []WebsocketConnectionOpenedEventHandle
|
||||
websocketConnectionReceivePacketEventHandles []WebsocketConnectionReceivePacketEventHandle
|
||||
}
|
||||
|
||||
// RegConnectionClosedEvent 注册连接关闭事件
|
||||
func (slf *websocketEvents) RegConnectionClosedEvent(handle ConnectionClosedEventHandle) {
|
||||
slf.connectionClosedEventHandles = append(slf.connectionClosedEventHandles, handle)
|
||||
// RegWebsocketConnectionClosedEvent 注册连接关闭事件
|
||||
func (slf *websocketEvents) RegWebsocketConnectionClosedEvent(handle WebsocketConnectionClosedEventHandle) {
|
||||
slf.websocketConnectionClosedEventHandles = append(slf.websocketConnectionClosedEventHandles, handle)
|
||||
}
|
||||
|
||||
func (slf *websocketEvents) OnConnectionClosedEvent(conn *Websocket, err any) {
|
||||
for _, handle := range slf.connectionClosedEventHandles {
|
||||
func (slf *websocketEvents) OnWebsocketConnectionClosedEvent(conn *Websocket, err any) {
|
||||
for _, handle := range slf.websocketConnectionClosedEventHandles {
|
||||
handle(conn, err)
|
||||
}
|
||||
}
|
||||
|
||||
// RegConnectionOpenedEvent 注册连接打开事件
|
||||
func (slf *websocketEvents) RegConnectionOpenedEvent(handle ConnectionOpenedEventHandle) {
|
||||
slf.connectionOpenedEventHandles = append(slf.connectionOpenedEventHandles, handle)
|
||||
// RegWebsocketConnectionOpenedEvent 注册连接打开事件
|
||||
func (slf *websocketEvents) RegWebsocketConnectionOpenedEvent(handle WebsocketConnectionOpenedEventHandle) {
|
||||
slf.websocketConnectionOpenedEventHandles = append(slf.websocketConnectionOpenedEventHandles, handle)
|
||||
}
|
||||
|
||||
func (slf *websocketEvents) OnConnectionOpenedEvent(conn *Websocket) {
|
||||
for _, handle := range slf.connectionOpenedEventHandles {
|
||||
func (slf *websocketEvents) OnWebsocketConnectionOpenedEvent(conn *Websocket) {
|
||||
for _, handle := range slf.websocketConnectionOpenedEventHandles {
|
||||
handle(conn)
|
||||
}
|
||||
}
|
||||
|
||||
// RegConnectionReceivePacketEvent 注册连接接收数据包事件
|
||||
func (slf *websocketEvents) RegConnectionReceivePacketEvent(handle ConnectionReceivePacketEventHandle) {
|
||||
slf.connectionReceivePacketEventHandles = append(slf.connectionReceivePacketEventHandles, handle)
|
||||
// RegWebsocketConnectionReceivePacketEvent 注册连接接收数据包事件
|
||||
func (slf *websocketEvents) RegWebsocketConnectionReceivePacketEvent(handle WebsocketConnectionReceivePacketEventHandle) {
|
||||
slf.websocketConnectionReceivePacketEventHandles = append(slf.websocketConnectionReceivePacketEventHandles, handle)
|
||||
}
|
||||
|
||||
func (slf *websocketEvents) OnConnectionReceivePacketEvent(conn *Websocket, packet server.Packet) {
|
||||
for _, handle := range slf.connectionReceivePacketEventHandles {
|
||||
func (slf *websocketEvents) OnWebsocketConnectionReceivePacketEvent(conn *Websocket, packet server.Packet) {
|
||||
for _, handle := range slf.websocketConnectionReceivePacketEventHandles {
|
||||
handle(conn, packet)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,8 +22,8 @@ func NewEndpoint(name, address string, options ...EndpointOption) *Endpoint {
|
|||
return 1 / (1 + 1.5*time.Duration(costUnixNano).Seconds())
|
||||
}
|
||||
}
|
||||
endpoint.client.RegConnectionClosedEvent(endpoint.onConnectionClosed)
|
||||
endpoint.client.RegConnectionReceivePacketEvent(endpoint.onConnectionReceivePacket)
|
||||
endpoint.client.RegWebsocketConnectionClosedEvent(endpoint.onConnectionClosed)
|
||||
endpoint.client.RegWebsocketConnectionReceivePacketEvent(endpoint.onConnectionReceivePacket)
|
||||
return endpoint
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue