public void MainTest()
        {
            AsyncPump.Run(
                async() =>
            {
                var writtenBytes = new byte[this.Random.Next(3072, 10241)];
                this.Random.NextBytes(writtenBytes);

                using (var output = new MemoryStream())
                {
                    using (var framer = new GlowOutput(1024, 0, (s, e) => output.Write(e.FramedPackage, 0, e.FramedPackageLength)))
                    {
                        framer.WriteBytes(writtenBytes);
                        framer.Finish();
                    }

                    output.Position = 0;
                    var reader      = new S101Reader(output.ReadAsync, 1024);
                    Assert.IsTrue(await reader.ReadAsync(CancellationToken.None));
                    Assert.IsInstanceOfType(reader.Message.Command, typeof(EmberData));

                    using (var input = new MemoryStream())
                    {
                        await reader.Payload.CopyToAsync(input);
                        CollectionAssert.AreEqual(writtenBytes, input.ToArray());
                    }

                    await reader.DisposeAsync(CancellationToken.None);
                }
            });
        }
示例#2
0
        void AcceptCallback(IAsyncResult result)
        {
            var listener = (TcpListener)result.AsyncState;

            try
            {
                var socket = listener.EndAcceptSocket(result);
                var client = new Client(socket, this);

                lock (_sync)
                    _clients.Add(client);

                if (_glow != null)
                {
                    var glowOutPut = new GlowOutput(0, (_, e) => socket.Send(e.FramedPackage));

                    using (glowOutPut)
                    {
                        _glow.Encode(glowOutPut);

                        glowOutPut.Finish();
                    }
                }

                listener.BeginAcceptSocket(AcceptCallback, listener);
            }
            catch
            {
            }
        }
        private static Task TestGlowOutput(byte[] block, int blockCount)
        {
            using (var output = new GlowOutput(1024, 0, (s, e) => { }))
            {
                for (int index = 0; index < blockCount; ++index)
                {
                    output.WriteBytes(block);
                }

                output.Finish();
            }

            return(Task.FromResult(false));
        }
        void saveWithFramingButton_Click(object sender, RoutedEventArgs e)
        {
            var dialog = new SaveFileDialog
            {
                Filter = S101FileDialogFilter,
            };

            if (dialog.ShowDialog() == true)
            {
                try
                {
                    var glow = EncodeGlow();

                    if (glow != null)
                    {
                        using (var stream = dialog.OpenFile())
                        {
                            var output = new GlowOutput(
                                0,
                                (_, packageArgs) =>
                            {
                                stream.Write(packageArgs.FramedPackage, 0, packageArgs.FramedPackageLength);
                            });

                            using (output)
                            {
                                glow.Encode(output);

                                output.Finish();
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: " + ex.Message);
                }
            }
        }