예제 #1
0
        /// <summary>
        /// Assign player and handle their orders
        /// <para/>Call either Init or Load afterward
        /// </summary>
        /// <param name="black"></param>
        /// <param name="white"></param>
        public GameHandler(OthelloPlayerServer black, OthelloPlayerServer white)
        {
            // Init Client 1
            OthelloPlayer1 = black;

            // Init Client 2
            OthelloPlayer2 = white;

            // Update handler
            OthelloPlayer1.SetOrderHandler(this);
            OthelloPlayer2.SetOrderHandler(this);

            if (OthelloPlayer1.PlayerType == PlayerType.Human & OthelloPlayer2.PlayerType == PlayerType.Human)
            {
                BattleType = BattleType.AgainstPlayer;
            }
            else
            {
                BattleType = BattleType.AgainstAI;
            }

            // TODO SEGAN (NiceToHave) : Ping clients to detect disconnect
            // Start to ping clients
            // pinger.Start();
        }
예제 #2
0
        /// <summary>
        /// Start to listen and accept clients
        /// </summary>
        /// <param name="env">Local or Online</param>
        /// <returns></returns>
        public bool StartListening(GameType env)
        {
            Environnement = env;
            if (Environnement == GameType.Local)
            {
                listener = new TcpListener(IPAddress.Parse(Tools.Properties.Settings.Default.LocalHostname), Tools.Properties.Settings.Default.LocalPort);
            }
            else
            {
                listener = new TcpListener(IPAddress.Parse(Tools.Properties.Settings.Default.OnlineHostname), Tools.Properties.Settings.Default.OnlinePort);
            }

            try
            {
                Running = true;

                // Start to listen
                listener.Start(100);

                // Start an asynchronous socket to listen for connections.
                Console.WriteLine("Waiting for connections...");

                #region AcceptConnection

                // Accept any new connection
                new Task(() =>
                {
                    // Infinite loop
                    while (Running)
                    {
                        // Accept connection
                        if (listener.Pending())
                        {
                            // Store the new connection inside the client list
                            var newConnection = listener.AcceptTcpClient();

                            // PlayerType will be fetched during the register method inside the matchmaking
                            var client = new OthelloTCPClient();
                            client.Bind(newConnection);

                            // Generate a new player
                            var othelloPlayer = new OthelloPlayerServer(client);
                            othelloPlayer.SetOrderHandler(Matchmaker.Instance);

                            // DEBUG
                            Console.WriteLine("NEW CLIENT CONNECTED");
                        }

                        // Wait before reading again
                        Thread.Sleep(1);
                    }
                }).Start();

                #endregion
            }
            catch (Exception ex)
            {
                Toolbox.LogError(ex);
            }

            return(true);
        }