private void ProcessUpdateTask(object sender, DoWorkEventArgs e) { var worker = sender as BackgroundWorker; var prevTime = Stopwatch.StartNew(); while (!e.Cancel) { if (worker.CancellationPending) { e.Cancel = true; break; } prevTime.Restart(); if (!IsRunning & !_lastUpdateBeforePausing) { goto FrameLimitStreamUpdate; } _lastUpdateBeforePausing = false; int timeToWait; if (!RefreshRam()) { goto FrameLimitStreamUpdate; } OnUpdate?.Invoke(this, new EventArgs()); FrameLimitStreamUpdate: // Calculate delay to match correct FPS prevTime.Stop(); timeToWait = (int)RefreshRateConfig.RefreshRateInterval - (int)prevTime.ElapsedMilliseconds; timeToWait = Math.Max(timeToWait, 0); // Calculate Fps lock (_fpsQueueLocker) { if (_fpsTimes.Count() >= 10) { _fpsTimes.Dequeue(); } _fpsTimes.Enqueue(prevTime.ElapsedMilliseconds + timeToWait); } FpsUpdated?.Invoke(this, new EventArgs()); if (timeToWait > 0) { Thread.Sleep(timeToWait); } else { Thread.Yield(); } } OnClose?.BeginInvoke(this, new EventArgs(), null, null); }
private void ProcessedAccept(IAsyncResult ar) { OnAccept.EndInvoke(ar); Client state = (Client)ar.AsyncState; Socket handler = state.m_socket; try { handler.BeginReceive(state.buffer, 0, Client.BUFFER_SIZE, 0, new AsyncCallback(ReadCallback), state); } catch (Exception e) { if (e is ObjectDisposedException || (e is SocketException)) { if (OnClose != null) { OnClose.BeginInvoke(state, new AsyncCallback(EndClose), state); } } else { throw; } } }
public void Working(ReceiveEventHandler receive = null) { FleckLog.Level = LogLevel.Debug; allSockets = new List <IWebSocketConnection>(); server = new WebSocketServer("ws://0.0.0.0:7066"); server.RestartAfterListenError = true; server.Start(socket => { socket.OnOpen = () => { allSockets.Add(socket); OnOpen?.BeginInvoke(socket, null, null); }; socket.OnClose = () => { allSockets.Remove(socket); OnClose?.BeginInvoke(socket, null, null);//通知断开 }; socket.OnMessage = message => { receive?.BeginInvoke(message, socket, null, null); }; socket.OnError = ex => { OnError?.BeginInvoke(ex, socket, null, null); }; }); }
private void close() { try { if (IsOpen) { port.Close(); if (!IsOpen) { OnClose?.BeginInvoke(null, null); } } } catch (Exception ex) { raiseError(ComPortErrorNames.OnClose, ex); } }
private void ReadCallback(IAsyncResult ar) { Client state = (Client)ar.AsyncState; Socket handler = state.m_socket; try { int bytesRead = handler.EndReceive(ar); if (bytesRead > 0) { int len = BitConverter.ToInt16(state.buffer, 0); if (bytesRead != len) { //If the packet is incomplete //Check if there is an incomplete packet in memory if (state.oldBuffer != null && state.oldBuffer.Length != 0) { //And concat the two. byte[] buffer = new byte[bytesRead + state.oldBuffer.Length]; Array.Copy(state.oldBuffer, buffer, state.oldBuffer.Length); Array.Copy(state.buffer, 0, buffer, state.oldBuffer.Length, bytesRead); state.buffer = buffer; if (OnRead != null) { byte[] buffer2 = new byte[bytesRead]; Array.Copy(state.buffer, buffer2, bytesRead); OnRead.BeginInvoke(state, buffer2, bytesRead, new AsyncCallback(ProcessedRead), null); } } else { //Otherwise, store the received data state.oldBuffer = new byte[state.buffer.Length]; state.buffer.CopyTo(state.oldBuffer, 0); //And listen for more. handler.BeginReceive(state.buffer, 0, Client.BUFFER_SIZE, 0, new AsyncCallback(ReadCallback), state); return; } } else { if (OnRead != null) { byte[] buffer = new byte[bytesRead]; Array.Copy(state.buffer, buffer, bytesRead); OnRead.BeginInvoke(state, buffer, bytesRead, new AsyncCallback(ProcessedRead), null); } } handler.BeginReceive(state.buffer, 0, Client.BUFFER_SIZE, 0, new AsyncCallback(ReadCallback), state); } } catch (Exception e) { if (e is ObjectDisposedException || e is SocketException) { if (OnClose != null) { OnClose.BeginInvoke(state, new AsyncCallback(EndClose), state); } else { throw; } } } }