Exemple #1
0
        public MessageQueue(int length, string queueName)
        {
            int cbMessage = Marshal.SizeOf <T>();

            if (cbMessage > 8192)
            {
                throw new ArgumentOutOfRangeException("By default, Linux limits size of queue messages to 8kb");
            }
            if (length > 10)
            {
                throw new ArgumentOutOfRangeException("By default, Linux limits the queue to 10 pending messages");
            }

            int    pid  = Process.GetCurrentProcess().Id;
            string name = $"/{ queueName }.{ pid }";

            MessageQueueAttributes mqa = new MessageQueueAttributes()
            {
                flags       = eMessageQueueFlags.NonBlocking,
                maxMessages = length,
                messageSize = cbMessage
            };
            int fd = MQ.mq_open(name, queueFlags, eFileAccess.OwnerAllAccess, ref mqa);

            if (fd < 0)
            {
                throw LibC.exception("mq_open", fd);
            }

            handle    = fd;
            this.name = name;
        }
Exemple #2
0
 public void finalize()
 {
     if (fd >= 0)
     {
         LibC.close(fd);
         fd = -1;
     }
 }
Exemple #3
0
        public static FileHandle openFile(string path, eFileFlags flags)
        {
            int fd = LibC.open(path, flags);

            if (fd >= 0)
            {
                return(new FileHandle(fd));
            }
            throw LibC.exception($"Unable to open the file \"{ path }\"", fd);
        }
Exemple #4
0
        public static EventHandle create()
        {
            int fd = LibC.eventfd(0, eEventFdFlags.NonBlocking | eEventFdFlags.DisableHandleInheritance);

            if (fd >= 0)
            {
                return(new EventHandle(fd));
            }
            throw LibC.exception($"Unable to create an event handle", fd);
        }
Exemple #5
0
        public static TimerHandle create()
        {
            int fd = LibC.timerfd_create(eClock.Monotonic, eTimerCreateFlags.DisableHandleInheritance);

            if (fd >= 0)
            {
                return(new TimerHandle(fd));
            }
            throwError($"Unable to create a timer handle", fd);
            throw new ApplicationException();
        }
Exemple #6
0
 public void reset()
 {
     unsafe
     {
         ulong counter = 0;
         int   res     = (int)LibC.read(fd, &counter, (UIntPtr)8);
         if (8 == res)
         {
             return;
         }
         throw LibC.exception("EventHandle.reset failed", res);
     }
 }
Exemple #7
0
 public void set()
 {
     unsafe
     {
         ulong increment = 1;
         int   res       = (int)LibC.write(fd, &increment, (UIntPtr)8);
         if (8 == res)
         {
             return;
         }
         throw LibC.exception("EventHandle.set failed", res);
     }
 }
Exemple #8
0
        public T read <T>(eControlCode code) where T : unmanaged
        {
            T result = new T();

            unsafe
            {
                T * pointer = &result;
                int res     = LibC.ioctl(fd, (uint)code, pointer);
                if (0 == res)
                {
                    return(result);
                }
                handleError(code, res);
                return(default);
Exemple #9
0
 static void throwError(string what, int returnedValue)
 {
     if (returnedValue == -1)
     {
         int    errno   = Marshal.GetLastWin32Error();
         string message = NativeErrorMessages.lookupLinuxError(errno);
         if (null != message)
         {
             throw new COMException($"{ what }: { message }", LibC.hresultFromLinux(errno));
         }
         throw new COMException($"{ what }: undocumented Linux error code { errno }", LibC.hresultFromLinux(errno));
     }
     throw new ApplicationException($"{ what }: unexpected result { returnedValue }");
 }
Exemple #10
0
        public static void mq_send <T>(int fd, T message, uint priority = 0) where T : unmanaged
        {
            size_t cb = (size_t)Marshal.SizeOf <T>();

            unsafe
            {
                T * ptr = &message;
                int res = mq_send(fd, ptr, cb, priority);
                if (0 == res)
                {
                    return;
                }
                throw LibC.exception("mq_send", res);
            }
        }
Exemple #11
0
        public static T mq_receive <T>(int fd, out uint priority) where T : unmanaged
        {
            size_t cb = (size_t)Marshal.SizeOf <T>();
            T      result;

            unsafe
            {
                T * ptr = &result;
                int res = (int)mq_receive(fd, ptr, cb, out priority);
                if (res == (int)cb)
                {
                    return(result);
                }
                throw LibC.exception("mq_receive", res);
            }
        }
Exemple #12
0
        public void dispose()
        {
            if (this.fd < 0)
            {
                return;
            }
            int fd = this.fd;

            this.fd = -1;
            int res = LibC.close(fd);

            if (0 == res)
            {
                return;
            }
            throw LibC.exception($"Error closing file descriptor { fd }", res);
        }
Exemple #13
0
 public void Dispose()
 {
     if (handle >= 0)
     {
         int res = MQ.mq_close(handle);
         if (res < 0)
         {
             throw LibC.exception("mq_close", res);
         }
         res = MQ.mq_unlink(name);
         if (res < 0)
         {
             throw LibC.exception("mq_unlink", res);
         }
     }
     GC.SuppressFinalize(this);
 }
Exemple #14
0
        public void dispose()
        {
            if (this.fd < 0)
            {
                return;
            }
            int fd = this.fd;

            this.fd = -1;
            int res = LibC.close(fd);

            if (0 == res)
            {
                return;
            }
            throwError($"Error closing timer handle { fd }", res);
        }
Exemple #15
0
        public void setTimeout(TimeSpan timeout)
        {
            if (fd < 0)
            {
                throw new ApplicationException("The timer was not created");
            }
            if (timeout.Ticks <= 0)
            {
                throw new ArgumentOutOfRangeException("The time is in the past");
            }

            sTimerDesc desc = default;

            desc.value = timeout;
            int res = LibC.timerfd_settime(fd, eTimerSetFlag.Relative, ref desc);

            if (res >= 0)
            {
                return;
            }
            throwError($"Unable to set the time", res);
            throw new ApplicationException();
        }