Bricklayer.Server.PingListener.Listen C# (CSharp) Метод

Listen() приватный Метод

Listens for ping requests, and sends back statistics
private Listen ( ) : void
Результат void
        private void Listen()
        {
            try
            {
                server = new TcpListener(IPAddress.Any, port); //Create a TcpListener to listen for requests
                server.Start(); //Start listening for client requests.

                byte[] bytes = new byte[1]; //Single byte buffer for reading data (should not exceed 1 byte)

                //Enter the listening loop.
                while (true)
                {
                    client = server.AcceptTcpClient(); //Wait and accept requests
                    stream = client.GetStream(); //Get a stream object for reading and writing
                    int i;
                    //Loop to receive all the data sent by the client
                    while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                    {
                        Debug.WriteLine(String.Format("PingListener: Data Received: {0}", bytes.ToString()));
                        //If ping request recieved, send back the data (0 is the ping packet id)
                        if ((byte)bytes[0] == 0x00)
                        {
                            Debug.WriteLine("PingListener: Data validated, query request recieved.");
                            //Get server stats to send back
                            ServerPingData pingData = Server.NetManager.GetQuery();
                            //Write the data to a steam and send
                            using (MemoryStream ms = new MemoryStream())
                            {
                                using (StreamWriter writer = new StreamWriter(ms))
                                {
                                    writer.WriteLine((byte)pingData.Online);
                                    writer.WriteLine((byte)pingData.MaxOnline);
                                    writer.WriteLine(pingData.Description);
                                }
                                byte[] msg = ms.ToArray();
                                //(stream.ReadByte() << 8) + stream.ReadByte();
                                if (msg.Length > 65535)
                                {
                                    throw new OverflowException("Please make your description smaller");
                                }

                                // Writes the size of the message, up to 65535
                                stream.WriteByte((byte)(msg.Length >> 8));
                                stream.WriteByte((byte)(msg.Length % 255));

                                stream.Write(msg, 0, msg.Length);
                            }
                        }
                    }

                    // Shutdown and end connection
                    client.Close();
                }
            }
            catch (Exception e)
            {
                Log.WriteLine(LogType.Error, "PingListener Error: \"{0}\"", e.Message);
            }
            finally
            {
                server.Stop();
                this.Start();
            }
        }