예제 #1
0
    public void read(ref short value, bool inverse = false)
    {
        int readLen = sizeof(short);

        if (!readCheck(readLen))
        {
            return;
        }
        value = BinaryUtility.readShort(mBuffer, ref mIndex, inverse);
    }
예제 #2
0
 public override void parseData(PacketHeader header, byte[] data, int dataCount, ref int offset)
 {
     base.parseData(header, data, dataCount, ref offset);
     mPower = (ushort)BinaryUtility.readShort(data, ref offset, true);
     mRPM   = (ushort)BinaryUtility.readShort(data, ref offset, true);
     // 最高位为0,角度为正
     if ((data[offset] & 0x80) == 0)
     {
         mAngle = BinaryUtility.readShort(data, ref offset, true);
     }
     // 最高位为1,角度为负
     else
     {
         data[offset] &= 0x7f;
         mAngle        = BinaryUtility.readShort(data, ref offset, true);
         mAngle        = (short)-mAngle;
     }
     mKeyStatus = BinaryUtility.readByte(data, ref offset);
 }
예제 #3
0
    public PARSE_RESULT parseData(byte[] data, int count)
    {
        // 长度不够包头时,数据不足
        if (count < mHeaderLength)
        {
            return(PARSE_RESULT.PR_NOT_ENOUGH);
        }
        // 确保一些固定数值是正确的
        if (data[0] != mReportID || data[1] != mMagicByte || data[2] != mVersion || data[mHeaderLength - 1] != mSeqID)
        {
            UnityUtility.logInfo("data : " + BinaryUtility.bytesToHEXString(data, true, true, count));
            UnityUtility.logInfo("数据错误");
            return(PARSE_RESULT.PR_ERROR);
        }
        int    offset        = sizeof(byte) + sizeof(byte) + sizeof(byte);
        ushort payloadLength = (ushort)BinaryUtility.readShort(data, ref offset, true);

        // 长度不够记录的数据长度,数据不足
        if (payloadLength > count - mHeaderLength)
        {
            return(PARSE_RESULT.PR_NOT_ENOUGH);
        }
        ushort crc = (ushort)BinaryUtility.readShort(data, ref offset, true);
        // 校验失败,数据错误
        ushort dataCRC16 = BinaryUtility.crc16(0xFF, data, payloadLength, mHeaderLength);

        if (crc != dataCRC16)
        {
            UnityUtility.logInfo("校验失败");
            return(PARSE_RESULT.PR_ERROR);
        }
        // 只有解析成功时,才保存数据
        mPayloadLength = payloadLength;
        mCRC16         = crc;
        return(PARSE_RESULT.PR_SUCCESS);
    }
예제 #4
0
파일: SHORT.cs 프로젝트: isoundy000/FPSDemo
 public override void readFromBuffer(byte[] buffer, ref int index)
 {
     mValue = BinaryUtility.readShort(buffer, ref index);
 }
예제 #5
0
    // 接收Socket消息
    protected void receiveSocket()
    {
        IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, 0);
        EndPoint   ep       = (EndPoint)endpoint;

        while (mRun)
        {
            byte[] recvBuff = new byte[mMaxReceiveCount];
            int    nRecv    = mServerSocket.ReceiveFrom(recvBuff, ref ep);
            if (nRecv < 0)
            {
                UnityUtility.logInfo("网络连接中断!");
                return;
            }
            else if (nRecv == 0)
            {
                UnityUtility.logInfo("已与服务器断开连接!");
                return;
            }
            int index = 0;
            while (true)
            {
                if (index + sizeof(short) > nRecv)
                {
                    break;
                }
                // 读取包类型(short)
                PACKET_TYPE type = (PACKET_TYPE)BinaryUtility.readShort(recvBuff, ref index);
                // 客户端接收到的必须是SC类型的
                if (type <= PACKET_TYPE.PT_SC_MIN || type >= PACKET_TYPE.PT_SC_MAX)
                {
                    UnityUtility.logInfo("packet type error : " + type);
                    break;
                }
                int packetSize = mSocketFactoryManager.getPacketSize(type);
                if (packetSize >= 0)
                {
                    // 读取消息长度(short)
                    short realDataSize = BinaryUtility.readShort(recvBuff, ref index);
                    if (realDataSize != packetSize)
                    {
                        UnityUtility.logError("error : wrong packet size! type : " + type + "readed : " + realDataSize + ", packet size : " + packetSize, false);
                        break;
                    }
                    if (packetSize > nRecv - sizeof(short))
                    {
                        UnityUtility.logError("error : wrong packet data! packet : " + type + ", need size : " + packetSize + ", receive size : " + (nRecv - sizeof(PACKET_TYPE)), false);
                        break;
                    }
                    else
                    {
                        UnityUtility.logInfo("receive : client : " + endpoint.Address.ToString() + ", type : " + type + ", size : " + packetSize);
                    }
                    lock (mRecieveList)
                    {
                        if (packetSize != 0)
                        {
                            byte[] recvData = new byte[packetSize];
                            // 读取消息内容(byte[])
                            BinaryUtility.readBytes(recvBuff, ref index, recvData);
                            mRecieveList.Add(new INPUT_ELEMENT(type, recvData));
                        }
                        else
                        {
                            byte[] recvData = null;
                            mRecieveList.Add(new INPUT_ELEMENT(type, recvData));
                        }
                        UnityUtility.logInfo("receive : type : " + type + ", count : " + nRecv + ", client ip : " + endpoint.Address.ToString());
                    }
                    // 该段消息内存已经解析完了
                    if (index == nRecv)
                    {
                        break;
                    }
                }
                // 如果消息解析发生错误,则不再解析
                else
                {
                    break;
                }
            }
            Thread.Sleep(10);
        }
    }
