public TimeServer(string multicastIp, int multicastPort) { _multicastPort = multicastPort; var interfaces = GeneralUtilities.GetNetworkInterfacesThatAreUp().Where(networkInterface => networkInterface.GetIPProperties().UnicastAddresses.All(information => !information.Address.Equals(IPAddress.Loopback))).ToList(); _multicastBroadcastServers = interfaces.ConvertAll(input => new MulticastBroadcastServer(multicastPort, multicastIp, input.Name, false, input.GetIPProperties().UnicastAddresses .First(information => information.Address.AddressFamily == AddressFamily.InterNetwork).Address.ToString())); _tcpServers = interfaces.ConvertAll(input => new MultithreadingServer( input.GetIPProperties().UnicastAddresses.First(information => information.Address.AddressFamily == AddressFamily.InterNetwork).Address.ToString(), LocalIdSupplier.CreatePort(), input.Name, int.MaxValue)); _exceptionReporter = new ExceptionReporter(); _discoverServerRequestReporter = new MessageReporter(); _statusReporter = new StatusReporter(); _newClientReporter = new ClientReporter(); _clientDisconnectedReporter = new ClientReporter(); _timeMessageRequestReporter = new MessageReporter(); RegisterServers(); }
private void RegisterServer(MultithreadingServer server) { server.AddExceptionSubscription((o, o1) => { if (o1 is ExceptionEvent exceptionEvent) { OnNewException(exceptionEvent.LastError, exceptionEvent.LastErrorCode); if (exceptionEvent.LastErrorCode == EventCode.Bind) { _workingServers.Remove(IPEndPoint.Parse($"{server.Ip}:{server.Port}")); do { server.Port = LocalIdSupplier.CreatePort(); } while (server.Port == _multicastPort); server.StartService(); _workingServers.Add(server.EndPoint); } } }); server.AddMessageSubscription((o, o1) => { if (o1 is MessageEvent messageEvent) { OnTimeMessageRequest(messageEvent.Message, messageEvent.From, server.EndPoint.ToString()); } }); server.AddNewClientSubscription((o, o1) => { if (o1 is ClientEvent clientEvent) { OnNewClient(clientEvent.Id, clientEvent.Ip, clientEvent.ServerIp); } }); server.AddOnDisconnectedSubscription((o, o1) => { if (o1 is ClientEvent clientEvent) { OnClientDisconnected(clientEvent.Id, clientEvent.Ip, clientEvent.ServerIp); } }); server.AddStatusSubscription((o, o1) => { if (o1 is StatusEvent statusEvent) { OnStatus(statusEvent.StatusCode, statusEvent.StatusMessage); } }); }