Esempio n. 1
0
        protected async Task PartialSendAsync(NetPeer peer, byte[] bytes)
        {
            var parts        = DataUtil.SplitBytes(bytes, Global.Config.MAX_PACKET_SIZE);
            var partialId    = Basic.GenID64();
            var totalPartNum = parts.Count();

            if (totalPartNum > 256)
            {
                Log.Error("send_bytes_too_long", peer.ConnId, totalPartNum);
                return;
            }
            for (short i = 0; i < parts.Count(); ++i)
            {
                var part       = parts.ElementAt(i);
                var partialBuf = Unpooled.DirectBuffer();
                partialBuf.WriteIntLE((int)OpCode.PARTIAL);
                partialBuf.WriteLongLE((long)partialId);
                partialBuf.WriteByte(i);
                partialBuf.WriteByte(parts.Count());
                partialBuf.WriteBytes(part);
                peer.Send(partialBuf);
                //partialBuf.Release();
                await Task.Delay(10);

                Log.Info("send_part", i, parts.Count(), part.Length);
            }
        }
Esempio n. 2
0
        public void TestCopyingToOutputStream()
        {
            IByteBuffer buf1 = Unpooled.DirectBuffer(10);
            IByteBuffer buf2 = Unpooled.Buffer(10);
            IByteBuffer buf3 = Unpooled.DirectBuffer(10);

            buf1.WriteBytes(Encoding.ASCII.GetBytes("a"));
            buf2.WriteBytes(Encoding.ASCII.GetBytes("b"));
            buf3.WriteBytes(Encoding.ASCII.GetBytes("c"));
            IByteBuffer composite   = Unpooled.WrappedUnmodifiableBuffer(buf1, buf2, buf3);
            IByteBuffer copy        = Unpooled.DirectBuffer(3);
            IByteBuffer copy2       = Unpooled.Buffer(3);
            var         copyStream  = new ByteBufferStream(copy);
            var         copy2Stream = new ByteBufferStream(copy2);

            try
            {
                composite.GetBytes(0, copyStream, 3);
                composite.GetBytes(0, copy2Stream, 3);
                Assert.Equal(0, ByteBufferUtil.Compare(copy, composite));
                Assert.Equal(0, ByteBufferUtil.Compare(copy2, composite));
                Assert.Equal(0, ByteBufferUtil.Compare(copy, copy2));
            }
            finally
            {
                copy.Release();
                copy2.Release();
                copyStream.Close();
                copy2Stream.Close();
                composite.Release();
            }
        }
Esempio n. 3
0
        public void TestHasNoMemoryAddressWhenMultipleBuffers()
        {
            IByteBuffer buf1 = Unpooled.DirectBuffer(10);

            if (!buf1.HasMemoryAddress)
            {
                buf1.Release();
                return;
            }

            IByteBuffer buf2 = Unpooled.DirectBuffer(10);
            IByteBuffer buf  = NewBuffer(buf1, buf2);

            Assert.False(buf.HasMemoryAddress);
            try
            {
                buf.GetPinnableMemoryAddress();
                Assert.False(true);
            }
            catch (NotSupportedException)
            {
                // expected
            }
            finally
            {
                buf.Release();
            }
        }
Esempio n. 4
0
        public void Register()
        {
            var buffer = Unpooled.DirectBuffer();

            buffer.WriteIntLE((int)OpCode.REGISTER_REQ);
            buffer.WriteIntLE((int)Global.Host.Id);
            buffer.WriteBytes(Encoding.UTF8.GetBytes(Global.Host.UniqueName));
            this.Send(buffer.ToArray());
        }
Esempio n. 5
0
        public byte[] Pack()
        {
            var buf = Unpooled.DirectBuffer();

            buf.WriteIntLE((int)this.ProtoCode);
            buf.WriteLongLE((long)this.Id);
            //buf.WriteIntLE((int)this.FromHostId);
            buf.WriteIntLE((int)this.FromActorId);
            buf.WriteIntLE((int)this.ToActorId);
            buf.WriteBytes(this.Payload);
            return(buf.ToArray());
        }
