public ReconnectingSamHelperSettings( ReconnectingSamHelperSettings copyFrom) { TimeoutOnIoException = copyFrom.TimeoutOnIoException; DelayOnConnectFailed = copyFrom.DelayOnConnectFailed; if(copyFrom.ImplementationHelperSettings != null) ImplementationHelperSettings = new SamHelperSettings( copyFrom.ImplementationHelperSettings); }
public ReconnectingSamHelperSettings( ReconnectingSamHelperSettings copyFrom) { TimeoutOnIoException = copyFrom.TimeoutOnIoException; DelayOnConnectFailed = copyFrom.DelayOnConnectFailed; if (copyFrom.ImplementationHelperSettings != null) { ImplementationHelperSettings = new SamHelperSettings( copyFrom.ImplementationHelperSettings); } }
public SamHelperSettings(SamHelperSettings copyFrom) { if (copyFrom == null) { throw new ArgumentNullException( MyNameof.GetLocalVarName(() => copyFrom)); } SamServerAddress = copyFrom.SamServerAddress; SamServerPort = copyFrom.SamServerPort; SessionPrivateKeys = copyFrom.SessionPrivateKeys; I2CpOptions = copyFrom.I2CpOptions; SessionId = copyFrom.SessionId; }
//ECreateInstanceErrCodes public static async Task<SamHelper> CreateInstance( SamHelperSettings settings, CancellationToken cancellationToken ) { if(settings == null) throw new ArgumentNullException( MyNameof.GetLocalVarName(() => settings)); if(cancellationToken == null) throw new ArgumentNullException( MyNameof.GetLocalVarName(() => cancellationToken)); settings.CheckMe(); var result = new SamHelper(); result._samTcpClient = new TcpClient(); try { await result._samTcpClient.ConnectAsync( settings.SamServerAddress, settings.SamServerPort ).ThrowIfCancelled(cancellationToken).ConfigureAwait(false); } catch (TimeoutException timeExc) { throw new EnumException<ECreateInstanceErrCodes>( ECreateInstanceErrCodes.ConnectTcpTimeout, innerException: timeExc ); } catch (Exception exc) { throw new EnumException<ECreateInstanceErrCodes>( ECreateInstanceErrCodes.ConnectTcp, innerException: exc ); } try { result._samStream = result._samTcpClient.GetStream(); try { if (!result._samStream.CanWrite) { throw new EnumException<SamHelperExcs>( SamHelperExcs.NetStreamCannotWrite ); } if (!result._samStream.CanRead) { throw new EnumException<SamHelperExcs>( SamHelperExcs.NetStreamCannotRead ); } result._samReader = new SamReader(result._samStream); result._subscriptions.Add( result._samReader.IoExceptionThrown.ObserveOn(TaskPoolScheduler.Default).Subscribe( i => result.IoExceptionThrown.OnNext(null) ) ); result._subscriptions.Add( result._samReader.DatagramDataReceived.ObserveOn(TaskPoolScheduler.Default).Subscribe( result.SamReaderOnDatagramDataReceived ) ); /*******************/ HelloReplyReceivedArgs helloAnswer; try { var writeBuffer = SamBridgeCommandBuilder.HelloReply(); var helloAnswerTask = result._samReader.HelloReplyReceived .FirstAsync() .ToTask(cancellationToken); await result.WriteBuffer(writeBuffer).ConfigureAwait(false); helloAnswer = await helloAnswerTask.ConfigureAwait(false); } catch (OperationCanceledException) { //timeout result._samTcpClient.Close(); throw new EnumException<ECreateInstanceErrCodes>( ECreateInstanceErrCodes.HelloReplyTimeoutExceed ); } if (!helloAnswer.Ok) { result._samTcpClient.Close(); throw new EnumException<ECreateInstanceErrCodes>( ECreateInstanceErrCodes.HelloReplyError, tag: helloAnswer ); } result._stateHelper.SetInitializedState(); try { var sessionName = settings.SessionId ?? Guid.NewGuid().ToString().Replace("-", "").Substring(0, 15); result._samSession = await result.CreateSession( sessionName, cancellationToken, settings.SessionPrivateKeys, settings.I2CpOptions ).ConfigureAwait(false); } catch (EnumException<ECreateSessionErrorCodes> createSessionExc) { throw new EnumException<ECreateInstanceErrCodes>( ECreateInstanceErrCodes.CreateSessionError, innerException: createSessionExc ); } return result; } catch { result._samStream.Close(); throw; } } catch { result._samTcpClient.Close(); throw; } }
public SamHelperSettings(SamHelperSettings copyFrom) { if(copyFrom == null) throw new ArgumentNullException( MyNameof.GetLocalVarName(() => copyFrom)); SamServerAddress = copyFrom.SamServerAddress; SamServerPort = copyFrom.SamServerPort; SessionPrivateKeys = copyFrom.SessionPrivateKeys; I2CpOptions = copyFrom.I2CpOptions; SessionId = copyFrom.SessionId; }
//ECreateInstanceErrCodes public static async Task <SamHelper> CreateInstance( SamHelperSettings settings, CancellationToken cancellationToken ) { if (settings == null) { throw new ArgumentNullException( MyNameof.GetLocalVarName(() => settings)); } if (cancellationToken == null) { throw new ArgumentNullException( MyNameof.GetLocalVarName(() => cancellationToken)); } settings.CheckMe(); var result = new SamHelper(); result._samTcpClient = new TcpClient(); try { await result._samTcpClient.ConnectAsync( settings.SamServerAddress, settings.SamServerPort ).ThrowIfCancelled(cancellationToken).ConfigureAwait(false); } catch (TimeoutException timeExc) { throw new EnumException <ECreateInstanceErrCodes>( ECreateInstanceErrCodes.ConnectTcpTimeout, innerException: timeExc ); } catch (Exception exc) { throw new EnumException <ECreateInstanceErrCodes>( ECreateInstanceErrCodes.ConnectTcp, innerException: exc ); } try { result._samStream = result._samTcpClient.GetStream(); try { if (!result._samStream.CanWrite) { throw new EnumException <SamHelperExcs>( SamHelperExcs.NetStreamCannotWrite ); } if (!result._samStream.CanRead) { throw new EnumException <SamHelperExcs>( SamHelperExcs.NetStreamCannotRead ); } result._samReader = new SamReader(result._samStream); result._subscriptions.Add( result._samReader.IoExceptionThrown.ObserveOn(TaskPoolScheduler.Default).Subscribe( i => result.IoExceptionThrown.OnNext(null) ) ); result._subscriptions.Add( result._samReader.DatagramDataReceived.ObserveOn(TaskPoolScheduler.Default).Subscribe( result.SamReaderOnDatagramDataReceived ) ); /*******************/ HelloReplyReceivedArgs helloAnswer; try { var writeBuffer = SamBridgeCommandBuilder.HelloReply(); var helloAnswerTask = result._samReader.HelloReplyReceived .FirstAsync() .ToTask(cancellationToken); await result.WriteBuffer(writeBuffer).ConfigureAwait(false); helloAnswer = await helloAnswerTask.ConfigureAwait(false); } catch (OperationCanceledException) { //timeout result._samTcpClient.Close(); throw new EnumException <ECreateInstanceErrCodes>( ECreateInstanceErrCodes.HelloReplyTimeoutExceed ); } if (!helloAnswer.Ok) { result._samTcpClient.Close(); throw new EnumException <ECreateInstanceErrCodes>( ECreateInstanceErrCodes.HelloReplyError, tag: helloAnswer ); } result._stateHelper.SetInitializedState(); try { var sessionName = settings.SessionId ?? Guid.NewGuid().ToString().Replace("-", "").Substring(0, 15); result._samSession = await result.CreateSession( sessionName, cancellationToken, settings.SessionPrivateKeys, settings.I2CpOptions ).ConfigureAwait(false); } catch (EnumException <ECreateSessionErrorCodes> createSessionExc) { throw new EnumException <ECreateInstanceErrCodes>( ECreateInstanceErrCodes.CreateSessionError, innerException: createSessionExc ); } return(result); } catch { result._samStream.Close(); throw; } } catch { result._samTcpClient.Close(); throw; } }