private IByteBuf Decode(IConnection connection, IByteBuf input)
        {
            // we at least need to read our start character
            if (input.ReadableBytes < 1)
            {
                return(null);
            }

            input.MarkReaderIndex();

            // check start byte
            if (!input.ReadByte().Equals(_mllpStartCharacter))
            {
                throw new CorruptedFrameException(string.Format("Message doesn't start with: {0}", _mllpStartCharacter));
            }

            // check if we have a complete frame
            if (input.GetByte(input.ReadableBytes).Equals(_mllpLastEndCharacter) &&
                input.GetByte(input.ReadableBytes - 1).Equals(_mllpFirstEndCharacter))
            {
                var startMessage      = input.ReaderIndex;
                var actualFrameLength = input.ReadableBytes;
                var messageLength     = actualFrameLength - 2;
                var frame             = ExtractFrame(connection, input, startMessage, messageLength);
                input.SetReaderIndex(startMessage + actualFrameLength);
                return(frame);
            }

            input.ResetReaderIndex();

            // not a complete frame
            return(null);
        }
        /// <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="IByteBuf.ReaderIndex" />,
        ///     <see cref="IByteBuf.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="IByteBuf.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(IByteBuf 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: " + lengthFieldLength +
                                           " (expected: 1, 2, 3, 4, or 8)");
            }
            return(frameLength);
        }
示例#3
0
        protected long GetUnadjustedFrameLength(IByteBuf buf, int offset, int length)
        {
            long framelength;

            switch (length)
            {
            case 1:
                framelength = buf.GetByte(offset);
                break;

            case 2:
                framelength = buf.GetShort(offset);
                break;

            case 4:
                framelength = buf.GetInt(offset);
                break;

            case 8:
                framelength = buf.GetLong(offset);
                break;

            default:
                throw new DecoderException(string.Format("unsupported lengtFieldLength: {0} (expected: 1, 2, 4, or 8)", length));
            }
            return(framelength);
        }
示例#4
0
        private IByteBuf Decode(IConnection connection, IByteBuf input)
        {
            // we at least need to read our start character
            if (input.ReadableBytes < 1)
            {
                return(null);
            }

            input.MarkReaderIndex();

            // check start byte
            if (!input.ReadByte().Equals(_mllpStartCharacter))
            {
                throw new CorruptedFrameException(string.Format("Message doesn't start with: {0}", _mllpStartCharacter));
            }

            var startMessage = input.ReaderIndex;
            var length       = input.ReadableBytes;

            // search for our end characters
            for (var i = 0; i < length; i++)
            {
                if (input.ReadByte().Equals(_mllpFirstEndCharacter) &&
                    input.GetByte(input.ReaderIndex).Equals(_mllpLastEndCharacter))
                {
                    var frame = ExtractFrame(connection, input, startMessage, i);
                    input.SkipBytes(1); // advance over our last character
                    return(frame);
                }
            }


            input.ResetReaderIndex();

            // not a complete frame
            return(null);
        }
示例#5
0
 protected override byte _GetByte(int index)
 {
     return(_buffer.GetByte(index));
 }
示例#6
0
 public byte GetByte(int index)
 {
     return(_buf.GetByte(index));
 }
