Exemplo n.º 1
0
		private void ReceiveCallback(IAsyncResult ar)
		{
			UdpClient listener = (UdpClient) ar.AsyncState;

			// Check if we already closed the server
			if (listener.Client == null) return;

			// WSAECONNRESET:
			// The virtual circuit was reset by the remote side executing a hard or abortive close. 
			// The application should close the socket; it is no longer usable. On a UDP-datagram socket 
			// this error indicates a previous send operation resulted in an ICMP Port Unreachable message.
			// Note the spocket settings on creation of the server. It makes us ignore these resets.
			IPEndPoint senderEndpoint = new IPEndPoint(0, 0);
			Byte[] receiveBytes = null;
			try
			{
				receiveBytes = listener.EndReceive(ar, ref senderEndpoint);
			}
			catch (Exception e)
			{
				Log.Error("Unexpected end of transmission?", e);
				if (listener.Client != null)
				{
					try
					{
						listener.BeginReceive(ReceiveCallback, listener);
					}
					catch (ObjectDisposedException dex)
					{
						Log.Error("Unexpected end of transmission?", dex);
					}
				}

				return;
			}

			if (receiveBytes.Length != 0)
			{
				listener.BeginReceive(ReceiveCallback, listener);
				ServerInfo.AvailableBytes = listener.Available;
				ServerInfo.NumberOfPacketsInPerSecond++;
				ServerInfo.TotalPacketSizeIn += receiveBytes.Length;
				try
				{
					if (!GreylistManager.IsWhitelisted(senderEndpoint.Address) && GreylistManager.IsBlacklisted(senderEndpoint.Address)) return;
					if (GreylistManager.IsGreylisted(senderEndpoint.Address)) return;
					ProcessMessage(receiveBytes, senderEndpoint);
				}
				catch (Exception e)
				{
					//Log.Warn(string.Format("Process message error from: {0}", senderEndpoint.Address), e);
				}
			}
			else
			{
				//Log.Error("Unexpected end of transmission?");
			}
		}
Exemplo n.º 2
0
        private void Receive(object state)
        {
            var listener = (UdpClient)state;

            while (true)
            {
                // Check if we already closed the server
                if (listener.Client == null)
                {
                    return;
                }

                // WSAECONNRESET:
                // The virtual circuit was reset by the remote side executing a hard or abortive close.
                // The application should close the socket; it is no longer usable. On a UDP-datagram socket
                // this error indicates a previous send operation resulted in an ICMP Port Unreachable message.
                // Note the spocket settings on creation of the server. It makes us ignore these resets.
                IPEndPoint senderEndpoint = null;
                try
                {
                    ReadOnlyMemory <byte> receiveBytes = listener.Receive(ref senderEndpoint);
                    //UdpReceiveResult result = listener.ReceiveAsync().Result;
                    //senderEndpoint = result.RemoteEndPoint;
                    //byte[] receiveBytes = result.Buffer;

                    Interlocked.Increment(ref ServerInfo.NumberOfPacketsInPerSecond);
                    Interlocked.Add(ref ServerInfo.TotalPacketSizeInPerSecond, receiveBytes.Length);

                    if (receiveBytes.Length != 0)
                    {
                        _receiveThreadPool.QueueUserWorkItem(() =>
                        {
                            try
                            {
                                if (!GreylistManager.IsWhitelisted(senderEndpoint.Address) && GreylistManager.IsBlacklisted(senderEndpoint.Address))
                                {
                                    return;
                                }
                                if (GreylistManager.IsGreylisted(senderEndpoint.Address))
                                {
                                    return;
                                }

                                ProcessMessage(this, receiveBytes, senderEndpoint);
                            }
                            catch (Exception e)
                            {
                                Log.Warn($"Process message error from: {senderEndpoint.Address}", e);
                            }
                        });
                    }
                    else
                    {
                        Log.Warn("Unexpected end of transmission?");
                        continue;
                    }
                }
                catch (SocketException e)
                {
                    if (e.ErrorCode != 10004)
                    {
                        Log.Error("Unexpected end of receive", e);
                    }

                    if (listener.Client != null)
                    {
                        continue;
                    }

                    return;
                }
            }
        }