public async Task <GetStateResult <T> > TryGetStateAsync <T>(string id, TimeSpan timeout) { var retVal = new GetStateResult <T>(); var stateReceivedWaitHandle = new EventWaitHandle(false, EventResetMode.ManualReset); try { await _socketIoClient.EmitAsync("getState", (response) => GetStateResponse <T>(response, stateReceivedWaitHandle, retVal, id), id); if (!stateReceivedWaitHandle.WaitOne(timeout)) { retVal.Success = false; retVal.Error = new TimeoutException($"Timeout for reading state of id: \"{id}\""); } } catch (Exception exception) { retVal.Success = false; retVal.Error = exception; } finally { stateReceivedWaitHandle.Dispose(); } return(retVal); }
private void GetStateResponse <T>(SocketIOResponse response, EventWaitHandle stateReceivedWaitHandle, GetStateResult <T> stateResult, string id) { var topic = response.GetValue <string>(); var obj = response.GetValue <State>(1); if (obj != null) { try { stateResult.Value = obj.GetConvertedValue <T>(); stateResult.Success = true; } catch (Exception e) { stateResult.Success = false; stateResult.Error = e; } } else { stateResult.Success = false; stateResult.Error = new Exception($"Id: \"{id}\" not found"); } stateReceivedWaitHandle.Set(); }