/// <summary> /// Creates a new TCP server to listen on specific IP endpoint. /// </summary> /// <param name="EndPoint">Specification of the interface and TCP port on which the TCP server should listen. IPAddress.Any is a valid value for the interface.</param> /// <param name="UseTls">Indication of whether to use TLS for this TCP server.</param> /// <param name="Roles">One or more roles of this server.</param> /// <param name="ClientKeepAliveTimeoutMs">Number of milliseconds after which the server's client is considered inactive and its connection can be terminated.</param> public TcpRoleServer(IPEndPoint EndPoint, bool UseTls, uint Roles, int ClientKeepAliveTimeoutMs) { string logPrefix = string.Format("[{0}/tcp{1}] ", EndPoint.Port, UseTls ? "_tls" : ""); log = new Logger("IopServerCore.Network.TcpRoleServer", logPrefix); log.Trace("(EndPoint:'{0}',UseTls:{1},Roles:{2},ClientKeepAliveTimeoutMs:{3})", EndPoint, UseTls, Roles, ClientKeepAliveTimeoutMs); this.UseTls = UseTls; this.Roles = Roles; this.EndPoint = EndPoint; this.clientKeepAliveTimeoutMs = ClientKeepAliveTimeoutMs; ShutdownSignaling = new ComponentShutdown(Base.ComponentManager.GlobalShutdown); serverComponent = (ServerBase <TIncomingClient>)Base.ComponentDictionary[ServerBase <TIncomingClient> .ComponentName]; clientList = serverComponent.GetClientList(); IsRunning = false; Listener = new TcpListener(this.EndPoint); Listener.Server.LingerState = new LingerOption(true, 0); Listener.Server.NoDelay = true; IdBase = ((uint)Roles << 24); log.Trace("(-)"); }
/// <summary> /// Obtains the client list. /// </summary> /// <returns>List of all server's clients.</returns> public IncomingClientList GetClientList() { log.Trace("()"); IncomingClientList res = clientList; log.Trace("(-)"); return(res); }
public override bool Init() { log.Info("()"); bool res = false; bool error = false; try { ConfigBase config = (ConfigBase)Base.ComponentDictionary[ConfigBase.ComponentName]; serverId = Crypto.Sha256(((KeysEd25519)config.Settings["Keys"]).PublicKey); clientList = new IncomingClientList(); foreach (RoleServerConfiguration roleServer in ((ConfigServerRoles)config.Settings["ServerRoles"]).RoleServers.Values) { if (roleServer.IsTcpServer) { IPEndPoint endPoint = new IPEndPoint((IPAddress)config.Settings["BindToInterface"], roleServer.Port); TcpRoleServer <TIncomingClient> server = new TcpRoleServer <TIncomingClient>(endPoint, roleServer.Encrypted, roleServer.Roles, roleServer.ClientKeepAliveTimeoutMs); tcpServers.Add(server.EndPoint.Port, server); } else { log.Fatal("UDP servers are not supported."); error = true; break; } } foreach (TcpRoleServer <TIncomingClient> 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(); foreach (TcpRoleServer <TIncomingClient> server in tcpServers.Values) { if (server.IsRunning) { server.Stop(); } } tcpServers.Clear(); } log.Info("(-):{0}", res); return(res); }