/* protected ZFrame(IntPtr data, int size) * : this(Alloc(data, size), size) * { } */ protected ZFrame(DispoIntPtr framePtr, int size) : base() { _framePtr = framePtr; _capacity = size; _position = 0; }
internal void WriteStringNative(string str, Encoding encoding, bool create) { if (str == null) { throw new ArgumentNullException("str"); } if (str == string.Empty) { if (create) { this._framePtr = CreateNative(0); this._capacity = 0; this._position = 0; } return; } byte[] bytes = encoding.GetBytes(str); if (create) { this._framePtr = CreateNative(bytes.Length); this._capacity = bytes.Length; this._position = 0; } Write(bytes, 0, bytes.Length); }
internal static DispoIntPtr CreateNative(int size) { var msg = DispoIntPtr.Alloc(zmq.sizeof_zmq_msg_t); ZError error; while (-1 == zmq.msg_init_size(msg, size)) { error = ZError.GetLastErr(); if (error == ZError.EINTR) { error = default(ZError); continue; } msg.Dispose(); if (error == ZError.ENOMEM) { throw new OutOfMemoryException("zmq_msg_init_size"); } throw new ZException(error, "zmq_msg_init_size"); } return(msg); }
/* protected ZFrame(IntPtr data, int size) * : this(Alloc(data, size), size) * { } */ protected ZFrame(DispoIntPtr frameIntPtr, int size) : base() { framePtr = frameIntPtr; length = size; position = 0; }
public static byte[] Decode(byte[] encoded) { int dataLen = encoded.Length; if (dataLen % 5 > 0) { throw new InvalidOperationException("encoded.Length must be divisible by 5"); } int destLen = (Int32)(encoded.Length * .8); var data = GCHandle.Alloc(encoded, GCHandleType.Pinned); using (var dest = DispoIntPtr.Alloc(destLen)) { if (IntPtr.Zero == zmq.z85_decode(dest, data.AddrOfPinnedObject())) { data.Free(); throw new InvalidOperationException(); } data.Free(); var decoded = new byte[destLen]; Marshal.Copy(dest, decoded, 0, decoded.Length); return(decoded); } }
public static byte[] Encode(byte[] decoded) { int dataLen = decoded.Length; if (dataLen % 4 > 0) { throw new InvalidOperationException("decoded.Length must be divisible by 4"); } int destLen = (Int32)(decoded.Length * 1.25); var data = GCHandle.Alloc(decoded, GCHandleType.Pinned); // the buffer dest must be one byte larger than destLen to accomodate the null termination character using (var dest = DispoIntPtr.Alloc(destLen + 1)) { if (IntPtr.Zero == zmq.z85_encode(dest, data.AddrOfPinnedObject(), dataLen)) { data.Free(); throw new InvalidOperationException(); } data.Free(); var bytes = new byte[destLen]; Marshal.Copy(dest, bytes, 0, destLen); return(bytes); } }
public void Dismiss() { if (_framePtr != null) { _framePtr.Dispose(); _framePtr = null; } }
public void Dismiss() { if (framePtr != null) { framePtr.Dispose(); framePtr = null; } GC.SuppressFinalize(this); }
public bool SetOption(ZSocketOption option, Int64 value) { int optionLength = Marshal.SizeOf(typeof(Int64)); using (var optionValue = DispoIntPtr.Alloc(optionLength)) { Marshal.WriteInt64(optionValue, value); return(SetOption(option, optionValue.Ptr, optionLength)); } }
/// public static bool Has(string capability) { using (var capabilityPtr = DispoIntPtr.AllocString(capability)) { if (0 < zmq.has(capabilityPtr)) { return(true); } } return(false); }
public bool SetOption(ZSocketOption option, string value) { if (value == null) { return(SetOptionNull(option)); } int optionLength; using (var optionValue = DispoIntPtr.AllocString(value, out optionLength)) { return(SetOption(option, optionValue, optionLength)); } }
private bool GetOption(ZSocketOption option, IntPtr optionValue, ref int optionLength) { EnsureNotDisposed(); using (var optionLengthP = DispoIntPtr.Alloc(IntPtr.Size)) { if (IntPtr.Size == 4) { Marshal.WriteInt32(optionLengthP.Ptr, optionLength); } else if (IntPtr.Size == 8) { Marshal.WriteInt64(optionLengthP.Ptr, (long)optionLength); } else { throw new PlatformNotSupportedException(); } ZError error; while (-1 == zmq.getsockopt(this._socketPtr, (int)option, optionValue, optionLengthP.Ptr)) { error = ZError.GetLastErr(); if (error == ZError.EINTR) { error = default(ZError); continue; } throw new ZException(error); } if (IntPtr.Size == 4) { optionLength = Marshal.ReadInt32(optionLengthP.Ptr); } else if (IntPtr.Size == 8) { optionLength = (int)Marshal.ReadInt64(optionLengthP.Ptr); } else { throw new PlatformNotSupportedException(); } } return(true); }
/// <summary> /// 准备 /// </summary> /// <param name="sockets"></param> /// <param name="events"></param> public void Prepare(ZPollEvent events, params ZSocket[] sockets) { Sockets = sockets; error = null; Size = sockets.Length; Ptr = DispoIntPtr.Alloc(sizeof(zmq_pollitem_windows_t) * sockets.Length); zmq_pollitem_windows_t *natives = (zmq_pollitem_windows_t *)Ptr.Ptr; for (int i = 0; i < Size; ++i) { zmq_pollitem_windows_t *native = natives + i; native->SocketPtr = sockets[i].SocketPtr; native->Events = (short)(events); native->ReadyEvents = (short)ZPollEvent.None; } }
public bool GetOption(ZSocketOption option, out string value) { value = null; int optionLength = MaxBinaryOptionSize; using (var optionValue = DispoIntPtr.Alloc(optionLength)) { if (GetOption(option, optionValue, ref optionLength)) { value = Marshal.PtrToStringAnsi(optionValue, optionLength); return(true); } return(false); } }
public bool GetOption(ZSocketOption option, out Int64 value) { value = default(Int64); int optionLength = Marshal.SizeOf(typeof(Int64)); using (var optionValue = DispoIntPtr.Alloc(optionLength)) { if (GetOption(option, optionValue.Ptr, ref optionLength)) { value = Marshal.ReadInt64(optionValue); return(true); } return(false); } }
public bool SetOption(ZSocketOption option, byte[] value) { if (value == null) { return(SetOptionNull(option)); } int optionLength = /* Marshal.SizeOf(typeof(byte)) * */ value.Length; using (var optionValue = DispoIntPtr.Alloc(optionLength)) { Marshal.Copy(value, 0, optionValue.Ptr, optionLength); return(SetOption(option, optionValue.Ptr, optionLength)); } }
public bool GetOption(ZSocketOption option, out byte[] value) { value = null; int optionLength = MaxBinaryOptionSize; using (var optionValue = DispoIntPtr.Alloc(optionLength)) { if (GetOption(option, optionValue, ref optionLength)) { value = new byte[optionLength]; Marshal.Copy(optionValue, value, 0, optionLength); return(true); } return(false); } }
public static void CurveKeypair(out byte[] publicKey, out byte[] secretKey) { const int destLen = 40; using (var publicKeyData = DispoIntPtr.Alloc(destLen + 1)) using (var secretKeyData = DispoIntPtr.Alloc(destLen + 1)) { if (0 != zmq.curve_keypair(publicKeyData, secretKeyData)) { throw new InvalidOperationException(); } publicKey = new byte[destLen]; Marshal.Copy(publicKeyData, publicKey, 0, destLen); secretKey = new byte[destLen]; Marshal.Copy(secretKeyData, secretKey, 0, destLen); } }
public static void CurvePublic(out byte[] publicKey, byte[] secretKey) { var data = GCHandle.Alloc(secretKey, GCHandleType.Pinned); const int destLen = 40; using (var publicKeyData = DispoIntPtr.Alloc(destLen + 1)) { if (0 != zmq.curve_public(publicKeyData, data.AddrOfPinnedObject())) { data.Free(); throw new InvalidOperationException(); } data.Free(); publicKey = new byte[destLen]; Marshal.Copy(publicKeyData, publicKey, 0, destLen); } }
/// <summary> /// Disconnect the specified endpoint. /// </summary> /// <param name="endpoint">A string consisting of a transport and an address, formatted as <c><em>transport</em>://<em>address</em></c>.</param> public bool Disconnect(string endpoint, out ZError error) { EnsureNotDisposed(); error = default(ZError); if (string.IsNullOrWhiteSpace(endpoint)) { throw new ArgumentException("IsNullOrWhiteSpace", "endpoint"); } using (var endpointPtr = DispoIntPtr.AllocString(endpoint)) { if (-1 == zmq.disconnect(_socketPtr, endpointPtr)) { error = ZError.GetLastErr(); return(false); } } return(true); }
public string GetOption(string property, out ZError error) { error = ZError.None; string result = null; using (var propertyPtr = DispoIntPtr.AllocString(property)) { IntPtr resultPtr; if (IntPtr.Zero == (resultPtr = zmq.msg_gets(this.framePtr, propertyPtr))) { error = ZError.GetLastErr(); return(null); } else { result = Marshal.PtrToStringAnsi(resultPtr); } } return(result); }
unsafe internal void WriteStringNative(string str, Encoding encoding, bool create) { if (str == null) { throw new ArgumentNullException("str"); } if (str == string.Empty) { if (create) { this.framePtr = CreateNative(0); this.length = 0; this.position = 0; } return; } int charCount = str.Length; Encoder enc = encoding.GetEncoder(); fixed(char *strP = str) { int byteCount = enc.GetByteCount(strP, charCount, false); if (create) { this.framePtr = CreateNative(byteCount); this.length = byteCount; this.position = 0; } else if (this.position + byteCount > this.Length) { // fail if frame is too small throw new InvalidOperationException(); } byteCount = enc.GetBytes(strP, charCount, (byte *)(this.DataPtr() + this.position), byteCount, true); this.position += byteCount; } }
internal static DispoIntPtr CreateEmptyNative() { var msg = DispoIntPtr.Alloc(zmq.sizeof_zmq_msg_t); ZError error; while (-1 == zmq.msg_init(msg)) { error = ZError.GetLastErr(); if (error == ZError.EINTR) { error = default(ZError); continue; } msg.Dispose(); throw new ZException(error, "zmq_msg_init"); } return(msg); }
/// <summary> /// Spawns a <see cref="ZSocketType.PAIR"/> socket that publishes all events for /// the specified socket over the inproc transport at the given endpoint. /// </summary> public static bool Monitor(this ZSocket socket, string endpoint, ZMonitorEvents eventsToMonitor, out ZError error) { if (socket == null) { throw new ArgumentNullException("socket"); } if (endpoint == null) { throw new ArgumentNullException("endpoint"); } if (endpoint == string.Empty) { throw new ArgumentException("Unable to publish socket events to an empty endpoint.", "endpoint"); } error = ZError.None; using (var endpointPtr = DispoIntPtr.AllocString(endpoint)) { while (-1 == zmq.socket_monitor(socket.SocketPtr, endpointPtr, (Int32)eventsToMonitor)) { error = ZError.GetLastErr(); if (error == ZError.EINTR) { error = default(ZError); continue; } return(false); } } return(true); }