- memcached_return_t rc;
- rc= memcached_send(ptr, group_key, group_key_length,
- key, key_length, value, value_length,
- expiration, flags, cas, CAS_OP);
- return rc;
-}
-
-static inline uint8_t get_com_code(memcached_storage_action_t verb, bool noreply)
-{
- /* 0 isn't a value we want, but GCC 4.2 seems to think ret can otherwise
- * be used uninitialized in this function. FAIL */
- uint8_t ret= 0;
-
- if (noreply)
- switch (verb)
- {
- case SET_OP:
- ret=PROTOCOL_BINARY_CMD_SETQ;
- break;
- case ADD_OP:
- ret=PROTOCOL_BINARY_CMD_ADDQ;
- break;
- case CAS_OP: /* FALLTHROUGH */
- case REPLACE_OP:
- ret=PROTOCOL_BINARY_CMD_REPLACEQ;
- break;
- case APPEND_OP:
- ret=PROTOCOL_BINARY_CMD_APPENDQ;
- break;
- case PREPEND_OP:
- ret=PROTOCOL_BINARY_CMD_PREPENDQ;
- break;
- default:
- WATCHPOINT_ASSERT(verb);
- break;
- }
- else
- switch (verb)
- {
- case SET_OP:
- ret=PROTOCOL_BINARY_CMD_SET;
- break;
- case ADD_OP:
- ret=PROTOCOL_BINARY_CMD_ADD;
- break;
- case CAS_OP: /* FALLTHROUGH */
- case REPLACE_OP:
- ret=PROTOCOL_BINARY_CMD_REPLACE;
- break;
- case APPEND_OP:
- ret=PROTOCOL_BINARY_CMD_APPEND;
- break;
- case PREPEND_OP:
- ret=PROTOCOL_BINARY_CMD_PREPEND;
- break;
- default:
- WATCHPOINT_ASSERT(verb);
- break;
- }
-
- return ret;
-}
-
-
-
-static memcached_return_t memcached_send_binary(memcached_st *ptr,
- memcached_server_write_instance_st server,
- uint32_t server_key,
- const char *key,
- size_t key_length,
- const char *value,
- size_t value_length,
- time_t expiration,
- uint32_t flags,
- uint64_t cas,
- memcached_storage_action_t verb)
-{
- bool flush;
- protocol_binary_request_set request= {};
- size_t send_length= sizeof(request.bytes);
-
- bool noreply= server->root->flags.no_reply;
-
- request.message.header.request.magic= PROTOCOL_BINARY_REQ;
- request.message.header.request.opcode= get_com_code(verb, noreply);
- request.message.header.request.keylen= htons((uint16_t)(key_length + memcached_array_size(ptr->_namespace)));
- request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
- if (verb == APPEND_OP || verb == PREPEND_OP)
- send_length -= 8; /* append & prepend does not contain extras! */
- else
- {
- request.message.header.request.extlen= 8;
- request.message.body.flags= htonl(flags);
- request.message.body.expiration= htonl((uint32_t)expiration);
- }
-
- request.message.header.request.bodylen= htonl((uint32_t) (key_length + memcached_array_size(ptr->_namespace) + value_length +
- request.message.header.request.extlen));
-
- if (cas)
- request.message.header.request.cas= memcached_htonll(cas);
-
- flush= (bool) ((server->root->flags.buffer_requests && verb == SET_OP) ? 0 : 1);
-
- if (server->root->flags.use_udp && ! flush)
- {
- size_t cmd_size= send_length + key_length + value_length;
-
- if (cmd_size > MAX_UDP_DATAGRAM_LENGTH - UDP_DATAGRAM_HEADER_LENGTH)
- {
- return MEMCACHED_WRITE_FAILURE;
- }
- if (cmd_size + server->write_buffer_offset > MAX_UDP_DATAGRAM_LENGTH)
- {
- memcached_io_write(server, NULL, 0, true);
- }
- }
-
- struct libmemcached_io_vector_st vector[]=
- {
- { send_length, request.bytes },
- { memcached_array_size(ptr->_namespace), memcached_array_string(ptr->_namespace) },
- { key_length, key },
- { value_length, value }
- };
-
- /* write the header */
- memcached_return_t rc;
- if ((rc= memcached_vdo(server, vector, 4, flush)) != MEMCACHED_SUCCESS)
- {
- memcached_io_reset(server);
- return (rc == MEMCACHED_SUCCESS) ? MEMCACHED_WRITE_FAILURE : rc;
- }
-
- if (verb == SET_OP && ptr->number_of_replicas > 0)
- {
- request.message.header.request.opcode= PROTOCOL_BINARY_CMD_SETQ;
- WATCHPOINT_STRING("replicating");
-
- for (uint32_t x= 0; x < ptr->number_of_replicas; x++)
- {
- memcached_server_write_instance_st instance;
-
- ++server_key;
- if (server_key == memcached_server_count(ptr))
- server_key= 0;
-
- instance= memcached_server_instance_fetch(ptr, server_key);
-
- if (memcached_vdo(instance, vector, 4, false) != MEMCACHED_SUCCESS)
- {
- memcached_io_reset(instance);
- }
- else
- {
- memcached_server_response_decrement(instance);
- }
- }
- }
-
- if (flush == false)
- {
- return MEMCACHED_BUFFERED;
- }
-
- if (noreply)
- {
- return MEMCACHED_SUCCESS;
- }
-
- return memcached_response(server, NULL, 0, NULL);