Ejemplo n.º 1
0
        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);
            });
        }
Ejemplo n.º 2
0
        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);
        }