public void TestMessageChecksum() { byte[] bytes = new byte[] { 4, 6, 4, 6, 6, 46, 6, 34, 64, 2, 64, 62, 47, 27, 247, 4, 6, 4, 6, 6, 46, 6, 34, 64, 2, 64, 62, 47, 27, 247 }; byte[] bigBytes = new byte[8 * 1024]; Array.Copy(bytes, bigBytes, bytes.Length); var calculatedChecksum = CoderHelper.CalculateChecksum(bytes, 0, bytes.Length); var newLength = CoderHelper.AddChecksum(bigBytes, 0, bytes.Length) + bytes.Length; byte checksumInBigBytes = bigBytes[bytes.Length]; Assert.AreEqual(checksumInBigBytes, calculatedChecksum); var writer = new MessageStreamWriter(); var reader = new MessageStreamReader(); var loginRequest = new LoginRequest() { accessToken = "alsdjflakjsdf", username = "******" }; var matchRequest = new MatchRequest() { value1 = 1, value2 = 2, value3 = 3, value4 = 4 }; writer.Write(loginRequest); writer.Write(matchRequest); writer.Use((buffer, len) => { reader.Add(buffer, len); var message = reader.Decode(); Assert.IsTrue(message.Value.Is(200)); Assert.AreEqual(loginRequest, message.Value.Parse <LoginRequest>()); message = reader.Decode(); Assert.IsTrue(message.Value.Is(201)); Assert.AreEqual(matchRequest, message.Value.Parse <MatchRequest>()); writer.DidWrite(len); }); writer.Write(loginRequest); writer.Write(matchRequest); writer.currentBuffer[0] = 23; writer.Use((buffer, len) => { reader.Add(buffer, len); var message = reader.Decode(); Assert.IsFalse(message.HasValue); Assert.IsTrue(message == null); message = reader.Decode(); Assert.IsTrue(message.Value.Is(201)); Assert.AreEqual(matchRequest, message.Value.Parse <MatchRequest>()); writer.DidWrite(len); }); }
public void TestMultithreadReadAndWrite() { var loginRequest = new LoginRequest() { accessToken = "askldfaljksdf", username = "******" }; var reader = new MessageStreamReader(); var writer = new MessageStreamWriter(); for (int reading = 0; reading < 50; reading++) { for (int write = 0; write < 2; write++) { writer.Write(loginRequest); Logger.Log($"Did Write loginRequest {write * reading}"); } writer.Use((buffer, len) => { Logger.Log($"Using buffer with len: {len}"); reader.Add(buffer, len); MessageContainer?message = null; while ((message = reader.Decode()).HasValue) { Logger.Log($"Decoded message: {message.Value.type}"); Assert.AreEqual(loginRequest, message.Value.Parse <LoginRequest>()); } writer.DidWrite(len); Logger.Log($"Did Write len: {len}"); }); } Assert.AreEqual(0, writer.currentBufferLength); Assert.AreEqual(0, reader.currentBufferLength); }
public void TestPartialStreamingDecoding() { var firstToken = "asldkfjalksdjfalkjsdf"; var username = "******"; var secondToken = "asdlkfalksjdgklashdioohweg"; var ip = "10.0.0.1"; var port = (short)6109; var loginRequest = new LoginRequest { accessToken = firstToken, username = username }; var matchRequest = new MatchRequest(); var connectRequest = new ConnectGameInstanceResponse { token = secondToken, ip = ip, port = port }; var encoder = new MessageStreamWriter(); List <byte> data = new List <byte>(); encoder.Write(loginRequest); encoder.Use((buffer, count) => data.AddRange(buffer, count)); encoder.Write(matchRequest); encoder.Use((buffer, count) => data.AddRange(buffer, count)); encoder.Write(connectRequest); encoder.Use((buffer, count) => data.AddRange(buffer, count)); var decoder = new MessageStreamReader(); this.Measure(() => { var position = 0; do { var x = data.GetRange(position, 1).ToArray(); decoder.Add(x, x.Length); var container = decoder.Decode(); if (container.HasValue) { if (container.Value.Is(200)) // LoginRequest { var message = container.Value.Parse <LoginRequest>(); Assert.AreEqual(message.accessToken, firstToken); Assert.AreEqual(message.username, username); } else if (container.Value.Is(201)) // MatchRequest { var message = container.Value.Parse <MatchRequest>(); Assert.AreNotEqual(message, null); } else if (container.Value.Is(202)) // ConnectGameInstanceResponse { var message = container.Value.Parse <ConnectGameInstanceResponse>(); Assert.AreEqual(message.ip, ip); Assert.AreEqual(message.port, port); Assert.AreEqual(message.token, secondToken); } } position += 1; } while (position < data.Count); }, "Partial Stream Decoding"); }