コード例 #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);
            });
        }
コード例 #2
0
 public void Write <TMessage>(TMessage message) where TMessage : ITypedMessage
 {
     lock (this.lockToken) {
         var startIndex = this.currentBufferLength;
         this.currentBufferLength += CoderHelper.WriteInt(message.type, this.currentBuffer, this.currentBufferLength);
         this.currentBufferLength += BinaryEncoder.Encode(message, this.currentBuffer, this.currentBufferLength);
         this.currentBufferLength += CoderHelper.AddChecksum(this.currentBuffer, startIndex, this.currentBufferLength);
         this.currentBufferLength += CoderHelper.InsertDelimiter(this.currentBuffer, this.currentBufferLength);
     }
 }