code/WPILib/Vision/AxisCamera.cpp
/**
 * This function actually reads the images from the camera.
 */
This function actually reads the images from the camera.
This function actually reads the images from the camera .
int AxisCamera::ReadImagesFromCamera()
{
	char *imgBuffer = NULL;
	int imgBufferLength = 0;
	int counter = 2;
	while (1)
	{
		char initialReadBuffer[kMaxPacketSize] = "";
		char intermediateBuffer[1];
		char *trailingPtr = initialReadBuffer;
		int trailingCounter = 0;
		while (counter)
		{
			if(recv(m_cameraSocket, intermediateBuffer, 1, 0) == ERROR)
			{
				perror ("AxisCamera: Failed to read image header");
				close (m_cameraSocket);
				return (ERROR);
			}
			strncat(initialReadBuffer, intermediateBuffer, 1);
			if (NULL != strstr(trailingPtr, "\r\n\r\n"))
			{
				--counter;
			}
			if (++trailingCounter >= 4)
			{
				trailingPtr++;
			}
		}
		counter = 1;
		char *contentLength = strstr(initialReadBuffer, "Content-Length: ");
		if (contentLength == NULL)
		{
			perror("AxisCamera: No content-length token found in packet");
			close(m_cameraSocket);
			return(ERROR);
		}
		contentLength = contentLength + 16; 
		int readLength = atol(contentLength); 
		if (imgBufferLength < readLength)
		{
			if (imgBuffer) delete[] imgBuffer;
			imgBufferLength = readLength + kImageBufferAllocationIncrement;
			imgBuffer = new char[imgBufferLength];
			if (imgBuffer == NULL)
			{
				imgBufferLength = 0;
				continue;
			}
		}
		int bytesRead = 0;
		int remaining = readLength;
		while(bytesRead < readLength)
		{
			int bytesThisRecv = recv(m_cameraSocket, &imgBuffer[bytesRead], remaining, 0);
			bytesRead += bytesThisRecv;
			remaining -= bytesThisRecv;
		}
		UpdatePublicImageFromCamera(imgBuffer, readLength);
	}
}
int AxisCamera :: ReadImagesFromCamera ( ) { char * imgBuffer = NULL ; int imgBufferLength = 0 ; int counter = 2 ; while ( 1 ) { char initialReadBuffer [ kMaxPacketSize ] = " " ; char intermediateBuffer [ 1 ] ; char * trailingPtr = initialReadBuffer ; int trailingCounter = 0 ; while ( counter ) { if ( recv ( m_cameraSocket , intermediateBuffer , 1 , 0 ) == ERROR ) { perror ( " " ) ; close ( m_cameraSocket ) ; return ( ERROR ) ; } strncat ( initialReadBuffer , intermediateBuffer , 1 ) ; if ( NULL != strstr ( trailingPtr , " \r \n \r \n " ) ) { -- counter ; } if ( ++ trailingCounter >= 4 ) { trailingPtr ++ ; } } counter = 1 ; char * contentLength = strstr ( initialReadBuffer , " " ) ; if ( contentLength == NULL ) { perror ( " " ) ; close ( m_cameraSocket ) ; return ( ERROR ) ; } contentLength = contentLength + 16 ; int readLength = atol ( contentLength ) ; if ( imgBufferLength < readLength ) { if ( imgBuffer ) delete [ ] imgBuffer ; imgBufferLength = readLength + kImageBufferAllocationIncrement ; imgBuffer = new char [ imgBufferLength ] ; if ( imgBuffer == NULL ) { imgBufferLength = 0 ; continue ; } } int bytesRead = 0 ; int remaining = readLength ; while ( bytesRead < readLength ) { int bytesThisRecv = recv ( m_cameraSocket , & imgBuffer [ bytesRead ] , remaining , 0 ) ; bytesRead += bytesThisRecv ; remaining -= bytesThisRecv ; } UpdatePublicImageFromCamera ( imgBuffer , readLength ) ; } }
This function actually reads the images from the camera.
This function actually reads the images from the camera .
//Infinite loop, task deletion handled by taskDeleteHook // Socket cleanup handled by destructor // TODO: these recv calls must be non-blocking. Otherwise if the camera // fails during a read, the code hangs and never retries when the camera comes // back up. // TODO: fix me... this cannot be the most efficient way to approach this, reading one byte at a time. // trailingCounter ensures that we start looking for the 4 byte string after // there is at least 4 bytes total. Kind of obscure. // look for 2 blank lines (\r\n) // skip past "content length" // get the image byte count // Make sure buffer is large enough // Read the image data for "Content-Length" bytes // Update image