Exemple #1
0
        /// <summary>
        /// Polls a File Descriptor for the passed in flags.
        /// </summary>
        /// <param name="fd">The descriptor to poll</param>
        /// <param name="events">The events to poll for</param>
        /// <param name="timeout">The amount of time to wait; -1 for infinite, 0 for immediate return, and a positive number is the number of milliseconds</param>
        /// <param name="triggered">The events that were returned by the poll call. May be PollEvents.POLLNONE in the case of a timeout.</param>
        /// <returns>An error or Error.SUCCESS.</returns>
        internal static unsafe Error Poll(SafeFileHandle fd, PollEvents events, int timeout, out PollEvents triggered)
        {
            bool gotRef = false;
            try
            {
                fd.DangerousAddRef(ref gotRef);

                var pollEvent = new PollEvent
                {
                    FileDescriptor = fd.DangerousGetHandle().ToInt32(),
                    Events = events,
                };

                uint unused;
                Error err = Poll(&pollEvent, 1, timeout, &unused);
                triggered = pollEvent.TriggeredEvents;
                return err;
            }
            finally
            {
                if (gotRef)
                {
                    fd.DangerousRelease();
                }
            }
        }
        public static Int16 FromPollEvents(PollEvents value)
        {
            Int16 rval;

            if (FromPollEvents(value, out rval) == -1)
            {
                ThrowArgumentException(value);
            }
            return(rval);
        }
Exemple #3
0
        /// <summary>
        /// Wait until a message is ready to be received/sent from this socket or until timeout is reached.
        /// If a message is available, the ReceiveReady/SendReady event is fired.
        /// </summary>
        /// <param name="timeout">a TimeSpan that represents the timeout-period</param>
        /// <returns>true if a message was available within the timeout, false otherwise</returns>
        public bool Poll(TimeSpan timeout)
        {
            PollEvents events = GetPollEvents();

            var result = Poll(events, timeout);

            InvokeEvents(this, result);

            return(result != PollEvents.None);
        }
        internal SocketEventArgs(ZmqSocket socket, PollEvents readyEvents)
        {
            if (socket == null)
            {
                throw new ArgumentNullException("socket");
            }

            this.Socket = socket;
            this.ReceiveReady = readyEvents.HasFlag(PollEvents.PollIn);
            this.SendReady = readyEvents.HasFlag(PollEvents.PollOut);
        }
Exemple #5
0
        internal SocketEventArgs(ZmqSocket socket, PollEvents readyEvents)
        {
            if (socket == null)
            {
                throw new ArgumentNullException("socket");
            }

            this.Socket       = socket;
            this.ReceiveReady = (((int)readyEvents) & ((int)PollEvents.PollIn)) > 0;
            this.SendReady    = (readyEvents & PollEvents.PollOut) > 0;
        }
Exemple #6
0
        internal SocketEventArgs(ZmqSocket socket, PollEvents readyEvents)
        {
            if (socket == null)
            {
                throw new ArgumentNullException("socket");
            }

            Socket       = socket;
            ReceiveReady = readyEvents.HasFlag(PollEvents.PollIn);
            SendReady    = readyEvents.HasFlag(PollEvents.PollOut);
        }
Exemple #7
0
        internal void InvokePollEvents(PollEvents readyEvents)
        {
            if (readyEvents.HasFlag(PollEvents.PollIn))
            {
                InvokeReceiveReady(readyEvents);
            }

            if (readyEvents.HasFlag(PollEvents.PollOut))
            {
                InvokeSendReady(readyEvents);
            }
        }
Exemple #8
0
        internal PollEvents GetPollEvents()
        {
            PollEvents events = PollEvents.PollError;

            if (m_sendReady != null)
            {
                events |= PollEvents.PollOut;
            }

            if (m_receiveReady != null)
            {
                events |= PollEvents.PollIn;
            }

            return(events);
        }
Exemple #9
0
        public PollItem(IntPtr socket, PollEvents pollEvents)
        {
            if (socket == IntPtr.Zero)
            {
                throw new ArgumentException("Expected a valid socket handle.", "socket");
            }

            Socket = socket;
#if UNIX
            FileDescriptor = 0;
#else
            FileDescriptor = IntPtr.Zero;
#endif
            Events      = (short)pollEvents;
            ReadyEvents = 0;
        }
