Example #1
0
        public static void Test()
        {
            MsgMove msgMove = new MsgMove();

            msgMove.x = 45;

            byte[] bytes = MsgBase.Encode(msgMove);

            Console.WriteLine(Encoding.UTF8.GetString(bytes));

            MsgMove msg = (MsgMove)MsgBase.Decode("MsgMove", bytes, 0, bytes.Length);

            Console.WriteLine(msg.x);

            if (DbManager.CreatePlayer("zjt"))
            {
                Console.WriteLine("成功创建");
            }

            if (DbManager.Register("wyl", "123456"))
            {
                Console.WriteLine("成功注册");
            }

            PlayerData playerData = DbManager.GetPlayerData("zjt");

            if (playerData != null)
            {
                Console.WriteLine("玩家的金币是:{0}", playerData.coin);
            }
        }
Example #2
0
        private static void OnReceiveData(ClientState state)
        {
            ByteArray readBuff = state.readBuff;

            //msglength
            if (readBuff.length <= 2)
            {
                return;
            }
            Int16 bodyLength = readBuff.ReadInt16();

            //msg
            if (readBuff.length < bodyLength)
            {
                return;
            }
            //name
            int    nameCount = 0;
            string protoName = MsgBase.DecodeName(readBuff.bytes, readBuff.readIdx, out nameCount);

            if (protoName == "")
            {
                Debug.WriteLine("OnReceiveData msgDecodeName failed");
                Close(state);
            }
            readBuff.readIdx += nameCount;

            //body
            int     bodyCount = bodyLength - nameCount;
            MsgBase msg       = MsgBase.Decode <MsgBase>(protoName, readBuff.bytes, readBuff.readIdx, bodyCount);

            readBuff.readIdx += bodyCount;
            readBuff.CheckAndMoveBytes();

            //fire
            System.Reflection.MethodInfo methodInfo = typeof(MsgHandler).GetMethod(protoName);
            object[] ob = { state, msg };
            Debug.WriteLine("Receive {0}", protoName);
            if (methodInfo != null)
            {
                methodInfo.Invoke(null, ob);
            }
            else
            {
                Debug.WriteLine("OnReceiveData invoke fail {0}", protoName);
            }
            //广播

            /*foreach (var item in clients.Values)
             * {
             *  Send(item, msg);
             * }*/
            if (readBuff.length > 2)
            {
                OnReceiveData(state);
            }
        }
Example #3
0
        /// <summary>
        /// 接收数据处理,根据信息解析协议,根据协议内容处理消息再下发到客户端
        /// </summary>
        /// <param name="clientSocket"></param>
        void OnReceiveData(ClientSocket clientSocket)
        {
            ByteArray readbuff = clientSocket.ReadBuff;

            byte[] bytes      = readbuff.Bytes;
            int    bodyLength = BitConverter.ToInt32(bytes, readbuff.ReadIdx);

            //判断接收到的信息长度是否小于包体长度+包体头长度,如果小于,代表我们的信息不全,大于代表信息全了(有可能有粘包存在)
            if (readbuff.Length < bodyLength + 4)
            {
                return;
            }

            //解析协议名
            ProtocolEnum proto = MsgBase.DecodeName(readbuff.Bytes);

            if (proto == ProtocolEnum.None)
            {
                Debug.LogError("OnReceiveData MsgBase.DecodeName  fail");
                CloseClient(clientSocket);
                return;
            }
            readbuff.ReadIdx += 4;
            //解析协议体
            int bodyCount = bodyLength - readbuff.Bytes[4] - 2;

            MsgBase msgBase = MsgBase.Decode(proto, readbuff.Bytes, bodyCount);

            if (msgBase == null)
            {
                Debug.LogError("{0}协议内容解析错误:" + proto.ToString());
                CloseClient(clientSocket);
                return;
            }
            //通过反射分发消息
            MethodInfo mi = typeof(MsgHandler).GetMethod(proto.ToString());

            object[] o = { clientSocket, msgBase };
            if (mi != null)
            {
                mi.Invoke(null, o);
            }
            else
            {
                Debug.LogError("OnReceiveData Invoke fail:" + proto.ToString());
            }

            readbuff.ReadIdx += bodyLength;
            readbuff.CheckAndMoveBytes();
            //继续读取消息 处理黏包
            if (readbuff.Length > 4)
            {
                OnReceiveData(clientSocket);
            }
        }
    // 数据处理
    public static void OnReceiveData()
    {
        // 消息长度
        if (readBuff.length < 2)
        {
            return;
        }

        // 获取消息体长度
        int readIdx = readBuff.readIdx;

        byte[] bytes = readBuff.bytes;
        // Debug.Log(System.Text.Encoding.ASCII.GetString(bytes));
        Int16 bodyLength = (Int16)((bytes[readIdx + 1] << 8) | bytes[readIdx]);

        if (readBuff.length < bodyLength + 2)
        {
            return;
        }
        readBuff.readIdx += 2;

        // 解析协议名
        int    nameCount = 0;
        string protoName = MsgBase.DecodeName(readBuff.bytes, readBuff.readIdx, out nameCount);

        if (protoName == "")
        {
            Debug.Log("OnReceiveData MsgBase.DecodeName fail!");
            return;
        }
        readBuff.readIdx += nameCount;

        // 解析协议体
        int     bodyCount = bodyLength - nameCount;
        MsgBase msgBase   = MsgBase.Decode(protoName, readBuff.bytes, readBuff.readIdx, bodyCount);

        readBuff.readIdx += bodyCount;

        // 将已读数据移除
        readBuff.CheckAndMoveBytes();

        // 将协议添加到消息队列
        lock (msgList)
        {
            msgList.Add(msgBase);
        }
        msgCount++;  // 避免操作msgList(不用msgList.Length()),防止发生线程冲突

        // 继续读取消息
        if (readBuff.length > 2)
        {
            OnReceiveData();
        }
    }
