/// <summary>
    /// xxx.xxx.xxx.xxx字符串形式的ip地址转换32位uint值IPV4,
    /// </summary>
    /// <param name="ipaddress"></param>
    /// <returns></returns>
    public static uint StringIPToUInt32(string ipaddress)
    {
        IPAddress address = IPAddress.Parse(ipaddress);

        byte[] ba = address.GetAddressBytes();
        return((uint)ArrayUtility.GetInt(ba, 0));
    }
Beispiel #2
0
        //把底层传来的数据分多次提交给使用者。
        //每次提交的数据称为包,其大小记录在包头,所有的包顺序排列。
        //如果现存的数据不足包长,则等待后续数据。
        protected override void OnReceivedDataCallBack(byte[] data /*整个包的起始地址,已在下层进行拼接*/, int length)
        {
            if (m_connectionState == ConnectionState.Uninitialised || length <= 0)
            {
                return;
            }
            m_iSegmentSize += length;       //合并收到的包
            int startOffset     = 0;        //amount for the submits to user.
            int rawpacketlength = 0;        //user packet size

            while (m_iSegmentSize >= Packet.HeaderSize)
            {
                //从消息头中读出包长度
                m_lengthOfCurrentPacket = Util.ArrayUtility.GetShort(data, startOffset + Packet.OffsetLength);
                //包长错误,忽略此包
                if (m_lengthOfCurrentPacket < Packet.HeaderSize || m_lengthOfCurrentPacket > Packet.MaxLength)
                {
                    m_iSegmentSize = 0;
                    return;
                }
                //数据不够组成一个消息包,等待后续数据
                if (m_iSegmentSize < m_lengthOfCurrentPacket)
                {
                    break;
                }
                rawpacketlength = m_lengthOfCurrentPacket;

                //按顺序读出包ID,DispatcherID,和标记位
                m_packetIDOfCurrentPacket     = ArrayUtility.GetShort(data, startOffset + Packet.OffsetPacketID);
                m_dispatcherIDOfCurrentPacket = ArrayUtility.GetInt(data, Packet.OffsetDispatcherID + startOffset);
                m_magicnumOfCurrentPacket     = Util.ArrayUtility.GetByte(data, startOffset + Packet.OffsetFlag);

                //HandleOneRawPacket();
                //处理明文数据(不加密也不压缩)
                if ((m_magicnumOfCurrentPacket & (short)PacketFlag.Encrypted) == 0 &&
                    (m_magicnumOfCurrentPacket & (short)PacketFlag.Compressed) == 0)
                {
                    //int packetserialnum = ArrayUtility.GetInt(data, 8 + startOffset);
                    //包的序列号错误
                    //if (packetserialnum != m_lastRecvPacketSerialNumber)
                    //{
                    //    packetserialnum = m_lastRecvPacketSerialNumber;//why?
                    //    if (m_encrypt != null)//非加密连接,出错就不管了,警告一下
                    //    {
                    //        CloseConnection();
                    //        return;
                    //    }
                    //    Console.WriteLine("Error:TcpConnection packetserialnum != m_lastRecvPacketSerialNumber!");
                    //}
                    m_lastRecvPacketSerialNumber++;
                    ProcessPacket(m_packetIDOfCurrentPacket, data, m_lengthOfCurrentPacket, startOffset);
                }
                else                //解密解压消息
                {
                    int securedatalength = m_lengthOfCurrentPacket - Packet.NoCryptHeaderLength;
                    if (securedatalength <= 0 || m_encrypt == null)
                    {
                        CloseConnection();
                        return;
                    }
                    System.Buffer.BlockCopy(data, startOffset,
                                            m_lastPacketData, 0, m_lengthOfCurrentPacket);
#if _NC_Compress
                    //解密
                    if ((m_magicnumOfCurrentPacket & (short)PacketFlag.Encrypted) == (short)PacketFlag.Encrypted)
                    {
                        m_encrypt.Decrypt(m_lastPacketData, Packet.NoCryptHeaderLength, securedatalength);
                        m_magicnumOfCurrentPacket &= ~(short)PacketFlag.Encrypted;
                    }
                    //解压
                    if ((m_magicnumOfCurrentPacket & (short)PacketFlag.Compressed) == (short)PacketFlag.Compressed)
                    {
                        if (m_compressneedchecksum)
                        {                        //check src32
                            uint crccheck = (uint)ArrayUtility.GetInt(m_lastPacketData, securedatalength);
                            securedatalength -= 4;
                            uint crccomp = HashHelp.CRC32hash(m_lastPacketData, Packet.NoCryptHeaderLength, securedatalength);
                            if (crccheck != crccomp)
                            {
                                CloseConnection();
                                return;
                            }
                        }
                        if (tls_decompressbuffer == null)
                        {
                            tls_decompressbuffer = new byte[Packet.MaxLength];
                        }
                        int outlen = Compress.LZO.Decompress(m_lastPacketData, Packet.NoCryptHeaderLength, securedatalength, tls_decompressbuffer, 0);
                        if (outlen <= Packet.HeaderSize - Packet.NoCryptHeaderLength || outlen >= Packet.MaxLength - Packet.NoCryptHeaderLength)                        //解压失败
                        {
                            CloseConnection();
                            return;
                        }
                        m_magicnumOfCurrentPacket &= ~(short)PacketFlag.Compressed;
                        Buffer.BlockCopy(tls_decompressbuffer, 0, m_lastPacketData, Packet.NoCryptHeaderLength, outlen);
                        m_lengthOfCurrentPacket = (short)(Packet.NoCryptHeaderLength + outlen);
                    }
#endif
                    m_packetIDOfCurrentPacket = ArrayUtility.GetInt(m_lastPacketData, 4);
                    int packetserialnum = ArrayUtility.GetInt(m_lastPacketData, 8);
                    if (packetserialnum != m_lastRecvPacketSerialNumber)
                    {
                        packetserialnum = m_lastRecvPacketSerialNumber;                        //why?
                        CloseConnection();
                        return;
                    }
                    m_lastRecvPacketSerialNumber++;
                    ArrayUtility.SetShort(m_lastPacketData, m_magicnumOfCurrentPacket, 0);
                    m_dispatcherIDOfCurrentPacket = ArrayUtility.GetInt(m_lastPacketData, 12);
                    ProcessPacket(m_packetIDOfCurrentPacket, m_lastPacketData, m_lengthOfCurrentPacket, 0);
                }

                if (m_connectionState == ConnectionState.Uninitialised)
                {
                    return;
                }

                m_iSegmentSize -= rawpacketlength;
                startOffset    += rawpacketlength;
            }
            if (startOffset > 0 && m_iSegmentSize > 0)
            {
                System.Buffer.BlockCopy(data, startOffset, m_RecvBuffer, 0, m_iSegmentSize);
            }
        }
 /// <summary>
 /// 从IPEndPoint得到IPV4的类型为32bit int的ip地址
 /// </summary>
 /// <param name="ep"></param>
 /// <returns></returns>
 public static uint GetIPV4FromIPEndPoint(IPEndPoint ep)
 {
     byte[] ba = ep.Address.GetAddressBytes();
     return((uint)ArrayUtility.GetInt(ba, 0));
 }