Exemple #10
0
        internal PollEvents GetPollEvents()
        {
            PollEvents events = PollEvents.None;

            if (ReceiveReady != null)
            {
                events |= PollEvents.PollIn;
            }

            if (SendReady != null)
            {
                events |= PollEvents.PollOut;
            }

            return(events);
        }
        public int GetSocketOption(ZmqSocketOptions option)
        {
            if (m_ctxTerminated)
            {
                throw TerminatingException.Create();
            }

            if (option == ZmqSocketOptions.ReceiveMore)
            {
                return(m_rcvMore ? 1 : 0);
            }
            if (option == ZmqSocketOptions.Events)
            {
                try
                {
                    ProcessCommands(0, false);
                }
                catch (NetMQException ex)
                {
                    if (ex.ErrorCode == ErrorCode.EINTR || ex.ErrorCode == ErrorCode.ETERM)
                    {
                        return(-1);
                    }
                    else
                    {
                        Debug.Assert(false);

                        throw;
                    }
                }

                PollEvents val = 0;
                if (HasOut())
                {
                    val |= PollEvents.PollOut;
                }
                if (HasIn())
                {
                    val |= PollEvents.PollIn;
                }
                return((int)val);
            }

            return((int)GetSocketOptionX(option));
        }
Exemple #12
0
        internal void InvokeEvents(object sender, PollEvents events)
        {
            if (m_isClosed != 0)
            {
                return;
            }

            m_socketEventArgs.Init(events);

            if (events.HasIn())
            {
                m_receiveReady?.Invoke(sender, m_socketEventArgs);
            }

            if (events.HasOut())
            {
                m_sendReady?.Invoke(sender, m_socketEventArgs);
            }
        }
Exemple #13
0
        public Object GetSocketOptionX(ZmqSocketOptions option)
        {
            CheckContextTerminated();

            if (option == ZmqSocketOptions.ReceiveMore)
            {
                return(m_rcvMore);
            }

            if (option == ZmqSocketOptions.Handle)
            {
                return(m_mailbox.Handle);
            }

            if (option == ZmqSocketOptions.Events)
            {
                try
                {
                    ProcessCommands(0, false);
                }
                catch (TerminatingException)
                {
                    return(-1);
                }

                PollEvents val = 0;
                if (HasOut())
                {
                    val |= PollEvents.PollOut;
                }
                if (HasIn())
                {
                    val |= PollEvents.PollIn;
                }
                return(val);
            }
            //  If the socket type doesn't support the option, pass it to
            //  the generic option parser.
            return(m_options.GetSocketOption(option));
        }
Exemple #14
0
 public SelectItem(SocketBase socket, PollEvents @event)
 {
     Socket = socket;
     Event  = @event;
 }
Exemple #15
0
 public SelectItem(Socket fileDescriptor, PollEvents @event)
 {
     FileDescriptor = fileDescriptor;
     Event          = @event;
 }
Exemple #16
0
 /// <summary>Test whether <paramref name="pollEvents" /> has the <see cref="PollEvents.PollOut" /> flag set.</summary>
 public static bool HasOut(this PollEvents pollEvents)
 {
     return((pollEvents & PollEvents.PollOut) == PollEvents.PollOut);
 }
		private static int ToPollEvents (Int16 value, out PollEvents rval)
		{
			throw new System.NotImplementedException();
		}
Exemple #18
0
 public Item(NetMQSocket socket, PollEvents @event)
 {
     Socket = socket;
     Event = @event;
 }
 private static extern int ToPollEvents(Int16 value, out PollEvents rval);
 public static bool TryFromPollEvents(PollEvents value, out Int16 rval)
 {
     return(FromPollEvents(value, out rval) == 0);
 }
 private static extern int FromPollEvents(PollEvents value, out Int16 rval);
 /// <summary>
 /// Initialise the ReceiveReady and SendReady flags from the given PollEvents value.
 /// </summary>
 /// <param name="events">a PollEvents value that indicates whether the socket is ready to send or receive without blocking</param>
 internal void Init(PollEvents events)
 {
     IsReadyToReceive = events.HasIn();
     IsReadyToSend    = events.HasOut();
 }
Exemple #23
0
 public PollerPollItem(NetMQSocket socket, PollEvents events)
     : base(socket.SocketHandle, events)
 {
     m_socket = socket;
 }
