public override async Task SyncService(BuildStatusToken statusToken) { try { isSyncing = true; connection.IsSynced = false; connection.Service = Service; Task publishTask = Task.Run(() => SendMessagesHandler(connection)); Task pingTask = Task.Run(() => SendPingsHandler(connection)); Task receiveTask = Task.Run(() => ReceiveHandler(connection)); Task processTask = Task.Run(() => ProcessHandler(connection)); await connection.SendCommand(SyncCmd, false, TimeSpan.FromSeconds(1000), statusToken.EndTask); connection.IsSynced = true; connection.Task = Task.WhenAll(publishTask, pingTask, receiveTask, processTask); } catch (Exception e) { try { await connection.CloseAsync(e, true); } catch { } throw; } finally { isSyncing = false; } }
private static async Task SendPingsHandler(OwnTcpClientConnection connection) { Task cancelTask = connection.PingSem.WaitAsync(); try { while (connection.Client?.Connected == true) { Task delayTask = Task.Delay(pingInterval); await Task.WhenAny(delayTask, cancelTask); if (cancelTask.IsCompleted) { return; } await connection.SendCommand(PingCmd, false, TimeSpan.FromSeconds(2), cancelTask); } } catch (Exception e) { await connection.CloseAsync(new Exception("ReceiveHandler error", e), false); } }