示例#1
0
        protected void CallProcess(ITcpSession session, RpcCallData data)
        {
            IServerController controller = rpcContainer.GetController(session.SessionId, data.Controller.ToLower());

            if (controller.Session == null)
            {
                controller.Session = session;
            }
            try
            {
                string   key    = (data.Controller + ":" + data.Action).ToLower();
                var      method = rpcContainer.GetMethod(key);
                object[] args   = null;
                if (data.Arguments != null && data.Arguments.Count > 0)
                {
                    args = new object[data.Arguments.Count];
                    var types = GetParaTypeList(key);
                    for (int i = 0; i < data.Arguments.Count; i++)
                    {
                        var          arg    = data.Arguments[i];
                        MemoryStream stream = new MemoryStream(arg, 0, arg.Length);
                        var          obj    = Serializer.NonGeneric.Deserialize(types[i], stream);
                        args[i] = obj;
                        stream.Dispose();
                    }
                }
                object        value  = method.Invoke(controller, args);
                RpcReturnData result = new RpcReturnData()
                {
                    Id = data.Id
                };
                var ms = new MemoryStream();
                Serializer.Serialize(ms, value);
                byte[] bytes = new byte[ms.Position];
                Buffer.BlockCopy(ms.GetBuffer(), 0, bytes, 0, bytes.Length);
                result.Value = bytes;
                ms.Position  = 0;
                ms.WriteByte(2);
                Serializer.Serialize(ms, result);
                byte[] rBytes = new byte[ms.Position];
                Buffer.BlockCopy(ms.GetBuffer(), 0, rBytes, 0, rBytes.Length);
                session.SendAsync(rBytes);
                ms.Dispose();
            }
            catch (Exception e)
            {
                RpcErrorInfo error = new RpcErrorInfo()
                {
                    Id = data.Id, Message = e.Message
                };
                var ms = new MemoryStream();
                ms.WriteByte(0);
                Serializer.Serialize(ms, error);
                byte[] rBytes = new byte[ms.Position];
                Buffer.BlockCopy(ms.GetBuffer(), 0, rBytes, 0, rBytes.Length);
                session.SendAsync(rBytes);
                ms.Dispose();
            }
        }
示例#2
0
        public List <Type> GetParaTypeList(string key)
        {
            List <Type> result;

            if (!methodParasDict.TryGetValue(key, out result))
            {
                result = RpcContainer.GetMethod(key).GetParameters().Select(p => p.ParameterType).ToList();
                methodParasDict.TryAdd(key, result);
            }
            return(result);
        }