/// <summary> /// Creates the encapsulation for a new TCP server client. /// </summary> /// <param name="Server">Role server that the client connected to.</param> /// <param name="TcpClient">TCP client class that holds the connection and allows communication with the client.</param> /// <param name="Id">Unique identifier of the client's connection.</param> /// <param name="UseTls">true if the client is connected to the TLS port, false otherwise.</param> /// <param name="KeepAliveIntervalSeconds">Number of seconds for the connection to this client to be without any message until the node can close it for inactivity.</param> public Client(TcpRoleServer Server, TcpClient TcpClient, ulong Id, bool UseTls, int KeepAliveIntervalSeconds) { this.TcpClient = TcpClient; this.Id = Id; RemoteEndPoint = this.TcpClient.Client.RemoteEndPoint; server = Server; string logPrefix = string.Format("[{0}<=>{1}|0x{2:X16}] ", server.EndPoint, RemoteEndPoint, Id); string logName = "HomeNet.Network.Client"; this.log = new PrefixLogger(logName, logPrefix); log.Trace("(UseTls:{0},KeepAliveIntervalSeconds:{1})", UseTls, KeepAliveIntervalSeconds); messageProcessor = new MessageProcessor(server, logPrefix); this.KeepAliveIntervalSeconds = KeepAliveIntervalSeconds; NextKeepAliveTime = DateTime.UtcNow.AddSeconds(this.KeepAliveIntervalSeconds); this.TcpClient.LingerState = new LingerOption(true, 0); this.TcpClient.NoDelay = true; this.UseTls = UseTls; Stream = this.TcpClient.GetStream(); if (this.UseTls) { Stream = new SslStream(Stream, false, PeerCertificateValidationCallback); } MessageBuilder = new MessageBuilder(server.IdBase, new List <byte[]>() { new byte[] { 1, 0, 0 } }, Base.Configuration.Keys); ConversationStatus = ClientConversationStatus.NoConversation; IsOurCheckedInClient = false; ApplicationServices = new ApplicationServices(logPrefix); log.Trace("(-)"); }
public override bool Init() { log.Info("()"); bool res = false; bool error = false; try { clientList = new ClientList(); checkInactiveClientConnectionsTimer = new Timer(CheckInactiveClientConnectionsTimerCallback, null, CheckInactiveClientConnectionsTimerInterval, CheckInactiveClientConnectionsTimerInterval); serversMaintenanceThread = new Thread(new ThreadStart(ServersMaintenanceThread)); serversMaintenanceThread.Start(); foreach (RoleServerConfiguration roleServer in Base.Configuration.ServerRoles.RoleServers.Values) { if (roleServer.IsTcpServer) { IPEndPoint endPoint = new IPEndPoint(Base.Configuration.ServerInterface, roleServer.Port); TcpRoleServer server = new TcpRoleServer(endPoint, roleServer.Encrypted, roleServer.Roles); tcpServers.Add(server.EndPoint.Port, server); } else { log.Fatal("UDP servers are not implemented."); error = true; break; } } foreach (TcpRoleServer server in tcpServers.Values) { if (!server.Start()) { log.Error("Unable to start TCP server {0}.", server.EndPoint); error = true; break; } } if (!error) { res = true; Initialized = true; } } catch (Exception e) { log.Error("Exception occurred: {0}", e.ToString()); } if (!res) { ShutdownSignaling.SignalShutdown(); if (checkInactiveClientConnectionsTimer != null) { checkInactiveClientConnectionsTimer.Dispose(); } checkInactiveClientConnectionsTimer = null; foreach (TcpRoleServer server in tcpServers.Values) { if (server.IsRunning) { server.Stop(); } } tcpServers.Clear(); } log.Info("(-):{0}", res); return(res); }