Example #5
0
    //数据处理
    public static void OnReceiveData(ClientState state)
    {
        ByteArray readBuff = state.readBuff;

        //消息长度
        if (readBuff.length <= 2)
        {
            return;
        }
        Int16 bodyLength = readBuff.ReadInt16();

        //消息体
        if (readBuff.length < bodyLength)
        {
            return;
        }
        //解析协议名
        int    nameCount = 0;
        string protoName = MsgBase.DecodeName(readBuff.bytes, readBuff.readIdx, out nameCount);

        if (protoName == "")
        {
            Console.WriteLine("OnReceiveData MsgBase.DecodeName fail");
            Close(state);
            return;
        }
        readBuff.readIdx += nameCount;
        //解析协议体
        int     bodyCount = bodyLength - nameCount;
        MsgBase msgBase   = MsgBase.Decode(protoName, readBuff.bytes, readBuff.readIdx, bodyCount);

        readBuff.readIdx += bodyCount;
        readBuff.CheckAndMoveBytes();
        //分发消息
        MethodInfo mi = typeof(MsgHandler).GetMethod(protoName);

        object[] o = { state, msgBase };
        Console.WriteLine("Receive " + protoName);
        if (mi != null)
        {
            mi.Invoke(null, o);
        }
        else
        {
            Console.WriteLine("OnReceiveData Invoke fail " + protoName);
        }
        //继续读取消息
        if (readBuff.length > 2)
        {
            OnReceiveData(state);
        }
    }
Example #6
0
    static void OnReceivingData(ClientState state)
    {
        ByteArray readBuffer = state.readBuffer;

        if (readBuffer.Length <= 2)
        {
            return;
        }
        Int16 bodyLength = (Int16)((readBuffer.bytes[readBuffer.readIndex + 1] << 8) | readBuffer.bytes[readBuffer.readIndex]);

        if (readBuffer.Length < bodyLength + 2)
        {
            return;
        }
        readBuffer.readIndex += 2;

        string msgName = MsgBase.DecodeName(readBuffer.bytes, readBuffer.readIndex, out int nameCount);

        if (msgName == "")
        {
            Console.WriteLine("On receiving data, Decode name fail");
            Close(state);
            return;
        }

        readBuffer.readIndex += nameCount;

        int     bodyCount = bodyLength - nameCount;
        MsgBase msg       = MsgBase.Decode(msgName, readBuffer.bytes, readBuffer.readIndex, bodyCount);

        readBuffer.readIndex += bodyCount;
        readBuffer.CheckAndMove();
        // have read all data, need to distribute message
        MethodInfo mi = typeof(MsgHandler).GetMethod(msgName);

        object[] o = { state, msg };
        Console.WriteLine("Receive " + msgName + " from: " + state.socket.RemoteEndPoint.ToString());
        if (mi != null)
        {
            mi.Invoke(null, o);
        }
        else
        {
            Console.WriteLine("OnReceivingData Invoke fail " + msgName);
        }
        if (readBuffer.Length > 2)
        {
            OnReceivingData(state);
        }
    }
