2025-05-29 17:54:47 +08:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"game/common/proto/pb"
|
|
|
|
"game/test/client/config"
|
|
|
|
"github.com/fox/fox/log"
|
|
|
|
"github.com/fox/fox/processor"
|
|
|
|
"github.com/fox/fox/ws"
|
|
|
|
"github.com/golang/protobuf/proto"
|
2025-05-29 23:02:40 +08:00
|
|
|
"time"
|
2025-05-29 17:54:47 +08:00
|
|
|
)
|
|
|
|
|
2025-05-29 20:23:33 +08:00
|
|
|
var Clients []*ClientService
|
2025-05-29 17:54:47 +08:00
|
|
|
|
|
|
|
type ClientService struct {
|
2025-05-29 20:47:59 +08:00
|
|
|
client *ws.Client
|
|
|
|
processor *processor.Processor
|
|
|
|
userId int64
|
|
|
|
username string
|
|
|
|
password string
|
|
|
|
lastServiceName string // 最近使用的服务的节点
|
2025-05-29 17:54:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Init() {
|
|
|
|
for i := 0; i < config.Command.ServiceNum; i++ {
|
|
|
|
sid := config.Command.ServiceId + i
|
|
|
|
if srv := newClientService(sid); srv != nil {
|
2025-05-29 20:23:33 +08:00
|
|
|
Clients = append(Clients, srv)
|
2025-05-29 17:54:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Stop() {
|
2025-05-29 20:23:33 +08:00
|
|
|
for _, s := range Clients {
|
|
|
|
s.client.Stop()
|
2025-05-29 17:54:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newClientService(serviceId int) *ClientService {
|
|
|
|
var err error
|
|
|
|
s := new(ClientService)
|
|
|
|
|
2025-05-29 20:47:59 +08:00
|
|
|
s.username = fmt.Sprintf("test%04d", serviceId)
|
|
|
|
s.password = "123456"
|
2025-05-29 17:54:47 +08:00
|
|
|
size := len(config.Cfg.Special.Address)
|
|
|
|
addr := config.Cfg.Special.Address[serviceId%size]
|
2025-05-29 23:02:40 +08:00
|
|
|
wsAddr := fmt.Sprintf("ws://%v", addr)
|
|
|
|
if s.client, err = ws.NewClient(wsAddr, s); err != nil {
|
|
|
|
log.Fatal(err.Error())
|
2025-05-29 17:54:47 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
s.processor = processor.NewProcessor()
|
|
|
|
s.initProcessor()
|
|
|
|
s.OnInit()
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ClientService) OnInit() {
|
|
|
|
s.client.Start()
|
|
|
|
log.Debug("onInit")
|
2025-05-29 23:02:40 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
s.login()
|
2025-05-29 17:54:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ClientService) CanStop() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// 处理其它服发送过来的消息
|
|
|
|
func (s *ClientService) OnMessage(data []byte) error {
|
|
|
|
var cMsg = &pb.ClientMsg{}
|
|
|
|
var err error
|
|
|
|
if err = proto.Unmarshal(data, cMsg); err != nil {
|
|
|
|
log.Error(err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if req, err := s.processor.Unmarshal(cMsg.MsgId, cMsg.Data); err == nil {
|
2025-05-29 20:47:59 +08:00
|
|
|
err = s.processor.Dispatch(cMsg.MsgId, cMsg, req)
|
2025-05-29 17:54:47 +08:00
|
|
|
}
|
|
|
|
//log.Debug(s.Log("on message:%v", string(cMsg)))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 向内部服务发送消息
|
2025-05-29 20:23:33 +08:00
|
|
|
func (s *ClientService) SendData(serviceTypeId pb.ServiceTypeId, msgId int32, data []byte) {
|
|
|
|
msg := &pb.ClientMsg{
|
|
|
|
ServiceTid: serviceTypeId,
|
2025-05-29 20:47:59 +08:00
|
|
|
ServiceName: s.lastServiceName,
|
2025-05-29 20:23:33 +08:00
|
|
|
UserId: s.userId,
|
|
|
|
MsgId: msgId,
|
|
|
|
Data: data,
|
|
|
|
}
|
|
|
|
d, _ := proto.Marshal(msg)
|
|
|
|
s.client.SendMsg(d)
|
2025-05-29 17:54:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 向内部服务发送消息
|
2025-05-29 20:23:33 +08:00
|
|
|
func (s *ClientService) SendMsg(serviceTypeId pb.ServiceTypeId, msgId int32, msg proto.Message) {
|
2025-05-29 17:54:47 +08:00
|
|
|
data, _ := proto.Marshal(msg)
|
2025-05-29 20:23:33 +08:00
|
|
|
s.SendData(serviceTypeId, msgId, data)
|
2025-05-29 17:54:47 +08:00
|
|
|
}
|