Esempio n. 1
0
        /// <summary>
        /// Starts the server.
        /// </summary>
        /// <param name="port">The port of the server.</param>
        /// <param name="message">The message to announce the server with.</param>
        /// <returns><see langword="true"/> if the server started successfully, <see langword="false"/> otherwise.</returns>
        public bool StartServer(int port, string message)
        {
            this.port       = port;
            announceMessage = message;

            // setup server
            Server = new ServerTcp(this.port);
            Server.ClientConnected    += OnClientConnected;
            Server.ClientDisconnected += OnClientDisconnected;
            ////Server.DataReceived += OnDataReceived;
            Server.DataReceived += OnDataReceivedAtServer;

            // start server
            bool success = Server.Start();

            if (success == false)
            {
                Debug.Log("Failed to start server!");
                return(false);
            }

            Debug.Log("Started server!");

            // announce server via broadcast
            announcer = new ClientUdp(broadcastIP, 11338);
            success   = announcer.Open();
            if (success == false)
            {
                Debug.Log("Failed to start announcing!");
                return(false);
            }

            InvokeRepeating(nameof(AnnounceServer), 1.0f, 2.0f);
            return(true);
        }
Esempio n. 2
0
        /// <summary>
        /// Stops the server.
        /// </summary>
        internal void StopServer()
        {
            if (announcer != null)
            {
                CancelInvoke("AnnounceServer");
                announcer.Close();
                announcer.Dispose();
                announcer = null;
            }

            if (Server != null)
            {
                Server.Stop();
                Server.Dispose();
                Server = null;
            }
        }