/// <summary> /// Initializes the server system with the configurations. /// </summary> public static void Initialize(ServerConfigs configs) { if (IsInitialized) { return; } Logger.Log("Initializing Server ...", Color.blue, "NetworkServer"); SetupConfigurations(configs); InitializeClientRegistry(); InitializeServerConnections(); Logger.Log($"Server started on port {Port}.", Color.black, "NetworkServer"); IsInitialized = true; }
/// <summary> /// Welcomes a incoming client's connection to the server. Welcome packets are handled internally. /// </summary> internal static void Welcome(TcpClient tcpClient, ServerConfigs configs) { var clientId = IdHelper.GetNextId(); var client = new Client(clientId, configs); if (!_clientRegistry.RegisterClient(client)) { Debug.LogError("Error registry: client id already exists."); return; } client.ConnectTcp(tcpClient); var packet = new Packet(PacketId.Welcome); packet.Write(WelcomeMessage); packet.Write(clientId); SendTcpData(clientId, packet); Debug.Log($"Client Id {clientId} Welcome"); }
/// <summary> /// Creates a listener in a IP address for the specified configurated port. /// </summary> public Tcp(ServerConfigs configs) { _configs = configs; _tcpListener = new TcpListener(IPAddress.Any, _configs.Port); }
/// <summary> /// Creates a UDP server connection with the specific configs. /// </summary> internal Udp(ServerConfigs configs) { _configs = configs; _udpListener = new UdpClient(_configs.Port); Connect(); }
/// <summary> /// Creates the server connections. /// </summary> internal Server(ServerConfigs configs) { _configs = configs; _tcp = new Tcp(_configs); }
/// <summary> /// Creates a UDP connection with the client. /// </summary> public Udp(int id, ServerConfigs configs) { _configs = configs; IpEndPoint = null; Id = id; }
/// <summary> /// Creates a TCP connection with the client. /// </summary> internal Tcp(int id, ServerConfigs configs) { Id = id; _configs = configs; _receiveBuffer = new byte[BufferSize]; }