// 调取最近录像列表 void OnGetReplayList(Session s, IReadableBuffer data, IWriteableBuffer buff) { var maxNum = data.ReadInt(); var arr = Room4Server.AllReplayTitles; var lst = new List <string>(); for (var i = 0; lst.Count < maxNum && i < arr.Length; i++) { var n = arr.Length - 1 - i; var t = arr[n]; var r = Room4Server.GetReplay(t); if (t.IndexOf(".crash.") < 0 && r.Length < 0 /* 两分钟 */) { continue; } lst.Add(t); } buff.Write(lst.Count); foreach (var rid in lst) { var r = Room4Server.GetReplay(rid); r.SerializeHeader(buff); } }
// 处理网络消息的映射 public override void OnMessage(Connection conn, IReadableBuffer data) { string op = data.ReadString(); if (noResponseMap.ContainsKey(op)) { noResponseMap[op](conn, data); } else if (responseMap.ContainsKey(op)) { Responser r = CreateResponser(conn); IWriteableBuffer buff = r.BeginResponse(); responseMap[op](conn, data, buff, () => { r.End(buff); }); } else if (responseMapImmediately.ContainsKey(op)) { Responser r = CreateResponser(conn); IWriteableBuffer buff = r.BeginResponse(); responseMapImmediately[op](conn, data, buff); r.End(buff); } else { conn.Close("unhandled message '" + op + "' in " + Name); throw new Exception("unhandled message '" + op + "' in " + Name); } }
// 客户端请求问卷 public void OnGetQuestionnaire(Session s, IReadableBuffer data, IWriteableBuffer buff, Action end) { var QuestionnaireName = data.ReadString(); QRC.Retrieve(QuestionnaireName + s.Usr.ID, (questionnaire) => { var isNew = questionnaire == null; buff.Write(isNew); if (isNew) { var q = GetQuestionnaire(QuestionnaireName); var isExists = q != null; buff.Write(isExists); if (isExists) { q.Serialize(buff); } } end(); }); }
// 调取自己最近录像 void OnGetMyReplayList(Session s, IReadableBuffer data, IWriteableBuffer buff) { var maxNum = data.ReadInt(); var usrInfo = s.Usr.Info; var lst = new List <string>(); var arr = usrInfo.MyReplays.ToArray(); for (var i = 0; lst.Count < maxNum && i < arr.Length; i++) { var r = arr[arr.Length - i - 1]; if (Room4Server.AllReplayTitles.FirstIndexOf(r) >= 0) { lst.Add(r); } else { usrInfo.MyReplays.Remove(r); } } buff.Write(lst.Count); foreach (var rid in lst) { var r = Room4Server.GetReplay(rid); r.SerializeHeader(buff); } }
public void Serialize(IWriteableBuffer buff) { SerializeHeader(buff); string lastMsgTitle = null; byte[] lastMsgData = null; var cnt = Msgs.Count; buff.Write(cnt); foreach (var msg in Msgs) { var title = msg.Key; var body = msg.Value; var len = body.Available; var msgData = len > 0 ? body.PeekBytes(len) : EmptyBytesArray; if (title == lastMsgTitle && AllSame(lastMsgData, msgData)) { buff.Write(true); } else { buff.Write(false); buff.Write(title); buff.Write(len); buff.Write(msgData); lastMsgTitle = msg.Key; lastMsgData = msgData; } } }
public static void Write(this IWriteableBuffer w, ISerializable v) { w.Write(v != null ? true : false); if (v != null) { v.Serialize(w); } }
// 序列化 public void Serialize(IWriteableBuffer w) { isWrite = true; this.w = w; MakeSureNotSyncing(); Sync(); MakeSureNotSyncing(); this.w = null; }
// 服务器寻路请求 void OnSrvFindPath(IReadableBuffer data, IWriteableBuffer buff) { var src = data.ReadVec2(); var dst = data.ReadVec2(); var size = data.ReadFix64(); Vec2[] path = PathFinder(src, dst, size); buff.Write(path); }
// 调取录像 void OnGetReplay(Session s, IReadableBuffer data, IWriteableBuffer buff) { var replayName = data.ReadString(); var r = Room4Server.GetReplay(replayName); buff.Write(r != null); if (r != null) { r.Serialize(buff); } }
void OnGetUnitCfgs(Connection conn, IReadableBuffer data, IWriteableBuffer buff, Action end) { buff.Write(UnitConfiguration.AllUnitTypes); for (int i = 0; i < UnitConfiguration.AllUnitTypes.Length; i++) { UnitUtils.WriteUnitInfo(UnitConfiguration.GetDefaultConfig(UnitConfiguration.AllUnitTypes[i]), buff); } end(); }
// 序列化一个录像的基本信息 public void SerializeHeader(IWriteableBuffer buff) { buff.Write(ID); buff.Write(Usr1); buff.Write(Usr2); buff.Write(UsrName1); buff.Write(UsrName2); buff.Write(Length); buff.Write(Date.Ticks); buff.Write(IsPVP); buff.Write(IsCrashReplay); }
public static void Write(this IWriteableBuffer w, ISerializable[] arr) { w.Write(arr != null ? true : false); if (arr != null) { w.Write(arr.Length); for (int i = 0; i < arr.Length; i++) { Write(w, arr[i]); } } }
public static void Write(this IWriteableBuffer buff, Vec2[] arr) { if (arr == null) { buff.Write(-1); } else { buff.Write(arr.Length); foreach (var v in arr) { buff.Write(v); } } }
// 发送消息给指定连接列表 public static void Broadcast(string op, string toComponent, Action <IWriteableBuffer> fun, Connection c) { if (c == null) { return; } IWriteableBuffer buff = c.BeginSend(toComponent); buff.Write(op); if (fun != null) { fun(buff); } c.End(buff); }
protected dpBase(IWriteableBuffer w, dpBase parent, params object[] ops) { this.w = w; if (parent != null) { Ops.AddRange(parent.Ops); } if (ops != null) { for (int i = 0; i < ops.Length; i++) { Ops.Add(ops[i]); } } }
// 发送请求给指定连接列表 public static void Request(string op, string toComponent, Action <IWriteableBuffer> fun, Action <IReadableBuffer> callback, Action <bool> onExpired, Connection c) { if (c == null || !c.IsConnected) { if (onExpired != null) { onExpired(false); } return; } IWriteableBuffer buff = c.BeginRequest(toComponent, callback, onExpired); buff.Write(op); if (fun != null) { fun(buff); } c.End(buff); }
// 序列化问卷信息 public void Serialize(IWriteableBuffer buff) { buff.Write(Info.Id); for (int i = 0; i < Info.Questions.KeyArray.Length; i++) { buff.Write(Info.Questions.KeyArray[i]); var acnt = Info.Questions[Info.Questions.KeyArray[i]].Count; buff.Write(acnt); if (acnt != 0) { for (int j = 0; j < acnt; j++) { buff.Write(Info.Questions[Info.Questions.KeyArray[i]][j]); } } } }
// 发送消息给指定连接列表 public static void Broadcast(string op, string toComponent, Action <IWriteableBuffer> fun, Connection[] toConnection, Connection excludeConn) { WriteBuffer tmpBuff = new WriteBuffer(true); if (fun != null) { fun(tmpBuff); } foreach (Connection c in toConnection) { if (c == null || c == excludeConn) { continue; } IWriteableBuffer buff = c.BeginSend(toComponent); buff.Write(op); buff.Write(tmpBuff.Data, 0, tmpBuff.Available); c.End(buff); } }
// 消息请求结束或发送 public void End(NetConnection nc, IWriteableBuffer buff) { WriteBuffer wb = (buff as WriteBuffer); // 如果没有被使用说明这个buff已经被还回 // 说明有什么地方调用了多次end if (!wb.IsUsing) { throw new Exception("call end more than once"); } int len = buff.Available - sizeof(int); wb.Unreserve(0, BitConverter.GetBytes(IPAddress.HostToNetworkOrder(len))); // if it's a request long no = -wb.PeekLong(sizeof(int)); CallbackNode cbn; if (callbacks.TryGetValue(no, out cbn)) { cbn.nc = nc; } // 长度信息不加密,余下加密 for (int i = sizeof(int); i < wb.Available; i++) { wb.Data[i] = nc.Encrypt(wb.Data[i]); } nc.SendData(wb.Data, 0, wb.Available); WriteBufferPool.Enqueue(wb); wb.IsUsing = false; if (OnSendMsg != null) { OnSendMsg(len); } }
// 从匹配列表移除 void OnCancel(Session s, IReadableBuffer data, IWriteableBuffer buff) { bool isSuccess = waitingList.Remove(s.ID); buff.Write(isSuccess); }
// 用户登录请求 void OnUserLoginMsg(Connection conn, IReadableBuffer data, IWriteableBuffer buff, Action end) { var uid = data.ReadString(); var deviceModel = ""; // 检查版本 var isNewVersion = false; var version = ""; var buildNo = ""; var platform = ""; if (data.Available != 0) { version = data.ReadString(); platform = data.ReadString(); } if (data.Available != 0) { deviceModel = data.ReadString(); buildNo = data.ReadString(); } isNewVersion = (version == SrvVersion) && (buildNo == SrvBuildNo); buff.Write(isNewVersion); if (!isNewVersion) { if (platform == "IOS") { buff.Write("https://www.apple.com"); } else if (platform == "ANDROID") { buff.Write("https://www.google.com"); } else { buff.Write("https://www.baidu.com"); } end(); return; } UC.Retrieve(uid, (usr) => { if (SC[uid] != null) { KickOut(uid); end(); return; } var isNew = usr == null; if (isNew) // 用户不存在就创建新的 { usr = new User(); usr.ID = uid; usr.Info = new UserInfo(); usr.Info.DeviceModel = deviceModel; UC.AddNew(usr); UserManager.SetDefaultInfo(usr); } // 创建会话 var s = new Session(); s.Usr = usr; s.Conn = conn; SC[uid] = s; // 登录日志 SrvLogger.Log(new LoginInfo(uid, usr.Info.Name, conn.GetIP(), isNew)); BeforeUserLogin.SC(s, isNew); // 通知登录成功 buff.Write(true); buff.Write(usr.Info); // 问卷调查 var totalCount = usr.Info.WinCount + usr.Info.LoseCount; if (totalCount >= 1 && totalCount < 5) { buff.Write("1"); end(); OnUserLogin.SC(s, isNew); } else if (totalCount >= 5) { QRC.Retrieve("1" + s.Usr.ID, (questionnaire) => { if (questionnaire == null) { buff.Write("1"); end(); OnUserLogin.SC(s, isNew); } else { buff.Write("2"); end(); OnUserLogin.SC(s, isNew); } }); } else { buff.Write("0"); end(); OnUserLogin.SC(s, isNew); } }); }
public static void WriteUnitInfo(UnitConfigInfo info, IWriteableBuffer writer) { writer.Write(info.DisplayName); writer.Write(info.Cost); writer.Write(info.GasCost); writer.Write(info.ConstructingTime); writer.Write(info.MaxNum); writer.Write(info.MaxVelocity); writer.Write(info.IsBuilding); writer.Write(info.IsBiological); writer.Write(info.IsMechanical); writer.Write(info.IsAirUnit); writer.Write(info.UnAttackable); writer.Write(info.VisionRadius); writer.Write(info.ChaseRadius); writer.Write(info.Suppliment); writer.Write(info.AITypes); bool hasAIParams = info.AIParams != null; writer.Write(hasAIParams); if (hasAIParams) { var len2 = info.AIParams.Length; writer.Write(len2); for (var i = 0; i < len2; i++) { writer.Write(info.AIParams [i] != null); if (info.AIParams[i] != null) { var len = info.AIParams[i].Length; writer.Write(len); for (var j = 0; j < len; j++) { writer.Write(info.AIParams[i][j]); } } } } writer.Write(info.SizeRadius); writer.Write(info.NoBody); writer.Write(info.NoCard); writer.Write(info.MaxHp); writer.Write(info.CanAttackGround); writer.Write(info.CanAttackAir); writer.Write(info.AttackType); writer.Write(info.AttackRange); writer.Write(info.AttackPower); var hasAttackInterval = info.AttackInterval != null; writer.Write(hasAttackInterval); if (hasAttackInterval) { var len6 = info.AttackInterval.Length; writer.Write(len6); for (var i = 0; i < len6; i++) { writer.Write(info.AttackInterval[i]); } } writer.Write(info.AOEType); bool hasAOEParams = info.AOEParams != null; writer.Write(hasAOEParams); if (hasAOEParams) { var len2 = info.AOEParams.Length; writer.Write(len2); for (var i = 0; i < len2; i++) { writer.Write(info.AOEParams [i] != null); if (info.AOEParams[i] != null) { var len = info.AOEParams[i].Length; writer.Write(len); for (var j = 0; j < len; j++) { writer.Write(info.AOEParams[i][j]); } } } } writer.Write(info.ArmorType); writer.Write(info.Defence); var hasPrerequisites = info.Prerequisites != null; writer.Write(hasPrerequisites); if (hasPrerequisites) { var len9 = info.Prerequisites.Length; writer.Write(len9); for (var i = 0; i < len9; i++) { writer.Write(info.Prerequisites [i] != null); if (info.Prerequisites [i] != null) { var len = info.Prerequisites [i].Length; writer.Write(len); for (int j = 0; j < len; j++) { writer.Write(info.Prerequisites[i][j]); } } } } writer.Write(info.ReconstructTo); writer.Write(info.ReconstructFrom); writer.Write(info.TechLevel); writer.Write(info.Desc); writer.Write(info.InVisible); writer.Write(info.IsObserver); writer.Write(info.ReboundDamage); writer.Write(info.Pets); writer.Write(info.OriginalType); writer.Write(info.IsThirdType); }
public static void Write(this IWriteableBuffer buff, Vec2 v2) { buff.Write(v2.x.RawValue); buff.Write(v2.y.RawValue); }
public void End(IWriteableBuffer buff) { h.End(nc, buff); }
static void SWrite(IWriteableBuffer w, Type t, object o) { if (dw == null) { dw = new Dictionary <Type, Action <IWriteableBuffer, object> >(); dw[typeof(string)] = (_w, _o) => { _w.Write((string)_o); }; dw[typeof(string[])] = (_w, _o) => { _w.Write((string[])_o); }; dw[typeof(ISerializable)] = (_w, _o) => { _w.Write((ISerializable)_o); }; dw[typeof(ISerializable[])] = (_w, _o) => { _w.Write((ISerializable[])_o); }; dw[typeof(char)] = (_w, _o) => { _w.Write((char)_o); }; dw[typeof(char[])] = (_w, _o) => { _w.Write((char[])_o); }; dw[typeof(byte)] = (_w, _o) => { _w.Write((byte)_o); }; dw[typeof(byte[])] = (_w, _o) => { _w.Write((byte[])_o); }; dw[typeof(bool)] = (_w, _o) => { _w.Write((bool)_o); }; dw[typeof(bool[])] = (_w, _o) => { _w.Write((bool[])_o); }; dw[typeof(short)] = (_w, _o) => { _w.Write((short)_o); }; dw[typeof(short[])] = (_w, _o) => { _w.Write((short[])_o); }; dw[typeof(int)] = (_w, _o) => { _w.Write((int)_o); }; dw[typeof(int[])] = (_w, _o) => { _w.Write((int[])_o); }; dw[typeof(long)] = (_w, _o) => { _w.Write((long)_o); }; dw[typeof(long[])] = (_w, _o) => { _w.Write((long[])_o); }; dw[typeof(ulong)] = (_w, _o) => { _w.Write((ulong)_o); }; dw[typeof(ulong[])] = (_w, _o) => { _w.Write((ulong[])_o); }; dw[typeof(float)] = (_w, _o) => { _w.Write((float)_o); }; dw[typeof(float[])] = (_w, _o) => { _w.Write((float[])_o); }; dw[typeof(double)] = (_w, _o) => { _w.Write((double)_o); }; dw[typeof(double[])] = (_w, _o) => { _w.Write((double[])_o); }; } if (t.IsEnum) { dw[typeof(int)](w, o); } else if (o is ISerializable) { dw[typeof(ISerializable)](w, o); } else if (o is ISerializable[]) // OK? { dw[typeof(ISerializable[])](w, o); } else if (dw.ContainsKey(t)) { dw[t](w, o); } else { throw new Exception("dpBase unknown type " + t.Name); } }
public static void Write(this IWriteableBuffer buff, Fix64 v) { buff.Write(v.RawValue); }
public void End(IWriteableBuffer buff) { conn.End(buff); }
// 消息请求结束或发送 public void End(NetConnection nc, IWriteableBuffer buff) { WriteBuffer wb = (buff as WriteBuffer); // 如果没有被使用说明这个buff已经被还回 // 说明有什么地方调用了多次end if (!wb.IsUsing) { throw new Exception("call end more than once"); } // 检查是否和上一条消息完全一致 var identifyWithLastOne = false; var rawData = wb.Data; var rawDataLen = wb.Available; if (nc.LastSent != null && nc.LastSent.Available == rawDataLen) { identifyWithLastOne = true; for (var i = 0; i < rawDataLen; i++) { if (nc.LastSent.Data[i] != rawData[i]) { identifyWithLastOne = false; break; } } } if (identifyWithLastOne) { nc.SendData(TrueBytes, 0, TrueBytes.Length); } else { wb.Unreserve(0, FalseByte); int len = buff.Available - sizeof(bool) - sizeof(int); wb.Unreserve(sizeof(bool), BitConverter.GetBytes(IPAddress.HostToNetworkOrder(len))); // if it's a request long no = -wb.PeekLong(FalseByte.Length + sizeof(int)); CallbackNode cbn; if (callbacks.TryGetValue(no, out cbn)) { cbn.nc = nc; } // 长度信息不加密,余下加密 for (int i = sizeof(int); i < wb.Available; i++) { wb.Data[i] = nc.Encrypt(wb.Data[i]); } nc.SendData(wb.Data, 0, wb.Available); Dealloc(nc.LastSent); nc.LastSent = wb; } wb.IsUsing = false; }
public void Serialize(IWriteableBuffer w) { }
public override void Serialize(IWriteableBuffer buff) { buff.Write(UniqueID); buff.Write(TID); base.Serialize(buff); }