Skip to content

Commit

Permalink
feat: server 包 Server 新增 RegMessageReadyEvent 函数
Browse files Browse the repository at this point in the history
  • Loading branch information
kercylan98 committed Sep 5, 2023
1 parent 034ca17 commit 04c40bf
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
34 changes: 34 additions & 0 deletions server/client/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package client_test

import (
"github.com/kercylan98/minotaur/server"
"github.com/kercylan98/minotaur/server/client"
"sync"
"testing"
)

func TestClient_WriteWS(t *testing.T) {
var wait sync.WaitGroup
wait.Add(1)
srv := server.New(server.NetworkWebsocket)
srv.RegConnectionReceivePacketEvent(func(srv *server.Server, conn *server.Conn, packet []byte) {
srv.Shutdown()
})
srv.RegStopEvent(func(srv *server.Server) {
wait.Done()
})
srv.RegMessageReadyEvent(func(srv *server.Server) {
cli := client.NewWebsocket("ws://127.0.0.1:9999")
cli.RegConnectionOpenedEvent(func(conn *client.Client) {
conn.WriteWS(2, []byte("Hello"))
})
if err := cli.Run(); err != nil {
panic(err)
}
})
if err := srv.Run(":9999"); err != nil {
panic(err)
}

wait.Wait()
}
26 changes: 25 additions & 1 deletion server/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type ShuntChannelCreatedEventHandle func(srv *Server, guid int64)
type ShuntChannelClosedEventHandle func(srv *Server, guid int64)
type ConnectionPacketPreprocessEventHandle func(srv *Server, conn *Conn, packet []byte, abort func(), usePacket func(newPacket []byte))
type MessageExecBeforeEventHandle func(srv *Server, message *Message) bool
type MessageReadyEventHandle func(srv *Server)

func newEvent(srv *Server) *event {
return &event{
Expand All @@ -46,6 +47,7 @@ func newEvent(srv *Server) *event {
shuntChannelClosedEventHandles: slice.NewPriority[ShuntChannelClosedEventHandle](),
connectionPacketPreprocessEventHandles: slice.NewPriority[ConnectionPacketPreprocessEventHandle](),
messageExecBeforeEventHandles: slice.NewPriority[MessageExecBeforeEventHandle](),
messageReadyEventHandles: slice.NewPriority[MessageReadyEventHandle](),
}
}

Expand All @@ -66,6 +68,7 @@ type event struct {
shuntChannelClosedEventHandles *slice.Priority[ShuntChannelClosedEventHandle]
connectionPacketPreprocessEventHandles *slice.Priority[ConnectionPacketPreprocessEventHandle]
messageExecBeforeEventHandles *slice.Priority[MessageExecBeforeEventHandle]
messageReadyEventHandles *slice.Priority[MessageReadyEventHandle]

consoleCommandEventHandles map[string]*slice.Priority[ConsoleCommandEventHandle]
consoleCommandEventHandleInitOnce sync.Once
Expand Down Expand Up @@ -147,6 +150,7 @@ func (slf *event) OnStartBeforeEvent() {
}

// RegStartFinishEvent 在服务器启动完成时将立刻执行被注册的事件处理函数
// - 需要注意该时刻服务器已经启动完成,但是还有可能未开始处理消息,客户端有可能无法连接,如果需要在消息处理器准备就绪后执行,请使用 RegMessageReadyEvent 函数
func (slf *event) RegStartFinishEvent(handle StartFinishEventHandle, priority ...int) {
slf.startFinishEventHandles.Append(handle, slice.GetValue(priority, 0))
log.Info("Server", log.String("RegEvent", runtimes.CurrentRunningFuncName()), log.String("handle", reflect.TypeOf(handle).String()))
Expand Down Expand Up @@ -365,7 +369,6 @@ func (slf *event) RegMessageExecBeforeEvent(handle MessageExecBeforeEventHandle,
log.Info("Server", log.String("RegEvent", runtimes.CurrentRunningFuncName()), log.String("handle", reflect.TypeOf(handle).String()))
}

// OnMessageExecBeforeEvent 执行消息处理前的事件处理函数
func (slf *event) OnMessageExecBeforeEvent(message *Message) bool {
if slf.messageExecBeforeEventHandles.Len() == 0 {
return true
Expand All @@ -384,6 +387,27 @@ func (slf *event) OnMessageExecBeforeEvent(message *Message) bool {
return result
}

// RegMessageReadyEvent 在服务器消息处理器准备就绪时立即执行被注册的事件处理函数
func (slf *event) RegMessageReadyEvent(handle MessageReadyEventHandle, priority ...int) {
slf.messageReadyEventHandles.Append(handle, slice.GetValue(priority, 0))
}

func (slf *event) OnMessageReadyEvent() {
if slf.messageReadyEventHandles.Len() == 0 {
return
}
defer func() {
if err := recover(); err != nil {
log.Error("Server", log.String("OnMessageReadyEvent", fmt.Sprintf("%v", err)))
debug.PrintStack()
}
}()
slf.messageReadyEventHandles.RangeValue(func(index int, value MessageReadyEventHandle) bool {
value(slf.Server)
return true
})
}

func (slf *event) check() {
switch slf.network {
case NetworkHttp, NetworkGRPC, NetworkNone:
Expand Down
8 changes: 8 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,10 @@ func (slf *Server) Run(addr string) error {
)
log.Info("Server", log.String(serverMark, "===================================================================="))
slf.OnStartFinishEvent()
time.Sleep(time.Second)
if !slf.isShutdown.Load() {
slf.OnMessageReadyEvent()
}

signal.Notify(slf.systemSignal, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
select {
Expand All @@ -354,6 +358,10 @@ func (slf *Server) Run(addr string) error {
}
} else {
slf.OnStartFinishEvent()
time.Sleep(time.Second)
if !slf.isShutdown.Load() {
slf.OnMessageReadyEvent()
}
}

return nil
Expand Down

0 comments on commit 04c40bf

Please sign in to comment.