2025-07-06 19:31:30 +08:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
|
|
import (
|
2025-07-15 18:10:25 +00:00
|
|
|
"blazing/common/data/share"
|
2025-07-26 03:48:06 +00:00
|
|
|
"blazing/cool"
|
2025-11-03 19:14:34 +08:00
|
|
|
|
2025-07-06 19:31:30 +08:00
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
"log"
|
|
|
|
|
|
2026-01-09 19:58:12 +08:00
|
|
|
config "blazing/modules/config/service"
|
2025-10-15 22:53:14 +00:00
|
|
|
|
2025-07-06 19:31:30 +08:00
|
|
|
"github.com/filecoin-project/go-jsonrpc"
|
2025-10-08 16:54:03 +08:00
|
|
|
"github.com/gogf/gf/v2/util/gconv"
|
2025-07-06 19:31:30 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Define the server handler
|
|
|
|
|
type ServerHandler struct{}
|
|
|
|
|
|
|
|
|
|
// 实现踢人
|
2026-01-25 03:40:29 +08:00
|
|
|
func (*ServerHandler) Kick(_ context.Context, userid uint32) error {
|
2025-11-01 00:40:19 +08:00
|
|
|
|
2025-07-15 18:10:25 +00:00
|
|
|
useid1, err := share.ShareManager.GetUserOnline(userid)
|
2025-07-06 19:31:30 +08:00
|
|
|
|
2025-07-15 13:51:10 +00:00
|
|
|
if err != nil {
|
2025-10-15 22:53:14 +00:00
|
|
|
return fmt.Errorf("user not found", err)
|
2025-07-06 19:31:30 +08:00
|
|
|
}
|
2025-11-07 22:50:34 +08:00
|
|
|
|
2025-12-09 02:48:41 +08:00
|
|
|
cl, ok := cool.GetClient(useid1)
|
2025-10-05 02:00:00 +00:00
|
|
|
if ok {
|
2025-10-16 16:28:00 +00:00
|
|
|
err := cl.KickPerson(userid) //实现指定服务器踢人
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("踢人失败", err)
|
|
|
|
|
}
|
2025-10-05 02:00:00 +00:00
|
|
|
}
|
2025-10-16 16:28:00 +00:00
|
|
|
return nil
|
2025-07-06 19:31:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 注册logic服务器
|
2026-01-25 03:40:29 +08:00
|
|
|
func (*ServerHandler) RegisterLogic(ctx context.Context, id, port uint16) error {
|
2026-02-02 18:32:41 +08:00
|
|
|
fmt.Println("注册logic服务器", id, port)
|
2025-11-03 19:14:34 +08:00
|
|
|
|
2025-07-06 22:58:39 +08:00
|
|
|
//TODO 待修复滚动更新可能导致的玩家可以同时在旧服务器和新服务器同时在线的bug
|
2025-12-09 02:48:41 +08:00
|
|
|
revClient, ok := jsonrpc.ExtractReverseClient[cool.ClientHandler](ctx)
|
2025-07-06 19:31:30 +08:00
|
|
|
if !ok {
|
|
|
|
|
return fmt.Errorf("no reverse client")
|
|
|
|
|
}
|
2026-01-09 19:58:12 +08:00
|
|
|
t := config.NewServerService().GetServerID((id))
|
2026-01-01 22:19:00 +08:00
|
|
|
|
2026-01-01 19:57:39 +08:00
|
|
|
aa, ok := cool.GetClient(t.Port)
|
2025-11-01 14:31:19 +08:00
|
|
|
if ok && aa != nil { //如果已经存在且这个端口已经被存过
|
2025-10-15 22:53:14 +00:00
|
|
|
aa.QuitSelf(0)
|
2025-07-06 22:58:39 +08:00
|
|
|
}
|
2025-12-09 02:48:41 +08:00
|
|
|
cool.AddClient(port, &revClient)
|
2025-10-15 22:53:14 +00:00
|
|
|
|
2025-11-03 15:30:41 +00:00
|
|
|
//Refurh()
|
2025-07-06 19:31:30 +08:00
|
|
|
return nil
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-25 03:40:29 +08:00
|
|
|
func CServer() *jsonrpc.RPCServer {
|
2025-07-06 19:31:30 +08:00
|
|
|
// create a new server instance
|
2025-12-09 02:48:41 +08:00
|
|
|
rpcServer := jsonrpc.NewServer(jsonrpc.WithReverseClient[cool.ClientHandler](""))
|
2025-07-06 19:31:30 +08:00
|
|
|
|
|
|
|
|
rpcServer.Register("", &ServerHandler{})
|
|
|
|
|
|
2026-01-25 03:40:29 +08:00
|
|
|
return rpcServer
|
2026-02-02 18:32:41 +08:00
|
|
|
|
2025-07-06 19:31:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var closer jsonrpc.ClientCloser
|
|
|
|
|
|
2025-07-17 05:20:30 +08:00
|
|
|
func StartClient(id, port uint16, callback any) *struct {
|
2025-07-15 13:51:10 +00:00
|
|
|
Kick func(uint32) error
|
2025-07-06 22:58:39 +08:00
|
|
|
|
2025-07-17 05:20:30 +08:00
|
|
|
RegisterLogic func(uint16, uint16) error
|
2025-07-06 19:31:30 +08:00
|
|
|
} {
|
2026-02-07 22:54:44 +08:00
|
|
|
cool.Config.File.Domain = "127.0.0.1"
|
2026-01-25 03:40:29 +08:00
|
|
|
var rpcaddr = "ws://" + cool.Config.File.Domain + gconv.String(cool.Config.Address) + "/rpc"
|
2026-02-07 22:54:44 +08:00
|
|
|
|
2025-07-17 05:20:30 +08:00
|
|
|
closer1, err := jsonrpc.NewMergeClient(context.Background(),
|
2026-01-25 03:40:29 +08:00
|
|
|
rpcaddr, "", []interface{}{
|
2025-07-17 05:20:30 +08:00
|
|
|
&RPCClient,
|
|
|
|
|
}, nil, jsonrpc.WithClientHandler("", callback),
|
2025-10-05 00:29:22 +08:00
|
|
|
jsonrpc.WithReconnFun(func() { RPCClient.RegisterLogic(id, port) }),
|
2025-07-17 05:20:30 +08:00
|
|
|
)
|
2025-07-06 19:31:30 +08:00
|
|
|
if err != nil {
|
|
|
|
|
log.Fatalf("Failed to create client: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-31 08:29:37 +00:00
|
|
|
//if port != 0 { //注册logic
|
2026-01-25 03:40:29 +08:00
|
|
|
defer RPCClient.RegisterLogic(id, port)
|
2025-07-06 19:31:30 +08:00
|
|
|
|
2025-07-31 08:29:37 +00:00
|
|
|
//}
|
2025-07-06 19:31:30 +08:00
|
|
|
|
|
|
|
|
closer = closer1
|
|
|
|
|
|
2026-01-25 03:40:29 +08:00
|
|
|
return &RPCClient
|
2025-07-06 19:31:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Setup RPCClient with reverse call handler
|
|
|
|
|
var RPCClient struct {
|
2025-07-15 13:51:10 +00:00
|
|
|
Kick func(uint32) error //踢人
|
2025-07-06 22:58:39 +08:00
|
|
|
|
2025-07-17 05:20:30 +08:00
|
|
|
RegisterLogic func(uint16, uint16) error
|
2025-07-06 19:31:30 +08:00
|
|
|
|
2025-07-15 12:14:17 +08:00
|
|
|
// UserLogin func(int32, int32) error //用户登录事件
|
|
|
|
|
// UserLogout func(int32, int32) error //用户登出事件
|
2025-07-06 19:31:30 +08:00
|
|
|
}
|