コード例 #1
0
        // 反序列化一个录像
        public static BattleReplay Deserialize(IReadableBuffer data)
        {
            var replay = DeserializeHeader(data);

            string lastMsgTitle = null;

            byte[] lastMsgData = null;
            var    msgs        = new List <KeyValuePair <string, IReadableBuffer> >();
            var    cnt         = data.ReadInt();

            FC.For(cnt, (i) =>
            {
                // 这里生成 msg 必须用网络字节序,因为这里的数据等同于是直接伪装成了网络上的消息,
                // 对 Room4Client 来说是一致读取的

                var sameAsLastOne = data.ReadBool();
                if (!sameAsLastOne)
                {
                    var msgTitle = data.ReadString();
                    var msgLen   = data.ReadInt();
                    var msgBody  = data.ReadBytes(msgLen);
                    msgs.Add(new KeyValuePair <string, IReadableBuffer>(msgTitle, new RingBuffer(true, true, msgBody)));

                    lastMsgTitle = msgTitle;
                    lastMsgData  = msgBody;
                }
                else
                {
                    msgs.Add(new KeyValuePair <string, IReadableBuffer>(lastMsgTitle, new RingBuffer(true, true, lastMsgData)));
                }
            });

            replay.Msgs = msgs;
            return(replay);
        }
コード例 #2
0
        // 反序列化一个录像的基本信息
        public static BattleReplay DeserializeHeader(IReadableBuffer data)
        {
            var replay = new BattleReplay();

            replay.ID            = data.ReadString();
            replay.Usr1          = data.ReadString();
            replay.Usr2          = data.ReadString();
            replay.UsrName1      = data.ReadString();
            replay.UsrName2      = data.ReadString();
            replay.Length        = data.ReadULong();
            replay.Date          = new DateTime(data.ReadLong());
            replay.IsPVP         = data.ReadBool();
            replay.IsCrashReplay = data.ReadBool();

            return(replay);
        }
コード例 #3
0
ファイル: Room4Client.cs プロジェクト: radtek/SCM2
    void OnBattleBeginMsg(IReadableBuffer data)
    {
        frameNoRecieved = 0;
        msgQ.Clear();

        inReplay = replayer.InReplaying;
        if (!inReplay)
        {
            replayer.Clear();
            replayer.Record("BattleBegin", data);
        }

        Clear();

        var randomSeed = data.ReadInt();
        var lvID       = data.ReadString();

        WinnerAward      = data.ReadInt();
        LoserAward       = data.ReadInt();
        WinnerAwardUnits = data.ReadStringArr();
        var roomID   = data.ReadString();
        var usrs     = data.ReadStringArr();
        var usrsInfo = data.ReadArr <UserInfo>();
        var sz       = data.ReadVec2();

        IsPVP = data.ReadBool();

        // 设置当前用户信息
        var gc = GameCore.Instance;

        gc.CurrentRoom = this;
        var meID = gc.MeID;

        if (usrs[1] == meID)
        {
            gc.MePlayer = 1;
        }
        else
        {
            gc.MePlayer = 2; // 观战者也用 2 视角
        }
        // 初始化房间信息
        var usr1 = usrs[1];
        var usr2 = usrs[2];

        OnBeforeBattleBegin.SC(usr1, usr2, sz, inReplay);
        Init(roomID, sz, lvID, usr1, usr2, usrsInfo[1], usrsInfo[2]);
        base.BattleBegin(randomSeed);

        OnBattleBegin.SC(this, inReplay);
    }
コード例 #4
0
        public static T Read <T>(this IReadableBuffer r) where T : class, ISerializable, new()
        {
            bool noNull = r.ReadBool();

            if (!noNull)
            {
                return(default(T));
            }

            T t = new T();

            t.Deserialize(r);
            return(t);
        }
コード例 #5
0
        public static T Read <T>(this IReadableBuffer r) where T : class, ISerializable, new()
        {
            bool hasValue = r.ReadBool();

            if (!hasValue)
            {
                return(null);
            }
            else
            {
                T v = new T();
                v.Deserialize(r);
                return(v);
            }
        }
コード例 #6
0
        public static ISerializable Read(this IReadableBuffer r, Func <ISerializable> creator)
        {
            bool noNull = r.ReadBool();

            if (!noNull)
            {
                return(null);
            }
            if (creator == null)
            {
                return(null);
            }
            ISerializable obj = creator();

            obj.Deserialize(r);
            return(obj);
        }
コード例 #7
0
        void OnModifyUsrAvatars(Session s, IReadableBuffer data)
        {
            var dic = new Dictionary <string, bool>();

            var cnt = data.ReadInt();

            for (int i = 0; i < cnt; i++)
            {
                var key = data.ReadString();
                var val = data.ReadBool();

                dic[key] = val;
            }

            s.Usr.Info.Avatars = dic;
            s.Usr.Update();
        }
