private async void AliveSendTimer_Elapsed(object sender, ElapsedEventArgs e) { try { if (!Connected || !State.SystemsAlive) { return; } var ao = new AliveObject { Ticks = TLCFIClient.CurrentTicks, Time = TimestampGenerator.GetTimestamp() }; var reply = await TLCProxy.AliveAsync(ao, _sessionCancellationToken); if (reply != null && reply.Ticks == ao.Ticks && reply.Time == ao.Time) { return; } _aliveSendFailCounter++; if (_aliveSendFailCounter > 2) { StopAliveTimers(); SendAliveTimeoutOccured?.Invoke(this, EventArgs.Empty); } } catch { // ignored } }
public void DisposeSession() { _aliveSendTimer.Dispose(); _aliveReceivedTimer.Dispose(); TLCProxy?.Dispose(); Client?.Dispose(); }
public async Task CloseSessionAsync(bool expected) { try { if (Connected && State.Controlling) { var maxdl = Task.Delay(TLCFIDataProvider.Default.Settings.MaxReleaseControlDuration, _sessionCancellationToken); var relct = Task.Run(async() => { while (State.Controlling) { await Task.Delay(100, _sessionCancellationToken); } }, _sessionCancellationToken); await Task.WhenAny(maxdl, relct); } StopAliveTimers(); if (Connected && State.Registered) { try { await TLCProxy.DeregisterAsync(new DeregistrationRequest(), _sessionCancellationToken); _logger.Info("Deregistered succesfully from TLC."); } catch (Exception e) { _logger.Error(e, "Error while deregistering: "); } } SessionEnded?.Invoke(this, expected); } catch (TaskCanceledException) { } }
public TLCFIClientSessionJsonRpcHandler( TLCFIClientStateManager stateManager, TLCProxy tlcProxy, TLCFIClientSessionState sessionState, TwoWayTcpClient tcpClient, CancellationToken token) { _stateManager = stateManager; _tlcProxy = tlcProxy; _sessionCancellationToken = token; tcpClient.DataReceived += async(o, e) => { if (!_jsonRpcMethodRegex.IsMatch(e)) { return; } var result = _service?.HandleRpc(e); if (result != null) { await tcpClient.SendDataAsync(result, token); } }; _service = JsonRpcProcedureBinder.Default.GetInstanceService(this); }
public TLCFIClientSession(TLCFIClientStateManager stateManager, IPEndPoint ep, CancellationToken token) { _endPoint = ep; _sessionCancellationToken = token; _stateManager = stateManager; Client = new TwoWayTcpClient(); Client.DataSent += (o, e) => { DataSent?.Invoke(this, e); }; Client.Disconnected += Client_Disconnected; RemoteEndPoint = ep; State = new TLCFIClientSessionState(); TLCProxy = new TLCProxy(Client); _jsonRpcHandler = new TLCFIClientSessionJsonRpcHandler(stateManager, TLCProxy, State, Client, token); _jsonRpcHandler.ReceivedAlive += (o, a) => { _aliveReceivedTimer.Stop(); _aliveReceivedTimer.Start(); }; _jsonRpcHandler.UpdateStateCalled += OnUpdateStateCalled; _jsonRpcHandler.NotifyEventCalled += OnNotifyEventCalled; }