Esempio n. 1
0
        private void Server_OnMessageReceived(byte[] bytes)
        {
            var         json    = Encoding.ASCII.GetString(bytes);
            PipeMessage message = JsonConvert.DeserializeObject <PipeMessage>(json);

            //get method call data
            //call implementation and get response
            //send response (if error goes here, need to throw something to the client. Also need to time this out on the client side)
            var request = message.GetPayload <MethodCallRequest>();

            object result = null;

            try
            {
                MethodInfo invokeMethod = typeof(T).GetMethod(request.MethodName);
                for (int i = 0; i < request.ParameterValues.Count; i++)
                {
                    var jObject = request.ParameterValues[i] as JObject;
                    if (jObject != null)
                    {
                        request.ParameterValues[i] = jObject.ToObject(request.ParameterTypes[i]);
                    }
                    else if (request.ParameterTypes[i].BaseType == typeof(Enum))
                    {
                        request.ParameterValues[i] = Convert.ChangeType(request.ParameterValues[i], typeof(int));
                    }
                    else
                    {
                        request.ParameterValues[i] = Convert.ChangeType(request.ParameterValues[i], request.ParameterTypes[i]);
                    }
                }
                result = invokeMethod.Invoke(instance, request.ParameterValues.ToArray());
            }
            catch (Exception ex)
            {
                request.ReturnType = typeof(ServerException);
                result             = ex.InnerException?.Message;
            }

            var response = new MethodCallResponse()
            {
                MethodName  = request.MethodName,
                ReturnType  = request.ReturnType,
                ReturnValue = result
            };
            var responseMessage = new PipeMessage <MethodCallResponse>(response);
            var responseJson    = JsonConvert.SerializeObject(responseMessage);

            byte[] responseBytes = Encoding.ASCII.GetBytes(responseJson);
            server.Send(responseBytes);
        }
Esempio n. 2
0
        private void Server_OnMessageReceived(PipeMessage e)
        {
            //get method call data
            //call implementation and get response
            //send response (if error goes here, need to throw something to the client. Also need to time this out on the client side)
            var request = e.GetPayload <MethodCallRequest>();

            object result = null;

            try
            {
                MethodInfo invokeMethod = typeof(T).GetMethod(request.MethodName);
                for (int i = 0; i < request.ParameterValues.Count; i++)
                {
                    var jObject = request.ParameterValues[i] as JObject;
                    if (jObject != null)
                    {
                        request.ParameterValues[i] = jObject.ToObject(request.ParameterTypes[i]);
                    }
                    else
                    {
                        request.ParameterValues[i] = Convert.ChangeType(request.ParameterValues[i], request.ParameterTypes[i]);
                    }
                }
                result = invokeMethod.Invoke(instance, request.ParameterValues.ToArray());
            }
            catch (Exception ex)
            {
                request.ReturnType = typeof(ServerException);
                result             = ex.InnerException?.Message;
            }

            var response = new MethodCallResponse()
            {
                MethodName  = request.MethodName,
                ReturnType  = request.ReturnType,
                ReturnValue = result
            };

            server.Send(new PipeMessage <MethodCallResponse>(response));
        }