Fix C++ CameraServer request handling.

The request is not guaranteed to be in a single packet, so it may be necessary
to perform multiple reads to get the complete request.  The previous code
could unpredictably fail as it only performed a single read.

Fixes artf4817.

Change-Id: I7734a12e1a2542f5d7ca0889453c387f0bb30538
This commit is contained in:
Peter Johnson
2016-02-24 00:18:59 -08:00
parent 6bd1654b80
commit 623a5fcf8d

View File

@@ -189,16 +189,26 @@ void CameraServer::Serve() {
}
Request req;
if (read(conn, &req, sizeof(req)) == -1) {
char reqBuf[sizeof(req)];
size_t reqPos = 0;
while (reqPos < sizeof(req)) {
ssize_t sizeRead = read(conn, &reqBuf[reqPos], sizeof(req) - reqPos);
if (sizeRead < 0) break;
reqPos += sizeRead;
}
if (reqPos < sizeof(req)) {
wpi_setErrnoError();
close(conn);
continue;
} else {
req.fps = ntohl(req.fps);
req.compression = ntohl(req.compression);
req.size = ntohl(req.size);
}
memcpy(&req, reqBuf, sizeof(req));
req.fps = ntohl(req.fps);
req.compression = ntohl(req.compression);
req.size = ntohl(req.size);
// TODO: Support the SW Compression. The rest of the code below will work as
// though this
// check isn't here