示例#1
0
文件: zmq.cs 项目: ssh352/quantmodel
        /// <summary>
        /// Polls the supplied items for events
        /// </summary>
        /// <param name="items">Items to Poll</param>
        /// <param name="timeout">Timeout</param>
        /// <returns>Number of Poll items with events</returns>
        public int Poll(PollItem[] items, long timeout)
        {
            int    sizeOfZPL = Marshal.SizeOf(typeof(ZMQPollItem));
            IntPtr offset    = IntPtr.Zero;
            int    rc        = 0;

            using (DisposableIntPtr itemList = new DisposableIntPtr(sizeOfZPL * items.Length)) {
                offset = itemList.Ptr;
                foreach (PollItem item in items)
                {
                    Marshal.StructureToPtr(item.ZMQPollItem, offset, false);
                    offset = new IntPtr(offset.ToInt32() + sizeOfZPL);
                }
                rc = C.zmq_poll(itemList.Ptr, items.Length, timeout);
                if (rc > 0)
                {
                    offset = itemList.Ptr;
                    for (int index = 0; index < items.Length; index++)
                    {
                        items[index].ZMQPollItem = (ZMQPollItem)
                                                   Marshal.PtrToStructure(offset, typeof(ZMQPollItem));
                        offset = new IntPtr(offset.ToInt32() + sizeOfZPL);
                    }
                    foreach (PollItem item in items)
                    {
                        item.FireEvents();
                    }
                }
                else if (rc < 0)
                {
                    throw new Exception();
                }
            }
            return(rc);
        }
示例#2
0
文件: zmq.cs 项目: ssh352/quantmodel
 /// <summary>
 /// Set Socket Option
 /// </summary>
 /// <param name="option">Socket Option</param>
 /// <param name="value">Option value</param>
 /// <exception cref="ZMQ.Exception">ZMQ Exception</exception>
 public void SetSockOpt(SocketOpt option, byte[] value)
 {
     using (DisposableIntPtr valPtr = new DisposableIntPtr(value.Length)) {
         Marshal.Copy(value, 0, valPtr.Ptr, value.Length);
         if (C.zmq_setsockopt(ptr, (int)option, valPtr.Ptr, value.Length) != 0)
         {
             throw new Exception();
         }
     }
 }
示例#3
0
文件: zmq.cs 项目: ssh352/quantmodel
        /// <summary>
        /// Set Socket Option
        /// </summary>
        /// <param name="option">Socket Option</param>
        /// <param name="value">Option value</param>
        /// <exception cref="ZMQ.Exception">ZMQ Exception</exception>
        public void SetSockOpt(SocketOpt option, long value)
        {
            int sizeOfValue = Marshal.SizeOf(typeof(long));

            using (DisposableIntPtr valPtr = new DisposableIntPtr(sizeOfValue)) {
                WriteSizeT(valPtr.Ptr, value);
                if (C.zmq_setsockopt(ptr, (int)option, valPtr.Ptr, sizeOfValue) != 0)
                {
                    throw new Exception();
                }
            }
        }
示例#4
0
文件: zmq.cs 项目: ssh352/quantmodel
        /// <summary>
        /// Get the socket option value
        /// </summary>
        /// <param name="option">Socket Option</param>
        /// <returns>Option value</returns>
        /// <exception cref="ZMQ.Exception">ZMQ Exception</exception>
        public object GetSockOpt(SocketOpt option)
        {
            object output;

            using (DisposableIntPtr len = new DisposableIntPtr(IntPtr.Size)) {
                if (option == SocketOpt.IDENTITY)
                {
                    using (DisposableIntPtr val = new DisposableIntPtr(255)) {
                        WriteSizeT(len.Ptr, 255);
                        if (C.zmq_getsockopt(ptr, (int)option, val.Ptr, len.Ptr) != 0)
                        {
                            throw new Exception();
                        }
                        byte[] buffer = new byte[Convert.ToInt32(ReadSizeT(len.Ptr))];
                        Marshal.Copy(val.Ptr, buffer, 0, buffer.Length);
                        output = buffer;
                    }
                }
                else
                {
                    using (DisposableIntPtr val = new DisposableIntPtr(IntPtr.Size)) {
                        WriteSizeT(len.Ptr, Marshal.SizeOf(typeof(long)));
                        if (C.zmq_getsockopt(ptr, (int)option, val.Ptr, len.Ptr) != 0)
                        {
                            throw new Exception();
                        }
                        //Unchecked casting of uint64 options
                        if (option == SocketOpt.HWM || option == SocketOpt.AFFINITY ||
                            option == SocketOpt.SNDBUF || option == SocketOpt.RCVBUF)
                        {
                            output = unchecked ((ulong)Marshal.ReadInt64(val.Ptr));
                        }
                        else
                        {
                            output = Marshal.ReadInt64(val.Ptr);
                        }
                    }
                }
            }
            return(output);
        }