private void CameraReceiverMethod() { byte[] framedata = new byte[1]; var responseCameraFrame = new ResponseCameraFrame(); while (!RequestedStop) { try { var responseBytes = new byte[responseCameraFrame.GetResponseLength()]; // Receive the first part of the frame. This part contains the informations like height, width or length Receive(_socket, responseBytes, responseBytes.Length); try { responseCameraFrame.FromByteArray(responseBytes); } catch (InvalidOperationException) // Error while receiving one frame. Close camera server { RequestedStop = true; TxtCommunication.SendCommand(new CommandStopCamera(), new ResponseStopCamera()); DisconnectFromCameraServerMethod(); break; } // Use the existing framedata object and resize if needed if (framedata.Length < responseCameraFrame.FrameSizeCompressed + 2) { Array.Resize(ref framedata, responseCameraFrame.FrameSizeCompressed + 2); } // Receive the second part of the frame. This part contains the compressed JPEG data Receive(_socket, framedata, responseCameraFrame.FrameSizeCompressed); // Add the missing EOI (End of image) tag framedata[framedata.Length - 2] = 0xFF; framedata[framedata.Length - 1] = 0xD9; // Store the received frame in the responseCameraFrame object responseCameraFrame.FrameData = framedata; // Process the received frame in another thread queue so that we can continue receiving frames ReceivedFrames.Enqueue(responseCameraFrame); _frameProcessingTaskQueue.DoWorkInQueue(() => { if (!ReceivedFrames.IsEmpty) { ResponseCameraFrame frame; if (ReceivedFrames.TryDequeue(out frame) && !RequestedStop) { FrameReceivedEventArgs eventArgs = new FrameReceivedEventArgs(framedata, responseCameraFrame.FrameSizeCompressed + 2); FrameReceived?.Invoke(this, eventArgs); } } }, false); // Send an acknowledge _socket.Send(BitConverter.GetBytes(TxtInterface.AcknowledgeIdCameraOnlineFrame)); } catch (Exception) { } } }
public void ProcessReceivedFrames(IFrame frame) { // Add frame to the list of history ReceivedFrames.Add(frame); switch (State) { case StreamState.Idle: if (frame.Type == FrameType.Headers) { State = StreamState.Open; } else if (frame.Type == FrameType.PushPromise) { State = StreamState.ReservedRemote; } // else if (frame.Type == FrameType.Priority) // ; // else // ; break; case StreamState.ReservedLocal: if (frame.Type == FrameType.RstStream) { State = StreamState.Closed; } break; case StreamState.HalfClosedRemote: if (frame.Type == FrameType.RstStream) { State = StreamState.Closed; } break; case StreamState.Open: if (frame.IsEndStream) { State = StreamState.HalfClosedRemote; } if (frame.Type == FrameType.RstStream) { State = StreamState.Closed; } break; case StreamState.ReservedRemote: if (frame.Type == FrameType.Headers) { State = StreamState.HalfClosedLocal; } else if (frame.Type == FrameType.RstStream) { State = StreamState.Closed; } break; case StreamState.HalfClosedLocal: if (frame.IsEndStream || frame.Type == FrameType.RstStream) { State = StreamState.Closed; } break; } // Server has cleared up more window space // Add more to the available window if (frame.Type == FrameType.WindowUpdate) { var windowUpdateFrame = (WindowUpdateFrame)frame; flowControlStateManager.IncreaseWindowSize(StreamIdentifer, windowUpdateFrame.WindowSizeIncrement); } // Raise the event OnFrameReceived?.Invoke(frame); }