public int Forward(IntPtr destinationHandle) { if (_msg.Init() == -1) { return(-1); } int receiveMore; int bytesSent; do { if (LibZmq.zmq_msg_recv(_msg, SocketHandle, 0) == -1) { return(-1); } if (GetReceiveMore(out receiveMore) == -1) { return(-1); } if ((bytesSent = LibZmq.zmq_msg_send(_msg, destinationHandle, receiveMore == 1 ? (int)SocketFlags.SendMore : 0)) == -1) { return(-1); } }while (receiveMore == 1); if (_msg.Close() == -1) { return(-1); } return(bytesSent); }
public int Receive(byte[] buffer, int flags) { if (LibZmq.zmq_msg_init_size(_message, buffer.Length) == -1) { return(-1); } int bytesReceived = RetryIfInterrupted(() => LibZmq.zmq_recvmsg(SocketHandle, _message, flags)); if (bytesReceived == 0 && LibZmq.MajorVersion < 3) { // 0MQ 2.x does not return number of bytes received bytesReceived = LibZmq.zmq_msg_size(_message); } if (bytesReceived > 0) { Marshal.Copy(LibZmq.zmq_msg_data(_message), buffer, 0, bytesReceived); } if (LibZmq.zmq_msg_close(_message) == -1) { return(-1); } return(bytesReceived); }
public int Forward(IntPtr destinationHandle) { if (LibZmq.zmq_msg_init(_message) == -1) { return(-1); } int receiveMore; int bytesSent; do { if (LibZmq.zmq_recvmsg(SocketHandle, _message, 0) == -1) { return(-1); } if (GetReceiveMore(out receiveMore) == -1) { return(-1); } if ((bytesSent = LibZmq.zmq_sendmsg(destinationHandle, _message, receiveMore == 1 ? (int)SocketFlags.SendMore : 0)) == -1) { return(-1); } }while (receiveMore == 1); if (LibZmq.zmq_msg_close(_message) == -1) { return(-1); } return(bytesSent); }
public int Send(byte[] buffer, int size, int flags) { if (LibZmq.zmq_msg_init_size(_message, size) == -1) { return(-1); } if (size > 0) { Marshal.Copy(buffer, 0, LibZmq.zmq_msg_data(_message), size); } int bytesSent = RetryIfInterrupted(() => LibZmq.zmq_sendmsg(SocketHandle, _message, flags)); if (bytesSent == 0 && LibZmq.MajorVersion < 3) { // 0MQ 2.x does not report number of bytes sent, so this may be inaccurate/misleading bytesSent = size; } if (LibZmq.zmq_msg_close(_message) == -1) { return(-1); } return(bytesSent); }
public int Init() { int rc = LibZmq.zmq_msg_init(_ptr); _initialized = true; return(rc); }
public int Init(int size) { int rc = LibZmq.zmq_msg_init_size(_ptr, size); _initialized = true; return(rc); }
public int Close() { int rc = LibZmq.zmq_msg_close(_ptr); _initialized = false; return(rc); }
public int SetSocketOption(int option, int value) { using (var optionValue = new DisposableIntPtr(Marshal.SizeOf(typeof(int)))) { Marshal.WriteInt32(optionValue, value); return(RetryIfInterrupted(() => LibZmq.zmq_setsockopt(SocketHandle, option, optionValue, sizeof(int)))); } }
public int SetSocketOption(int option, ulong value) { using (var optionValue = new DisposableIntPtr(Marshal.SizeOf(typeof(ulong)))) { Marshal.WriteInt64(optionValue, unchecked (Convert.ToInt64(value))); return(RetryIfInterrupted(() => LibZmq.zmq_setsockopt(SocketHandle, option, optionValue, sizeof(ulong)))); } }
public int SetSocketOption(int option, byte[] value) { using (var optionValue = new DisposableIntPtr(value.Length)) { Marshal.Copy(value, 0, optionValue, value.Length); return(RetryIfInterrupted(() => LibZmq.zmq_setsockopt(SocketHandle, option, optionValue, value.Length))); } }
public static int IfInterrupted <T1, T2, T3>(Func <T1, T2, T3, int> operation, T1 arg1, T2 arg2, T3 arg3) { int rc; do { rc = operation(arg1, arg2, arg3); }while (rc == -1 && LibZmq.zmq_errno() == ErrorCode.EINTR); return(rc); }
private static int RetryIfInterrupted(Func <int> func) { int rc; do { rc = func(); }while (rc == -1 && LibZmq.zmq_errno() == ErrorCode.EINTR); return(rc); }
public int Initialize() { try { ContextHandle = LibZmq.zmq_ctx_new(); } catch (Exception ex) { } return(ContextHandle == IntPtr.Zero ? -1 : 0); }
public int Copy(ZmqMsgT dest) { if (!_initialized) { dest.Close(); return(0); } int rc = LibZmq.zmq_msg_copy(dest._ptr, _ptr); dest._initialized = rc == 0; return(rc); }
public int GetSocketOption(int option, out ulong value) { using (var optionLength = new DisposableIntPtr(IntPtr.Size)) using (var optionValue = new DisposableIntPtr(Marshal.SizeOf(typeof(ulong)))) { Marshal.WriteInt32(optionLength, sizeof(ulong)); int rc = RetryIfInterrupted(() => LibZmq.zmq_getsockopt(SocketHandle, option, optionValue, optionLength)); value = unchecked (Convert.ToUInt64(Marshal.ReadInt64(optionValue))); return(rc); } }
public int Close() { // Allow Close to be called repeatedly without failure if (SocketHandle == IntPtr.Zero) { return 0; } int rc = LibZmq.zmq_close(SocketHandle); SocketHandle = IntPtr.Zero; return rc; }
public int GetSocketOption(int option, out byte[] value) { using (var optionLength = new DisposableIntPtr(IntPtr.Size)) using (var optionValue = new DisposableIntPtr(MaxBinaryOptionSize)) { Marshal.WriteInt32(optionLength, MaxBinaryOptionSize); int rc = RetryIfInterrupted(() => LibZmq.zmq_getsockopt(SocketHandle, option, optionValue, optionLength)); value = new byte[Marshal.ReadInt32(optionLength)]; Marshal.Copy(optionValue, value, 0, value.Length); return(rc); } }
public static ErrorDetails GetLastError() { int errorCode = GetErrorCode(); if (KnownErrors.ContainsKey(errorCode)) { return(KnownErrors[errorCode]); } string message = Marshal.PtrToStringAnsi(LibZmq.zmq_strerror(errorCode)); var errorDetails = new ErrorDetails(errorCode, message); KnownErrors[errorCode] = errorDetails; return(errorDetails); }
public static ErrorDetails GetLastError() { int errorCode = GetErrorCode(); if (KnownErrors.ContainsKey(errorCode)) { return(KnownErrors[errorCode]); } string message = Marshal.PtrToStringUni(LibZmq.zmq_strerror(errorCode)); byte[] temp = Encoding.Unicode.GetBytes(message); message = Encoding.ASCII.GetString(temp, 0, temp.Length).Replace('\0', ' ').Trim();; var errorDetails = new ErrorDetails(errorCode, message); KnownErrors[errorCode] = errorDetails; return(errorDetails); }
public void Terminate() { if (ContextHandle == IntPtr.Zero) { return; } while (LibZmq.zmq_term(ContextHandle) != 0) { int errorCode = ErrorProxy.GetErrorCode(); // If zmq_term fails, valid return codes are EFAULT or EINTR. If EINTR is set, termination // was interrupted by a signal and may be safely retried. if (errorCode == ErrorCode.EFAULT) { // This indicates an invalid context was passed in. There's nothing we can do about it here. // It's arguably not a fatal error, so throwing an exception would be bad seeing as this may // run inside a finalizer. break; } } ContextHandle = IntPtr.Zero; }
public byte[] Receive(byte[] buffer, int flags, out int size) { size = -1; if (LibZmq.zmq_msg_init(_message) == -1) { return(buffer); } int bytesReceived = RetryIfInterrupted(() => LibZmq.zmq_recvmsg(SocketHandle, _message, flags)); if (bytesReceived >= 0) { if (bytesReceived == 0 && LibZmq.MajorVersion < 3) { // 0MQ 2.x does not return number of bytes received bytesReceived = LibZmq.zmq_msg_size(_message); } size = bytesReceived; if (buffer == null || size > buffer.Length) { buffer = new byte[size]; } Marshal.Copy(LibZmq.zmq_msg_data(_message), buffer, 0, size); } if (LibZmq.zmq_msg_close(_message) == -1) { size = -1; } return(buffer); }
public IntPtr CreateSocket(int socketType) { return(LibZmq.zmq_socket(ContextHandle, socketType)); }
public IntPtr Data() { return(LibZmq.zmq_msg_data(_ptr)); }
public int RegisterMonitor(LibZmq.MonitorFuncCallback callback) { MonitorRegistered = true; return LibZmq.zmq_ctx_set_monitor(ContextHandle, callback); }
public int Initialize() { ContextHandle = LibZmq.zmq_init(ThreadPoolSize); return(ContextHandle == IntPtr.Zero ? -1 : 0); }
public int Monitor(string endpoint, int events) { return LibZmq.zmq_socket_monitor(SocketHandle, Encoding.ASCII.GetBytes(endpoint), events); }
public int RegisterMonitor(LibZmq.MonitorFuncCallback callback) { _callback = callback; return LibZmq.zmq_ctx_set_monitor(ContextHandle, _callback); }
public int Disconnect(string endpoint) { return LibZmq.zmq_disconnect(SocketHandle, Encoding.ASCII.GetBytes(endpoint)); }
public int Unbind(string endpoint) { return LibZmq.zmq_unbind(SocketHandle, Encoding.ASCII.GetBytes(endpoint)); }
public int SetContextOption(int option, int value) { return(LibZmq.zmq_ctx_set(ContextHandle, option, value)); }
public static int GetErrorCode() { return(LibZmq.zmq_errno()); }
public int Size() { return(LibZmq.zmq_msg_size(_ptr)); }