Exemplo n.º 1
0
        public void TotMessageTest()
        {
            Assert.Equal(TotPurpose.Ping, TotPing.Instance.Purpose);
            Assert.Equal(TotPurpose.Pong, TotPong.Instance(TotMessageId.Random).Purpose);

            Assert.Equal(TotPurpose.Success, TotResponse.Success(TotMessageId.Random).Purpose);
            Assert.Equal(TotPurpose.BadRequest, TotResponse.BadRequest(TotMessageId.Random).Purpose);
            Assert.Equal(TotPurpose.VersionMismatch, TotResponse.VersionMismatch(TotMessageId.Random).Purpose);
            Assert.Equal(TotPurpose.UnsuccessfulRequest, TotResponse.UnsuccessfulRequest(TotMessageId.Random).Purpose);

            var x = new TotRequest("status");

            Assert.Equal(97, x.GetLastCellFullnessPercentage());
            Assert.Equal(1, x.GetNumberOfCells());
            Assert.Equal(497, x.GetNumberOfDummyBytesInLastCell());

            var messages = TotMessageBase.SplitByMessages(ByteHelpers.Combine(
                                                              TotPing.Instance.ToBytes(),
                                                              TotPong.Instance(TotMessageId.Random).ToBytes(),
                                                              TotResponse.BadRequest(TotMessageId.Random).ToBytes(),
                                                              TotResponse.Success(TotMessageId.Random).ToBytes(),
                                                              TotResponse.UnsuccessfulRequest(TotMessageId.Random).ToBytes(),
                                                              TotResponse.VersionMismatch(TotMessageId.Random).ToBytes(),
                                                              new TotRequest("fooPurpose", new TotContent("foo content")).ToBytes()));

            Assert.Equal(7, messages.Count());
        }
Exemplo n.º 2
0
        // async void is fine, because in case of exception it logs and it should not be awaited
        private async void ProcessMessageBytesAsync(byte[] bytes)
        {
            try
            {
                Guard.NotNull(nameof(bytes), bytes);

                var messageType = new TotMessageType();
                messageType.FromByte(bytes[1]);

                if (messageType == TotMessageType.Pong)
                {
                    var response = new TotPong();
                    response.FromBytes(bytes);
                    ResponseCache.TryAdd(response.MessageId, response);
                }
                if (messageType == TotMessageType.Response)
                {
                    var response = new TotResponse();
                    response.FromBytes(bytes);
                    ResponseCache.TryAdd(response.MessageId, response);
                    return;
                }

                var stream = TcpClient.GetStream();

                var requestId = TotMessageId.Random;
                try
                {
                    if (messageType == TotMessageType.Ping)
                    {
                        var request = new TotPing();
                        request.FromBytes(bytes);
                        requestId = request.MessageId;
                        AssertVersion(request.Version, TotVersion.Version1);

                        var responseBytes = TotPong.Instance(request.MessageId).ToBytes();
                        await SendAsync(responseBytes).ConfigureAwait(false);

                        return;
                    }

                    if (messageType == TotMessageType.Request)
                    {
                        var request = new TotRequest();
                        request.FromBytes(bytes);
                        requestId = request.MessageId;
                        AssertVersion(request.Version, TotVersion.Version1);

                        OnRequestArrived(request);
                        return;
                    }
                }
                catch (TotRequestException)
                {
                    await RespondVersionMismatchAsync(requestId).ConfigureAwait(false);

                    throw;
                }
                catch
                {
                    await SendAsync(new TotResponse(TotPurpose.BadRequest, new TotContent("Couldn't process the received message bytes."), requestId).ToBytes()).ConfigureAwait(false);

                    throw;
                }
            }
            catch (Exception ex)
            {
                var exception = new TotRequestException("Couldn't process the received message bytes.", ex);
                Logger.LogWarning <TotClient>(exception, LogLevel.Debug);
            }
        }