示例#1
0
        /// <summary>
        ///     Decodes the specified region of the buffer into an unadjusted frame length.  The default implementation is
        ///     capable of decoding the specified region into an unsigned 8/16/24/32/64 bit integer.  Override this method to
        ///     decode the length field encoded differently.
        ///     Note that this method must not modify the state of the specified buffer (e.g.
        ///     <see cref="IByteBuffer.ReaderIndex" />,
        ///     <see cref="IByteBuffer.WriterIndex" />, and the content of the buffer.)
        /// </summary>
        /// <param name="buffer">The buffer we'll be extracting the frame length from.</param>
        /// <param name="offset">The offset from the absolute <see cref="IByteBuffer.ReaderIndex" />.</param>
        /// <param name="length">The length of the framelenght field. Expected: 1, 2, 3, 4, or 8.</param>
        /// <param name="order">The preferred <see cref="ByteOrder" /> of <see cref="buffer" />.</param>
        /// <returns>A long integer that represents the unadjusted length of the next frame.</returns>
        protected long GetUnadjustedFrameLength(IByteBuffer buffer, int offset, int length, ByteOrder order)
        {
            buffer = buffer.WithOrder(order);
            long frameLength;

            switch (length)
            {
            case 1:
                frameLength = buffer.GetByte(offset);
                break;

            case 2:
                frameLength = buffer.GetShort(offset);
                break;

            case 4:
                frameLength = buffer.GetInt(offset);
                break;

            case 8:
                frameLength = buffer.GetLong(offset);
                break;

            default:
                throw new DecoderException("unsupported lengthFieldLength: " + this.lengthFieldLength + " (expected: 1, 2, 3, 4, or 8)");
            }
            return(frameLength);
        }
        protected override long GetUnadjustedFrameLength(IByteBuffer buffer, int offset, int length, ByteOrder order)
        {
            buffer = buffer.WithOrder(order);
            var scalarPrefix = buffer.GetByte(offset++);

            if (buffer.ReadableBytes - (offset - buffer.ReaderIndex) < scalarPrefix)
            {
                return(scalarPrefix);
            }

            switch (scalarPrefix)
            {
            case 1:
                return(buffer.GetByte(offset) + scalarPrefix);

            case 2:
                return(buffer.GetShort(offset) + scalarPrefix);

            case 4:
                return(buffer.GetInt(offset) + scalarPrefix);

            default:
                throw new ProudException("Invalid scalar prefix " + scalarPrefix);
            }
        }
示例#3
0
 public static ushort GetUnsignedShort(this IByteBuffer buf, int index)
 {
     unchecked
     {
         return((ushort)buf.GetShort(index));
     }
 }
示例#4
0
        public void handleReceive(IByteBuffer byteBuf, Ukcp ukcp)
        {
            short curCount = byteBuf.GetShort(byteBuf.ReaderIndex);

            Console.WriteLine(Thread.CurrentThread.Name + " 收到消息 " + curCount);
            ukcp.write(byteBuf);
            if (curCount == -1)
            {
                ukcp.close();
            }
        }
示例#5
0
        ///<summary>
        ///    Returns the closing status code as per http://tools.ietf.org/html/rfc6455#section-7.4 RFC 6455.
        ///    If a status code is set, -1 is returned.
        /// </summary>
        public int StatusCode()
        {
            IByteBuffer binaryData = this.Content;

            if (binaryData is null || 0u >= (uint)binaryData.Capacity)
            {
                return(-1);
            }

            _ = binaryData.SetReaderIndex(0);
            return(binaryData.GetShort(0));
        }
示例#6
0
        protected override long GetUnadjustedFrameLength(IByteBuffer buffer, int offset, int length, ByteOrder order)
        {
            buffer = buffer.WithOrder(ByteOrder.LittleEndian);
            var scalarPrefix = buffer.GetByte(offset++);

            switch (scalarPrefix)
            {
            case 1:
                return(buffer.ReadableBytes < 1 ? 1 : buffer.GetByte(offset) + 1);

            case 2:
                return(buffer.ReadableBytes < 2 ? 2 : buffer.GetShort(offset) + 2);

            case 4:
                return(buffer.ReadableBytes < 4 ? 4 : buffer.GetInt(offset) + 4);

            default:
                throw new ProudException("Invalid scalar prefix " + scalarPrefix);
            }
        }
示例#7
0
        protected override long GetUnadjustedFrameLength(IByteBuffer buffer, int offset, int length, ByteOrder order)
        {
            buffer = buffer.WithOrder(ByteOrder.LittleEndian);
            var scalarPrefix = buffer.GetByte(offset++);

            // lengthFieldOffset from constructor + scalarPrefix from above
            var bytesLeft = buffer.ReadableBytes - Math.Abs(buffer.ReaderIndex - offset) + 1;

            switch (scalarPrefix)
            {
            case 1:
                return(bytesLeft < 1 ? 1 : buffer.GetByte(offset) + 1);

            case 2:
                return(bytesLeft < 2 ? 2 : buffer.GetShort(offset) + 2);

            case 4:
                return(bytesLeft < 4 ? 4 : buffer.GetInt(offset) + 4);

            default:
                throw new ProudException("Invalid scalar prefix " + scalarPrefix);
            }
        }