コード例 #8
0
        void OnModifyUsrUUlocks(Session s, IReadableBuffer data)
        {
            var dic = new Dictionary <int, bool>();

            var cnt = data.ReadInt();

            for (int i = 0; i < cnt; i++)
            {
                var key = data.ReadInt();
                var val = data.ReadBool();

                dic[key] = val;
            }

            s.Usr.Info.UUnlocks = dic;
            s.Usr.Update();
        }
コード例 #9
0
        public static T[] ReadArr <T>(this IReadableBuffer r) where T : class, ISerializable, new()
        {
            bool hasValue = r.ReadBool();

            if (!hasValue)
            {
                return(null);
            }
            else
            {
                int len = r.ReadInt();
                T[] arr = new T[len];
                for (int i = 0; i < len; i++)
                {
                    arr[i] = Read <T>(r);
                }
                return(arr);
            }
        }
コード例 #10
0
        public void SyncInt(ref bool v)
        {
            MakeSureSyncing();

            if (isWrite)
            {
                w.Write(v);
            }
            else
            {
                if (IsEnd())
                {
                    return;
                }

                v = r.ReadBool();
            }
        }
コード例 #11
0
        // 时间驱动处理所有已经接收到的网络消息,并分发到其它各个组件对象
        public void OnTimeElapsed(int te)
        {
            // 每帧发送一次网络数据
            p.ProcessDoSend();

            // 处理所有未完成的连接操作
            p.ProcessPendingConnecting();

            // 处理所有网络消息
            NetConnection[] connections = p.AllConnections;
            foreach (NetConnection nc in connections)
            {
                IReadableBuffer data = nc.ReceivedData;

                // 处理已经完整接收的部分
                while (true)
                {
                    // 判断是否接收到重复上一条消息的指示头
                    if (data.Available < sizeof(bool))
                    {
                        break;
                    }

                    var repeatLastOne = false;
                    if (!data.PeekBool(ref repeatLastOne))
                    {
                        break;
                    }

                    Connection connWrap = GetConnectionWrap(nc);

                    if (repeatLastOne)
                    {
                        data.Skip(sizeof(bool));
                        var componentName = nc.LastData2Component;
                        if (componentName == null)
                        {
                            LogWarning("componentName repeated is null");
                            LogWarning("connected: " + nc.IsConnected + " : " + ((IPEndPoint)nc.Socket.RemoteEndPoint).Address.ToString());
                            nc.Close("componentName repeated is null");
                            break;
                        }

                        var          msgBody = new RingBuffer(true, true);
                        NetComponent c       = GetCom(componentName) as NetComponent;
                        msgBody.Write(nc.LastGetData, 0, nc.LastGetData.Length);
                        c.OnMessage(connWrap, msgBody);
                    }
                    else
                    {
                        // 判断消息是否已经完整接收
                        if (data.Available < sizeof(bool) + sizeof(int))
                        {
                            break;
                        }

                        int len = 0;
                        if (!data.PeekInt(sizeof(bool), ref len))
                        {
                            break;
                        }

                        if (len < 0 || len > NetConnection.MaxRecieveBufferSize)
                        {
                            string ex = "invalid message length: " + len;
                            nc.Close(ex);
                            throw new Exception(ex);
                            // break;
                        }

                        if (data.Available < len + sizeof(bool) + sizeof(int))
                        {
                            break;
                        }

                        data.Skip(sizeof(bool));
                        data.Skip(sizeof(int));

                        // 对该条消息解密
                        data.TravelReplaceBytes4Read(0, len, (byte d) => { return(connWrap.Decrypt(d)); });

                        if (OnMessageRecieved != null)
                        {
                            OnMessageRecieved();
                        }

                        // 消息序号,正数为普通消息,负数为应答消息
                        bool hasNo  = data.ReadBool();
                        long no     = hasNo ? data.ReadLong() : 0;
                        int  offset = hasNo ? sizeof(long) + sizeof(bool) : sizeof(bool);

                        if (no >= 0)
                        {
                            helper.responseID = -no;
                            string componentName = data.ReadString();
                            if (componentName == null)
                            {
                                throw new Exception("componentName try to get is null");
                            }
                            NetComponent c       = GetCom(componentName) as NetComponent;
                            byte[]       msgData = data.ReadBytes(len - offset - sizeof(int) - componentName.Length);

                            nc.LastGetData        = msgData;
                            nc.LastData2Component = componentName;

                            // 没找到对应的模块则忽略
                            if (c == null)
                            {
                                string ex = "no such a component named: " + componentName;
                                nc.Close(ex);
                                throw new Exception(ex);
                                // break;
                            }

                            // 投递消息
                            var msgBody = new RingBuffer(true, true);
                            msgBody.Write(msgData, 0, msgData.Length);
                            c.OnMessage(connWrap, msgBody);
                        }
                        else
                        {
                            byte[] msgData = data.ReadBytes(len - offset);
                            var    msgBody = new RingBuffer(true, true);
                            msgBody.Write(msgData, 0, msgData.Length);

                            // 投递消息
                            CallbackNode tcb;
                            if (callbacks.TryGetValue(no, out tcb))
                            {
                                Action <IReadableBuffer> cb = tcb.cb;
                                callbacks.Remove(no);
                                usrDefinedExpireProcess.Remove(no);

                                if (cb != null)
                                {
                                    cb(msgBody);
                                }
                            }
                            else
                            {
                                string ex = "no request callback for " + no;
                                nc.Close(ex);
                                throw new Exception(ex);
                                // break;
                            }
                        }
                    }
                }
            }

            // 检查请求超时
            CheckNextCallbackTimeout();
        }
