示例#1
0
        static void Main()
        {
            int port = 8880;

            // Print the logs in the console
            Logger.StreamOutput           = new StreamWriter(Console.OpenStandardOutput());
            Logger.StreamOutput.AutoFlush = true;

            // Simulate 10% packet loss
            QuicConnection.PacketLossPercentage = 10;
            QuicListener server = new QuicListener(port);

            // We only use one chatroom in this sample, but multiple could be instantiated
            Chatroom chatroom = new Chatroom();

            Dictionary <QuicConnection, Thread> threads = new Dictionary <QuicConnection, Thread>();

            Console.WriteLine("Server listening on port : {0}", port);

            server.Start();

            while (true)
            {
                foreach (QuicConnection connection in server.getConnectionPool().GetPool())
                {
                    if (!chatroom.containsConnection(connection))
                    {
                        // Every new connection is added to the chatroom and a new listening thread is created
                        chatroom.addConnection(connection);
                        Thread t = new Thread(new ThreadStart(() => ProcessMessagesFromConnection(connection, chatroom)));
                        t.Start();
                        threads.Add(connection, t);
                    }
                }

                foreach (QuicConnection connection in chatroom.Connections)
                {
                    if (!server.getConnectionPool().GetPool().Contains(connection))
                    {
                        // Whenever a connection is closed by the client, we need to remove it from the chatroom and close the corresponding thread
                        chatroom.removeConnection(connection);
                        threads[connection].Abort();
                        threads.Remove(connection);
                    }
                }
            }
        }