public void CheckMe()
 {
     if (TimeoutOnIoException < TimeSpan.Zero ||
         TimeoutOnIoException > TimeSpan.FromSeconds(100.0d)
         )
     {
         throw new ArgumentOutOfRangeException(
                   this.MyNameOfProperty(e => e.TimeoutOnIoException));
     }
     if (ImplementationHelperSettings == null)
     {
         throw new ArgumentNullException(
                   this.MyNameOfProperty(e => e.ImplementationHelperSettings));
     }
     ImplementationHelperSettings.CheckMe();
 }
Beispiel #2
0
        //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;
            }
        }
Beispiel #3
0
        //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;
            }
        }