Example #7
0
    //数据处理
    public static void OnReceiveData()
    {
        //消息长度
        if (readBuff.length <= INT32)
        {
            return;
        }
        //获取消息体长度
        int readIdx = readBuff.readIdx;

        byte[] bytes = readBuff.bytes;
        // Int16 bodyLength = (Int16)((bytes[readIdx+1] << 8 )| bytes[readIdx]);

        byte[] bodyLengthBytes = new byte[INT32];
        ArrayUtils.copy(bytes, bodyLengthBytes, INT32);
        int bodyLength = ArrayUtils.byteArrayToInt(bodyLengthBytes);

        if (readBuff.length < bodyLength)
        {
            return;
        }
        readBuff.readIdx += INT32;
        //解析协议名
        int    nameCount = 0;
        string protoName = MsgBase.DecodeName(readBuff.bytes, readBuff.readIdx, out nameCount);

        if (protoName == "")
        {
            Debug.Log("OnReceiveData MsgBase.DecodeName fail");
            return;
        }
        readBuff.readIdx += nameCount;
        //解析协议体
        int     bodyCount = bodyLength - nameCount;
        MsgBase msgBase   = MsgBase.Decode(protoName, readBuff.bytes, readBuff.readIdx, bodyCount);

        readBuff.readIdx += bodyCount;
        readBuff.CheckAndMoveBytes();
        //添加到消息队列
        lock (msgList){
            msgList.Add(msgBase);
            msgCount++;
        }
        //继续读取消息
        if (readBuff.length > 2)
        {
            OnReceiveData();
        }
    }
Example #8
0
    private static void OnReceiveData()
    {
        if (readBuff.length <= 2)
        {
            return;
        }
        // header
        int readIdx = readBuff.readIdx;

        byte[] bytes      = readBuff.bytes;
        Int16  bodyLength = (Int16)((bytes[readIdx + 1] << 8) | bytes[readIdx]);

        if (readBuff.length < bodyLength)
        {
            return;
        }
        readBuff.readIdx += 2;
        // name
        int    nameCount = 0;
        string protoName = MsgBase.DecodeName(readBuff.bytes, readBuff.readIdx, out nameCount);

        if (protoName == "")
        {
            Debug.WriteLine("OnReceiveData msgBase.Decodename fail");
            return;
        }
        Debug.WriteLine("OnReceiveData msgBase:" + protoName);

        readBuff.readIdx += nameCount;
        //body
        int     bodyCount = bodyLength - nameCount;
        MsgBase msgBase   = MsgBase.Decode <MsgBase>(protoName, readBuff.bytes, readBuff.readIdx, bodyCount);

        readBuff.readIdx += bodyCount;
        readBuff.CheckAndMoveBytes();
        //add list
        lock (msgList)
        {
            msgList.Add(msgBase);
        }
        msgCount++;
        if (readBuff.length > 2)
        {
            OnReceiveData();
        }
    }
Example #9
0
    //数据处理
    public static void OnReceiveData()
    {
        //消息长度
        if (readBuff.length <= msgLenth)
        {
            return;
        }
        //获取消息体长度
        int readIdx = readBuff.readIdx;

        byte[] bytes      = readBuff.bytes;
        Int16  bodyLength = (Int16)(bytes[readIdx]);

        if (readBuff.length < bodyLength)
        {
            return;
        }
        readBuff.readIdx += msgLenth;
        //解析协议名
        int    nameCount = 0;
        string protoName = MsgBase.DecodeName(readBuff.bytes, readBuff.readIdx, out nameCount);

        if (protoName == "")
        {
            Debug.Log("OnReceiveData MsgBase.DecodeName fail");
            return;
        }
        readBuff.readIdx += nameCount;
        //解析协议体
        int     bodyCount = bodyLength - nameCount;
        MsgBase msgBase   = MsgBase.Decode(protoName, readBuff.bytes, readBuff.readIdx, bodyCount);

        readBuff.readIdx += bodyCount;
        readBuff.CheckAndMoveBytes();
        //添加到消息队列
        lock (msgList){
            msgList.Add(msgBase);
            msgCount++;
        }
        //继续读取消息
        if (readBuff.length > msgLenth)
        {
            OnReceiveData();
        }
    }
Example #10
0
        public static void OnReceiveDate(ClientState state)
        {
            Console.WriteLine(Encoding.UTF8.GetString(state.readBuffer.bytes));

            ByteArray readBuffer = state.readBuffer;

            //判断消息长度是否合法
            if (readBuffer.length <= 2)
            {
                return;
            }

            Int16 bodyLength = readBuffer.ReadInt16();

            if (readBuffer.length < bodyLength)
            {
                return;
            }

            //解析协议名
            int nameCount = 0;

            string proName = MsgBase.DecodeName(readBuffer.bytes, readBuffer.readIndex, out nameCount);

            if (proName == "")
            {
                Console.WriteLine("OnReceiveDate MsgBase.DecodeName Fail ");

                Close(state);

                return;
            }

            readBuffer.readIndex += nameCount;

            //解析协议体
            int bodyCount = bodyLength - nameCount;

            MsgBase msgBase = MsgBase.Decode(proName, readBuffer.bytes, readBuffer.readIndex, bodyCount);

            readBuffer.readIndex += bodyCount;

            readBuffer.MoveBytes();

            //分发消息
            MethodInfo mi = typeof(MsgHandler).GetMethod(proName);

            object[] o = { state, msgBase };

            if (mi != null)
            {
                mi.Invoke(null, o);
            }
            else
            {
                Console.WriteLine("OnReceiveData InVoke Fail " + proName);
            }

            if (readBuffer.length > 2)
            {
                //紧凑函数
                OnReceiveDate(state);
            }
        }