Esempio n. 6
0
        public override byte[] Pack()
        {
            var buf = Unpooled.DirectBuffer();

            buf.WriteIntLE((int)this.ProtoCode);
            buf.WriteLongLE((long)this.Id);
            buf.WriteLongLE((long)this.FromActorId);
            buf.WriteLongLE((long)this.ToActorId);
            buf.WriteBytes(this.Payload);
            var bytes = buf.ToArray();

            //buf.Release();
            return(bytes);
        }
Esempio n. 7
0
        public void TestHasMemoryAddressWithSingleBuffer()
        {
            IByteBuffer buf1 = Unpooled.DirectBuffer(10);

            if (!buf1.HasMemoryAddress)
            {
                buf1.Release();
                return;
            }
            IByteBuffer buf = NewBuffer(buf1);

            Assert.True(buf.HasMemoryAddress);
            Assert.Equal(buf1.GetPinnableMemoryAddress(), buf.GetPinnableMemoryAddress());
            buf.Release();
        }
Esempio n. 8
0
        /**
         * count+timestamp+dataLen+data
         *
         * @param count
         * @return
         */
        public IByteBuffer rttMsg(int count)
        {
            IByteBuffer buf = Unpooled.DirectBuffer(10);

            buf.WriteShort(count);
            buf.WriteInt((int)(KcpUntils.currentMs() - startTime));

            //int dataLen = new Random().nextInt(200);
            //buf.writeBytes(new byte[dataLen]);

            int dataLen = data.ReadableBytes;

            buf.WriteShort(dataLen);
            buf.WriteBytes(data, data.ReaderIndex, dataLen);
            return(buf);
        }
Esempio n. 9
0
        public void TestExtractNioBuffers()
        {
            IByteBuffer buf1 = Unpooled.DirectBuffer(10);
            IByteBuffer buf2 = Unpooled.Buffer(10);
            IByteBuffer buf3 = Unpooled.DirectBuffer(10);

            buf1.WriteBytes(Encoding.ASCII.GetBytes("a"));
            buf2.WriteBytes(Encoding.ASCII.GetBytes("b"));
            buf3.WriteBytes(Encoding.ASCII.GetBytes("c"));
            IByteBuffer composite   = Unpooled.WrappedUnmodifiableBuffer(buf1, buf2, buf3);
            var         byteBuffers = composite.GetIoBuffers(0, 3);

            Assert.Equal(3, byteBuffers.Length);
            Assert.Single(byteBuffers[0]);
            Assert.Single(byteBuffers[1]);
            Assert.Single(byteBuffers[2]);
            composite.Release();
        }
Esempio n. 10
0
        public KcpRttExampleClient()
        {
            data = Unpooled.DirectBuffer(200);
            for (int i = 0; i < data.Capacity; i++)
            {
                data.WriteByte((byte)i);
            }

            rtts = new int[300];
            for (int i = 0; i < rtts.Length; i++)
            {
                rtts[i] = -1;
            }

            timer20.Enabled  = true;
            timer20.Interval = 20;
            timer20.Start();
            startTime = KcpUntils.currentMs();
        }
