Ejemplo n.º 1
0
        /// <summary>
        /// Writes a message to a stream
        /// </summary>
        public async Task Write(Stream stream, TmqMessage message)
        {
            WriteType(stream, DataType.Insert);
            await WriteId(stream, message.MessageId);

            message.Content.Position = 0;

            await using MemoryStream ms = new MemoryStream();
            await _writer.Write(message, ms);

            ms.Position = 0;

            await WriteContent(Convert.ToInt32(ms.Length), ms, stream);
        }
Ejemplo n.º 2
0
        public void DisconnectDueToPingTimeout()
        {
            TestMqServer server = new TestMqServer();

            server.Initialize(42105);
            server.Start();

            TcpClient client = new TcpClient();

            client.Connect("127.0.0.1", 42105);

            NetworkStream stream = client.GetStream();

            stream.Write(PredefinedMessages.PROTOCOL_BYTES);
            TmqMessage msg = new TmqMessage();

            msg.Type        = MessageType.Server;
            msg.ContentType = KnownContentTypes.Hello;
            msg.SetStringContent("GET /\r\nName: Test-42105");
            msg.CalculateLengths();
            TmqWriter writer = new TmqWriter();

            writer.Write(msg, stream).Wait();
            Thread.Sleep(1000);
            Assert.Equal(1, server.ClientConnected);

            ThreadPool.UnsafeQueueUserWorkItem(async s =>
            {
                byte[] buffer = new byte[128];
                while (client.Connected)
                {
                    int r = await s.ReadAsync(buffer);
                    if (r == 0)
                    {
                        client.Dispose();
                        break;
                    }
                }
            }, stream, false);

            Thread.Sleep(25000);

            Assert.False(client.Connected);
            Assert.Equal(1, server.ClientDisconnected);
        }