public void SendDelimitedMessage() { var payloadMultiplier = 10; // Arrange CreateAndStartServer(IPAddress.Any, TestPort, 0, new TestDelimiterValidator()); var clientIdx = CreateAndStartClient(IPAddress.Parse(TestIpAdress), TestPort, 500, 0, new TestDelimiterValidator()); WaitForConnectionState(clientIdx, new TimeSpan(0, 0, 0, 5), BinaryConnectionState.Connected); // Act SendMessages(1, 1, payloadMultiplier, ServerConnections, "ClientIdx"); Assert.IsTrue(WaitForMessageReception(new TimeSpan(0, 0, 0, 5), 1, Clients)); var payload = Clients[0].Received[0].Payload; BinaryMessage published = null; Array.Copy(payload, 0, _context.ReadBuffer, 0, payload.Length); _interpreter.ProcessReadBytes(_context, payload.Length, m => published = m); // Assert Assert.IsFalse(_context.StartFound); Assert.AreEqual(0, _context.CurrentIndex); Assert.NotNull(published); Assert.AreEqual(TestDelimiterInterpreter.TestStartDelimiter.Length + payloadMultiplier * 4 + EndDelimiterOnlyInterpreter.TestEndDelimiter.Length, published.Payload.Length); }
public void ParsePartialDelimiterMessage(bool useStartDelimiter) { if (!useStartDelimiter) { _interpreter = new EndDelimiterOnlyInterpreter(); } var text = Encoding.Unicode.GetBytes("Wie passend, du kämpfst"); // Arrange var message = new List <byte>(); message.AddRange(TestDelimiterInterpreter.TestStartDelimiter); message.AddRange(text); var readMessage = message.ToArray(); // Act Array.Copy(readMessage, 0, _context.ReadBuffer, 0, readMessage.Length); _interpreter.ProcessReadBytes(_context, readMessage.Length, m => { }); // Assert Assert.IsTrue(_context.StartFound); Assert.AreEqual(readMessage.Length, _context.CurrentIndex); }
private void ReadComplete(IAsyncResult ar) { int read; if (_disconnected) { return; } try { read = _stream.EndRead(ar); } catch (Exception ex) { Disconnect(ex); return; } var transmission = (IReadContext)ar.AsyncState; ByteReadResult result; if (read > 0) { result = _interpreter.ProcessReadBytes(transmission, read, PublishMessage); } else { Disconnect(); return; } // Error in stream - send last will and close transmission if (result == ByteReadResult.Failure) { byte[] lastWill; if (_interpreter.ErrorResponse(transmission, out lastWill)) { _stream.Write(lastWill, 0, lastWill.Length); } Disconnect(new InvalidHeaderException("Header invalid or no matching validator found!")); return; } BeginRead(transmission); }
public void ParseHtmlMessageAndUseHtmlInterpreter() { // Arrange _interpreter = HtmlInterpreter.Instance; _context = (DelimitedMessageContext)_interpreter.CreateContext(); var text = Encoding.UTF8.GetBytes(Input); // Act BinaryMessage published = null; Array.Copy(text, 0, _context.ReadBuffer, _context.CurrentIndex, text.Length); _interpreter.ProcessReadBytes(_context, text.Length, m => published = m); // Assert Assert.NotNull(published); Assert.AreEqual(published.Payload, Encoding.UTF8.GetBytes(Output)); }