/// <summary>
        /// 远程方法调用
        /// </summary>
        /// <param name="recvAddress">接收节点的虚拟IP地址,如3.1/param>
        /// <param name="interfaceName">接口名称</param>
        /// <param name="methodName">方法名称</param>
        /// <param name="paramDict">参数字段</param>
        /// <param name="messageType">消息类型</param>
        public void Call(string recvAddress, ushort destVPort, string interfaceName, string methodName, Dictionary <string, object> paramDict, TMessageType messageType = TMessageType.RpcCall)
        {
            try
            {
                VirtuaIP receiverVIP = new VirtuaIP(recvAddress);
                string   msgID       = Guid.NewGuid().ToString();

                if (!InterfaceImplementMap.InterfaceAndImplementMap.ContainsKey(interfaceName))
                {
                    throw new ApplicationException("找不到接口" + interfaceName + "的具体实现!!");
                }

                //本地应用端口随机获取,目标端口确定
                TMessage          reqMsg            = new TMessage(interfaceName + "_" + methodName, messageType, msgID, -1, "", "", "", "");
                DynMethodInstance dynMethodInstance = new DynMethodInstance(interfaceName, methodName);
                TSerializer       serializerReq     = new TBinarySerializer();

                serializerReq.WriteMessageBegin(reqMsg);
                foreach (var para in paramDict)
                {
                    dynMethodInstance[para.Key] = para.Value;
                }

                DynSerialize.WriteDynMethodInstance(serializerReq, dynMethodInstance);
                serializerReq.WriteMessageEnd();
                byte[] reqData = serializerReq.ToBytes();

                serializerReq.Flush();

                Send(receiverVIP, destVPort, reqData);
            }
            catch (Exception ex)
            {
                this.RaiseDealMessageExceptionEvent(ex);
                throw new ApplicationException(ex.Message);
            }
        }
Example #2
0
        /// <summary>
        /// 远程方法调用
        /// </summary>
        /// <param name="recvAddress">接收节点的虚拟IP地址,如3.1/param>
        /// <param name="interfaceName">接口名称</param>
        /// <param name="methodName">方法名称</param>
        /// <param name="paramDict">参数字段</param>
        /// <param name="liveLife">生命周期,以秒为单位</param>
        /// <param name="messageType">消息类型</param>
        /// <returns>服务端返回的数据或者null</returns>
        public object Call(string recvAddress, ushort destVPort, string interfaceName, string methodName, Dictionary <string, object> paramDict, short liveLife, TMessageType messageType = TMessageType.RpcCall)
        {
            object result = null;

            _msgID = _msgID < int.MaxValue ? ++_msgID : 0;

            try
            {
                VirtuaIP receiverVIP = new VirtuaIP(recvAddress);
                string   msgID       = _msgID.ToString();

                if (!InterfaceImplementMap.InterfaceAndImplementMap.ContainsKey(interfaceName))
                {
                    throw new ApplicationException("找不到接口" + interfaceName + "的具体实现!");
                }

                DynMethodInstance dynMethodInstance = new DynMethodInstance(interfaceName, methodName);
                TMessage          reqMsg            = new TMessage(interfaceName + "_" + methodName, messageType, msgID, liveLife, "", "", "", "");
                TSerializer       serializerReq     = new TBinarySerializer();
                //将消息头信息写入流
                serializerReq.WriteMessageBegin(reqMsg);
                foreach (var para in paramDict)
                {
                    dynMethodInstance[para.Key] = para.Value;
                }
                //将方法的实例写入流
                DynSerialize.WriteDynMethodInstance(serializerReq, dynMethodInstance);

                //什么都不做
                serializerReq.WriteMessageEnd();
                //将消息流转换成二进制流
                byte[] reqData = serializerReq.ToBytes();
                serializerReq.Flush();

                //发送消息
                Send(receiverVIP, destVPort, reqData);

                var timeout = new TimeSpan(0, 0, liveLife);
                //创建一个 Stopwatch 实例:使用 StartNew() 会创建一个 Stopwatch 实例并马上开始计时,即等效于如下代码:
                //Stopwatch sw2 = new Stopwatch();
                //sw2.Start();
                var  timer     = Stopwatch.StartNew();
                bool isReceive = false;

                do
                {
                    ZmqMessage zmqMessage = _reqSocket.ReceiveMessage(timeout - timer.Elapsed);
                    if (zmqMessage.FrameCount == 0)
                    {
                        throw new TimeoutException("调用超时,请检查网络是否联通!");
                    }
                    else
                    {
                        //zmqMessage[zmqMessage.FrameCount - 1].Buffer 只需要处理zmqMessage中最后一帧,这一帧存的是消息中的数据部分
                        result = DealRepMessage(zmqMessage[zmqMessage.FrameCount - 1].Buffer, msgID, out isReceive);

                        //消息ID不匹配且超时时间未到时,丢掉当前接收到的数据包,继续接受数据
                        if (isReceive)
                        {
                            return(result);
                        }
                    }
                }while (timer.Elapsed <= timeout);

                throw new TimeoutException("调用超时,请检查网络是否联通!");
            }
            catch (Exception ex)
            {
                this.RaiseDealMessageExceptionEvent(ex);
                throw;
            }
        }