Esempio n. 11
0
        public void MultipartRequest()
        {
            string BOUNDARY = "01f136d9282f";

            byte[] bodyBytes = Encoding.UTF8.GetBytes("--" + BOUNDARY + "\n" +
                                                      "Content-Disposition: form-data; name=\"msg_id\"\n" +
                                                      "\n" +
                                                      "15200\n" +
                                                      "--" + BOUNDARY + "\n" +
                                                      "Content-Disposition: form-data; name=\"msg\"\n" +
                                                      "\n" +
                                                      "test message\n" +
                                                      "--" + BOUNDARY + "--");
            IByteBuffer byteBuf = Unpooled.DirectBuffer(bodyBytes.Length);

            byteBuf.WriteBytes(bodyBytes);

            IFullHttpRequest req = new DefaultFullHttpRequest(HttpVersion.Http10, HttpMethod.Post, "/up", byteBuf);

            req.Headers.Add(HttpHeaderNames.ContentType, "multipart/form-data; boundary=" + BOUNDARY);

            HttpPostRequestDecoder decoder =
                new HttpPostRequestDecoder(new DefaultHttpDataFactory(DefaultHttpDataFactory.MinSize),
                                           req,
                                           Encoding.UTF8);

            Assert.True(decoder.IsMultipart);
            Assert.NotEmpty(decoder.GetBodyHttpDatas());
            Assert.Equal(2, decoder.GetBodyHttpDatas().Count);

            IAttribute attrMsg = (IAttribute)decoder.GetBodyHttpData("msg");

            Assert.True(attrMsg.GetByteBuffer().IsDirect);
            Assert.Equal("test message", attrMsg.Value);
            IAttribute attrMsgId = (IAttribute)decoder.GetBodyHttpData("msg_id");

            Assert.True(attrMsgId.GetByteBuffer().IsDirect);
            Assert.Equal("15200", attrMsgId.Value);

            decoder.Destroy();
            Assert.True(req.Release());
        }
Esempio n. 12
0
        public void DecodeFullHttpRequestWithUrlEncodedBody()
        {
            byte[]      bodyBytes = Encoding.UTF8.GetBytes("foo=bar&a=b&empty=&city=%3c%22new%22%20york%20city%3e&other_city=los+angeles");
            IByteBuffer content   = Unpooled.DirectBuffer(bodyBytes.Length);

            content.WriteBytes(bodyBytes);

            IFullHttpRequest       req     = new DefaultFullHttpRequest(HttpVersion.Http11, HttpMethod.Post, "/", content);
            HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(req);

            Assert.NotEmpty(decoder.GetBodyHttpDatas());

            Assert.NotEmpty(decoder.GetBodyHttpDatas());
            Assert.Equal(5, decoder.GetBodyHttpDatas().Count);

            IAttribute attr = (IAttribute)decoder.GetBodyHttpData("foo");

            Assert.True(attr.GetByteBuffer().IsDirect);
            Assert.Equal("bar", attr.Value);

            attr = (IAttribute)decoder.GetBodyHttpData("a");
            Assert.True(attr.GetByteBuffer().IsDirect);
            Assert.Equal("b", attr.Value);

            attr = (IAttribute)decoder.GetBodyHttpData("empty");
            Assert.True(attr.GetByteBuffer().IsDirect);
            Assert.Equal("", attr.Value);

            attr = (IAttribute)decoder.GetBodyHttpData("city");
            Assert.True(attr.GetByteBuffer().IsDirect);
            Assert.Equal("<\"new\" york city>", attr.Value);

            attr = (IAttribute)decoder.GetBodyHttpData("other_city");
            Assert.True(attr.GetByteBuffer().IsDirect);
            Assert.Equal("los angeles", attr.Value);

            decoder.Destroy();
            req.Release();
        }
Esempio n. 13
0
        public void DecodeFullHttpRequestWithUrlEncodedBodyWithInvalidHexNibbleLo()
        {
            byte[]      bodyBytes = Encoding.UTF8.GetBytes("foo=bar&a=b&empty=%2g&city=london");
            IByteBuffer content   = Unpooled.DirectBuffer(bodyBytes.Length);

            content.WriteBytes(bodyBytes);

            IFullHttpRequest req = new DefaultFullHttpRequest(HttpVersion.Http11, HttpMethod.Post, "/", content);

            try
            {
                new HttpPostRequestDecoder(req);
                Assert.False(true); // Was expecting an ErrorDataDecoderException
            }
            catch (ErrorDataDecoderException e)
            {
                Assert.Equal("Invalid hex byte at index '0' in string: '%2g'", e.Message);
            }
            finally
            {
                req.Release();
            }
        }
