Skip to content

Instantly share code, notes, and snippets.

@DavidEichmann
Last active November 17, 2020 10:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DavidEichmann/66c6b13a971ce5c1a7ff56f5369b011b to your computer and use it in GitHub Desktop.
Save DavidEichmann/66c6b13a971ce5c1a7ff56f5369b011b to your computer and use it in GitHub Desktop.
stub.cpp large commands
/* return non-zero on error */
static void handle_connection(const unsigned int sock_fd) {
Socket sock(sock_fd);
char *buf = new char[MAX_CMD_SIZE];
while (true) {
uint32_t cmdlen_n, cmdlen;
sock.read((char *)&cmdlen_n, 4);
cmdlen = ntohl(cmdlen_n);
cmdlen = ntohl(cmdlen_n);
char *large_buf = buf;
bool use_large_buf = cmdlen > MAX_CMD_SIZE;
if (use_large_buf) {
large_buf = new char[cmdlen];
}
trace("LEN: %d\n", cmdlen);
sock.read(large_buf, cmdlen);
trace("CONT:%s\n", buf);
try {
trace("LEN2: %d\n", cmdlen);
handle_command(sock, large_buf, cmdlen);
} catch (Parser::EndOfInput e) {
barf("error");
Response resp(sock);
resp.finish(RESP_BAD_COMMAND);
}
if (use_large_buf) {
delete[] large_buf;
}
}
delete[] buf;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment