/* Sog 游戏基础库 2016 by zouwei */ using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Sog; using Sog.Gate; namespace Gate { //处理和后端服务器的通信消息 public class ServerMsgTrans : BaseReloadableService { private ServerApp m_app; private uint m_backendserverseq = 0; public override int GetServiceType() { return (int)GateServiceType.ServerMsgTrans; } public override void Dispose() { m_app = null; } public ServerMsgTrans(ServerApp app) { m_app = app; } public void NotifyServerClientDisconnected(long sessionId, uint logicSvrId) { GateMsgHeader gateHeader; gateHeader.OpCode = (int)GateMsgOpCode.DisconnectedReq; gateHeader.SessionID = sessionId; NotifyServer(logicSvrId, gateHeader); } public void NotifyServerClientConnected(GateClientInfo info, GateClientConnectedReq req) { uint serverID = GateServerUtils.GetServerRoute().SelectBackEndServerID(info); if (serverID == 0) { TraceLog.Error("get route SelectBackEndServerID 0 invalid,session id {0}", info.SessionID); return; } GateMsgHeader gateHeader; gateHeader.OpCode = (int)GateMsgOpCode.ConnectedReq; gateHeader.SessionID = info.SessionID; byte[] headerBytes = GateMsgHeaderPacker.Pack(ref gateHeader); byte[] reqBytes = req.Pack(); MessageData newMessage = new MessageData(); newMessage.Header.Type = (int)SpecialMessageType.GateMsgHeaderType; newMessage.Header.Length = reqBytes.Length + headerBytes.Length; newMessage.Header.ObjectID = info.SessionID; newMessage.Header.ServerID = 0; //newMessage.Data = new byte[newMessage.Header.Length]; newMessage.MallocData(newMessage.Header.Length); Buffer.BlockCopy(headerBytes, 0, newMessage.Buffer.Data, 0, headerBytes.Length); Buffer.BlockCopy(reqBytes, 0, newMessage.Buffer.Data, headerBytes.Length, reqBytes.Length); m_app.GetCluster().Send(serverID, newMessage); } public void NotifyServer(uint serverID, GateMsgHeader gateHeader) { MessageData message = new MessageData(); message.Header.Length = GateMsgHeaderPacker.GetLength(); message.Header.Type = (int)SpecialMessageType.GateMsgHeaderType; message.Header.ObjectID = gateHeader.SessionID; message.Header.ServerID = 0; message.MallocData(GateMsgHeaderPacker.GetLength()); GateMsgHeaderPacker.Pack(ref gateHeader, message.Buffer.Data); m_app.GetCluster().Send(serverID, message); } public void TransClientMessageToServer(GateClientInfo info, MessageData message) { GateMsgHeader gateHeader; gateHeader.OpCode = (int)GateMsgOpCode.Trans; gateHeader.SessionID = info.SessionID; byte[] headerBytes = GateMsgHeaderPacker.Pack(ref gateHeader); MessageData newMessage = new MessageData(); newMessage.Header.Type = message.Header.Type; newMessage.Header.Length = message.Header.Length + headerBytes.Length; newMessage.Header.ObjectID = info.SessionID; newMessage.Header.ServerID = 0; //newMessage.Data = new byte[newMessage.Header.Length]; newMessage.MallocData(newMessage.Header.Length); Buffer.BlockCopy(headerBytes,0, newMessage.Buffer.Data,0, headerBytes.Length); Buffer.BlockCopy(message.Buffer.Data, 0, newMessage.Buffer.Data, headerBytes.Length, message.Buffer.Length); m_app.GetCluster().Send(info.LinkServerID, newMessage); } public void NotifyServerClientCount(int count) { GateMsgHeader gateHeader; gateHeader.OpCode = (int)GateMsgOpCode.ClientCount; gateHeader.SessionID = count; //随机选一台就行 int backEndType = GateServerUtils.GetServerConfig().backEndServerType; uint[] serverIDs = m_app.GetCluster().GetRemoteAppID(backEndType); int index = (int)(m_backendserverseq % serverIDs.Length); m_backendserverseq++; NotifyServer(serverIDs[index], gateHeader); } } }