Exemplo n.º 1
0
    private void ListenForNewSockets()
    {
        TcpListener server = new TcpListener(listenerIp, port);

        server.AllowNatTraversal(true);
        server.Start();
        Print("Started Game Server!");

        while (shouldRun && acceptNewSockets)
        {
            //blocking until there is a client
            try
            {
                Socket sock = server.AcceptSocket();
                Print("Accepted new Socket!");
                // if new is accepted, add to list and listen to it
                if (nextLobby == null || nextLobby.GetSocketCount() >= 4)
                {
                    Lobby newLobby = new Lobby(lobbies.Count, lobbyHostString, bufferSize, sendTickRateMS);
                    lobbies.Add(newLobby);
                    nextLobby = newLobby;
                    Console.WriteLine("Created new Lobby");
                }
                //new Task(()=>nextLobby.AddSocket(sock)).Start();
                nextLobby.AddSocket(sock);
            }
            catch (Exception e)
            {
                Print("exception at listen");
                Print(e.Message);
            }
        }

        server.Stop();
        Print("Stopped Server");
    }