private async Task testSendReceiveFunc( ReliableSamHelper reliableHelperRecv, ReconnectingSamHelper reconnectingSamHelperSend, ReliableSamHelper reliableHelperSend, byte[] data, SamHelper samHelperReceive ) { var waitRecvMessage = reliableHelperRecv.ReliableMessageReceived .Where(x => x.Destination == reconnectingSamHelperSend.Session.Destination) .FirstAsync() .ToTask(); try { var messageId = await reliableHelperSend.SendReliableMessage( samHelperReceive.Session.Destination, data ).ConfigureAwait(false); _logger.Trace("Message sent id {0}", messageId); } catch (EnumException <ReliableSamHelper.SendReliableMessageExcs> exc) { _logger.Trace("Send error {0}", exc.ExceptionCode); throw; } try { var receivedMessage = await waitRecvMessage.ConfigureAwait(false); _logger.Trace("Message received {0} bytes", receivedMessage.Data.Length); Assert.Equal(data, receivedMessage.Data); } catch (OperationCanceledException exc) { _logger.Trace("Recv timeout {0}", exc); throw; } catch (Exception exc) { _logger.Trace("Recv error {0}", exc); throw; } }
private async Task testSendReceiveFunc( ReliableSamHelper reliableHelperRecv, ReconnectingSamHelper reconnectingSamHelperSend, ReliableSamHelper reliableHelperSend, byte[] data, SamHelper samHelperReceive ) { var waitRecvMessage = reliableHelperRecv.ReliableMessageReceived .Where(x => x.Destination == reconnectingSamHelperSend.Session.Destination) .FirstAsync() .ToTask(); try { var messageId = await reliableHelperSend.SendReliableMessage( samHelperReceive.Session.Destination, data ).ConfigureAwait(false); _logger.Trace("Message sent id {0}", messageId); } catch (EnumException<ReliableSamHelper.SendReliableMessageExcs> exc) { _logger.Trace("Send error {0}", exc.ExceptionCode); throw; } try { var receivedMessage = await waitRecvMessage.ConfigureAwait(false); _logger.Trace("Message received {0} bytes", receivedMessage.Data.Length); Assert.Equal(data, receivedMessage.Data); } catch (OperationCanceledException exc) { _logger.Trace("Recv timeout {0}", exc); throw; } catch (Exception exc) { _logger.Trace("Recv error {0}", exc); throw; } }
public async Task TestSendReceiveOneMessageImpl() { const string addressSend = "192.168.56.102";//"192.168.1.132"; const int portSend = 7656; const string addressRecv = addressSend; //"127.0.0.1"; const int portRecv = 7656; var samHelperSend = await SamHelper.CreateInstance( new SamHelperSettings() { SamServerAddress = addressSend, SamServerPort = portSend }, CancellationToken.None ).ConfigureAwait(false); var samHelperReceive = await SamHelper.CreateInstance( new SamHelperSettings() { SamServerAddress = addressRecv, SamServerPort = portRecv }, CancellationToken.None ).ConfigureAwait(false); var reliableHelperSend = new ReliableSamHelper( samHelperSend, new ReliableSamHelperSettings() ); var reliableHelperRecv = new ReliableSamHelper( samHelperReceive, new ReliableSamHelperSettings() ); var data = new byte[1]; MiscFuncs.GetRandomBytes(data); var waitRecvMessage = reliableHelperRecv.ReliableMessageReceived .Where(x => x.Destination == samHelperSend.Session.Destination) .FirstAsync() .ToTask(); var sendProgress = new Progress<OutMessageProgressInfo>( x => _logger.Trace(x.ToString()) ); try { var messageId = await reliableHelperSend.SendReliableMessage( samHelperReceive.Session.Destination, data, sendProgress ).ConfigureAwait(false); _logger.Trace("Message sent id {0}", messageId); } catch (EnumException<ReliableSamHelper.SendReliableMessageExcs> exc) { _logger.Trace("Send error {0}", exc.ExceptionCode); throw; } try { var receivedMessage = await waitRecvMessage.ConfigureAwait(false); _logger.Trace("Message received {0} bytes", receivedMessage.Data.Length); Assert.Equal(data,receivedMessage.Data); } catch (OperationCanceledException exc) { _logger.Trace("Recv timeout {0}", exc); throw; } catch (Exception exc) { _logger.Trace("Recv error {0}", exc); throw; } _logger.Trace("End Total memory - {0} bytes", GC.GetTotalMemory(true)); await reliableHelperSend.MyDisposeAsync().ConfigureAwait(false); _logger.Trace("reliableHelperSend disposed"); await reliableHelperRecv.MyDisposeAsync().ConfigureAwait(false); _logger.Trace("reliableHelperRecv disposed"); await samHelperSend.MyDisposeAsync().ConfigureAwait(false); _logger.Trace("samHelperSend disposed"); await samHelperReceive.MyDisposeAsync().ConfigureAwait(false); _logger.Trace("samHelperReceive disposed"); }
public async Task TestSendReceiveOneMessageImpl() { const string addressSend = "192.168.56.102"; //"192.168.1.132"; const int portSend = 7656; const string addressRecv = addressSend; //"127.0.0.1"; const int portRecv = 7656; var samHelperSend = await SamHelper.CreateInstance( new SamHelperSettings() { SamServerAddress = addressSend, SamServerPort = portSend }, CancellationToken.None ).ConfigureAwait(false); var samHelperReceive = await SamHelper.CreateInstance( new SamHelperSettings() { SamServerAddress = addressRecv, SamServerPort = portRecv }, CancellationToken.None ).ConfigureAwait(false); var reliableHelperSend = new ReliableSamHelper( samHelperSend, new ReliableSamHelperSettings() ); var reliableHelperRecv = new ReliableSamHelper( samHelperReceive, new ReliableSamHelperSettings() ); var data = new byte[1]; MiscFuncs.GetRandomBytes(data); var waitRecvMessage = reliableHelperRecv.ReliableMessageReceived .Where(x => x.Destination == samHelperSend.Session.Destination) .FirstAsync() .ToTask(); var sendProgress = new Progress <OutMessageProgressInfo>( x => _logger.Trace(x.ToString()) ); try { var messageId = await reliableHelperSend.SendReliableMessage( samHelperReceive.Session.Destination, data, sendProgress ).ConfigureAwait(false); _logger.Trace("Message sent id {0}", messageId); } catch (EnumException <ReliableSamHelper.SendReliableMessageExcs> exc) { _logger.Trace("Send error {0}", exc.ExceptionCode); throw; } try { var receivedMessage = await waitRecvMessage.ConfigureAwait(false); _logger.Trace("Message received {0} bytes", receivedMessage.Data.Length); Assert.Equal(data, receivedMessage.Data); } catch (OperationCanceledException exc) { _logger.Trace("Recv timeout {0}", exc); throw; } catch (Exception exc) { _logger.Trace("Recv error {0}", exc); throw; } _logger.Trace("End Total memory - {0} bytes", GC.GetTotalMemory(true)); await reliableHelperSend.MyDisposeAsync().ConfigureAwait(false); _logger.Trace("reliableHelperSend disposed"); await reliableHelperRecv.MyDisposeAsync().ConfigureAwait(false); _logger.Trace("reliableHelperRecv disposed"); await samHelperSend.MyDisposeAsync().ConfigureAwait(false); _logger.Trace("samHelperSend disposed"); await samHelperReceive.MyDisposeAsync().ConfigureAwait(false); _logger.Trace("samHelperReceive disposed"); }
private async Task <object> DoInvokeImpl(InvocationInfo info) { using (var cts = new CancellationTokenSource()) { try { string methodName = info.TargetMethod.Name; if (!_methodsDb.ContainsKey(methodName)) { throw new EnumException <DoInvokeImplExcs>( DoInvokeImplExcs.MethodNotFound, tag: methodName ); } var methodInfo = _methodsDb[methodName]; uint sendMessageId = await _reliableSamHelper.GetNextOutMessageId().ConfigureAwait(false); var pingCallbackTimeoutTcs = new TaskCompletionSource <object>(); IDisposable pingCallbackCheckSubscription = _reliableSamHelper.RawDatagramReceived .Where( x => x.ReplyToMessageId == sendMessageId && x.MessageKind == (byte)EJsonRpcSamI2PMessageKinds.ReceiveCallbackPing && x.Destination == _serverDestination ).Timeout(_samClientSettings.CallbackPingTimeout) .ObserveOn(TaskPoolScheduler.Default) .Subscribe( i => { }, exc => { if (exc is TimeoutException) { pingCallbackTimeoutTcs.TrySetResult(null); } } ); try { Task <ReliableSamHelper.ReliableMessageReceivedArgs> waitResult; if (methodInfo.ReturnResultReliable) { waitResult = _reliableSamHelper.ReliableMessageReceived .Where( x => x.ReplyToMessageId == sendMessageId && x.MessageKind == (byte)EJsonRpcSamI2PMessageKinds.ReceiveResult && x.Destination == _serverDestination ) .FirstAsync() .ToTask(cts.Token); } else { waitResult = _reliableSamHelper.RawDatagramReceived .Where( x => x.ReplyToMessageId == sendMessageId && x.MessageKind == (byte)EJsonRpcSamI2PMessageKinds.ReceiveResult && x.Destination == _serverDestination ) .FirstAsync() .ToTask(cts.Token); } var waitResultErr = _reliableSamHelper.RawDatagramReceived .Where( x => x.ReplyToMessageId == sendMessageId && x.MessageKind == (byte)EJsonRpcSamI2PMessageKinds.ReceiveError && x.Destination == _serverDestination ).FirstAsync() .ToTask(cts.Token); var joe = JsonRpcClientProcessor.GetJsonRpcRequest(info); string jsonRequest = JsonConvert.SerializeObject(joe, Formatting.Indented); byte[] byteJsonRequest = Encoding.UTF8.GetBytes(jsonRequest); if (_samClientSettings.CompressData) { byteJsonRequest = byteJsonRequest.Compress(); } try { if (methodInfo.CallReliable) { await _reliableSamHelper.SendReliableMessage( _serverDestination, byteJsonRequest, messageId : sendMessageId, messageKind : (byte)EJsonRpcSamI2PMessageKinds.SendCall, token : cts.Token ).ConfigureAwait(false); } else { await _reliableSamHelper.SendRawDatagram( _serverDestination, byteJsonRequest, sendMessageId, (byte)EJsonRpcSamI2PMessageKinds.SendCall ).ConfigureAwait(false); } } catch ( EnumException < ReliableSamHelper.SendReliableMessageExcs > reliableExc ) { if ( reliableExc.ExceptionCode == ReliableSamHelper .SendReliableMessageExcs .HandshakeTimeout || reliableExc.ExceptionCode == ReliableSamHelper .SendReliableMessageExcs .SendBlocksTimeout ) { ServiceStatus.OnNext(false); throw new I2PTimeoutException( "Send rpc request i2p error", reliableExc ); } throw; } var delyOrResultTask = await Task.WhenAny( waitResult, waitResultErr, Task.Delay( _samClientSettings.TotalReturnTimeout, cts.Token ), pingCallbackTimeoutTcs.Task ).ConfigureAwait(false); if (delyOrResultTask != waitResult) { if (delyOrResultTask == waitResultErr) { var errResult = await waitResultErr.ConfigureAwait(false); ServiceStatus.OnNext(true); var errResultData = errResult.Data; if (_samClientSettings.CompressData) { errResultData = errResultData.Decompress(); } var jsonResult = JsonConvert.DeserializeObject <JObject>( Encoding.UTF8.GetString(errResultData) ); return(await JsonRpcClientProcessor.GetJsonRpcResult( jsonResult, info ).ConfigureAwait(false)); } ServiceStatus.OnNext(false); if (delyOrResultTask == pingCallbackTimeoutTcs.Task) { throw new I2PTimeoutException( "Callback ping timeout", null ); } throw new I2PTimeoutException( "Get result timeout", null ); } { var retArgs = await waitResult.ConfigureAwait(false); ServiceStatus.OnNext(true); var retArgsData = retArgs.Data; if (_samClientSettings.CompressData) { retArgsData = retArgsData.Decompress(); } var jsonResult = JsonConvert.DeserializeObject <JObject>( Encoding.UTF8.GetString(retArgsData) ); return(await JsonRpcClientProcessor.GetJsonRpcResult(jsonResult, info) .ConfigureAwait(false)); } } finally { pingCallbackCheckSubscription.Dispose(); } } catch (TimeoutException) { throw; } catch (RpcRethrowableException rpcExc) { if (_advancedRpcRethrowableExceptionHandling != null) { _advancedRpcRethrowableExceptionHandling(rpcExc); } throw; } catch (EnumException <DoInvokeImplExcs> ) { throw; } catch (Exception exc) { throw new EnumException <DoInvokeImplExcs>( DoInvokeImplExcs.UnexpectedError, innerException: exc ); } finally { cts.Cancel(); } } }