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()); }
/// <summary> /// Throws TotRequestException if not success response. /// </summary> public async Task <TotContent> RequestAsync(TotRequest request, int timeout = 3000) { Guard.MinimumAndNotNull(nameof(timeout), timeout, 1); var requestBytes = request.ToBytes(); await SendAsync(requestBytes).ConfigureAwait(false); var response = await ReceiveAsync(request.MessageId, request.Version, timeout).ConfigureAwait(false) as TotResponse; // Note: version assertion is inside receive. AssertSuccess(response); return(response.Content); }
private async void RespondsTest_Server_RequestArrivedAsync(object sender, TotRequest request) { var client = sender as TotClient; var messageId = request.MessageId; try { if (request.Purpose.ToString() == "hello") { await client.RespondSuccessAsync(messageId, new TotContent("world")); } else { await client.RespondBadRequestAsync(messageId); } } catch (Exception ex) { await client.RespondUnsuccessfulRequestAsync(messageId, ex.Message); } }
private async void RespondsInParallelWithDelayTest_Server_RequestArrivedAsync(object sender, TotRequest request) { var client = sender as TotClient; var messageId = request.MessageId; try { if (request.Purpose.ToString() == "hello") { await Task.Delay(new Random().Next(1, 1000)); await client.RespondSuccessAsync(messageId, new TotContent("world")); Interlocked.Increment(ref _respondsInParallelWithDelayCount); } else { await client.RespondBadRequestAsync(messageId); } } catch (Exception ex) { await client.RespondUnsuccessfulRequestAsync(messageId, ex.Message); } }
private void OnRequestArrived(TotRequest request) => RequestArrived?.Invoke(this, request);
// 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); } }
private void OnRequestArrived(TotClient client, TotRequest request) => RequestArrived?.Invoke(client, request);
private void TotClient_RequestArrived(object sender, TotRequest request) => OnRequestArrived(sender as TotClient, request);