public static void WriteBytes(SafeMailslotHandle h, byte[] bytes) { var success = WriteFile(h, bytes, bytes.Length, out var written, IntPtr.Zero); if (!success || bytes.Length != written) { throw Throw("Failed to write message"); } }
public static byte[] ReadBytes(SafeMailslotHandle h, int count) { var ret = new byte[count]; var res = ReadFile(h, ret, count, out var read, IntPtr.Zero); if (!res) { throw Throw("Failed to read bytes"); } return(ret); }
public static int?GetInfo(SafeMailslotHandle h) { var result = GetMailslotInfo(h, IntPtr.Zero, out var ret, out var count, IntPtr.Zero); if (!result) { throw Throw("Failed to get info about mail slot"); } if (ret == MailslotNoMessage) { return(null); } return(ret); }
private bool disposedValue = false; // To detect redundant calls protected virtual void Dispose(bool disposing) { if (!disposedValue) { if (disposing) { // TODO: dispose managed state (managed objects). } if (_handle != null) { _handle.Close(); _handle = null; } disposedValue = true; } }
private MailSlot(string name, bool server) { try { if (server) { _handle = RawMailSlot.CreateMailSlot(name); } else { _handle = RawMailSlot.CreateFile(name); } } catch (Exception ex) { throw new Exception($"Unable to create new mailslot with path {name}", ex); } if (_handle.IsInvalid) { throw new Exception($"Unable to create new mailslot with path {name}", new Win32Exception()); } }
private static extern bool WriteFile(SafeMailslotHandle handle, byte[] bytes, int numBytesToWrite, out int numBytesWritten, IntPtr overlapped);
private static extern bool ReadFile(SafeMailslotHandle handle, byte[] bytes, int numBytesToRead, out int numBytesRead, IntPtr overlapped);
private static extern bool GetMailslotInfo(SafeMailslotHandle hMailslot, IntPtr lpMaxMessageSize, out int lpNextSize, out int lpMessageCount, IntPtr lpReadTimeout);