/// <summary> /// 向每个场景服务器发送ping /// </summary> public ErrorCode PingSS(long utcTime) { if (this._GSSSInfoMap.Count == 0) { return(ErrorCode.Success); } foreach (KeyValuePair <int, GSSSInfo> kv in this._GSSSInfoMap) { GSSSInfo ssInfo = kv.Value; if (0 == ssInfo.ssID || 0 == ssInfo.nsID) { continue; } if (utcTime - ssInfo.pingTickCounter < Consts.DEFAULT_PING_CD_TICK) { continue; } GSToSS.AskPing sMsg = new GSToSS.AskPing { Time = utcTime }; byte[] data = sMsg.ToByteArray(); GS.instance.TranMsgToSession(ssInfo.nsID, data, 0, data.Length, ( int )GSToSS.MsgID.EMsgToSsfromGsAskPing, 0, 0); ssInfo.pingTickCounter = utcTime; } return(ErrorCode.Success); }
private ErrorCode MsgInitHandler(byte[] data, int offset, int size, int msgID) { GSSSInfo ssInfo = GS.instance.gsStorage.GetSSInfo(this.logicID); if (ssInfo == null || data == null) { Logger.Error(string.Empty); return(ErrorCode.InvaildLogicID); } offset += 2 * sizeof(int); size -= 2 * sizeof(int); SSToGS.AskRegisteRet askRegisteRet = new SSToGS.AskRegisteRet(); askRegisteRet.MergeFrom(data, offset, size); if (( int )ErrorCode.Success != askRegisteRet.State) { Logger.Warn($"register to SS {ssInfo.ssID} Fail with error code {askRegisteRet.State}."); return(( ErrorCode )askRegisteRet.State); } ssInfo.ssNetState = ServerNetState.Free; Logger.Info($"register to SS {ssInfo.ssID} success at session {ssInfo.nsID}."); this.SetInited(true, true); return(ErrorCode.Success); }
private ErrorCode MsgInitHandler(byte[] data, int offset, int size, int transID, int msgID, uint gcNetID) { CSToGS.AskRegisteRet askRegisteRet = new CSToGS.AskRegisteRet(); askRegisteRet.MergeFrom(data, offset, size); if (( int )ErrorCode.Success != askRegisteRet.Registe) { Logger.Warn($"CS Register Error(ret={askRegisteRet.Registe})!"); return(( ErrorCode )askRegisteRet.Registe); } long csMilsec = askRegisteRet.Curtime; long selfMilsec = TimeUtils.utcTime; GS.instance.gsStorage.csTimeError = csMilsec - selfMilsec; GS.instance.gsStorage.ssBaseIdx = askRegisteRet.Ssbaseid; int ssinfoCount = askRegisteRet.Ssinfo.Count; if (0 < ssinfoCount) { GS.instance.gsStorage.ssConnectNum = 0; for (int i = 0; i < ssinfoCount; i++) { if (0 == askRegisteRet.Ssinfo[i].Ssid) { continue; } if (GS.instance.gsStorage.ContainsSSInfo(askRegisteRet.Ssinfo[i].Ssid)) { continue; } GSSSInfo ssInfo = new GSSSInfo(); ssInfo.ssID = askRegisteRet.Ssinfo[i].Ssid; ssInfo.listenIp = askRegisteRet.Ssinfo[i].Ip.Replace("\0", string.Empty); ssInfo.listenPort = askRegisteRet.Ssinfo[i].Port; ssInfo.ssNetState = ( ServerNetState )askRegisteRet.Ssinfo[i].Netstate; GS.instance.gsStorage.AddSSInfo(ssInfo.ssID, ssInfo); if (ssInfo.ssNetState == ServerNetState.Closed) { continue; } this.owner.CreateConnector(SessionType.ClientG2S, ssInfo.listenIp, ssInfo.listenPort, Consts.SOCKET_TYPE, Consts.PROTOCOL_TYPE, 10240, ssInfo.ssID); ++GS.instance.gsStorage.ssConnectNum; } } this.SetInited(true, true); return(ErrorCode.Success); }
protected override void OnClose() { GSSSInfo ssInfo = GS.instance.gsStorage.GetSSInfo(this.logicID); if (ssInfo == null) { Logger.Error(string.Empty); return; } Logger.Info($"SS({ssInfo.ssID}) DisConnect."); GS.instance.gsStorage.OnSSClosed(ssInfo); ssInfo.nsID = 0; }
protected override void OnRealEstablish() { GSSSInfo ssInfo = GS.instance.gsStorage.GetSSInfo(this.logicID); if (ssInfo == null) { Logger.Error(string.Empty); return; } Logger.Info($"SS({ssInfo.ssID}) Connected and register ok."); ssInfo.nsID = this.id; ssInfo.lastConnMilsec = TimeUtils.utcTime; ssInfo.pingTickCounter = 0; }
/// <summary> /// 处理场景服务器返回的ping消息 /// </summary> private ErrorCode OnMsgFromSSAskPingRet(byte[] data, int offset, int size, int msgID) { GSSSInfo ssInfo = GS.instance.gsStorage.GetSSInfo(this.logicID); if (ssInfo == null) { return(ErrorCode.SSNotFound); } SSToGS.AskPingRet pPingRet = new SSToGS.AskPingRet(); pPingRet.MergeFrom(data, offset, size); long curMilsec = TimeUtils.utcTime; long tickSpan = curMilsec - pPingRet.Time; Logger.Info($"Ping SS {ssInfo.ssID} returned, Tick span {tickSpan}."); return(ErrorCode.Success); }
private ErrorCode OnTransToSS(byte[] data, int offset, int size, int msgID) { GSSSInfo ssInfo = GS.instance.gsStorage.GetUserSSInfo(this.id); if (ssInfo == null) { Logger.Error($"nsid:{this.id} send msg:{msgID} error, can't get ssinfo."); return(ErrorCode.SSNotFound); } if (ssInfo.nsID > 0) { GS.instance.TranMsgToSession(ssInfo.nsID, data, offset, size, msgID, ( int )GSToSS.MsgID.EMsgToSsfromGsReportGcmsg, this.id); return(ErrorCode.Success); } Logger.Error($"invalid ssID:{ssInfo.nsID}!!"); return(ErrorCode.InvalidSSID); }
protected override void SendInitData() { GSSSInfo ssInfo = GS.instance.gsStorage.GetSSInfo(this.logicID); if (ssInfo == null) { Logger.Error(string.Empty); return; } Logger.Info($"SS({ssInfo.ssID}) Connected, try to register me."); ssInfo.nsID = this.id; GSToSS.AskRegiste askRegiste = new GSToSS.AskRegiste() { Gsid = GS.instance.gsConfig.n32GSID, Pwd = GS.instance.gsConfig.aszMyUserPwd }; byte[] data = askRegiste.ToByteArray(); this.owner.TranMsgToSession(ssInfo.nsID, data, 0, data.Length, ( int )GSToSS.MsgID.EMsgToSsfromGsAskRegiste, 0, 0); }
public void OnSSClosed(GSSSInfo ssInfo) { foreach (KeyValuePair <uint, GSSSInfo> kv in this._user2SSInfoMap) { if (ssInfo != kv.Value) { continue; } GS.instance.PostGameClientDisconnect(kv.Key); this._userToDelete.Add(kv.Key); } int count = this._userToDelete.Count; if (count <= 0) { return; } for (int i = 0; i < count; i++) { this._user2SSInfoMap.Remove(this._userToDelete[i]); } this._userToDelete.Clear(); }
/// <summary> /// 中心服务器通知场景服务器内的客户端信息 /// </summary> private ErrorCode OnMsgFromCSUserConnectedToSS(byte[] data, int offset, int size, int transID, int msgID, uint gcNetID) { CSToGS.UserConnectedSS userConnectedSS = new CSToGS.UserConnectedSS(); userConnectedSS.MergeFrom(data, offset, size); GSSSInfo ssInfo = GS.instance.gsStorage.GetSSInfo(userConnectedSS.Ssid); if (null == ssInfo) { Logger.Error($"ssInfo is null with ssid({userConnectedSS.Ssid})"); return(ErrorCode.SSNotFound); } //客户端id和场景服务器信息建立映射关系 int count = userConnectedSS.Gcnid.Count; for (int i = 0; i < count; ++i) { uint gc = ( uint )userConnectedSS.Gcnid[i]; GS.instance.gsStorage.AddUserSSInfo(gc, ssInfo); Logger.Log($"user netID({gc}) connect with SS({ssInfo.ssID})"); } return(ErrorCode.Success); }
private ErrorCode MsgOneSSConnectedHandler(byte[] data, int offset, int size, int msgID) { offset += 2 * sizeof(int); size -= 2 * sizeof(int); CSToGS.OneSSConnected oneSsConnected = new CSToGS.OneSSConnected(); oneSsConnected.MergeFrom(data, offset, size); GSSSInfo pcSSInfo = GS.instance.gsStorage.GetSSInfo(oneSsConnected.Ssid); if (pcSSInfo != null) { pcSSInfo.listenIp = oneSsConnected.Ip.Replace("\0", string.Empty); pcSSInfo.listenPort = oneSsConnected.Port; if (pcSSInfo.ssNetState == ServerNetState.Closed) { pcSSInfo.ssNetState = ( ServerNetState )oneSsConnected.Netstate; pcSSInfo.nsID = 0; this.owner.CreateConnector(SessionType.ClientG2S, pcSSInfo.listenIp, pcSSInfo.listenPort, Consts.SOCKET_TYPE, Consts.PROTOCOL_TYPE, 10240, oneSsConnected.Ssid); } } return(ErrorCode.Success); }
private ErrorCode OnMsgToGsfromSsOrderPostToGc(byte[] data, int offset, int size, int transID, int msgID, uint gcNetID) { if (gcNetID == 0) { GS.instance.BroadcastToGameClient(data, offset, size, msgID); } else { if (msgID == ( int )GSToGC.MsgID.EMsgToGcfromGsNotifyBattleBaseInfo) { GSSSInfo ssInfo = GS.instance.gsStorage.GetSSInfo(this.logicID); if (ssInfo == null) { return(ErrorCode.SSNotFound); } //该消息的路由:ss-cs-gs-gc //该消息从ss发出,目标端是网络id为gcNetID的客户端,消息体是场景信息 //当该消息流经gs时建立该客户端和场景信息的映射关系 GS.instance.gsStorage.AddUserSSInfo(gcNetID, ssInfo); } GS.instance.PostToGameClient(gcNetID, data, offset, size, msgID); } return(ErrorCode.Success); }
/// <summary> /// 添加场景服务器信息 /// </summary> public void AddUserSSInfo(uint gcNetID, GSSSInfo gsssInfo) => this._user2SSInfoMap[gcNetID] = gsssInfo;
/// <summary> /// 添加场景服务器信息 /// </summary> public void AddSSInfo(int ssID, GSSSInfo gsssInfo) => this._GSSSInfoMap[ssID] = gsssInfo;