/// <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; // Move processing to thread pool. ThreadPool.QueueUserWorkItem(delegate(object state){ try{ m_pEventArgs = new UDP_e_PacketReceived(); m_pBuffer = new byte[m_BufferSize]; if(Net_Utils.IsIoCompletionPortsSupported()){ 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); } }; 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); } }); }
/// <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); } }
/// <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> /// 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> /// Processes received UDP packet. /// </summary> /// <param name="e">UDP packet.</param> private void ProcessUdpPacket(UDP_e_PacketReceived e) { try{ if(m_IsDisposed){ return; } DnsServerResponse serverResponse = ParseQuery(e.Buffer); DNS_ClientTransaction transaction = null; // Pass response to transaction. if(m_pTransactions.TryGetValue(serverResponse.ID,out transaction)){ if(transaction.State == DNS_ClientTransactionState.Active){ // Cache query. if(m_UseDnsCache && serverResponse.ResponseCode == DNS_RCode.NO_ERROR){ DnsCache.AddToCache(transaction.QName,(int)transaction.QType,serverResponse); } transaction.ProcessResponse(serverResponse); } } // No such transaction or transaction has timed out before answer received. //else{ //} } catch{ // We don't care about receiving errors here, skip them. } }
/// <summary> /// This method is called when flow gets new UDP packet. /// </summary> /// <param name="e">Event data..</param> /// <exception cref="ArgumentNullException">Is raised when <b>e</b> is null reference.</exception> internal void OnUdpPacketReceived(UDP_e_PacketReceived e) { if(e == null){ throw new ArgumentNullException("e"); } m_LastActivity = DateTime.Now; byte[] data = new byte[e.Count]; Array.Copy(e.Buffer,data,e.Count); m_pStack.TransportLayer.OnMessageReceived(this,data); }
/// <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); } }
/// <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> /// 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); } }); }
/// <summary> /// This method is called when new SIP UDP packet has received. /// </summary> /// <param name="sender">Sender.</param> /// <param name="e">Event data.</param> private void m_pUdpServer_PacketReceived(object sender,UDP_e_PacketReceived e) { try{ SIP_Flow flow = m_pFlowManager.GetOrCreateFlow(true,(IPEndPoint)e.Socket.LocalEndPoint,e.RemoteEP,SIP_Transport.UDP); flow.OnUdpPacketReceived(e); } catch(Exception x){ m_pStack.OnError(x); } }