int mdb_t::get ( const char * key, size_t key_len, conn_ctxt_t conn, std::string * & rsp, int * rsp_len ) { if ( unlikely ( ! m_ok ) ) { return EINVAL; } std::string * rspi = m_buffers[ conn.worker_id ]; int r = process_get_command ( ( char * ) key, key_len, ( char * ) & ( * rspi ) [ 0 ], rsp_len ); if ( unlikely ( ! r ) ) { return ENOENT; } rsp = rspi; return 0; }
void process_command_connection(int con, int i2c_handle, bool verbose) { char request[256]; char *back; char response[256]; struct line_reader reader; init_line_reader(&reader, read_from_socket, BUFFER_SIZE, &con); while (1) { int result = read_line(&reader, request, sizeof(request)); if (result != 0) break; /* Get rid of any trailing \r or \n. */ back = request + strlen(request) - 1; while (back >= request && (*back == '\n' || *back == '\r')) *(back--) = 0; if (verbose) printf("Request: %s\n", request); if (strncmp("ping", request, 4) == 0) { process_ping_command(request, response, sizeof(response)); } else if (strncmp("get", request, 3) == 0) { process_get_command(request, i2c_handle, response, sizeof(response)); } else if (strncmp("set", request, 3) == 0) { process_set_command(request, i2c_handle, response, sizeof(response)); } else if (strncmp("addpoll", request, 7) == 0) { process_add_poll_command(request, response, sizeof(response)); } else if (strncmp("rmpoll", request, 5) == 0) { process_remove_poll_command(request, response, sizeof(response)); } else if (strncmp("help", request, 4) == 0) { process_help(request, response, sizeof(response)); } else { fprintf(stderr, "ERROR: unknown command\n"); strcpy(response, "ERROR\r\n"); } if (verbose) printf("Response: %s", response); result = send(con, response, strlen(response), MSG_NOSIGNAL); if (result != strlen(response)) { fprintf(stderr, "ERROR: Error writing to socket\n"); break; } } close_reader(&reader); }
// parse a single memcached request. bool parse_command(conn *c, char *command) { token_t tokens[MAX_TOKENS]; size_t ntokens; int comm; assert(c != NULL); ntokens = tokenize_command(command, tokens, MAX_TOKENS); if (ntokens >= 3 && ((strcmp(tokens[COMMAND_TOKEN].value, "get") == 0) || (strcmp(tokens[COMMAND_TOKEN].value, "bget") == 0))) { process_get_command(c, tokens, ntokens, false); } else if (ntokens >= 3 && (strcmp(tokens[COMMAND_TOKEN].value, "gets") == 0)) { process_get_command(c, tokens, ntokens, true); } else if ((ntokens == 6 || ntokens == 7) && ((strcmp(tokens[COMMAND_TOKEN].value, "add") == 0 && (comm = NREAD_ADD)) || (strcmp(tokens[COMMAND_TOKEN].value, "set") == 0 && (comm = NREAD_SET)) || (strcmp(tokens[COMMAND_TOKEN].value, "replace") == 0 && (comm = NREAD_REPLACE)) || (strcmp(tokens[COMMAND_TOKEN].value, "prepend") == 0 && (comm = NREAD_PREPEND)) || (strcmp(tokens[COMMAND_TOKEN].value, "append") == 0 && (comm = NREAD_APPEND)) )) { process_update_command(c, tokens, ntokens, comm, false); } else if ((ntokens == 7 || ntokens == 8) && (strcmp(tokens[COMMAND_TOKEN].value, "cas") == 0 && (comm = NREAD_CAS))) { process_update_command(c, tokens, ntokens, comm, true); /* } else if ((ntokens == 4 || ntokens == 5) && */ /* (strcmp(tokens[COMMAND_TOKEN].value, "incr") == 0)) { */ /* process_arithmetic_command(c, tokens, ntokens, 1); */ /* } else if ((ntokens == 4 || ntokens == 5) && */ /* (strcmp(tokens[COMMAND_TOKEN].value, "decr") == 0)) { */ /* process_arithmetic_command(c, tokens, ntokens, 0); */ /* } else if (ntokens >= 3 && ntokens <= 5 && */ /* (strcmp(tokens[COMMAND_TOKEN].value, "delete") == 0)) { */ /* process_delete_command(c, tokens, ntokens); */ /* } else if ((ntokens == 4 || ntokens == 5) && */ /* (strcmp(tokens[COMMAND_TOKEN].value, "touch") == 0)) { */ /* process_touch_command(c, tokens, ntokens); */ } else if (ntokens >= 2 && (strcmp(tokens[COMMAND_TOKEN].value, "stats") == 0)) { process_stat_command(c, tokens, ntokens); /* } else if (ntokens >= 2 && ntokens <= 4 && */ /* (strcmp(tokens[COMMAND_TOKEN].value, "flush_all") == 0)) { */ /* process_flush_cmd(c, tokens, ntokens); */ /* } else if (ntokens == 2 && */ /* (strcmp(tokens[COMMAND_TOKEN].value, "version") == 0)) { */ /* process_version_cmd(c, tokens, ntokens); */ /* } else if (ntokens == 2 && */ /* (strcmp(tokens[COMMAND_TOKEN].value, "quit") == 0)) { */ /* process_quite_cmd(c, tokens, ntokens); */ /* } else if (ntokens == 2 && */ /* (strcmp(tokens[COMMAND_TOKEN].value, "shutdown") == 0)) { */ /* process_shutdown_cmd(c, tokens, ntokens); */ /* } else if (ntokens > 1 && */ /* (strcmp(tokens[COMMAND_TOKEN].value, "slabs") == 0)) { */ /* process_slab_cmd(c, tokens, ntokens); */ /* } else if ((ntokens == 3 || ntokens == 4) && */ /* (strcmp(tokens[COMMAND_TOKEN].value, "verbosity") == 0)) { */ /* process_verbosity_command(c, tokens, ntokens); */ } else { return false; } // NOTE: No refcnt needed here as not changing pointer structure. mutex_lock(&c->stats->lock); c->stats->requests++; mutex_unlock(&c->stats->lock); return true; }