示例#7
0
        public override void ChannelRead(IChannelHandlerContext context, object message)
        {
            IByteBuf ibuff = message as IByteBuf;

            if (!login)
            {
                if (string.IsNullOrEmpty(ConfigHelper.Password))
                {
                    login = true;
                    LogHelper.DebugGreen("login success server password empty");
                    context.WriteAndFlushAsync(Unpooled.WrappedBuffer(Encoding.Default.GetBytes("1")));
                }
                else
                {
                    string pwd = Encoding.Default.GetString(ibuff.ToArray());
                    if (pwd.Equals(ConfigHelper.Password))
                    {
                        login = true;
                        LogHelper.DebugGreen("login success client password " + pwd);
                        context.WriteAndFlushAsync(Unpooled.WrappedBuffer(Encoding.Default.GetBytes("1")));
                    }
                    else //password error
                    {
                        context.WriteAndFlushAsync(Unpooled.WrappedBuffer(Encoding.Default.GetBytes("-1")));
                        LogHelper.DebugRed("login error client password " + pwd + " <> server password " + ConfigHelper.Password);
                    }
                }
                return;
            }

            int    idType   = ibuff.GetByte(0); //请求类型
            int    count    = ibuff.GetInt(1);  //请求个数
            string idString = null;

            if (idType == 0)
            {
                if (count == 1)
                {
                    idString = Guid.NewGuid().ToString();
                }
                else
                {
                    for (int i = 0; i < count; i++)
                    {
                        sb.Append(Guid.NewGuid().ToString());
                        if (i != count - 1)
                        {
                            sb.Append(",");
                        }
                    }
                    idString = sb.ToString();
                    sb.Clear();
                }
            }
            else if (idType == 1)
            {
                if (count == 1)
                {
                    idString = ObjectId.GenerateNewId().ToString();
                }
                else
                {
                    for (int i = 0; i < count; i++)
                    {
                        sb.Append(ObjectId.GenerateNewId().ToString());
                        if (i != count - 1)
                        {
                            sb.Append(",");
                        }
                    }
                    idString = sb.ToString();
                    sb.Clear();
                }
            }
            else if (idType == 2)
            {
                if (count == 1)
                {
                    idString = SnowflakeId.idWorker.NextId().ToString();
                }
                else
                {
                    for (int i = 0; i < count; i++)
                    {
                        sb.Append(SnowflakeId.idWorker.NextId().ToString());
                        if (i != count - 1)
                        {
                            sb.Append(",");
                        }
                    }
                    idString = sb.ToString();
                    sb.Clear();
                }
            }
            else if (idType == 3)
            {
                if (count == 1)
                {
                    idString = Base36Id.Base16.NewId();
                }
                else
                {
                    for (int i = 0; i < count; i++)
                    {
                        sb.Append(Base36Id.Base16.NewId());
                        if (i != count - 1)
                        {
                            sb.Append(",");
                        }
                    }
                    idString = sb.ToString();
                    sb.Clear();
                }
            }
            else if (idType == 4)
            {
                if (count == 1)
                {
                    idString = Base36Id.Base20.NewId();
                }
                else
                {
                    for (int i = 0; i < count; i++)
                    {
                        sb.Append(Base36Id.Base20.NewId());
                        if (i != count - 1)
                        {
                            sb.Append(",");
                        }
                    }
                    idString = sb.ToString();
                    sb.Clear();
                }
            }
            else if (idType == 5)
            {
                if (count == 1)
                {
                    idString = Base36Id.Base25.NewId();
                }
                else
                {
                    for (int i = 0; i < count; i++)
                    {
                        sb.Append(Base36Id.Base25.NewId());
                        if (i != count - 1)
                        {
                            sb.Append(",");
                        }
                    }
                    idString = sb.ToString();
                    sb.Clear();
                }
            }
            else if (idType == 6)
            {
                if (count == 1)
                {
                    idString = Guid.NewGuid().ToString("N");
                }
                else
                {
                    for (int i = 0; i < count; i++)
                    {
                        sb.Append(Guid.NewGuid().ToString("N"));
                        if (i != count - 1)
                        {
                            sb.Append(",");
                        }
                    }
                    idString = sb.ToString();
                    sb.Clear();
                }
            }
            else
            {
                idString = "0";
            }
            context.WriteAndFlushAsync(Unpooled.WrappedBuffer(Encoding.Default.GetBytes(idString)));
            LogHelper.DebugGreen("send id " + idString);
        }
示例#8
0
 protected override byte _GetByte(int index)
 {
     return(_buffer.GetByte(index + _adjustment));
 }
 protected long GetUnadjustedFrameLength(IByteBuf buf, int offset, int length)
 {
     long framelength;
     switch (length)
     {
         case 1:
             framelength = buf.GetByte(offset);
             break;
         case 2:
             framelength = buf.GetShort(offset);
             break;
         case 4:
             framelength = buf.GetInt(offset);
             break;
         case 8:
             framelength = buf.GetLong(offset);
             break;
         default:
             throw new DecoderException(string.Format("unsupported lengtFieldLength: {0} (expected: 1, 2, 4, or 8)", length));
     }
     return framelength;
 }
 /// <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="IByteBuf.ReaderIndex" />,
 ///     <see cref="IByteBuf.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="IByteBuf.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(IByteBuf 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: " + lengthFieldLength +
                                        " (expected: 1, 2, 3, 4, or 8)");
     }
     return frameLength;
 }