示例#1
0
        static IDisposable RedirectOut()
        {
            var oldOut = Console.Out;

            var newOutFs = File.Open(@".\log.txt", FileMode.Create, FileAccess.Write, FileShare.Read);
            var newOut   = new StreamWriter(newOutFs);

            var echo = new EchoStream(oldOut, newOut);

            Console.SetOut(echo);

            return(echo);
        }
示例#2
0
        public Stream DownloadObject(string fileName)
        {
            var stream = new EchoStream(maxQueueDepth: 32)
            {
                ReadTimeout = (int)TimeSpan.FromSeconds(100).TotalMilliseconds
            };

            stream.TaskToWatch(_client.DownloadObjectAsync(
                                   _bucketName,
                                   fileName,
                                   destination: stream,
                                   new DownloadObjectOptions
            {
                ChunkSize = (int)DownloadChunkSize.GetValue(SizeUnit.Bytes)
            },
                                   cancellationToken: CancellationToken));

            return(stream);
        }
示例#3
0
        GetGmAgentConnections()
        {
            Stream agentToGmStream = new EchoStream();
            Stream gmToAgentStream = new EchoStream();
            Stream agentSideStream = new StreamRWJoin(gmToAgentStream, agentToGmStream);
            Stream gmSideStream    = new StreamRWJoin(agentToGmStream, gmToAgentStream);
            var    agentQueueAdapter
                = new SenderReceiverQueueAdapter(
                      new StreamMessageSenderReceiver(
                          agentSideStream,
                          new Parser()));
            var gmQueueAdapter
                = new SenderReceiverQueueAdapter(
                      new StreamMessageSenderReceiver(
                          gmSideStream,
                          new Parser()));

            return(agentQueueAdapter, gmQueueAdapter);
        }
        public void TestCallbackCalledOnParseError()
        {
            Exception receivedException = null;
            Stream    agentToGmStream   = new EchoStream();
            Stream    gmToAgentStream   = new EchoStream();
            Stream    agentSideStream   = new StreamRWJoin(gmToAgentStream, agentToGmStream);
            Stream    gmSideStream      = new StreamRWJoin(agentToGmStream, gmToAgentStream);

            StreamMessageSenderReceiver agentSenderReceiver
                = new StreamMessageSenderReceiver(agentSideStream, new Parser());
            Semaphore semaphore = new Semaphore(0, 100);

            agentSenderReceiver.StartReceiving(message => { }, e =>
                                               { receivedException = e; semaphore.Release(); });

            string messageText = System.Text.Json.JsonSerializer.Serialize(new { a = "abc" });

            byte[] messageBytes       = Encoding.UTF8.GetBytes(messageText);
            byte[] messageLengthBytes = BitConverter.GetBytes((ushort)messageBytes.Length);
            if (!BitConverter.IsLittleEndian)
            {
                Array.Reverse(messageLengthBytes);
            }

            //when
            gmSideStream.Write(messageLengthBytes, 0, 2);
            gmSideStream.Write(messageBytes, 0, messageBytes.Length);
            semaphore.WaitOne();

            //then
            Assert.IsNotNull(receivedException);
            Assert.IsInstanceOfType(receivedException, typeof(CommunicationLibrary.Exceptions.ParsingException));

            //after
            agentSenderReceiver.Dispose();
            gmSideStream.Close();
            semaphore.Dispose();
        }
        public void TestStreamMessageSenderReceiverCanUseParser()
        {
            Stream stream = new EchoStream();

            Message <JoinGameRequest> expected = new Message <JoinGameRequest>
                                                     (new JoinGameRequest {
                TeamId = "Hello"
            });
            StreamMessageSenderReceiver streamMessageSenderReceiver
                = new StreamMessageSenderReceiver(stream, new Parser());

            BlockingCollection <Message> messages = new BlockingCollection <Message>();

            streamMessageSenderReceiver.StartReceiving(message => messages.Add(message));

            //when
            streamMessageSenderReceiver.Send(expected);
            Message received = messages.Take();

            //then
            Assert.AreEqual(expected.MessagePayload.TeamId,
                            ((Message <JoinGameRequest>)received).MessagePayload.TeamId);
        }
        public void TestCanTransferMessage()
        {
            Stream stream = new EchoStream();

            Message <JoinGameRequest> expected = new Message <JoinGameRequest>
                                                     (new JoinGameRequest {
                TeamId = "Hello"
            });

            SenderReceiverQueueAdapter adapter
                = new SenderReceiverQueueAdapter(
                      new StreamMessageSenderReceiver(stream, new Parser()));

            //when
            adapter.Send(expected);
            Message received = adapter.Take();

            //then
            Assert.AreEqual(expected.MessagePayload.TeamId,
                            ((Message <JoinGameRequest>)received).MessagePayload.TeamId);

            //after
            adapter.Dispose();
        }