Exemplo n.º 1
0
        /// <summary>
        /// Called when the channel has been disconnected.
        /// </summary>
        /// <param name="handler">The handler.</param>
        private void Handler_OnDisconnected(MyceliumClientHandler handler)
        {
            Log.Information("Disconnected from Mycelium.");

            // reconnect
            Connect();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Starts the controller.
        /// </summary>
        public void Start()
        {
            var ipAddresses = Dns.GetHostAddresses(_config.Ip);

            if (0 == ipAddresses.Length)
            {
                throw new Exception("Could not resolve Mycelium host.");
            }

            _ip = ipAddresses[0];

            var metrics = new PassthroughNetworkMetricsProvider();
            var binder  = new TypeBinder();

            _group     = new MultithreadEventLoopGroup();
            _bootstrap = new Bootstrap();

            _bootstrap
            .Group(_group)
            .Channel <TcpSocketChannel>()
            .Option(ChannelOption.TcpNodelay, true)
            .Handler(new ActionChannelInitializer <ISocketChannel>(channel =>
            {
                var pipeline = channel.Pipeline;
                pipeline.AddLast(
                    new PayloadEncoder(metrics, binder),
                    new PayloadDecoder(metrics, binder));

                _handler = new MyceliumClientHandler(_config.Token);
                _handler.OnDisconnected += Handler_OnDisconnected;

                pipeline.AddLast("handler", _handler);
            }));

            Connect();
        }