public void ParseIncomingData(int bytesTransfered) { var token = new NetDataToken(); for (int i = 0; !token.IsMessageComplete; i++) { byte[] incomingBuffer = this._buffer.Skip(i * bytesTransfered).Take(bytesTransfered).ToArray(); this._packetParser.ParseIncomingData(token, incomingBuffer, bytesTransfered); } Assert.Equal(this._packetProcessor.HeaderSize, token.ReceivedHeaderBytesCount); Assert.Equal(this._packetProcessor.HeaderSize, token.HeaderData.Length); Assert.Equal(this._bufferHeader, token.HeaderData); Assert.NotNull(token.MessageSize); Assert.Equal(this._messageSize, token.MessageSize); Assert.Equal(this._messageContentSize, token.ReceivedMessageBytesCount); Assert.True(token.IsMessageComplete); Assert.Equal(this._bufferContent, token.MessageData); Assert.Equal(this._messageContent, Encoding.UTF8.GetString(token.MessageData.Skip(sizeof(int)).ToArray())); token.Reset(); Assert.Null(token.HeaderData); Assert.Null(token.MessageData); Assert.Null(token.MessageSize); Assert.Equal(0, token.ReceivedHeaderBytesCount); Assert.Equal(0, token.ReceivedMessageBytesCount); Assert.False(token.IsMessageComplete); }
public void ParseIncomingDataWithInvalidSizeTest() { _packetParser = new NetPacketParser(_packetProcessor); var token = new NetDataToken(); Assert.Throws <InvalidOperationException>(() => _packetParser.ParseIncomingData(token, _invalidBuffer, 32)); }
public void ParseIncomingDataWithHeaderTest(int bytesTransfered) { _packetParser = new NetPacketParser(new DefaultNetPacketProcessor(includeHeader: true)); var token = new NetDataToken(); int numberOfReceivesNeeded = (_buffer.Length / bytesTransfered) + 1; var receviedMessages = new List <byte[]>(); for (int i = 0; i < numberOfReceivesNeeded; i++) { byte[] incomingBuffer = _buffer.Skip(i * bytesTransfered).Take(bytesTransfered).ToArray(); IEnumerable <byte[]> messages = _packetParser.ParseIncomingData(token, incomingBuffer, Math.Min(bytesTransfered, incomingBuffer.Length)); if (messages.Any()) { receviedMessages.AddRange(messages); } } Assert.Single(receviedMessages); string messageContent = Encoding.UTF8.GetString(_buffer); Assert.Equal(messageContent, Encoding.UTF8.GetString(receviedMessages.Single().ToArray())); }
public NetMessageDispatcherTest() { this._randomizer = new Randomizer(); this._clientMock = new CustomClientMock(); byte[] messageData = this._randomizer.Bytes(this._randomizer.Byte()); this._netDataToken = new NetDataToken { HeaderData = BitConverter.GetBytes((long)messageData.Length), MessageData = messageData, MessageSize = messageData.Length }; }
/// <inheritdoc /> public void DispatchMessage(INetUser client, NetDataToken token) { var bufferSize = this.PacketProcessor.IncludeHeader ? this.PacketProcessor.HeaderSize + token.MessageSize.Value : token.MessageSize.Value; var buffer = new byte[bufferSize]; if (this.PacketProcessor.IncludeHeader) { Array.Copy(token.HeaderData, 0, buffer, 0, this.PacketProcessor.HeaderSize); Array.Copy(token.MessageData, 0, buffer, this.PacketProcessor.HeaderSize, token.MessageSize.Value); } else { Array.Copy(token.MessageData, 0, buffer, 0, token.MessageSize.Value); } Task.Run(() => { using (INetPacketStream packet = this.PacketProcessor.CreatePacket(buffer)) client.HandleMessage(packet); }); }
public void ParseIncomingData_WithInvalidSize() { var token = new NetDataToken(); Assert.Throws <InvalidOperationException>(() => this._packetParser.ParseIncomingData(token, this._invalidBuffer, 32)); }