Пример #1
0
        private object InternalGetOption(Type retType, int option)
        {
            Func <IntPtr, Int64, object> unmarshaller;
            Int64 bufferLen;

            BuildUnmarshaller(retType, out unmarshaller, out bufferLen);

            object retValue = null;

            MarshalExt.AllocAndRun(sizePtr =>
            {
                Marshal.WriteInt64(sizePtr, bufferLen);

                MarshalExt.AllocAndRun(bufferPtr =>
                {
                    var res = Native.Socket.zmq_getsockopt(this.SocketPtr, option, bufferPtr, sizePtr);
                    if (res == Native.ErrorCode)
                    {
                        Native.ThrowZmqError();
                    }

                    retValue = unmarshaller(bufferPtr, bufferLen);
                }, (int)bufferLen);
            }, sizeof(Int64));

            return(retValue);
        }
Пример #2
0
        private void InternalSetOption(int option, Type valueType, object value, bool ignoreError)
        {
            // it would be great to assert that the option and value match their expected type
            // as it varies per option

            Action <IntPtr, object> marshaller = null;
            var bufferSize = 0;

            if (valueType == typeof(int))
            {
                bufferSize = sizeof(int);
                marshaller = (ptr, v) => Marshal.WriteInt32(ptr, (int)v);
            }
            else if (valueType == typeof(Int64))
            {
                bufferSize = sizeof(Int64);
                marshaller = (ptr, v) => Marshal.WriteInt64(ptr, (long)v);
            }
            else if (valueType == typeof(bool))
            {
                bufferSize = sizeof(int);

                marshaller = (ptr, v) =>
                {
                    var b = (bool)v;
                    Marshal.WriteInt32(ptr, b ? 1 : 0);
                };
            }
            else if (valueType == typeof(byte[]))
            {
                var b = (byte[])value;
                bufferSize = b.Length;

                marshaller = (ptr, v) =>
                {
                    var array = (byte[])v;
                    Marshal.Copy(array, 0, ptr, array.Length);
                };
            }
            else if (valueType == typeof(string))
            {
                var s = (string)value;
                bufferSize = Encoding.UTF8.GetBytes(s).Length;

                marshaller = (ptr, v) =>
                {
                    var vStr  = (string)value;
                    var array = Encoding.UTF8.GetBytes(vStr);
                    Marshal.Copy(array, 0, ptr, array.Length);
                };
            }
            else
            {
                throw new ArgumentException("Unsupported type for value " + valueType.Name);
            }

            MarshalExt.AllocAndRun(bufPtr =>
            {
                marshaller(bufPtr, value);

                var res = Native.Socket.zmq_setsockopt(this.SocketPtr, option, bufPtr, bufferSize);
                if (!ignoreError && res == Native.ErrorCode)
                {
                    Native.ThrowZmqError("setting option " + option + " with value " + value);
                }
            }, bufferSize);
        }