Esempio n. 14
0
        public void TestCopyingToOtherBuffer()
        {
            IByteBuffer buf1 = Unpooled.DirectBuffer(10);
            IByteBuffer buf2 = Unpooled.Buffer(10);
            IByteBuffer buf3 = Unpooled.DirectBuffer(10);

            buf1.WriteBytes(Encoding.ASCII.GetBytes("a"));
            buf2.WriteBytes(Encoding.ASCII.GetBytes("b"));
            buf3.WriteBytes(Encoding.ASCII.GetBytes("c"));
            IByteBuffer composite = Unpooled.WrappedUnmodifiableBuffer(buf1, buf2, buf3);
            IByteBuffer copy      = Unpooled.DirectBuffer(3);
            IByteBuffer copy2     = Unpooled.Buffer(3);

            copy.SetBytes(0, composite, 0, 3);
            copy2.SetBytes(0, composite, 0, 3);
            copy.SetWriterIndex(3);
            copy2.SetWriterIndex(3);
            Assert.Equal(0, ByteBufferUtil.Compare(copy, composite));
            Assert.Equal(0, ByteBufferUtil.Compare(copy2, composite));
            Assert.Equal(0, ByteBufferUtil.Compare(copy, copy2));
            copy.Release();
            copy2.Release();
            composite.Release();
        }
Esempio n. 15
0
        public void TestNioBuffersExpand()
        {
            TestChannel channel = new TestChannel();

            ChannelOutboundBuffer buffer = new ChannelOutboundBuffer(channel);

            IByteBuffer buf = Unpooled.DirectBuffer().WriteBytes(Encoding.ASCII.GetBytes("buf1"));

            for (int i = 0; i < 64; i++)
            {
                buffer.AddMessage(buf.Copy(), buf.ReadableBytes, channel.VoidPromise());
            }
            Assert.Equal(0, buffer.NioBufferCount); // Should still be 0 as not flushed yet
            buffer.AddFlush();
            var buffers = buffer.GetSharedBufferList();

            Assert.Equal(64, buffer.NioBufferCount);
            for (int i = 0; i < buffer.NioBufferCount; i++)
            {
                Assert.Equal(buffers[i], buf.GetIoBuffer(buf.ReaderIndex, buf.ReadableBytes));
            }
            Release(buffer);
            buf.Release();
        }
Esempio n. 16
0
 public void NotLeakDirectBufferWhenWrapIllegalArgumentException()
 {
     Assert.Throws <ErrorDataDecoderException>(() => NotLeakWhenWrapIllegalArgumentException(Unpooled.DirectBuffer()));
 }
