Пример #1
0
 /// <summary>
 /// Closes all sockets and stops listening for new packets
 /// </summary>
 public void Stop()
 {
     lock (socketLock)
     {
         audioBuffer.Stop();
         if (audioListener != null)
         {
             audioListener.Stop();
             audioListener = null;
         }
         if (audioSocket != null)
         {
             audioSocket.Close();
             audioSocket = null;
         }
     }
 }
Пример #2
0
        /// <summary>
        /// Opens the sockets and begins listening for packets
        /// </summary>
        /// <returns>True if setup successful</returns>
        public bool Start()
        {
            lock (socketLock)
            {
                int  tries  = 0;
                bool result = false;
                while (tries < 10 && port < ushort.MaxValue - 1)
                {
                    try
                    {
                        audioSocket = new UdpClient(port);
                    }
                    catch (SocketException)
                    {
                        port++;
                        tries++;
                        continue;
                    }
                    result = true;
                    break;
                }

                if (result)
                {
                    Logger.Debug("Audio Server: Using port {0}", port);
                    audioListener = new UdpListener(audioSocket);
                    audioListener.OnPacketReceived += packetReceived;
                    audioListener.Start();
                }
                else
                {
                    Logger.Error("Audio Server: Failed to locate an available port");
                    if (audioSocket != null)
                    {
                        audioSocket.Close();
                        audioSocket = null;
                    }
                    return(false);
                }
            }
            return(true);
        }