82 lines
2.1 KiB
Go
82 lines
2.1 KiB
Go
package common
|
|
|
|
import (
|
|
rawproto "google.golang.org/protobuf/proto"
|
|
"mongo.games.com/game/proto"
|
|
protocol_game "mongo.games.com/game/protocol/server"
|
|
"mongo.games.com/goserver/core/logger"
|
|
"mongo.games.com/goserver/core/netlib"
|
|
"mongo.games.com/goserver/srvlib/protocol"
|
|
)
|
|
|
|
type WGCoinSceneChange struct {
|
|
SnId int32
|
|
SceneId int32
|
|
}
|
|
|
|
type WGAddCoin struct {
|
|
SnId int32
|
|
Coin int64
|
|
GainWay int32
|
|
Oper string
|
|
Remark string
|
|
Broadcast bool
|
|
WriteLog bool
|
|
}
|
|
|
|
func createMulticastPacket(packetid int, data interface{}, sis ...*protocol.MCSessionUnion) (rawproto.Message, error) {
|
|
pack := &protocol.SSPacketMulticast{
|
|
Sessions: sis,
|
|
PacketId: proto.Int(packetid),
|
|
}
|
|
if byteData, ok := data.([]byte); ok {
|
|
pack.Data = byteData
|
|
} else {
|
|
byteData, err := netlib.MarshalPacket(packetid, data)
|
|
if err == nil {
|
|
pack.Data = byteData
|
|
} else {
|
|
logger.Logger.Info("MulticastPacketFactory.CreateMulticastPacket err:", err)
|
|
return nil, err
|
|
}
|
|
}
|
|
proto.SetDefaults(pack)
|
|
return pack, nil
|
|
}
|
|
|
|
func SendToGate(sid int64, packetId int, pack interface{}, s *netlib.Session) bool {
|
|
if s == nil || pack == nil || sid == 0 {
|
|
return false
|
|
}
|
|
pack, err := createMulticastPacket(packetId, pack,
|
|
&protocol.MCSessionUnion{
|
|
Mccs: &protocol.MCClientSession{
|
|
SId: proto.Int64(sid)}})
|
|
if err == nil && pack != nil {
|
|
if d, err := netlib.MarshalPacket(int(protocol.SrvlibPacketID_PACKET_SS_MULTICAST), pack); err == nil {
|
|
return s.Send(int(protocol.SrvlibPacketID_PACKET_SS_MULTICAST), d, true)
|
|
} else {
|
|
logger.Logger.Warn("SendToGate err:", err)
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// TransmitToServer 转发消息到指定服务器
|
|
// sid: 客户端连接标识
|
|
// packetId: 消息id
|
|
// pack: 消息内容
|
|
// s: 接收消息的服务器连接
|
|
func TransmitToServer(sid int64, packetId int, pack interface{}, s *netlib.Session) bool {
|
|
if d, err := netlib.MarshalPacket(packetId, pack); err == nil {
|
|
pack := &protocol_game.SSTransmit{
|
|
PacketData: d,
|
|
SessionId: sid,
|
|
}
|
|
return s.Send(int(protocol_game.TransmitPacketID_PACKET_SS_PACKET_TRANSMIT), pack, true)
|
|
} else {
|
|
logger.Logger.Warn("TransmitToServer err:", err)
|
|
}
|
|
return false
|
|
}
|