public DefaultClientChannelInboundHandler( CommandProcessorManager cmdProcessorManager, Arch.CMessaging.Client.MetaEntity.Entity.Endpoint endpoint, EndpointSession endpointSession, DefaultEndpointClient endpointClient, CoreConfig config) { this.cmdProcessorManager = cmdProcessorManager; this.endpoint = endpoint; this.endpointSession = endpointSession; this.endpointClient = endpointClient; this.config = config; }
private Bootstrap CreateBootstrap(Endpoint endpoint, EndpointSession endpointSession) { return new Bootstrap() .Option(SessionOption.SO_KEEPALIVE, true) .Option(SessionOption.CONNECT_TIMEOUT_MILLIS, 5000) .Option(SessionOption.TCP_NODELAY, true) .Option(SessionOption.SO_SNDBUF, config.SendBufferSize) .Option(SessionOption.SO_RCVBUF, config.ReceiveBufferSize) .Option(SessionOption.ANY_IDLE_TIME, config.EndpointSessionMaxIdleTime) .OnSessionDestroyed((session) => this.RemoveSession(endpoint, endpointSession)) .Handler(chain => { chain.AddLast( new ExceptionHandler(), new MagicNumberPrepender(), new LengthFieldPrepender(4), new ProtocolCodecFilter(new CommandCodecFactory())); }) .Handler(new DefaultClientChannelInboundHandler(commandProcessorManager, endpoint, endpointSession, this, config)); }
public void RemoveSession(Endpoint endpoint, EndpointSession endpointSession) { EndpointSession removedSession = null; if (Endpoint.BROKER.Equals(endpoint.Type) && sessions.ContainsKey(endpoint)) { lock (syncRoot) { if (sessions.ContainsKey(endpoint)) { EndpointSession tmp = sessions[endpoint]; if (tmp == endpointSession) { sessions.TryRemove(endpoint, out removedSession); } } } } if (removedSession != null) { log.Info(string.Format("Closing idle connection to broker({0}:{1})", endpoint.Host, endpoint.Port)); removedSession.Close(); } }
private void Connect(Endpoint endpoint, EndpointSession endpointSession) { Debug.WriteLine("producer try connect {0}:{1}", endpoint.Host, endpoint.Port); var future = CreateBootstrap(endpoint, endpointSession) .Connect(new IPEndPoint(IPAddress.Parse(endpoint.Host), endpoint.Port)); future.Complete += (s, e) => { var connectFuture = e.Future as DefaultConnectFuture; if (!endpointSession.IsClosed) { if (!connectFuture.Connected) { Log.Error(string.Format("producer try connect failed at {0}:{1}", endpoint.Host, endpoint.Port)); if (metaService.ContainsEndpoint(endpoint)) { endpointSession.SetSessionFuture(null); System.Threading.Thread.Sleep(config.EndpointSessionAutoReconnectDelay * 1000); Debug.WriteLine("producer try connect failed at {0}:{1}", endpoint.Host, endpoint.Port); Connect(endpoint, endpointSession); } } else { Log.Info(String.Format("Connected to broker {0}", connectFuture.Session.RemoteEndPoint.ToString())); endpointSession.SetSessionFuture(connectFuture); } } else { if (connectFuture.Connected) future.Session.Close(true); } }; }
private EndpointSession CreateSession(Endpoint endpoint) { var session = new EndpointSession(this); Connect(endpoint, session); return session; }