Exemple #24
0
        /// <summary>
        /// Polls a File Descriptor for the passed in flags.
        /// </summary>
        /// <param name="fd">The descriptor to poll</param>
        /// <param name="events">The events to poll for</param>
        /// <param name="timeout">The amount of time to wait; -1 for infinite, 0 for immediate return, and a positive number is the number of milliseconds</param>
        /// <param name="triggered">The events that were returned by the poll call. May be PollEvents.POLLNONE in the case of a timeout.</param>
        /// <returns>An error or Error.SUCCESS.</returns>
        internal static unsafe Error Poll(SafeHandle fd, PollEvents events, int timeout, out PollEvents triggered)
        {
            bool gotRef = false;

            try
            {
                fd.DangerousAddRef(ref gotRef);

                var pollEvent = new PollEvent
                {
                    FileDescriptor = fd.DangerousGetHandle().ToInt32(),
                    Events         = events,
                };

                uint  unused;
                Error err = Poll(&pollEvent, 1, timeout, &unused);
                triggered = pollEvent.TriggeredEvents;
                return(err);
            }
            finally
            {
                if (gotRef)
                {
                    fd.DangerousRelease();
                }
            }
        }
		private static int FromPollEvents (PollEvents value, out Int16 rval)
		{
			throw new System.NotImplementedException();
		}
 public static bool TryToPollEvents(Int16 value, out PollEvents rval)
 {
     return(ToPollEvents(value, out rval) == 0);
 }
Exemple #27
0
 public PollerPollItem(NetMQSocket socket, PollEvents events)
     : base(socket.SocketHandle, events)
 {
     m_socket = socket;
 }
 public Item(NetMQSocket socket, PollEvents @event)
 {
     Socket = socket;
     Event  = @event;
 }
 internal void Init(PollEvents events)
 {
     this.ReceiveReady = events.HasFlag(PollEvents.PollIn);
     this.SendReady = events.HasFlag(PollEvents.PollOut);
 }
Exemple #30
0
 /// <summary>
 /// Initialise the ReceiveReady and SendReady flags from the given PollEvents value.
 /// </summary>
 /// <param name="events">a PollEvents value that indicates whether the socket is ready to send or receive without blocking</param>
 internal void Init(PollEvents events)
 {
     IsReadyToReceive = events.HasIn();
     IsReadyToSend = events.HasOut();
 }
Exemple #31
0
 /// <summary>Test whether <paramref name="pollEvents" /> has the <see cref="PollEvents.PollIn" /> flag set.</summary>
 public static bool HasIn(this PollEvents pollEvents)
 {
     return((pollEvents & PollEvents.PollIn) == PollEvents.PollIn);
 }
		private static extern int FromPollEvents (PollEvents value, out Int16 rval);
Exemple #33
0
 /// <summary>Test whether <paramref name="pollEvents" /> has the <see cref="PollEvents.PollError" /> flag set.</summary>
 public static bool HasError(this PollEvents pollEvents)
 {
     return((pollEvents & PollEvents.PollError) == PollEvents.PollError);
 }
Exemple #34
0
 private static int FromPollEvents(PollEvents value, out Int16 rval)
 {
     throw new System.NotImplementedException();
 }