示例#8
0
 public virtual short GetShort(int index) => Buf.GetShort(index);
 /// <summary>
 ///     Decodes the specified region of the buffer into an unadjusted frame length.  The default implementation is
 ///     capable of decoding the specified region into an unsigned 8/16/24/32/64 bit integer.  Override this method to
 ///     decode the length field encoded differently.
 ///     Note that this method must not modify the state of the specified buffer (e.g.
 ///     <see cref="IByteBuffer.ReaderIndex" />,
 ///     <see cref="IByteBuffer.WriterIndex" />, and the content of the buffer.)
 /// </summary>
 /// <param name="buffer">The buffer we'll be extracting the frame length from.</param>
 /// <param name="offset">The offset from the absolute <see cref="IByteBuffer.ReaderIndex" />.</param>
 /// <param name="length">The length of the framelenght field. Expected: 1, 2, 3, 4, or 8.</param>
 /// <param name="order">The preferred <see cref="ByteOrder" /> of <see cref="buffer" />.</param>
 /// <returns>A long integer that represents the unadjusted length of the next frame.</returns>
 protected long GetUnadjustedFrameLength(IByteBuffer buffer, int offset, int length, ByteOrder order)
 {
     buffer = buffer.WithOrder(order);
     long frameLength;
     switch (length)
     {
         case 1:
             frameLength = buffer.GetByte(offset);
             break;
         case 2:
             frameLength = buffer.GetShort(offset);
             break;
         case 4:
             frameLength = buffer.GetInt(offset);
             break;
         case 8:
             frameLength = buffer.GetLong(offset);
             break;
         default:
             throw new DecoderException("unsupported lengthFieldLength: " + this.lengthFieldLength + " (expected: 1, 2, 3, 4, or 8)");
     }
     return frameLength;
 }
示例#10
0
        /// <summary>
        ///     Return how much bytes can be read out of the encrypted data. Be aware that this method will not increase
        ///     the readerIndex of the given <see cref="IByteBuffer"/>.
        /// </summary>
        /// <param name="buffer">
        ///     The <see cref="IByteBuffer"/> to read from. Be aware that it must have at least
        ///     <see cref="SSL_RECORD_HEADER_LENGTH"/> bytes to read,
        ///     otherwise it will throw an <see cref="ArgumentException"/>.
        /// </param>
        /// <param name="offset">Offset to record start.</param>
        /// <returns>
        ///     The length of the encrypted packet that is included in the buffer. This will
        ///     return <c>-1</c> if the given <see cref="IByteBuffer"/> is not encrypted at all.
        /// </returns>
        public static int GetEncryptedPacketLength(IByteBuffer buffer, int offset)
        {
            int packetLength = 0;

            // SSLv3 or TLS - Check ContentType
            bool tls;

            switch (buffer.GetByte(offset))
            {
            case SSL_CONTENT_TYPE_CHANGE_CIPHER_SPEC:
            case SSL_CONTENT_TYPE_ALERT:
            case SSL_CONTENT_TYPE_HANDSHAKE:
            case SSL_CONTENT_TYPE_APPLICATION_DATA:
            case SSL_CONTENT_TYPE_EXTENSION_HEARTBEAT:
                tls = true;
                break;

            default:
                // SSLv2 or bad data
                tls = false;
                break;
            }

            if (tls)
            {
                // SSLv3 or TLS - Check ProtocolVersion
                int majorVersion = buffer.GetByte(offset + 1);
                if (majorVersion == 3)
                {
                    // SSLv3 or TLS
                    packetLength = buffer.GetUnsignedShort(offset + 3) + SSL_RECORD_HEADER_LENGTH;
                    if ((uint)packetLength <= SSL_RECORD_HEADER_LENGTH)
                    {
                        // Neither SSLv3 or TLSv1 (i.e. SSLv2 or bad data)
                        tls = false;
                    }
                }
                else
                {
                    // Neither SSLv3 or TLSv1 (i.e. SSLv2 or bad data)
                    tls = false;
                }
            }

            if (!tls)
            {
                // SSLv2 or bad data - Check the version
                uint uHeaderLength = (buffer.GetByte(offset) & 0x80) != 0 ? 2u : 3u;
                uint uMajorVersion = buffer.GetByte(offset + (int)uHeaderLength + 1);
                if (uMajorVersion == 2u || uMajorVersion == 3u)
                {
                    // SSLv2
                    packetLength = uHeaderLength == 2u ?
                                   (buffer.GetShort(offset) & 0x7FFF) + 2 : (buffer.GetShort(offset) & 0x3FFF) + 3;
                    if (uHeaderLength >= (uint)packetLength)
                    {
                        return(NOT_ENOUGH_DATA);
                    }
                }
                else
                {
                    return(NOT_ENCRYPTED);
                }
            }

            return(packetLength);
        }
示例#11
0
 public short GetShort(int index)
 {
     CheckIndex(index, 2);
     return(_buffer.GetShort(index));
 }
示例#12
0
 public static char GetChar(this IByteBuffer buf, int index) => Convert.ToChar(buf.GetShort(index));