예제 #6
0
 // 接收Socket消息
 protected bool receiveSocket()
 {
     try
     {
         IPEndPoint endpoint = null;
         if (endpoint == null)
         {
             endpoint = new IPEndPoint(IPAddress.Any, 0);
         }
         EndPoint ep       = (EndPoint)endpoint;
         byte[]   recvBuff = null;
         if (recvBuff == null)
         {
             recvBuff = new byte[mMaxReceiveCount];
         }
         int nRecv = mServerSocket.ReceiveFrom(recvBuff, ref ep);
         if (nRecv <= 0)
         {
             CommandSocketManagerNetState cmd = newCmd(out cmd, true, true);
             cmd.mNetState = NET_STATE.NS_NET_CLOSE;
             pushDelayCommand(cmd, this);
             return(false);
         }
         int index = 0;
         while (true)
         {
             if (index + sizeof(short) > nRecv)
             {
                 break;
             }
             // 读取包类型(short)
             PACKET_TYPE type = (PACKET_TYPE)BinaryUtility.readShort(recvBuff, ref index);
             // 客户端接收到的必须是SC类型的
             if (type <= PACKET_TYPE.PT_SC_MIN || type >= PACKET_TYPE.PT_SC_MAX)
             {
                 UnityUtility.logError("packet type error : " + type);
                 break;
             }
             int packetSize = mSocketFactory.getPacketSize(type);
             if (packetSize >= 0)
             {
                 // 读取消息长度(short)
                 short realDataSize = BinaryUtility.readShort(recvBuff, ref index);
                 if (realDataSize != packetSize)
                 {
                     UnityUtility.logError("error : wrong packet size! type : " + type + "readed : " + realDataSize + ", packet size : " + packetSize, false);
                     break;
                 }
                 if (packetSize > nRecv - sizeof(short))
                 {
                     UnityUtility.logError("error : wrong packet data! packet : " + type + ", need size : " + packetSize + ", receive size : " + (nRecv - sizeof(PACKET_TYPE)), false);
                     break;
                 }
                 mReceiveLock.waitForUnlock();
                 if (packetSize != 0)
                 {
                     byte[] recvData = new byte[packetSize];
                     // 读取消息内容(byte[])
                     BinaryUtility.readBytes(recvBuff, ref index, recvData);
                     mRecieveList.Add(new INPUT_ELEMENT(type, recvData));
                 }
                 else
                 {
                     byte[] recvData = null;
                     mRecieveList.Add(new INPUT_ELEMENT(type, recvData));
                 }
                 mReceiveLock.unlock();
                 // 该段消息内存已经解析完了
                 if (index == nRecv)
                 {
                     break;
                 }
             }
             // 如果消息解析发生错误,则不再解析
             else
             {
                 break;
             }
         }
     }
     catch (SocketException)
     {
         CommandSocketManagerNetState cmd = newCmd(out cmd, true, true);
         cmd.mNetState = NET_STATE.NS_SERVER_CLOSE;
         pushDelayCommand(cmd, this);
         return(false);
     }
     return(true);
 }