Skip to content

Commit

Permalink
refactor: 调整事件函数名称
Browse files Browse the repository at this point in the history
  • Loading branch information
kercylan98 committed Aug 22, 2023
1 parent ab19bd6 commit dc76196
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package client

type websocketPacket struct {
type Packet struct {
websocketMessageType int // websocket 消息类型
packet []byte // 数据包
callback func(err error) // 回调函数
Expand Down
18 changes: 9 additions & 9 deletions server/client/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
42 changes: 21 additions & 21 deletions server/client/websocket_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
4 changes: 2 additions & 2 deletions server/gateway/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit dc76196

Please sign in to comment.