public DtlsClientHandler(AsyncDtlsClientProtocol protocol, DtlsStateHandler handler)
 {
     this.protocol = protocol;
     this.handler  = handler;
 }
Esempio n. 2
0
    public Boolean Init(ConnectionListener<string> listener, IPEndPoint localEndPoint, string certFile, string certPassword, bool fLoadWholeChain)
    {
      if (channel == null)
      {
        this._listener = listener;
        bootstrap = new Bootstrap();
        loopGroup = new MultithreadEventLoopGroup(workerThreads);
        bootstrap.Group(loopGroup);

        bootstrap.Channel<SocketDatagramChannel>();

        UDPClient currClient = this;
        bootstrap.Handler(new ActionChannelInitializer<SocketDatagramChannel>(channel =>
        {
          IChannelPipeline pipeline = channel.Pipeline;
          if (isSecured)
          {
            Pkcs12Store keystore = null;
            if (certificate != null && certificate.Length > 0)
              keystore = CertificatesHelper.loadBC(certificate, certificatePassword);

            AsyncDtlsClient client = new MyAsyncDtlsClient(certFile, certPassword, fLoadWholeChain);
            _clientProtocol = new AsyncDtlsClientProtocol(client, new SecureRandom(), channel, currClient, true, ProtocolVersion.DTLSv12);
            pipeline.AddLast(new DtlsClientHandler(_clientProtocol, this));
          }


          pipeline.AddLast("handler", new RawMessageHandler(listener));
#if false
          pipeline.AddLast(new CoapEncoder(channel));
          pipeline.AddLast(new ExceptionHandler());
#endif
        }));

        bootstrap.RemoteAddress(address);

#if true

        try
        {
          Task<IChannel> task;
          if (localEndPoint != null)
          {
             task = bootstrap.BindAsync(localEndPoint);
          }
          else
          {
            task = bootstrap.BindAsync(IPEndPoint.MinPort);
          }

          task.GetAwaiter().OnCompleted(() =>
          {
            try
            {
              channel = task.Result;
              Task connectTask = channel.ConnectAsync(address);
              
              
              connectTask.GetAwaiter().OnCompleted(() =>
              {
                if (_clientProtocol == null)
                {
                  if (channel != null)
                    listener.Connected();
                  else
                    listener.ConnectFailed();
                }
                else
                {
                  startHandhshakeTimer();
                  _clientProtocol.InitHandshake(null);
                }
              });
            }
            catch (Exception)
            {
              listener.ConnectFailed();
              return;
            }
          });
        }
        catch (Exception)
        {
          return false;
        }
      }
#endif
      return true;
    }
Esempio n. 3
0
        public Boolean Init(ConnectionListener <SNMessage> listener)
        {
            if (channel == null)
            {
                this._listener = listener;
                bootstrap      = new Bootstrap();
                loopGroup      = new MultithreadEventLoopGroup(workerThreads);
                bootstrap.Group(loopGroup);
                bootstrap.Channel <SocketDatagramChannel>();
                UDPClient currClient = this;
                bootstrap.Handler(new ActionChannelInitializer <SocketDatagramChannel>(channel =>
                {
                    IChannelPipeline pipeline = channel.Pipeline;
                    if (isSecured)
                    {
                        Pkcs12Store keystore = null;
                        if (certificate != null && certificate.Length > 0)
                        {
                            keystore = CertificatesHelper.loadBC(certificate, certificatePassword);
                        }

                        AsyncDtlsClient client = new AsyncDtlsClient(keystore, certificatePassword, null);
                        _clientProtocol        = new AsyncDtlsClientProtocol(client, new SecureRandom(), channel, null, currClient, true, ProtocolVersion.DTLSv12);
                        pipeline.AddLast(new DtlsClientHandler(_clientProtocol, this));
                    }

                    pipeline.AddLast("handler", new SNHandler(listener));
                    pipeline.AddLast("encoder", new SNEncoder(channel));
                    pipeline.AddLast(new ExceptionHandler());
                }));

                bootstrap.RemoteAddress(address);

                try
                {
                    com.mobius.software.windows.iotbroker.mqtt_sn.net.UDPClient curr = this;
                    Task <IChannel> task = bootstrap.BindAsync(IPEndPoint.MinPort);
                    task.GetAwaiter().OnCompleted(() =>
                    {
                        try
                        {
                            channel          = task.Result;
                            Task connectTask = channel.ConnectAsync(address);
                            connectTask.GetAwaiter().OnCompleted(() =>
                            {
                                if (_clientProtocol == null)
                                {
                                    if (channel != null)
                                    {
                                        listener.Connected();
                                    }
                                    else
                                    {
                                        listener.ConnectFailed();
                                    }
                                }
                                else
                                {
                                    startHandhshakeTimer();
                                    _clientProtocol.InitHandshake(null);
                                }
                            });
                        }
                        catch (Exception)
                        {
                            listener.ConnectFailed();
                            return;
                        }
                    });
                }
                catch (Exception)
                {
                    return(false);
                }
            }

            return(true);
        }