Example #11
0
        //数据处理
        public static void OnReceiveData(ClientState state)
        {
            ByteArray readBuff = state.readBuff;

            //消息长度
            if (readBuff.length <= 2)
            {
                return;
            }
            //消息体长度
            int readIdx = readBuff.readIdx;

            byte[] bytes      = readBuff.bytes;
            Int16  bodyLength = (Int16)((bytes[readIdx + 1] << 8) | bytes[readIdx]);

            if (readBuff.length < bodyLength)
            {
                return;
            }
            readBuff.readIdx += 2;
            //解析协议名
            int    nameCount = 0;
            string protoName = MsgBase.DecodeName(readBuff.bytes, readBuff.readIdx, out nameCount);

            if (protoName == "")
            {
                Console.WriteLine("OnReceiveData MsgBase.DecodeName fail");
                Close(state);
                return;
            }
            readBuff.readIdx += nameCount;
            //解析协议体
            int bodyCount = bodyLength - nameCount;

            if (bodyCount <= 0)
            {
                Console.WriteLine("OnReceiveData fail, bodyCount <=0 ");
                Close(state);
                return;
            }
            MsgBase msgBase = MsgBase.Decode(protoName, readBuff.bytes, readBuff.readIdx, bodyCount);

            readBuff.readIdx += bodyCount;
            readBuff.CheckAndMoveBytes();



            if (state.player != null && state.player.roomId > 0)
            {
                //广播业务消息
                int roomId = state.player.roomId;
                if (roomId <= 0)
                {
                    Console.WriteLine("player not in room");
                }
                else
                {
                    Room room = RoomManager.GetRoom(roomId);
                    room.Broadcast(msgBase);
                }
            }
            else
            {
                //系统消息
                MethodInfo mi;
                bool       isSuccess = msgHandlers.TryGetValue(protoName, out mi);
                if (isSuccess)
                {
                    object[] o = { state, msgBase };
                    Debug.WriteLine("Receive " + protoName);
                    if (mi != null)
                    {
                        mi.Invoke(null, o);
                    }
                    else
                    {
                        Console.WriteLine("OnReceiveData Invoke fail " + protoName);
                    }
                }
            }
            //继续读取消息
            if (readBuff.length > 2)
            {
                OnReceiveData(state);
            }
        }
Example #12
0
    /// <summary>
    /// 对数据进行处理
    /// </summary>
    void OnReceiveData()
    {
        if (m_ReadBuff.Length <= 4 || m_ReadBuff.ReadIdx < 0)
        {
            return;
        }

        int readIdx = m_ReadBuff.ReadIdx;

        byte[] bytes      = m_ReadBuff.Bytes;
        int    bodyLength = BitConverter.ToInt32(bytes, readIdx);

        //读取协议长度之后进行判断,如果消息长度小于读出来的消息长度,证明是没有一条完整的数据
        if (m_ReadBuff.Length < bodyLength + 4)
        {
            return;
        }

        m_ReadBuff.ReadIdx += 4;
        int          nameCount = 0;
        ProtocolEnum protocol  = MsgBase.DecodeName(m_ReadBuff.Bytes, m_ReadBuff.ReadIdx, out nameCount);

        if (protocol == ProtocolEnum.None)
        {
            Debug.LogError("OnReceiveData MsgBase.DecodeName fail");
            ClientClose();
            return;
        }

        m_ReadBuff.ReadIdx += nameCount;
        //解析协议体
        int bodyCount = bodyLength - nameCount;

        try
        {
            MsgBase msgBase = MsgBase.Decode(protocol, m_ReadBuff.Bytes, m_ReadBuff.ReadIdx, bodyCount);
            if (msgBase == null)
            {
                Debug.LogError("接受数据协议内容解析出错");
                ClientClose();
                return;
            }
            m_ReadBuff.ReadIdx += bodyCount;
            m_ReadBuff.CheckAndMoveBytes();
            //协议具体的操作
            lock (m_MsgList)
            {
                m_MsgList.Add(msgBase);
            }
            //处理粘包
            if (m_ReadBuff.Length > 4)
            {
                OnReceiveData();
            }
        }
        catch (Exception ex)
        {
            Debug.LogError("Socket OnReceiveData error:" + ex.ToString());
            ClientClose();
        }
    }