Ejemplo n.º 1
0
 /// <summary>
 /// Raises PacketReceived event.
 /// </summary>
 /// <param name="e">Event data.</param>
 private void OnUdpPacketReceived(UDP_e_PacketReceived e)
 {
     if (this.PacketReceived != null)
     {
         this.PacketReceived(this, e);
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Processes specified incoming UDP packet.
        /// </summary>
        /// <param name="e">Packet event data.</param>
        /// <exception cref="ArgumentNullException">Is raised when <b>e</b> is null reference.</exception>
        private void ProcessUdpPacket(UDP_e_PacketReceived e)
        {
            if (e == null)
            {
                throw new ArgumentNullException("e");
            }

            OnUdpPacketReceived(e);
        }
        /// <summary>
        /// Cleans up any resources being used.
        /// </summary>
        public void Dispose()
        {
            if (m_IsDisposed)
            {
                return;
            }
            m_IsDisposed = true;

            m_pSocket = null;
            m_pBuffer = null;
            if (m_pSocketArgs != null)
            {
                m_pSocketArgs.Dispose();
                m_pSocketArgs = null;
            }
            m_pEventArgs = null;

            this.PacketReceived = null;
            this.Error          = null;
        }
        /// <summary>
        /// Starts receiving data.
        /// </summary>
        /// <exception cref="ObjectDisposedException">Is raised when this calss is disposed and this method is accessed.</exception>
        public void Start()
        {
            if (m_IsDisposed)
            {
                throw new ObjectDisposedException(this.GetType().Name);
            }
            if (m_IsRunning)
            {
                return;
            }
            m_IsRunning = true;

            bool isIoCompletionSupported = Net_Utils.IsSocketAsyncSupported();

            m_pEventArgs = new UDP_e_PacketReceived();
            m_pBuffer    = new byte[m_BufferSize];

            if (isIoCompletionSupported)
            {
                m_pSocketArgs = new SocketAsyncEventArgs();
                m_pSocketArgs.SetBuffer(m_pBuffer, 0, m_BufferSize);
                m_pSocketArgs.RemoteEndPoint = new IPEndPoint(m_pSocket.AddressFamily == AddressFamily.InterNetwork ? IPAddress.Any : IPAddress.IPv6Any, 0);
                m_pSocketArgs.Completed     += delegate(object s1, SocketAsyncEventArgs e1){
                    if (m_IsDisposed)
                    {
                        return;
                    }

                    try{
                        if (m_pSocketArgs.SocketError == SocketError.Success)
                        {
                            OnPacketReceived(m_pBuffer, m_pSocketArgs.BytesTransferred, (IPEndPoint)m_pSocketArgs.RemoteEndPoint);
                        }
                        else
                        {
                            OnError(new Exception("Socket error '" + m_pSocketArgs.SocketError + "'."));
                        }

                        IOCompletionReceive();
                    }
                    catch (Exception x) {
                        OnError(x);
                    }
                };
            }

            // Move processing to thread pool.
            ThreadPool.QueueUserWorkItem(delegate(object state){
                if (m_IsDisposed)
                {
                    return;
                }

                try{
                    if (isIoCompletionSupported)
                    {
                        IOCompletionReceive();
                    }
                    else
                    {
                        EndPoint rtpRemoteEP = new IPEndPoint(m_pSocket.AddressFamily == AddressFamily.InterNetwork ? IPAddress.Any : IPAddress.IPv6Any, 0);
                        m_pSocket.BeginReceiveFrom(
                            m_pBuffer,
                            0,
                            m_BufferSize,
                            SocketFlags.None,
                            ref rtpRemoteEP,
                            new AsyncCallback(this.AsyncSocketReceive),
                            null
                            );
                    }
                }
                catch (Exception x) {
                    OnError(x);
                }
            });
        }