public void errorOccured(IChannel channel) { _listener.ConnectFailed(); }
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; }
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); }
public Boolean Init(ConnectionListener <MQMessage> listener) { if (channel == null) { bootstrap = new Bootstrap(); loopGroup = new MultithreadEventLoopGroup(workerThreads); bootstrap.Group(loopGroup); bootstrap.Channel <TcpSocketChannel>(); bootstrap.Option(ChannelOption.TcpNodelay, true); bootstrap.Option(ChannelOption.SoKeepalive, true); X509Certificate2 cert = null; if (certificate != null && certificate.Length > 0) { cert = CertificatesHelper.load(certificate, certificatePassword); } WebSocketClientHandshaker handshaker = WebSocketClientHandshakerFactory.NewHandshaker(this.getUri(), WebSocketVersion.V13, null, false, EmptyHttpHeaders.Default, 1280000); WebSocketClientHandler handler = new WebSocketClientHandler(this, handshaker, listener); bootstrap.Handler(new ActionChannelInitializer <ISocketChannel>(channel => { IChannelPipeline pipeline = channel.Pipeline; if (isSecured) { if (cert != null) { pipeline.AddLast("ssl", new TlsHandler(stream => new SslStream(stream, true, (sender, certificate, chain, errors) => true, (sender, targetHost, localCertificates, remoteCertificate, acceptableIssuers) => cert), new ClientTlsSettings(address.Host, new List <X509Certificate>() { cert }))); } else { pipeline.AddLast("ssl", TlsHandler.Client(address.Host)); } } pipeline.AddLast("http - codec", new HttpClientCodec()); pipeline.AddLast("aggregator", new HttpObjectAggregator(65536)); pipeline.AddLast("handler", handler); pipeline.AddLast(new ExceptionHandler()); })); bootstrap.RemoteAddress(address); try { Task <IChannel> task = bootstrap.ConnectAsync(); task.GetAwaiter().OnCompleted(() => { try { channel = task.Result; } catch (Exception) { listener.ConnectFailed(); return; } if (channel != null) { listener.Connected(); } else { listener.ConnectFailed(); } }); } catch (Exception) { return(false); } } return(true); }
public Boolean Init(ConnectionListener <AMQPHeader> listener) { if (channel == null) { bootstrap = new Bootstrap(); loopGroup = new MultithreadEventLoopGroup(workerThreads); bootstrap.Group(loopGroup); bootstrap.Channel <TcpSocketChannel>(); bootstrap.Option(ChannelOption.TcpNodelay, true); bootstrap.Option(ChannelOption.SoKeepalive, true); X509Certificate2 cert = null; if (certificate != null && certificate.Length > 0) { cert = CertificatesHelper.load(certificate, certificatePassword); } bootstrap.Handler(new ActionChannelInitializer <ISocketChannel>(channel => { IChannelPipeline pipeline = channel.Pipeline; if (isSecured) { if (cert != null) { pipeline.AddLast("ssl", new TlsHandler(stream => new SslStream(stream, true, (sender, certificate, chain, errors) => true, (sender, targetHost, localCertificates, remoteCertificate, acceptableIssuers) => cert), new ClientTlsSettings(address.Host, new List <X509Certificate>() { cert }))); } else { pipeline.AddLast("ssl", TlsHandler.Client(address.Host)); } } pipeline.AddLast(new AMQPDecoder()); pipeline.AddLast("handler", new AMQPHandler(listener)); pipeline.AddLast(new AMQPEncoder()); pipeline.AddLast(new ExceptionHandler()); })); bootstrap.RemoteAddress(address); try { Task <IChannel> task = bootstrap.ConnectAsync(); task.GetAwaiter().OnCompleted(() => { try { channel = task.Result; } catch (Exception) { listener.ConnectFailed(); return; } if (channel != null) { listener.Connected(); } else { listener.ConnectFailed(); } }); } catch (Exception) { return(false); } } return(true); }