Exemple #35
0
        public bool Select(SelectItem[] items, int itemsCount, int timeout)
        {
            if (items == null)
            {
                throw new FaultException();
            }
            if (itemsCount == 0)
            {
                if (timeout == 0)
                {
                    return(false);
                }
                Thread.Sleep(timeout);
                return(false);
            }

            bool firstPass      = true;
            int  numberOfEvents = 0;

            Stopwatch stopwatch = null;

            while (true)
            {
                int currentTimeoutMicroSeconds;

                if (firstPass)
                {
                    currentTimeoutMicroSeconds = 0;
                }
                else if (timeout == -1)
                {
                    currentTimeoutMicroSeconds = -1;
                }
                else
                {
                    currentTimeoutMicroSeconds = (int)((timeout - stopwatch.ElapsedMilliseconds) * 1000);

                    if (currentTimeoutMicroSeconds < 0)
                    {
                        currentTimeoutMicroSeconds = 0;
                    }
                }

                m_checkRead.Clear();
                m_checkWrite.Clear();
                m_checkError.Clear();

                for (int i = 0; i < itemsCount; i++)
                {
                    var pollItem = items[i];

                    if (pollItem.Socket != null)
                    {
                        if (pollItem.Event != PollEvents.None && pollItem.Socket.Handle.Connected)
                        {
                            m_checkRead.Add(pollItem.Socket.Handle);
                        }
                    }
                    else
                    {
                        if ((pollItem.Event & PollEvents.PollIn) == PollEvents.PollIn)
                        {
                            m_checkRead.Add(pollItem.FileDescriptor);
                        }

                        if ((pollItem.Event & PollEvents.PollOut) == PollEvents.PollOut)
                        {
                            m_checkWrite.Add(pollItem.FileDescriptor);
                        }
                    }
                }


                try
                {
                    SocketUtility.Select(m_checkRead, m_checkWrite, m_checkError, currentTimeoutMicroSeconds);
                }
                catch (SocketException)
                {
                    throw new FaultException();
                }

                for (int i = 0; i < itemsCount; i++)
                {
                    var selectItem = items[i];

                    selectItem.ResultEvent = PollEvents.None;

                    if (selectItem.Socket != null)
                    {
                        PollEvents events = (PollEvents)selectItem.Socket.GetSocketOption(ZmqSocketOptions.Events);

                        if ((selectItem.Event & PollEvents.PollIn) == PollEvents.PollIn && (events & PollEvents.PollIn) == PollEvents.PollIn)
                        {
                            selectItem.ResultEvent |= PollEvents.PollIn;
                        }

                        if ((selectItem.Event & PollEvents.PollOut) == PollEvents.PollOut &&
                            (events & PollEvents.PollOut) == PollEvents.PollOut)
                        {
                            selectItem.ResultEvent |= PollEvents.PollOut;
                        }
                    }
                    else
                    {
                        if (m_checkRead.Contains(selectItem.FileDescriptor))
                        {
                            selectItem.ResultEvent |= PollEvents.PollIn;
                        }

                        if (m_checkWrite.Contains(selectItem.FileDescriptor))
                        {
                            selectItem.ResultEvent |= PollEvents.PollOut;
                        }
                    }

                    if (selectItem.ResultEvent != PollEvents.None)
                    {
                        numberOfEvents++;
                    }
                }

                if (timeout == 0)
                {
                    break;
                }

                if (numberOfEvents > 0)
                {
                    break;
                }

                if (timeout < 0)
                {
                    if (firstPass)
                    {
                        firstPass = false;
                    }

                    continue;
                }

                if (firstPass)
                {
                    stopwatch = Stopwatch.StartNew();
                    firstPass = false;
                    continue;
                }

                if (stopwatch.ElapsedMilliseconds > timeout)
                {
                    break;
                }
            }

            return(numberOfEvents > 0);
        }
Exemple #36
0
 public PollItem(SocketBase socket, PollEvents events)
 {
     Socket = socket;
     Events = events;
     FileDescriptor = null;
 }
		public static bool TryFromPollEvents (PollEvents value, out Int16 rval)
		{
			return FromPollEvents (value, out rval) == 0;
		}
Exemple #38
0
 public PollItem(Socket fileDescriptor, PollEvents events)
 {
     Socket         = null;
     Events         = events;
     FileDescriptor = fileDescriptor;
 }
Exemple #39
0
 private static int ToPollEvents(Int16 value, out PollEvents rval)
 {
     throw new System.NotImplementedException();
 }
		public static Int16 FromPollEvents (PollEvents value)
		{
			Int16 rval;
			if (FromPollEvents (value, out rval) == -1)
				ThrowArgumentException (value);
			return rval;
		}
Exemple #41
0
 public PollItem(Socket fileDescriptor, PollEvents events)
 {
     Socket = null;
     Events = events;
     FileDescriptor = fileDescriptor;
 }
Exemple #42
0
 public SelectItem(SocketBase socket, PollEvents @event)
 {
     Socket = socket;
     Event = @event;
 }