Esempio n. 17
0
        public void ChunkCorrect()
        {
            const string Payload = "town=794649819&town=784444184&town=794649672&town=794657800&town=" +
                                   "794655734&town=794649377&town=794652136&town=789936338&town=789948986&town=" +
                                   "789949643&town=786358677&town=794655880&town=786398977&town=789901165&town=" +
                                   "789913325&town=789903418&town=789903579&town=794645251&town=794694126&town=" +
                                   "794694831&town=794655274&town=789913656&town=794653956&town=794665634&town=" +
                                   "789936598&town=789904658&town=789899210&town=799696252&town=794657521&town=" +
                                   "789904837&town=789961286&town=789958704&town=789948839&town=789933899&town=" +
                                   "793060398&town=794659180&town=794659365&town=799724096&town=794696332&town=" +
                                   "789953438&town=786398499&town=794693372&town=789935439&town=794658041&town=" +
                                   "789917595&town=794655427&town=791930372&town=794652891&town=794656365&town=" +
                                   "789960339&town=794645586&town=794657688&town=794697211&town=789937427&town=" +
                                   "789902813&town=789941130&town=794696907&town=789904328&town=789955151&town=" +
                                   "789911570&town=794655074&town=789939531&town=789935242&town=789903835&town=" +
                                   "789953800&town=794649962&town=789939841&town=789934819&town=789959672&town=" +
                                   "794659043&town=794657035&town=794658938&town=794651746&town=794653732&town=" +
                                   "794653881&town=786397909&town=794695736&town=799724044&town=794695926&town=" +
                                   "789912270&town=794649030&town=794657946&town=794655370&town=794659660&town=" +
                                   "794694617&town=799149862&town=789953234&town=789900476&town=794654995&town=" +
                                   "794671126&town=789908868&town=794652942&town=789955605&town=789901934&town=" +
                                   "789950015&town=789937922&town=789962576&town=786360170&town=789954264&town=" +
                                   "789911738&town=789955416&town=799724187&town=789911879&town=794657462&town=" +
                                   "789912561&town=789913167&town=794655195&town=789938266&town=789952099&town=" +
                                   "794657160&town=789949414&town=794691293&town=794698153&town=789935636&town=" +
                                   "789956374&town=789934635&town=789935475&town=789935085&town=794651425&town=" +
                                   "794654936&town=794655680&town=789908669&town=794652031&town=789951298&town=" +
                                   "789938382&town=794651503&town=794653330&town=817675037&town=789951623&town=" +
                                   "789958999&town=789961555&town=794694050&town=794650241&town=794656286&town=" +
                                   "794692081&town=794660090&town=794665227&town=794665136&town=794669931";

            var defaultHttpRequest = new DefaultHttpRequest(HttpVersion.Http11, HttpMethod.Post, "/");

            var decoder = new HttpPostRequestDecoder(defaultHttpRequest);

            const int FirstChunk  = 10;
            const int MiddleChunk = 1024;

            byte[] payload1 = Encoding.UTF8.GetBytes(Payload.Substring(0, FirstChunk));
            byte[] payload2 = Encoding.UTF8.GetBytes(Payload.Substring(FirstChunk, MiddleChunk));
            byte[] payload3 = Encoding.UTF8.GetBytes(Payload.Substring(FirstChunk + MiddleChunk, MiddleChunk));
            byte[] payload4 = Encoding.UTF8.GetBytes(Payload.Substring(FirstChunk + MiddleChunk * 2));

            IByteBuffer buf1 = Unpooled.DirectBuffer(payload1.Length);
            IByteBuffer buf2 = Unpooled.DirectBuffer(payload2.Length);
            IByteBuffer buf3 = Unpooled.DirectBuffer(payload3.Length);
            IByteBuffer buf4 = Unpooled.DirectBuffer(payload4.Length);

            buf1.WriteBytes(payload1);
            buf2.WriteBytes(payload2);
            buf3.WriteBytes(payload3);
            buf4.WriteBytes(payload4);

            decoder.Offer(new DefaultHttpContent(buf1));
            decoder.Offer(new DefaultHttpContent(buf2));
            decoder.Offer(new DefaultHttpContent(buf3));
            decoder.Offer(new DefaultLastHttpContent(buf4));

            Assert.NotEmpty(decoder.GetBodyHttpDatas());
            Assert.Equal(139, decoder.GetBodyHttpDatas().Count);

            IAttribute attr = (IAttribute)decoder.GetBodyHttpData("town");

            Assert.Equal("794649819", attr.Value);

            decoder.Destroy();
            buf1.Release();
            buf2.Release();
            buf3.Release();
            buf4.Release();
        }
Esempio n. 18
0
 /// <summary>
 /// Creates a new big-endian direct buffer with the specified <see cref="IByteBuffer.Capacity"/>, which
 /// expands its capacity boundlessly on demand. The new buffer's <see cref="IByteBuffer.ReaderIndex"/> and
 /// <see cref="IByteBuffer.WriterIndex"/> are <c>0</c>.
 /// </summary>
 /// <param name="initialCapacity"></param>
 /// <returns></returns>
 public static IByteBuffer DirectBuffer(int initialCapacity)
 {
     return(Unpooled.DirectBuffer(initialCapacity));
 }