```
feat(socket): 添加服务器优雅退出机制 在 Server 结构体中新增 quit 字段,并在 OnTick 方法中检查该字段, 若为 true 则调用 os.Exit(0) 实现程序正常退出。同时清理了 controller 中 冗余的导入和无用逻辑,优化 server 启动流程并修复 RPC 客户端传递错误问题。 ```
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -60,6 +61,10 @@ func (s *Server) OnClose(c gnet.Conn, _ error) (action gnet.Action) {
|
|||||||
}
|
}
|
||||||
func (s *Server) OnTick() (delay time.Duration, action gnet.Action) {
|
func (s *Server) OnTick() (delay time.Duration, action gnet.Action) {
|
||||||
cool.Loger.Infof(context.Background(), "[connected-count=%v]", atomic.LoadInt64(&s.connected))
|
cool.Loger.Infof(context.Background(), "[connected-count=%v]", atomic.LoadInt64(&s.connected))
|
||||||
|
if s.quit {
|
||||||
|
//执行正常退出逻辑
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
return 10 * time.Second, gnet.None
|
return 10 * time.Second, gnet.None
|
||||||
}
|
}
|
||||||
func (s *Server) OnBoot(eng gnet.Engine) gnet.Action {
|
func (s *Server) OnBoot(eng gnet.Engine) gnet.Action {
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ type Server struct {
|
|||||||
codec codec.SocketCodec
|
codec codec.SocketCodec
|
||||||
handler Handler
|
handler Handler
|
||||||
discorse bool
|
discorse bool
|
||||||
|
quit bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type Option func(*Server)
|
type Option func(*Server)
|
||||||
|
|||||||
19
common/socket/kick.go
Normal file
19
common/socket/kick.go
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package socket
|
||||||
|
|
||||||
|
import (
|
||||||
|
"blazing/logic/service/player"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (h *Server) KickPerson(a int) error {
|
||||||
|
|
||||||
|
fmt.Println("检测到踢人请求", a)
|
||||||
|
player.KickPlayer(uint32(a))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
func (h *Server) QuitSelf(a int) error {
|
||||||
|
//TODO 这里待退出
|
||||||
|
fmt.Println("检测到退出请求")
|
||||||
|
h.quit = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -6,9 +6,7 @@ import (
|
|||||||
"blazing/cool"
|
"blazing/cool"
|
||||||
|
|
||||||
"blazing/logic/service/player"
|
"blazing/logic/service/player"
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
@@ -22,6 +20,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var Maincontroller = NewController() //注入service
|
var Maincontroller = NewController() //注入service
|
||||||
|
|
||||||
|
func NewController() *Controller {
|
||||||
|
return &Controller{}
|
||||||
|
}
|
||||||
|
|
||||||
// 分发cmd逻辑实现Controller
|
// 分发cmd逻辑实现Controller
|
||||||
type Controller struct {
|
type Controller struct {
|
||||||
Port uint16
|
Port uint16
|
||||||
@@ -32,40 +35,6 @@ type Controller struct {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type LogicClient struct {
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *LogicClient) KickPerson(a int) error {
|
|
||||||
|
|
||||||
fmt.Println("检测到踢人请求", a)
|
|
||||||
player.KickPlayer(uint32(a))
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
func (h *LogicClient) QuitSelf(a int) error {
|
|
||||||
//TODO 这里待退出
|
|
||||||
fmt.Println("检测到退出请求")
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
|
|
||||||
for {
|
|
||||||
|
|
||||||
//entity.ConutPlayer()
|
|
||||||
fmt.Println("当前在线人数", player.ConutPlayer())
|
|
||||||
|
|
||||||
if player.ConutPlayer() <= 0 {
|
|
||||||
//执行退出逻辑
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
<-time.After(5 * time.Second)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
//service.KickPlayer(uint32(a))
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
func NewController() *Controller {
|
|
||||||
return &Controller{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func ParseCmd[T any](a T, data []byte) T {
|
func ParseCmd[T any](a T, data []byte) T {
|
||||||
// := info.NewLoginSidInfo()
|
// := info.NewLoginSidInfo()
|
||||||
struc.Unpack(bytes.NewBuffer(data), &a)
|
struc.Unpack(bytes.NewBuffer(data), &a)
|
||||||
|
|||||||
@@ -82,21 +82,19 @@ func Start(serverid uint16) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to determine port: %v", err)
|
log.Fatalf("Failed to determine port: %v", err)
|
||||||
}
|
}
|
||||||
|
ser := socket.NewServer(
|
||||||
|
socket.WithCORS(),
|
||||||
|
socket.WithPort(port),
|
||||||
|
|
||||||
|
socket.WithSocketHandler(head))
|
||||||
// go func() {
|
// go func() {
|
||||||
t := rpc.StartClient(serverid, uint16(port), new(controller.LogicClient))
|
t := rpc.StartClient(serverid, uint16(port), ser)
|
||||||
|
|
||||||
controller.Maincontroller.RPCClient = *t //将RPC赋值Start
|
controller.Maincontroller.RPCClient = *t //将RPC赋值Start
|
||||||
controller.Maincontroller.Port = uint16(port) //赋值服务器ID
|
controller.Maincontroller.Port = uint16(port) //赋值服务器ID
|
||||||
|
|
||||||
blservice.NewLoginServiceService().SetServerID(serverid, gconv.Uint16(port), t)
|
blservice.NewLoginServiceService().SetServerID(serverid, gconv.Uint16(port), t)
|
||||||
socket.
|
ser.Boot()
|
||||||
NewServer(
|
|
||||||
socket.WithCORS(),
|
|
||||||
socket.WithPort(port),
|
|
||||||
|
|
||||||
socket.WithSocketHandler(head)).
|
|
||||||
Boot()
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
go rpc.StartServer()
|
go rpc.StartServer()
|
||||||
|
|||||||
Reference in New Issue
Block a user