/// <summary>
        /// 处理返回消息
        /// </summary>
        /// <param name="msgData">返回消息</param>
        private object DealRepMessage(byte[] msgData)
        {
            TSerializer serializerResp = new TBinarySerializer();

            serializerResp.FromBytes(msgData);
            TMessage respMsg = serializerResp.ReadMessageBegin();

            if (respMsg.Type == TMessageType.Exception)
            {
                this.RaiseDealMessageExceptionEvent(TApplicationException.Read(serializerResp));

                serializerResp.ReadMessageEnd();
                serializerResp.Flush();

                return(null);;
            }

            if (respMsg.Type != TMessageType.RpcReply)
            {
                this.RaiseDealMessageExceptionEvent(new ApplicationException("非法的返回类型:" + respMsg.Type.ToString()));

                serializerResp.ReadMessageEnd();
                serializerResp.Flush();

                return(null);
            }

            string[]          temp = respMsg.Name.Split('_');
            DynMethodInstance dynMethodInstance = new DynMethodInstance(temp[0], temp[1]);

            try
            {
                dynMethodInstance.Result = DynSerialize.ReadResult(serializerResp, dynMethodInstance);
            }
            catch (Exception ex)
            {
                this.RaiseDealMessageExceptionEvent(ex);

                serializerResp.ReadMessageEnd();
                serializerResp.Flush();
            }

            // 消息结束
            serializerResp.ReadMessageEnd();
            serializerResp.Flush();
            return(dynMethodInstance.Result);
        }
Exemple #2
0
        /// <summary>
        /// 处理返回消息
        /// </summary>
        /// <param name="msgData">返回消息</param>
        private object DealRepMessage(byte[] msgData, string msgID, out bool isReceive)
        {
            //初始化序列化容器
            TSerializer serializerResp = new TBinarySerializer();

            //将数据读入序列化容器
            serializerResp.FromBytes(msgData);
            //从序列化容器中读取消息
            TMessage respMsg = serializerResp.ReadMessageBegin();

            //如果消息的id不对返回空值
            if (msgID != respMsg.MsgID)
            {
                //空方法
                serializerResp.ReadMessageEnd();
                serializerResp.Flush();

                isReceive = false;
                return(null);
            }

            //如果是异常消息
            if (respMsg.Type == TMessageType.Exception)
            {
                TApplicationException appException = TApplicationException.Read(serializerResp);
                this.RaiseDealMessageExceptionEvent(appException);

                serializerResp.ReadMessageEnd();
                serializerResp.Flush();

                throw appException;
            }
            //如果消息是远程调用回应
            if (respMsg.Type != TMessageType.RpcReply)
            {
                this.RaiseDealMessageExceptionEvent(new ApplicationException("非法的返回类型:" + respMsg.Type.ToString()));

                serializerResp.ReadMessageEnd();
                serializerResp.Flush();

                isReceive = true;
                return(null);
            }

            //构造方法的实例
            string[]          temp = respMsg.Name.Split('_');
            DynMethodInstance dynMethodInstance = new DynMethodInstance(temp[0], temp[1]);

            try
            {
                dynMethodInstance.Result = DynSerialize.ReadResult(serializerResp, dynMethodInstance);
            }
            catch (Exception ex)
            {
                this.RaiseDealMessageExceptionEvent(ex);

                serializerResp.ReadMessageEnd();
                serializerResp.Flush();
            }

            // 消息结束
            serializerResp.ReadMessageEnd();
            serializerResp.Flush();

            isReceive = true;
            return(dynMethodInstance.Result);
        }
Exemple #3
0
        /// <summary>
        /// 重载父类方法
        /// </summary>
        /// <param name="msgData">接收到的消息</param>
        private void DealMessage(object obj)
        {
            ZmqMessage zmqMessage = obj as ZmqMessage;
            //记录源地址和端口
            VirtuaIP originVIP  = new VirtuaIP(zmqMessage[1].Buffer);
            ushort   originPort = (ushort)BitConverter.ToInt16(zmqMessage[3].Buffer, 0);

            byte[] requestData = zmqMessage[zmqMessage.FrameCount - 1].Buffer;

            if (requestData != null && requestData.Length >= 4)
            {
                TSerializer serializer = new TBinarySerializer();
                serializer.FromBytes(requestData);

                // 获取消息
                TMessage msg = serializer.ReadMessageBegin();

                if (msg.Type == TMessageType.RpcCall || msg.Type == TMessageType.RpcOneway)
                {
                    //获取方法信息构造Method
                    string[]          temp           = msg.Name.Split('_');
                    DynMethodInstance methodInstance = null;
                    try
                    {
                        methodInstance = DynSerialize.ReadDynMethodInstance(serializer, temp[0], temp[1]);
                        serializer.ReadMessageEnd();
                        serializer.Flush();

                        Dictionary <string, object> paramValues = new Dictionary <string, object>();
                        //获取参数信
                        foreach (string paramName in methodInstance.DynMethod.GetParameterNames())
                        {
                            paramValues[paramName] = methodInstance[paramName];
                        }

                        string className      = InterfaceImplementMap.InterfaceAndImplementMap[temp[0]];
                        string methodFullName = className + "_" + temp[1];
                        //方法调用
                        object ret = DynTypeManager.MethodHandler(null, methodFullName, paramValues);

                        if (methodInstance.DynMethod.Result.DynType != DynType.Void)
                        {
                            methodInstance.Result = ret;
                        }

                        // 序列化返回结果
                        serializer = new TBinarySerializer();
                        TMessage respMsg = new TMessage(msg.Name, TMessageType.RpcReply, msg.MsgID, msg.LiveLife, "", "", "", "");
                        serializer.WriteMessageBegin(respMsg);
                        DynSerialize.WriteResult(serializer, methodInstance);
                        serializer.WriteMessageEnd();
                        byte[] respData = serializer.ToBytes();
                        serializer.Flush();

                        // 返回客户端
                        Send(originVIP, originPort, respData);
                    }
                    catch (Exception ex)
                    {
                        serializer.ReadMessageEnd();
                        serializer.Flush();

                        //如果服务端需要记录日志,则由本事件把异常信息发出
                        RaiseDealMessageExceptionEvent(ex);

                        byte[] returnData = ReturnExceptionToClient("RpcServer.DealMessageException", msg, ex.Message);
                        // 返回客户端
                        Send(originVIP, originPort, returnData);
                        return;
                    }
                }
            }
        }