private void HandleMessage(string message) { List <object> data = JsonConvert.DeserializeObject <List <object> >(message); string msgType = (string)data[0]; if (msgType == "call-reply") { int callID = Convert.ToInt32(data[1]); if (ActiveCalls.ContainsKey(callID)) { bool success = (bool)data[2]; object retValOrException = data[3]; ActiveCalls[callID].SetResult(success ? "result" : "exception", retValOrException); } else { throw new Error(ErrorCode.CONNECTION_ERROR, "Received a response for an unrecognized call id: " + callID); } } else if (msgType == "call") { int callID = Convert.ToInt32(data[1]); string methodName = (string)data[2]; if (RegisteredFunctions.ContainsKey(methodName)) { Delegate nativeMethod = RegisteredFunctions[methodName]; ParameterInfo[] paramInfo = nativeMethod.Method.GetParameters(); if (paramInfo.Length != data.Count - 3) { throw new Error(ErrorCode.INVALID_ARGUMENT, "Incorrect number of arguments for method: " + methodName + ". Expected: " + paramInfo.Length + ". Got: " + (data.Count - 3)); } List <object> parameters = new List <object>(); for (int i = 0; i < paramInfo.Length; i++) { parameters.Add(ConversionUtils.CastJObject( data[i + 3], paramInfo[i].ParameterType)); } object returnValue = null; object exception = null; bool success = true; try { returnValue = nativeMethod.DynamicInvoke(parameters.ToArray()); } catch (Exception e) { exception = e; success = false; } if (!IsOneWay(methodName)) { // Send call-reply message. List <object> callReplyMessage = new List <object>(); callReplyMessage.Add("call-reply"); callReplyMessage.Add(callID); callReplyMessage.Add(success); if (!success) { callReplyMessage.Add(exception); } else if (nativeMethod.Method.ReturnType != typeof(void)) { callReplyMessage.Add(returnValue); } Connection.Send(JsonConvert.SerializeObject(callReplyMessage)); } } else { throw new Error(ErrorCode.CONNECTION_ERROR, "Received a call for an unregistered method: " + methodName); } } else { throw new Error(ErrorCode.CONNECTION_ERROR, "Unknown message type: " + msgType); } }