protected override IFuncCall CallFunc(string funcName, params object[] args) { int callID = getValidCallID(); // Register delegates as callbacks. Pass their registered names instead. List <int> callbacks; List <object> convertedArgs = convertCallbackArguments(args, out callbacks); List <object> callMessage = createCallMessage(callID, funcName, callbacks, convertedArgs); IWSJFuncCall callObj = null; if (!IsOneWay(funcName)) { callObj = wsjFuncCallFactory.Construct(); // It is important to add an active call to the list before sending it, otherwise we may end up // receiving call-reply before this happens, which will trigger unnecessary call-error and crash the // other end. lock (activeCalls) activeCalls.Add(callID, callObj); } SendMessage(callMessage); return(callObj); }
private void HandleCallError(List <JToken> data) { int callID = data[1].ToObject <int>(); string reason = data[2].ToObject <string>(); // Call error with callID = -1 means we've sent something that was not understood by other side or was // malformed. This probably means that protocols aren't incompatible or incorrectly implemented on either // side. if (callID == -1) { throw new Exception(reason); } IWSJFuncCall failedCall = null; lock (activeCalls) { if (activeCalls.ContainsKey(callID)) { failedCall = activeCalls[callID]; activeCalls.Remove(callID); } } if (failedCall != null) { failedCall.HandleError(reason); } else { SendCallError(-1, "Invalid callID: " + callID); } }
private void HandleCallReply(List <JToken> data) { int callID = (int)data[1]; IWSJFuncCall completedCall = null; lock (activeCalls) { if (activeCalls.ContainsKey(callID)) { completedCall = activeCalls[callID]; activeCalls.Remove(callID); } } if (completedCall != null) { bool success = data[2].ToObject <bool>(); JToken result = data.Count == 4 ? data[3] : new JValue((object)null); if (success) { completedCall.HandleSuccess(result); } else { completedCall.HandleException(result); } } else { SendCallError(-1, "Invalid callID: " + callID); } }
internal void HandleClosed(object sender, EventArgs e) { IWSJFuncCall[] removedCalls = new IWSJFuncCall[activeCalls.Count]; lock (activeCalls) { activeCalls.Values.CopyTo(removedCalls, 0); activeCalls.Clear(); } foreach (var call in removedCalls) { call.HandleError("Connection closed."); } if (Closed != null) { Closed(this, e); } }