private List <NetPack> DecodeBuffer(byte[] buffer, int bufSize) { List <NetPack> packs = new List <NetPack>(); try { int offset = 0; int restCount = bufSize;// 剩余缓存大小 while (restCount > 0) { if (curPack == null) { curPack = new NetPack(); } if (curPack.FillPack(buffer, ref offset, ref restCount)) { CBuffer cBuffer = curPack.Assembly(); packs.Add(curPack); curPack = null; } } } catch (System.Exception ex) { Debug.LogErrorFormat("Decode pack error. {1}", ex.ToString()); } return(packs); }
public void Send(NetPack pack) { if (tcpsocket != null) { tcpsocket.SendPack(pack); } }
// [ protocol : 4 // k : 4 // len : 4 // index : 4 // len : 4 // opcode : 4 // value : // ... // index : 4 private void ExecFrame() { int k; NetFrame frame; if (_queue.Dequeue(out k, out frame) == 1) { try { int offset = 0; int index = 0, len = 0; offset = NetUnpack.Unpackli(frame.buffer, offset, out index); offset = NetUnpack.Unpackli(frame.buffer, offset, out len); OpCodeParse(index, frame.buffer, offset, len); } catch (Exception ex) { k = _queue.BackK(); int len = 8; byte[] msg = new byte[len]; int offset = 0; offset = NetPack.Packli(msg, offset, (int)ProtocolType.PT_FETCHK); offset = NetPack.Packli(msg, offset, k); _ctx.SendUdp(msg, 0, len); } } else { int len = 8; byte[] msg = new byte[len]; int offset = 0; offset = NetPack.Packli(msg, offset, (int)ProtocolType.PT_FETCHK); offset = NetPack.Packli(msg, offset, k); _ctx.SendUdp(msg, 0, len); } }
public static void Send(this Chunk chunk, NetContext context) { NetPack netPack = context.GetNetBlock((int)NetMessageType.Chunk, Chunk.c_minimumSize); chunk.ToNetBlock(netPack); context.Send(netPack); }
public byte[] PackPos() { byte[] res = new byte[12]; NetPack.Packlf(res, 0, _pos.x); NetPack.Packlf(res, 4, _pos.y); NetPack.Packlf(res, 8, _pos.z); return(res); }
public static void DelaySend(this Chunk chunk, User user) { if (user.netPacks.Count < 8192) { NetPack netPack = user.netContext.GetNetBlock((int)NetMessageType.Chunk, Chunk.c_minimumSize); chunk.ToNetBlock(netPack); user.netPacks.Enqueue(netPack); } }
public byte[] PackDir() { Vector3 direction = _dir; byte[] res = new byte[12]; NetPack.Packlf(res, 0, direction.x); NetPack.Packlf(res, 4, direction.y); NetPack.Packlf(res, 8, direction.z); return(res); }
public static Chunk GetChunk(this NetPack pack) { Chunk chunk = new Chunk(); chunk.position = VectorExt.Int3FromBytes(new Span <byte>(pack.data, 0, 12)); chunk.chunkFlags = (ChunkFlag)BitConverter.ToInt32(pack.data, 12); chunk.stamp = BitConverter.ToInt64(pack.data, 16); new Span <byte>(pack.data, 64, 16384).CopyTo(MemoryMarshal.Cast <int, byte>(new Span <int>(chunk.data))); return(chunk); }
public static void ToNetBlock(this Chunk chunk, NetPack netPack) { Debug.Assert(netPack.data.Length >= Chunk.c_minimumSize); netPack.length = Chunk.c_minimumSize; chunk.position.ToBytes(new Span <byte>(netPack.data, 0, 12)); MemoryMarshal.Cast <byte, int>(new Span <byte>(netPack.data, 12, 4))[0] = (int)chunk.chunkFlags; MemoryMarshal.Cast <byte, long>(new Span <byte>(netPack.data, 16, 8))[0] = chunk.stamp; Span <byte> span1 = MemoryMarshal.Cast <int, byte>(new Span <int>(chunk.data)); span1.TryCopyTo(new Span <byte>(netPack.data, 64, 16384)); }
public void OnS2CNotify(NetPack pack) { //Debug.LogFormat("提示通知/对白框相关系统消息协议NotifyMsgID,CMD={0}, ", pack.CMD); switch ((NotifyMsgID)pack.CMD) { case NotifyMsgID.S2CNotifyNarmal: PB_NotifyNarmal mNotifyNarmal = PB_NotifyNarmal.Parser.ParseFrom(pack.BodyBuffer.Bytes); Debug.LogFormat("OnS2CNotify,普通悬浮提示:mNotifyNarmal={0}", mNotifyNarmal.ToString()); NotifyManager mNotifyManager = FindObjectOfType <NotifyManager>(); mNotifyManager.AddNotify(mNotifyNarmal.Msg); break; } }
public GameController(Context ctx) : base(ctx) { _ui = new UIRootActor(_ctx, this); // 4, protocol _syncmsg1 = new byte[4]; NetPack.Packli(_syncmsg1, 0, 1); //EventListenerCmd listener1 = new EventListenerCmd(MyEventCmd.EVENT_SETUP_SCENE, SetupScene); //_ctx.EventDispatcher.AddCmdEventListener(listener1); //EventListenerCmd listener2 = new EventListenerCmd(MyEventCmd.EVENT_SETUP_MAP, SetupMap); //_ctx.EventDispatcher.AddCmdEventListener(listener2); //EventListenerCmd listener3 = new EventListenerCmd(MyEventCmd.EVENT_SETUP_VIEW, SetupCamera); //_ctx.EventDispatcher.AddCmdEventListener(listener3); }
public bool HandleMsg(NetPack pack) { try { Handle handler = null; MsgID msgID = (MsgID)pack.HeadID; //Debug.LogFormat("msg.pack.id = {0}", pack.MessageID); if (msgHandle.TryGetValue(msgID, out handler)) { if (handler != null) { handler(pack); pack.BodyBuffer.Dispose(); pack.HeaderBuffer.Dispose(); return(true); } else { Debug.LogWarningFormat("Error -- find message handler = null with MessageID: {0}", msgID); } } else { if (msgID != MsgID.HeartbeatId) { //TODO:主要是因为在PVP时,有可能出错一方发广播消息,另一方还没有注册此消息,引发错误 //后面改回来,规范流程 Debug.LogErrorFormat("Error -- Can't find message handler with MessageID: {0}", msgID); } } } catch (System.Exception ex) { Debug.LogException(ex); } return(false); }
public GameController(Context ctx) : base(ctx) { _systems = new Entitas.Systems(); _indexsystem = new IndexSystem(Contexts.sharedInstance.game); _mapsystem = new MapSystem(Contexts.sharedInstance.game); _joinsystem = new JoinSystem(Contexts.sharedInstance.game); _myplayersystem = new MyPlayerSystem(Contexts.sharedInstance.game); _systems.Add(_indexsystem) .Add(_mapsystem) .Add(_joinsystem) .Add(_myplayersystem); // 4, protocol _syncmsg1 = new byte[4]; NetPack.Packli(_syncmsg1, 0, 1); EventListenerCmd listener2 = new EventListenerCmd(MyEventCmd.EVENT_SETUP_MAP, SetupMap); _ctx.EventDispatcher.AddCmdEventListener(listener2); //EventListenerCmd listener3 = new EventListenerCmd(MyEventCmd.EVENT_SETUP_VIEW, SetupCamera); //_ctx.EventDispatcher.AddCmdEventListener(listener3); }
public void OnS2CMatch(NetPack pack) { Debug.LogFormat("匹配系统MatchMsgID,CMD={0}, ", pack.CMD); //MatchManager mMatchManager = GameObject.Find("MatchView").GetComponent<MatchManager>(); MatchManager mMatchManager = FindObjectOfType <MatchManager>(); switch ((MatchMsgID)pack.CMD) { case MatchMsgID.Fms2Gs2CMatchAdd: PB_MatchTeamAdd_FMS2GS2C mMatchTeamAdd = PB_MatchTeamAdd_FMS2GS2C.Parser.ParseFrom(pack.BodyBuffer.Bytes); if (mMatchTeamAdd.Ret) { NetData.Instance.Set(MsgID.S2CMatch, (uint)MatchMsgID.Fms2Gs2CMatchAdd, mMatchTeamAdd); } mMatchManager.ShowMatching(mMatchTeamAdd.Ret); Debug.LogFormat("OnS2CMatch,请求加入匹配队列结果:mMatchTeamAdd={0}", mMatchTeamAdd.ToString()); break; case MatchMsgID.Fms2Gs2CMatchDel: PB_MatchTeamDel_FMS2GS2C mMatchTeamDel = PB_MatchTeamDel_FMS2GS2C.Parser.ParseFrom(pack.BodyBuffer.Bytes); Debug.LogFormat("OnS2CMatch,请求取消匹配队列结果:mMatchTeamDel={0}", mMatchTeamDel.ToString()); if (mMatchTeamDel.Ret) { mMatchManager.ShowMatching(false); } break; case MatchMsgID.Fms2Gs2CMatchStart: PB_MatchTeamReady_FMS2GS2C mMatchTeamReady = PB_MatchTeamReady_FMS2GS2C.Parser.ParseFrom(pack.BodyBuffer.Bytes); mMatchManager.ShowMatched(true, mMatchTeamReady.Lasttime); foreach (PB_MatchTeamMemberInfo OnePlayersinfo in mMatchTeamReady.Playersinfo) { if (OnePlayersinfo.Pid != NetData.Instance.PlayerID) { continue; } if (OnePlayersinfo.Ready) { mMatchManager.SureFightBtn.interactable = false; mMatchManager.sSureFightBtnText.text = "已准备"; } } Debug.LogFormat("OnS2CMatch,推送匹配成功,及相关信息(多次推送当作刷新信息):mMatchTeamReady={0}", mMatchTeamReady.ToString()); break; case MatchMsgID.Fms2Gs2CMatchFail: PB_MatchTeamFail_FMS2GS2C mMatchTeamFail = PB_MatchTeamFail_FMS2GS2C.Parser.ParseFrom(pack.BodyBuffer.Bytes); Debug.LogFormat("OnS2CMatch,推送战斗准备失败(重新进入匹配队列):mMatchTeamFail={0}", mMatchTeamFail.ToString()); if (mMatchTeamFail.Notmatchpidlist.Contains(NetData.Instance.PlayerID)) { mMatchManager.ShowMatched(false, 0); mMatchManager.ShowMatching(false); } else { mMatchManager.ShowMatched(false, 0); mMatchManager.ShowMatching(true); } break; case MatchMsgID.Fms2Gs2CMatchFight: PB_MatchTeamFight_FMS2GS2C mMatchTeamFight = PB_MatchTeamFight_FMS2GS2C.Parser.ParseFrom(pack.BodyBuffer.Bytes); Debug.LogFormat("OnS2CMatch,推送战斗准备成功,开始战斗,及相关信息:mMatchTeamFight={0}", mMatchTeamFight.ToString()); NetData.Instance.Set(MsgID.S2CMatch, (uint)MatchMsgID.Fms2Gs2CMatchFight, mMatchTeamFight); LoadingManager.LoadSceneAsync(SceneConfig.Fight); break; } }
public void OnS2CExchange(NetPack pack) { Debug.LogFormat("金币兑换水晶,CMD={0}, ", pack.CMD); }
public void OnS2CFriend(NetPack pack) { Debug.LogFormat("好友系统FriendMsgID,CMD={0}, ", pack.CMD); }
public void OnS2CActivity(NetPack pack) { Debug.LogFormat("活动Activity,CMD={0}, ", pack.CMD); }
public void OnS2CSummon(NetPack pack) { Debug.LogFormat("summon相关SummonMsgID,CMD={0}, ", pack.CMD); }
public void OnS2CStore(NetPack pack) { Debug.LogFormat("商城StoreMsgID,CMD={0}, ", pack.CMD); }
public void OnS2CHuodongOther(NetPack pack) { Debug.LogFormat("和之前活动导表不同的活动HuodongMsgID,CMD={0}, ", pack.CMD); }
public void OnS2CShop(NetPack pack) { Debug.LogFormat("商店,CMD={0}, ", pack.CMD); }
public void OnS2CMail(NetPack pack) { Debug.LogFormat("邮件MailMsgID,CMD={0}, ", pack.CMD); }
public void OnS2CAlliance(NetPack pack) { Debug.LogFormat("帮派,CMD={0}, ", pack.CMD); }
public void Send <T>(MsgID msgID, uint cmd, T content) where T : Google.Protobuf.IMessage { NetPack pack = NetPack.SerializeToPack(content, (uint)msgID, cmd); Send(pack); }
public void OnS2CHuodong(NetPack pack) { Debug.LogFormat("活动相关协议HuodongMsgID,CMD={0}, ", pack.CMD); }
public void OnS2CEquip(NetPack pack) { Debug.LogFormat("装备,CMD={0}, ", pack.CMD); }
public void OnS2CLuckyshop(NetPack pack) { Debug.LogFormat("幸运商店,CMD={0}, ", pack.CMD); }
public void OnS2CPrompt(NetPack pack) { Debug.LogFormat("红点功能,CMD={0}, ", pack.CMD); }
public void OnS2CRank(NetPack pack) { Debug.LogFormat("排行榜,CMD={0}, ", pack.CMD); }
public void OnS2CChannel(NetPack pack) { Debug.LogFormat("聊天系统ChannelMsgID,CMD={0}, ", pack.CMD); }