public void Start(string configuration, params IPEndPoint[] endpoints) { if (endpoints == null || endpoints.Length == 0) { throw new ArgumentNullException("endpoints"); } if (connectSockets != null) { throw new InvalidOperationException("Already started"); } connectSockets = new Tuple <Socket, EndPoint> [endpoints.Length]; var tmp = messageProcessor; if (tmp != null) { tmp.StartProcessor(Context, configuration); } for (int i = 0; i < endpoints.Length; i++) { Log?.WriteLine("{0}\tService starting: {1}", Connection.GetConnectIdent(endpoints[i]), endpoints[i]); EndPoint endpoint = endpoints[i]; Socket connectSocket = StartAcceptListener(endpoint); if (connectSocket == null) { throw new InvalidOperationException("Unable to start all endpoints"); } connectSockets[i] = Tuple.Create(connectSocket, endpoint); } timer = new System.Threading.Timer(Heartbeat, null, LogFrequency, LogFrequency); }
public void Start(string configuration, params IPEndPoint[] endpoints) { if (endpoints == null || endpoints.Length == 0) { throw new ArgumentNullException(nameof(endpoints)); } if (connectSockets != null) { throw new InvalidOperationException("Already started"); } connectSockets = new Tuple <Socket, EndPoint> [endpoints.Length]; var tmp = MessageProcessor; tmp?.StartProcessor(Context, configuration); for (var i = 0; i < endpoints.Length; i++) { Logger?.Info($"{Connection.GetConnectIdent(endpoints[i])}\tService starting: {endpoints[i]}"); EndPoint endpoint = endpoints[i]; var connectSocket = StartAcceptListener(endpoint); if (connectSocket == null) { throw new InvalidOperationException("Unable to start all endpoints"); } connectSockets[i] = Tuple.Create(connectSocket, endpoint); } timer = new Timer(Heartbeat, null, logFrequency, logFrequency); }
public void Stop() { stopped = true; if (timer != null) { timer.Dispose(); timer = null; } var ctx = Context; ctx?.DoNotAccept(); var proc = MessageProcessor; if (proc != null) { Logger?.Info($"{Connection.GetLogIdent()}\tShutting down connections..."); foreach (var pair in allConnections) { var conn = pair.Value; try { proc.OnShutdown(ctx, conn); // processor first conn.GracefulShutdown(ctx); // then protocol } catch (Exception ex) { Logger?.Error(ex, Connection.GetIdent(conn)); } } Thread.Sleep(100); } foreach (var pair in allConnections) { var conn = pair.Value; var socket = conn.Socket; if (socket == null) { continue; } try { socket.Close(); } catch (Exception ex) { Logger?.Error(ex, Connection.GetIdent(conn)); } try { ((IDisposable)socket).Dispose(); } catch (Exception ex) { Logger?.Error(ex, Connection.GetIdent(conn)); } } if (connectSockets != null) { foreach (var tuple in connectSockets) { var connectSocket = tuple.Item1; if (connectSocket == null) { continue; } EndPoint endpoint = null; try { endpoint = connectSocket.LocalEndPoint; Logger?.Info($"{Connection.GetConnectIdent(endpoint)}\tService stopping: {endpoint}"); connectSocket.Close(); } catch (Exception ex) { Logger?.Error(ex, Connection.GetConnectIdent(endpoint)); } try { ((IDisposable)connectSocket).Dispose(); } catch (Exception ex) { Logger?.Error(ex, Connection.GetConnectIdent(endpoint)); } } connectSockets = null; var tmp = MessageProcessor; tmp?.EndProcessor(Context); } WriteLog(); }
public void Stop() { stopped = true; if (timer != null) { timer.Dispose(); timer = null; } var ctx = Context; if (ctx != null) { ctx.DoNotAccept(); } var proc = MessageProcessor; if (proc != null) { Console.WriteLine("{0}\tShutting down connections...", Connection.GetLogIdent()); foreach (var conn in allConnections) { try { proc.OnShutdown(ctx, conn); // processor first conn.GracefulShutdown(ctx); // then protocol } catch (Exception ex) { Console.Error.WriteLine("{0}\t{1}", Connection.GetIdent(conn), ex.Message); } } Thread.Sleep(100); } foreach (var conn in allConnections) { var socket = conn.Socket; if (socket != null) { try { socket.Close(); } catch (Exception ex) { Console.Error.WriteLine("{0}\t{1}", Connection.GetIdent(conn), ex.Message); } try { ((IDisposable)socket).Dispose(); } catch (Exception ex) { Console.Error.WriteLine("{0}\t{1}", Connection.GetIdent(conn), ex.Message); } } } if (connectSockets != null) { foreach (var tuple in connectSockets) { var connectSocket = tuple.Item1; if (connectSocket == null) { continue; } EndPoint endpoint = null; try { endpoint = connectSocket.LocalEndPoint; Console.WriteLine("{0}\tService stopping: {1}", Connection.GetConnectIdent(endpoint), endpoint); connectSocket.Close(); } catch (Exception ex) { Console.Error.WriteLine("{0}\t{1}", Connection.GetConnectIdent(endpoint), ex.Message); } try { ((IDisposable)connectSocket).Dispose(); } catch (Exception ex) { Console.Error.WriteLine("{0}\t{1}", Connection.GetConnectIdent(endpoint), ex.Message); } } connectSockets = null; var tmp = messageProcessor; if (tmp != null) { tmp.EndProcessor(Context); } } WriteLog(); }