refactor(common): 优化缓存和RPC模块,调整用户在线状态数据类型为uint16
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package rpc
|
||||
|
||||
import (
|
||||
"blazing/common/data/cache"
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
@@ -11,24 +12,24 @@ import (
|
||||
|
||||
const rpcaddr = "127.0.0.1:40000"
|
||||
|
||||
var clientmap = make(map[int]*ClientHandler) //客户端map
|
||||
var usermap = make(map[int]int) //用户->客户端的map
|
||||
var clientmap = make(map[uint16]*ClientHandler) //客户端map
|
||||
//var usermap = make(map[int]int) //用户->客户端的map
|
||||
|
||||
// Define the client handler interface
|
||||
type ClientHandler struct {
|
||||
KickPerson func(int) error //踢人,这里是返回具体的logic
|
||||
QuitSelf func(int) error //关闭服务器进程
|
||||
KickPerson func(uint32) error //踢人,这里是返回具体的logic
|
||||
QuitSelf func(int) error //关闭服务器进程
|
||||
}
|
||||
|
||||
// Define the server handler
|
||||
type ServerHandler struct{}
|
||||
|
||||
// 实现踢人
|
||||
func (h *ServerHandler) Kick(ctx context.Context, userid int) error {
|
||||
func (h *ServerHandler) Kick(ctx context.Context, userid uint32) error {
|
||||
|
||||
useid1, ok := usermap[userid]
|
||||
useid1, err := cache.NewSessionManager().GetUserOnline(userid)
|
||||
|
||||
if !ok {
|
||||
if err != nil {
|
||||
return fmt.Errorf("user not found")
|
||||
}
|
||||
|
||||
@@ -43,21 +44,8 @@ func (h *ServerHandler) Kick(ctx context.Context, userid int) error {
|
||||
//return nil
|
||||
}
|
||||
|
||||
// 退出指定服务器
|
||||
// func (h *ServerHandler) Quit(ctx context.Context, portid int) error {
|
||||
|
||||
// a, ok := clientmap[portid]
|
||||
|
||||
// if ok && a != nil {
|
||||
// a.QuitSelf(0)
|
||||
// }
|
||||
// //clientmap[portid].QuitSelf(0)
|
||||
// return nil
|
||||
|
||||
// }
|
||||
|
||||
// 注册logic服务器
|
||||
func (h *ServerHandler) RegisterLogic(ctx context.Context, port int) error {
|
||||
func (h *ServerHandler) RegisterLogic(ctx context.Context, port uint16) error {
|
||||
//TODO 待修复滚动更新可能导致的玩家可以同时在旧服务器和新服务器同时在线的bug
|
||||
revClient, ok := jsonrpc.ExtractReverseClient[ClientHandler](ctx)
|
||||
if !ok {
|
||||
@@ -73,21 +61,6 @@ func (h *ServerHandler) RegisterLogic(ctx context.Context, port int) error {
|
||||
|
||||
}
|
||||
|
||||
// 用户登录事件
|
||||
func (h *ServerHandler) UserLogin(ctx context.Context, port int, userid int) error {
|
||||
|
||||
usermap[userid] = port
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
// 用户登出事件
|
||||
func (h *ServerHandler) UserLogout(ctx context.Context, port int, userid int) error {
|
||||
delete(usermap, userid)
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
func StartServer() {
|
||||
// create a new server instance
|
||||
rpcServer := jsonrpc.NewServer(jsonrpc.WithReverseClient[ClientHandler](""))
|
||||
@@ -105,9 +78,9 @@ func StartServer() {
|
||||
var closer jsonrpc.ClientCloser
|
||||
|
||||
func StartClient(port uint16, callback any) *struct {
|
||||
Kick func(int32) error
|
||||
Kick func(uint32) error
|
||||
|
||||
RegisterLogic func(int32) error
|
||||
RegisterLogic func(uint16) error
|
||||
} {
|
||||
|
||||
closer1, err := jsonrpc.NewMergeClient(context.Background(), "ws://"+rpcaddr, "", []interface{}{
|
||||
@@ -118,7 +91,7 @@ func StartClient(port uint16, callback any) *struct {
|
||||
}
|
||||
|
||||
if port != 0 { //注册logic
|
||||
RPCClient.RegisterLogic(int32(port))
|
||||
RPCClient.RegisterLogic(port)
|
||||
|
||||
}
|
||||
|
||||
@@ -135,9 +108,9 @@ func CloseClient() {
|
||||
|
||||
// Setup RPCClient with reverse call handler
|
||||
var RPCClient struct {
|
||||
Kick func(int32) error //踢人
|
||||
Kick func(uint32) error //踢人
|
||||
|
||||
RegisterLogic func(int32) error //注册服务器消息
|
||||
RegisterLogic func(uint16) error //注册服务器消息
|
||||
|
||||
// UserLogin func(int32, int32) error //用户登录事件
|
||||
// UserLogout func(int32, int32) error //用户登出事件
|
||||
|
||||
Reference in New Issue
Block a user