コード例 #12
0
ファイル: UnitUtils.cs プロジェクト: radtek/SCM2
        public static UnitConfigInfo ReadUnitInfo(IReadableBuffer reader)
        {
            var info = new UnitConfigInfo();

            info.DisplayName      = reader.ReadString();
            info.Cost             = reader.ReadInt();
            info.GasCost          = reader.ReadInt();
            info.ConstructingTime = reader.ReadFix64();
            info.MaxNum           = reader.ReadInt();
            info.MaxVelocity      = reader.ReadFix64();
            info.IsBuilding       = reader.ReadBool();
            info.IsBiological     = reader.ReadBool();
            info.IsMechanical     = reader.ReadBool();
            info.IsAirUnit        = reader.ReadBool();
            info.UnAttackable     = reader.ReadBool();
            info.VisionRadius     = reader.ReadFix64();
            info.ChaseRadius      = reader.ReadFix64();
            info.Suppliment       = reader.ReadFix64();
            info.AITypes          = reader.ReadStringArr();

            if (reader.ReadBool())
            {
                var len2 = reader.ReadInt();
                info.AIParams = new Fix64[len2][];
                for (var i = 0; i < len2; i++)
                {
                    if (reader.ReadBool())
                    {
                        var len = reader.ReadInt();
                        info.AIParams[i] = new Fix64[len];
                        for (var j = 0; j < len; j++)
                        {
                            info.AIParams[i][j] = reader.ReadFix64();
                        }
                    }
                }
            }

            info.SizeRadius      = reader.ReadInt();
            info.NoBody          = reader.ReadBool();
            info.NoCard          = reader.ReadBool();
            info.MaxHp           = reader.ReadInt();
            info.CanAttackGround = reader.ReadBool();
            info.CanAttackAir    = reader.ReadBool();

            info.AttackType  = reader.ReadStringArr();
            info.AttackRange = reader.ReadIntArr();
            info.AttackPower = reader.ReadIntArr();

            if (reader.ReadBool())
            {
                var len6 = reader.ReadInt();
                info.AttackInterval = new Fix64[len6];

                for (var i = 0; i < len6; i++)
                {
                    info.AttackInterval[i] = reader.ReadFix64();
                }
            }

            info.AOEType = reader.ReadStringArr();

            if (reader.ReadBool())
            {
                var len8 = reader.ReadInt();
                info.AOEParams = new Fix64[len8][];
                for (var i = 0; i < len8; i++)
                {
                    if (reader.ReadBool())
                    {
                        var len = reader.ReadInt();
                        info.AOEParams [i] = new Fix64[len];
                        for (var j = 0; j < len; j++)
                        {
                            info.AOEParams[i][j] = reader.ReadFix64();
                        }
                    }
                }
            }

            info.ArmorType = reader.ReadString();
            info.Defence   = reader.ReadInt();

            if (reader.ReadBool())
            {
                var len9 = reader.ReadInt();
                info.Prerequisites = new string[len9][];
                for (var i = 0; i < len9; i++)
                {
                    if (reader.ReadBool())
                    {
                        var len = reader.ReadInt();
                        info.Prerequisites[i] = new string[len];
                        for (var j = 0; j < len; j++)
                        {
                            info.Prerequisites [i] [j] = reader.ReadString();
                        }
                    }
                }
            }

            info.ReconstructTo   = reader.ReadStringArr();
            info.ReconstructFrom = reader.ReadString();
            info.TechLevel       = reader.ReadInt();
            info.Desc            = reader.ReadString();
            info.InVisible       = reader.ReadBool();
            info.IsObserver      = reader.ReadBool();
            info.ReboundDamage   = reader.ReadFix64();
            info.Pets            = reader.ReadStringArr();
            info.OriginalType    = reader.ReadString();
            info.IsThirdType     = reader.ReadBool();

            return(info);
        }