Exemple #1
0
 public SocketHandler ConnectPlayer()
 {
     TcpClient client = tcpListener.AcceptTcpClient();
     SocketHandler player = new SocketHandler(client);
     clients.Add(player);
     return player;
 }
Exemple #2
0
        public SocketManager CreateSocketManager()
        {
            if (maxClients <= 0)
                return null;

            socketManager = new SocketManager(maxClients, settings.Port);
            socketManager.Start();

            WindowThread<ProgressWindow> windowThread = new WindowThread<ProgressWindow>();
            windowThread.Update(p => p.Closed += (sender, args) => Cancel());
            windowThread.Start();

            while (socketManager.CountClients < maxClients)
            {
                windowThread.Update(p => p.StatusText.Text = string.Format("Waiting for {0} players..",
                                                                           maxClients -
                                                                           socketManager.CountClients));
                if (windowThread.IsClosed())
                {
                    socketManager.Stop();
                    Cancel();
                    break;
                }

                if (socketManager.HasIncoming)
                {
                    SocketHandler player = socketManager.ConnectPlayer();
                    windowThread.Update(p => p.StatusText.Text = "Waiting for name..");
                    string playerName = player.WaitForName();
                    windowThread.Update(p => p.List.Children.Add(new TextBlock { Text = playerName }));
                }
                Thread.Sleep(10);
            }
            windowThread.Stop();
            return socketManager;
        }