/// <summary> /// Waits for response PSObject to be set and then returns it. Returns null /// if wait was aborted. /// </summary> /// <param name="callId"> /// </param> /// <param name="defaultValue"> /// default return value (in case the remote end did not send response). /// </param> internal T GetResponse(long callId, T defaultValue) { // Note: Only GetResponse removes records. AsyncObject <T> responseAsyncObject = null; lock (_responseAsyncObjects) { responseAsyncObject = GetResponseAsyncObject(callId); } // This will block until Value is set on this AsyncObject. T remoteHostResponse = responseAsyncObject.Value; // Remove table entry to conserve memory: this table could be alive for a long time. lock (_responseAsyncObjects) { _responseAsyncObjects.Remove(callId); } // return caller specified value in case there is no response // from remote end. if (remoteHostResponse == null) { return(defaultValue); } return(remoteHostResponse); }
/// <summary> /// Get response async object. /// </summary> private AsyncObject <T> GetResponseAsyncObject(long callId) { AsyncObject <T> responseAsyncObject = null; Dbg.Assert(_responseAsyncObjects.ContainsKey(callId), "Expected _responseAsyncObjects.ContainsKey(callId)"); responseAsyncObject = _responseAsyncObjects[callId]; Dbg.Assert(responseAsyncObject != null, "Expected responseAsyncObject != null"); return(responseAsyncObject); }
internal long CreateNewCallId() { long key = Interlocked.Increment(ref this._nextCallId); AsyncObject <T> asyncObject = new AsyncObject <T>(); lock (this._responseAsyncObjects) this._responseAsyncObjects[key] = asyncObject; return(key); }
internal long CreateNewCallId() { long num = Interlocked.Increment(ref this._nextCallId); AsyncObject <T> obj2 = new AsyncObject <T>(); lock (this._responseAsyncObjects) { this._responseAsyncObjects[num] = obj2; } return(num); }
internal T GetResponse(long callId, T defaultValue) { AsyncObject <T> asyncObject = (AsyncObject <T>)null; lock (this._responseAsyncObjects) asyncObject = this.GetResponseAsyncObject(callId); T obj = asyncObject.Value; lock (this._responseAsyncObjects) this._responseAsyncObjects.Remove(callId); return((object)obj == null ? defaultValue : obj); }
/// <summary> /// Abort call. /// </summary> private void AbortCall(long callId) { // The response-async-object might not exist if the call was already aborted. if (!_responseAsyncObjects.ContainsKey(callId)) { return; } // Releases blocked thread by setting null as return value, which should be detected by caller of GetResponse. AsyncObject <T> responseAsyncObject = GetResponseAsyncObject(callId); responseAsyncObject.Value = null; }
/// <summary> /// Create new call id. /// </summary> internal long CreateNewCallId() { // Note: Only CreateNewCallId adds new records. long callId = Interlocked.Increment(ref _nextCallId); AsyncObject <T> responseAsyncObject = new AsyncObject <T>(); lock (_responseAsyncObjects) { _responseAsyncObjects[callId] = responseAsyncObject; } return(callId); }
/// <summary> /// Set response. /// </summary> internal void SetResponse(long callId, T remoteHostResponse) { Dbg.Assert(remoteHostResponse != null, "Expected remoteHostResponse != null"); lock (_responseAsyncObjects) { // The response-async-object might not exist if the call was aborted by Ctrl-C or if // the call had a void return and no return value was expected. if (!_responseAsyncObjects.ContainsKey(callId)) { return; } // Unblock the AsyncObject by setting its value. AsyncObject <T> responseAsyncObject = GetResponseAsyncObject(callId); responseAsyncObject.Value = remoteHostResponse; } }
internal T GetResponse(long callId, T defaultValue) { AsyncObject <T> responseAsyncObject = null; lock (this._responseAsyncObjects) { responseAsyncObject = this.GetResponseAsyncObject(callId); } T local = responseAsyncObject.Value; lock (this._responseAsyncObjects) { this._responseAsyncObjects.Remove(callId); } if (local == null) { return(defaultValue); } return(local); }