public static bool BoolFromMessage(RpcMessage.Parameter parameter) { bool value = false; string error = null; if (!ParameterConverter.BoolFromMessage(parameter, ref value, ref error)) throw new ArgumentException( String.Format("Cannot convert server return value into expected bool return type ({0})", error)); return value; }
internal override void Send(RpcMessage message) { queueMutex.WaitOne(); queuedMessages.Enqueue(message); queueEvent.Set(); queueMutex.ReleaseMutex(); }
public static float FloatFromMessage(RpcMessage.Parameter parameter) { float value = 0.0f; string error = null; if (!ParameterConverter.FloatFromMessage(parameter, ref value, ref error)) throw new ArgumentException( String.Format("Cannot convert server return value into expected float return type ({0})", error)); return value; }
public static ulong Uint64FromMessage(RpcMessage.Parameter parameter) { ulong value = 0; string error = null; if (!ParameterConverter.Uint64FromMessage(parameter, ref value, ref error)) throw new ArgumentException( String.Format("Cannot convert server return value into expected uint64 return type ({0})", error)); return value; }
public static bool BoolFromMessage(RpcMessage.Parameter message, ref bool value, ref string error) { if (!message.BoolParamSpecified) { error = "Parameter did not have expected bool type."; return false; } value = message.BoolParam; return true; }
public static object ProtoFromMessage(RpcMessage.Parameter parameter, Type type) { object value = null; string error = null; if (!ParameterConverter.ProtoFromMessage(parameter, type, ref value, ref error)) throw new ArgumentException( String.Format("Cannot convert server return value into expected protobuf return type '{0}' ({1})", type, error)); return value; }
public static bool FloatFromMessage(RpcMessage.Parameter message, ref float value, ref string error) { if (!message.FloatParamSpecified) { error = "Parameter did not have expected float type."; return false; } value = message.FloatParam; return true; }
public static bool DoubleFromMessage(RpcMessage.Parameter message, ref double value, ref string error) { if (!message.DoubleParamSpecified) { error = "Parameter did not have expected double type."; return false; } value = message.DoubleParam; return true; }
public static bool BoolFromMessage(RpcMessage.Parameter parameter, RpcMessage.Result resultMessage, ref bool value) { string error = null; if (!ParameterConverter.BoolFromMessage(parameter, ref value, ref error)) { if (resultMessage != null) { resultMessage.IsFailed = true; resultMessage.ErrorMessage = String.Format("Cannot convert call parameter value into expected bool type ({0})", error); } return false; } return true; }
protected IAsyncResult BeginAsyncCallHelper(string methodName, RpcMessage.Parameter[] parameters, AsyncCallback callback, object state) { return client.Call(serviceName, methodName, parameters, callback, state); }
/// <summary> /// Converts a parameter message into an object. /// </summary> /// <param name="message"></param> /// <param name="parameterType"></param> /// <param name="parameter"></param> /// <param name="errorMsg"></param> /// <returns></returns> public static bool FromMessage(RpcMessage.Parameter message, Type parameterType, out object parameter, out string errorMsg) { if (parameterType.IsPrimitive) { if (parameterType == typeof(bool) && message.BoolParamSpecified) parameter = message.BoolParam; else if (parameterType == typeof(byte) && message.UintParamSpecified) parameter = (byte)message.UintParam; else if (parameterType == typeof(sbyte) && message.IntParamSpecified) parameter = (sbyte)message.IntParam; else if (parameterType == typeof(char) && message.UintParamSpecified) parameter = (char)message.UintParam; else if (parameterType == typeof(short) && message.IntParamSpecified) parameter = (short)message.IntParam; else if (parameterType == typeof(ushort) && message.UintParamSpecified) parameter = (ushort)message.UintParam; else if (parameterType == typeof(int) && message.IntParamSpecified) parameter = message.IntParam; else if (parameterType == typeof(uint) && message.UintParamSpecified) parameter = message.UintParam; else if (parameterType == typeof(long) && message.Int64ParamSpecified) parameter = message.Int64Param; else if (parameterType == typeof(ulong) && message.Uint64ParamSpecified) parameter = message.Uint64Param; else if (parameterType == typeof(float) && message.FloatParamSpecified) parameter = message.FloatParam; else if (parameterType == typeof(double) && message.DoubleParamSpecified) parameter = message.DoubleParam; else { errorMsg = String.Format("Expected return type '{0}' was not found in the parameter message, or type is not supported. Only integral, string, and protobuf types are supported", parameterType); parameter = null; return false; } } else if (parameterType == typeof(string)) { if (message.IsNull) { parameter = null; } else { if (!message.StringParamSpecified) { errorMsg = String.Format("Expected string return type was not found in the parameter message"); parameter = null; return false; } parameter = message.StringParam; } } else { //not a primitive or string, so it must be a protobuf message object[] attrs = parameterType.GetCustomAttributes(typeof(ProtoContractAttribute), true); if (attrs.Length < 1) { errorMsg = String.Format("Expected return type '{0}' is not supported. Only integral, string, and protobuf types are supported", parameterType); parameter = null; return false; } if (message.IsNull) { parameter = null; } else { //check the server returned a protobuf message if (message.ProtoParam != null) { //can't check the protobuf message was the expected type, as protobuf-net is much too lenient, it doesn't //check required fields or care about invalid fields either var memStream = new MemoryStream(message.ProtoParam); parameter = Serializer.NonGeneric.Deserialize(parameterType, memStream); } else { errorMsg = String.Format("Server had unexpected return type, was expecting a ProtoBuf message of type {0}.", parameterType); parameter = null; return false; } } } errorMsg = null; return true; }
public static bool Uint64FromMessage(RpcMessage.Parameter message, ref ulong value, ref string error) { if (!message.Uint64ParamSpecified) { error = "Parameter did not have expected ulong type."; return false; } value = message.Uint64Param; return true; }
/// <summary> /// Converts an object to a parameter message. /// </summary> /// <param name="parameter"></param> /// <param name="message"></param> /// <param name="errorMsg"></param> /// <returns></returns> public static bool ToMessage(object parameter, out RpcMessage.Parameter message, out string errorMsg) { message = new RpcMessage.Parameter(); if (parameter == null) { message.IsNull = true; } else { if (parameter is string) message.StringParam = (string)parameter; else if (parameter is bool) message.BoolParam = (bool)parameter; else if (parameter is int) message.IntParam = (int)parameter; else if (parameter is uint) message.UintParam = (uint)parameter; else if (parameter is float) message.FloatParam = (float)parameter; else if (parameter is double) message.DoubleParam = (double)parameter; else if (parameter is sbyte) message.IntParam = (sbyte)parameter; else if (parameter is byte) message.UintParam = (byte)parameter; else if (parameter is char) message.UintParam = (char)parameter; else if (parameter is short) message.IntParam = (short)parameter; else if (parameter is ushort) message.UintParam = (ushort)parameter; else if (parameter is long) message.Int64Param = (long)parameter; else if (parameter is ulong) message.Uint64Param = (ulong)parameter; else { //Must be a protobuf message Type parameterType = parameter.GetType(); object[] attrs = parameterType.GetCustomAttributes(typeof(ProtoContractAttribute), true); if (attrs.Length < 1) { errorMsg = String.Format("Parameter of type '{0}' is not supported. Only integral, string, and protobuf types are supported", parameterType); return false; } var memStream = new MemoryStream(); Serializer.NonGeneric.Serialize(memStream, parameter); message.ProtoParam = memStream.ToArray(); } } errorMsg = null; return true; }
public static bool StringFromMessage(RpcMessage.Parameter message, ref string value, ref string error) { if (message.IsNull) { value = null; return true; } if (!message.StringParamSpecified) { error = "Parameter did not have expected string type."; return false; } value = message.StringParam; return true; }
public static bool ProtoFromMessage(RpcMessage.Parameter message, Type type, ref object value, ref string error) { if (message.IsNull) { value = null; return true; } //check the server returned a protobuf message if (message.ProtoParam == null) { error = String.Format( "Server had unexpected return type, was expecting a ProtoBuf message of type {0}.", type); return false; } //can't check the protobuf message was the expected type, as protobuf-net is much too lenient, it doesn't //check required fields or care about invalid fields either var memStream = new MemoryStream(message.ProtoParam); value = Serializer.NonGeneric.Deserialize(type, memStream); return true; }
protected RpcMessage.Parameter BlockingCallHelper(string methodName, RpcMessage.Parameter[] parameters) { IAsyncResult asyncResult = BeginAsyncCallHelper(methodName, parameters, null, null); return EndAsyncCallHelper(methodName, asyncResult); }
public void InvokeMethod(object service, string methodName, List<RpcMessage.Parameter> parameters, RpcMessage resultMessage) { RpcMethodDescriptor method = GetMethod(methodName); if (method != null) { method.Invoke(service, parameters, resultMessage); } else { //return an error message to the client if it is expecting a result, otherwise we fail silently if (resultMessage != null) { resultMessage.ResultMessage.IsFailed = true; resultMessage.ResultMessage.ErrorMessage = String.Format("Unknown method '{1}' in service '{0}'", Name, methodName); } } }
public void InvokeMethod(object service, string methodName, List <RpcMessage.Parameter> parameters, RpcMessage resultMessage) { RpcMethodDescriptor method = GetMethod(methodName); if (method != null) { method.Invoke(service, parameters, resultMessage); } else { //return an error message to the client if it is expecting a result, otherwise we fail silently if (resultMessage != null) { resultMessage.ResultMessage.IsFailed = true; resultMessage.ResultMessage.ErrorMessage = String.Format("Unknown method '{1}' in service '{0}'", Name, methodName); } } }
public void ReceiveResult(RpcMessage.Result resultMessage) { result = resultMessage.CallResult; isCompleted = true; isFailed = resultMessage.IsFailed; serverErrorMessage = resultMessage.ErrorMessage; if (asyncCallback != null) asyncCallback(this); waitHandle.Set(); }
public void Invoke(object service, IList<RpcMessage.Parameter> parameters, RpcMessage resultMessage) { ParameterInfo[] parameterInfos = syncMethodInfo.GetParameters(); if (parameterInfos.Length != parameters.Count) { if (resultMessage != null) { resultMessage.ResultMessage.IsFailed = true; resultMessage.ResultMessage.ErrorMessage = String.Format( "Wrong number of parameters for method '{1}' in service '{0}'", serviceName, Name); } return; } var invokeParameters = new object[parameters.Count]; for (int i=0; i<parameters.Count; ++i) { string errorMsg; if (!ParameterConverter.FromMessage(parameters[i], parameterInfos[i].ParameterType, out invokeParameters[i], out errorMsg)) { if (resultMessage != null) { resultMessage.ResultMessage.IsFailed = true; resultMessage.ResultMessage.ErrorMessage = String.Format( "Parameter {0} could not be converted to expected type '{1}' for method '{2}' in service '{3}' ({4}).", i, parameterInfos[i].ParameterType, Name, serviceName, errorMsg); } return; } } object result = syncMethodInfo.Invoke(service, invokeParameters); if (resultMessage != null) { RpcMessage.Parameter resultParameter; string errorMsg; if (ParameterConverter.ToMessage(result, out resultParameter, out errorMsg)) { resultMessage.ResultMessage.CallResult = resultParameter; } else { resultMessage.ResultMessage.IsFailed = true; resultMessage.ResultMessage.ErrorMessage = String.Format( "Result type {0} could not be converted to a message for method '{1}' in service '{2}' ({3}).", syncMethodInfo.ReturnType, Name, serviceName, errorMsg); return; } } }
protected void CallWithoutResultHelper(string methodName, RpcMessage.Parameter[] parameters) { client.CallWithoutResult(serviceName, methodName, parameters); }
public static bool VerifyParameterCount(IList<RpcMessage.Parameter> parameters, int expectedCount, RpcMessage.Result resultMessage) { if (parameters.Count != expectedCount) { if (resultMessage != null) { resultMessage.IsFailed = true; resultMessage.ErrorMessage = "Wrong number of parameters"; } return false; } return true; }