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; }
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); }
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); }
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); } }
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); } }
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); } }
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); } }
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); }
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); }