private IEnumerable <Danmu> EnumerateDanmus() { while (true) { _cachedCheckRead.Clear(); for (int i = 0; i < _clients.Length; i++) { if (_isConnecteds[i]) { // 只需要检查已连接的客户端 _cachedCheckRead.Add(_clients[i].Client); } } if (_cachedCheckRead.Count == 0) { // 此时没有任何连接到服务器的客户端(比如刚刚调用Start方法的时候),checkRead为空会报错 goto sleep; } Socket.Select(_cachedCheckRead, null, null, 1000); if (_cachedCheckRead.Count == 0) { // 说明没有收到弹幕的客户端 goto sleep; } break; sleep: Thread.Sleep(1000); // TODO: 延时时间加入配置文件 } return(_cachedCheckRead.Select(socket => DanmuApi.ResolveDanmu(socket)).Where(danmu => danmu != Danmu.Empty)); }
private async Task ExecuteLoopImplAsync() { while (true) { Danmu danmu; if (_isDisposed) { return; } try { using (CancellationTokenSource timeoutCts = new CancellationTokenSource(_receiveTimeout)) using (CancellationTokenSource linkedCts = CancellationTokenSource.CreateLinkedTokenSource(timeoutCts.Token, _manualCts.Token, _cancellationToken)) danmu = await DanmuApi.ReadDanmuAsync(_client).WithCancellation(linkedCts.Token); } catch (OperationCanceledException) { return; } catch (Exception ex) { if (!_isDisposed) { // 可能是资源释放不同步导致错误,不记录 GlobalSettings.Logger.LogException(ex); } return; } if (_isDisposed) { return; } switch (danmu.Type) { case DanmuType.Command: try { DanmuHandler?.Invoke(this, new DanmuHandlerEventArgs(danmu)); } catch (Exception ex) { if (!_isDisposed) { GlobalSettings.Logger.LogException(ex); } } break; case DanmuType.Handshaking: GlobalSettings.Logger.LogInfo($"{_id} 号弹幕监控进入房间 {_roomId}"); break; } } }
public async Task ExecuteLoopAsync(TimeSpan delay) { if (_isDisposed) { throw new ObjectDisposedException($"弹幕监控器已被Dispose,若要重新启动,请重新实例化{nameof(DanmuMonitorImpl)}"); } await Task.Delay(delay); await DanmuApi.ConnectAsync(_client); await DanmuApi.EnterRoomAsync(_client, _roomId); _ = ExecuteHeartBeatLoopImplAsync(); await ExecuteLoopImplAsync(); Dispose(); }
private async Task ExecuteHeartBeatLoopImplAsync() { while (true) { DateTime startTime; TimeSpan span; startTime = DateTime.Now; if (_isDisposed) { return; } try { await DanmuApi.SendHeartBeatAsync(_client); if (_showHeartBeat) { GlobalSettings.Logger.LogInfo($"{_id} 号弹幕监控已发送心跳"); } } catch (Exception ex) { if (!_isDisposed) { GlobalSettings.Logger.LogException(ex); } return; } if (_isDisposed) { return; } span = DanmuApi.HeartBeatInterval - (DateTime.Now - startTime); if (span.Ticks > 0) { await Task.Delay(span); } } }
private void ConnectAndEnterRoom(List <TcpClient> unconnectedClients, int i) { DanmuApi.Connect(unconnectedClients[i]); DanmuApi.EnterRoom(unconnectedClients[i], _roomIds[i]); DanmuApi.SendHeartBeat(unconnectedClients[i]); }