From 623a5fcf8d5ba35e1d263ffaf262e7e38fe3ec5f Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Wed, 24 Feb 2016 00:18:59 -0800 Subject: [PATCH] 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 --- wpilibc/Athena/src/CameraServer.cpp | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/wpilibc/Athena/src/CameraServer.cpp b/wpilibc/Athena/src/CameraServer.cpp index 54b7e0639e..5906cac397 100644 --- a/wpilibc/Athena/src/CameraServer.cpp +++ b/wpilibc/Athena/src/CameraServer.cpp @@ -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