Exemple #43
0
        public static int Poll(PollItem[] items, int itemsCount, int timeout)
        {
            if (items == null)
            {
                throw NetMQException.Create(ErrorCode.EFAULT);
            }
            if (itemsCount == 0)
            {
                if (timeout <= 0)
                {
                    return(0);
                }
                Thread.Sleep(timeout);
                return(0);
            }

            bool firstPass = true;
            int  nevents   = 0;

            List <Socket> writeList = new List <Socket>();
            List <Socket> readList  = new List <Socket>();
            List <Socket> errorList = new List <Socket>();

            for (int i = 0; i < itemsCount; i++)
            {
                var pollItem = items[i];

                if (pollItem.Socket != null)
                {
                    if (pollItem.Events != PollEvents.None)
                    {
                        readList.Add(pollItem.Socket.FD);
                    }
                }
                else
                {
                    if ((pollItem.Events & PollEvents.PollIn) == PollEvents.PollIn)
                    {
                        readList.Add(pollItem.FileDescriptor);
                    }

                    if ((pollItem.Events & PollEvents.PollOut) == PollEvents.PollOut)
                    {
                        writeList.Add(pollItem.FileDescriptor);
                    }

                    if ((pollItem.Events & PollEvents.PollError) == PollEvents.PollError)
                    {
                        errorList.Add(pollItem.FileDescriptor);
                    }
                }
            }

            List <Socket> inset    = new List <Socket>(readList.Count);
            List <Socket> outset   = new List <Socket>(writeList.Count);
            List <Socket> errorset = new List <Socket>(errorList.Count);

            Stopwatch stopwatch = null;

            while (true)
            {
                int currentTimeoutMicroSeconds;

                if (firstPass)
                {
                    currentTimeoutMicroSeconds = 0;
                }
                else if (timeout == -1)
                {
                    currentTimeoutMicroSeconds = -1;
                }
                else
                {
                    currentTimeoutMicroSeconds = (int)((timeout - stopwatch.ElapsedMilliseconds) * 1000);

                    if (currentTimeoutMicroSeconds < 0)
                    {
                        currentTimeoutMicroSeconds = 0;
                    }
                }

                inset.AddRange(readList.Where(x => x.Connected || x.ProtocolType != ProtocolType.Tcp));
                outset.AddRange(writeList.Where(x => x.Connected || x.ProtocolType != ProtocolType.Tcp));
                errorset.AddRange(errorList.Where(x => x.Connected || x.ProtocolType != ProtocolType.Tcp));

                try
                {
                    System.Net.Sockets.Socket.Select(inset, outset, errorset, currentTimeoutMicroSeconds);
                }
                catch (SocketException ex)
                {
                    throw NetMQException.Create(ErrorCode.ESOCKET, ex);
                }

                for (int i = 0; i < itemsCount; i++)
                {
                    var pollItem = items[i];

                    pollItem.ResultEvent = PollEvents.None;

                    if (pollItem.Socket != null)
                    {
                        PollEvents events = (PollEvents)GetSocketOption(pollItem.Socket, ZmqSocketOptions.Events);

                        if ((pollItem.Events & PollEvents.PollIn) == PollEvents.PollIn &&
                            (events & PollEvents.PollIn) == PollEvents.PollIn)
                        {
                            pollItem.ResultEvent |= PollEvents.PollIn;
                        }

                        if ((pollItem.Events & PollEvents.PollOut) == PollEvents.PollOut &&
                            (events & PollEvents.PollOut) == PollEvents.PollOut)
                        {
                            pollItem.ResultEvent |= PollEvents.PollOut;
                        }
                    }
                    else
                    {
                        if (inset.Contains(pollItem.FileDescriptor))
                        {
                            pollItem.ResultEvent |= PollEvents.PollIn;
                        }

                        if (outset.Contains(pollItem.FileDescriptor))
                        {
                            pollItem.ResultEvent |= PollEvents.PollOut;
                        }

                        if (errorset.Contains(pollItem.FileDescriptor))
                        {
                            pollItem.ResultEvent |= PollEvents.PollError;
                        }
                    }

                    if (pollItem.ResultEvent != PollEvents.None)
                    {
                        nevents++;
                    }
                }

                inset.Clear();
                outset.Clear();
                errorset.Clear();

                if (timeout == 0)
                {
                    break;
                }

                if (nevents > 0)
                {
                    break;
                }

                if (timeout < 0)
                {
                    if (firstPass)
                    {
                        firstPass = false;
                    }

                    continue;
                }

                if (firstPass)
                {
                    stopwatch = Stopwatch.StartNew();
                    firstPass = false;
                    continue;
                }

                if (stopwatch.ElapsedMilliseconds > timeout)
                {
                    break;
                }
            }

            return(nevents);
        }
 /// <summary>
 /// Initialize the ReceiveReady and SendReady flags from the given PollEvents value.
 /// </summary>
 /// <param name="events">a PollEvents value that indicates whether the socket is ready to send or receive without blocking</param>
 internal void Init(PollEvents events)
 {
     ReceiveReady = events.HasFlag(PollEvents.PollIn);
     SendReady    = events.HasFlag(PollEvents.PollOut);
 }
		private static extern int ToPollEvents (Int16 value, out PollEvents rval);
 /// <summary>
 /// Initialise the ReceiveReady and SendReady flags from the given PollEvents value.
 /// </summary>
 /// <param name="events">a PollEvents value that indicates whether the socket is ready to send or receive without blocking</param>
 internal void Init(PollEvents events)
 {
     IsReadyToReceive = events.HasFlag(PollEvents.PollIn);
     IsReadyToSend = events.HasFlag(PollEvents.PollOut);
 }
Exemple #47
0
 public SelectItem(Socket fileDescriptor, PollEvents @event)
 {
     FileDescriptor = fileDescriptor;
     Event = @event;
 }
		public static bool TryToPollEvents (Int16 value, out PollEvents rval)
		{
			return ToPollEvents (value, out rval) == 0;
		}