C#如何调用ZeroMQ

自己写的C#调用zeromq,希望有人能够用得着,其它的应该可以借用下

[StructLayout(LayoutKind.Sequential)]
    public unsafe struct zmq_msg_t
    {
        public fixed ushort _[32];
    }

    public enum Socket_Types
    {
        ZMQ_PAIR = 0,
        ZMQ_PUB = 1,
        ZMQ_SUB = 2,
        ZMQ_REQ = 3,
        ZMQ_REP = 4,
        ZMQ_DEALER = 5,
        ZMQ_ROUTER = 6,
        ZMQ_PULL = 7,
        ZMQ_PUSH = 8,
        ZMQ_XPUB = 9,
        ZMQ_XSUB = 10,
    }

    public enum Deprecated_Aliases
    {
        ZMQ_XREQ = Socket_Types.ZMQ_DEALER,
        ZMQ_XREP = Socket_Types.ZMQ_ROUTER
    }

    public enum Socket_Options
    {
        ZMQ_AFFINITY = 4,
        ZMQ_IDENTITY = 5,
        ZMQ_SUBSCRIBE = 6,
        ZMQ_UNSUBSCRIBE = 7,
        ZMQ_RATE = 8,
        ZMQ_RECOVERY_IVL = 9,
        ZMQ_SNDBUF = 11,
        ZMQ_RCVBUF = 12,
        ZMQ_RCVMORE = 13,
        ZMQ_FD = 14,
        ZMQ_EVENTS = 15,
        ZMQ_TYPE = 16,
        ZMQ_LINGER = 17,
        ZMQ_RECONNECT_IVL = 18,
        ZMQ_BACKLOG = 19,
        ZMQ_RECONNECT_IVL_MAX = 21,
        ZMQ_MAXMSGSIZE = 22,
        ZMQ_SNDHWM = 23,
        ZMQ_RCVHWM = 24,
        ZMQ_MULTICAST_HOPS = 25,
        ZMQ_RCVTIMEO = 27,
        ZMQ_SNDTIMEO = 28,
        ZMQ_IPV4ONLY = 31,
        ZMQ_LAST_ENDPOINT = 32,
        ZMQ_ROUTER_MANDATORY = 33,
        ZMQ_TCP_KEEPALIVE = 34,
        ZMQ_TCP_KEEPALIVE_CNT = 35,
        ZMQ_TCP_KEEPALIVE_IDLE = 36,
        ZMQ_TCP_KEEPALIVE_INTVL = 37,
        ZMQ_TCP_ACCEPT_FILTER = 38,
        ZMQ_DELAY_ATTACH_ON_CONNECT = 39,
        ZMQ_XPUB_VERBOSE = 40,
    }

    public enum Message_Options
    {
        ZMQ_MORE = 1,
    }

    public enum Send_Recv_Options
    {
        ZMQ_DONTWAIT = 1,
        ZMQ_SNDMORE = 2,
    }

    public unsafe class libzmq
    {
        [DllImport("libzmq.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern void* zmq_ctx_new();

        [DllImport("libzmq.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern void* zmq_socket(void* context, int type);

        [DllImport("libzmq.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern int zmq_close(void* socket);

        [DllImport("libzmq.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern int zmq_bind(void* socket, string addr);

        [DllImport("libzmq.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern int zmq_connect(void* socket, string addr);

        [DllImport("libzmq.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern int zmq_msg_send(zmq_msg_t* msg, void* socket, int flag);

        [DllImport("libzmq.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern int zmq_msg_recv(zmq_msg_t* msg, void* socket, int flag);

        [DllImport("libzmq.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern int zmq_msg_init(zmq_msg_t* msg);

        [DllImport("libzmq.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern int zmq_msg_init_size(zmq_msg_t* msg, int size);

        [DllImport("libzmq.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern int zmq_setsockopt(void* socket, int option, void* optval, int optvallen);

        [DllImport("libzmq.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern void* zmq_msg_data(zmq_msg_t* msg);

        [DllImport("libzmq.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern int zmq_msg_close(zmq_msg_t* msg);
    }

主要还是下面这部分

应答模式请求端    建立Socket

void* context_req = libzmq.zmq_ctx_new();
Data.socket_req = libzmq.zmq_socket(context_req, (int)Socket_Types.ZMQ_REQ);
int result = libzmq.zmq_connect(Data.socket_req, Data.Instance.Config.ServerIP + ":" + Data.Instance.Config.Req_Port);

发送命令以及接受


	/// 
        /// 发送命令
        /// 
        /// 命令
        public unsafe static void send_cmd(string cmd)
        {
            byte[] byte_send = Encoding.Default.GetBytes(cmd);
            zmq_msg_t* msg1 = stackalloc zmq_msg_t[1];
            libzmq.zmq_msg_init_size(msg1, byte_send.Length);
            void* ptr1 = libzmq.zmq_msg_data(msg1);
            Marshal.Copy(byte_send, 0, (IntPtr)ptr1, byte_send.Length);
            int result = libzmq.zmq_msg_send(msg1, Data.socket_req, 0);

            zmq_msg_t* msg = stackalloc zmq_msg_t[1];
            int rec_ionti = libzmq.zmq_msg_init(msg);
            int size = libzmq.zmq_msg_recv(msg, Data.socket_req, 0);
            void* ptr = libzmq.zmq_msg_data(msg);
            string str = Marshal.PtrToStringAnsi((IntPtr)ptr);
            libzmq.zmq_msg_close(msg);          
        }

广播模式接受端


void* context = libzmq.zmq_ctx_new();
void* socket = libzmq.zmq_socket(context, 2);
result = libzmq.zmq_connect(socket, Data.Instance.Config.ServerIP + ":" + Data.Instance.Config.Sub_Port);
int rc = libzmq.zmq_setsockopt(socket, (int)Socket_Options.ZMQ_SUBSCRIBE, (void*)null, 0);
zmq_msg_t* msg = stackalloc zmq_msg_t[1];
int rec_ionti = libzmq.zmq_msg_init(msg);
int size = libzmq.zmq_msg_recv(msg, socket, 0);
void* ptr = libzmq.zmq_msg_data(msg);
string str = Marshal.PtrToStringAnsi((IntPtr)ptr).Substring(0, size);


你可能感兴趣的:(C#)