Exemple #1
0
/**
 * Spool data into the send-buffer for a client.
 *
 * @param client the client to spool the data for
 * @param data the data to spool
 * @param length the number of bytes of data to spool
 * @return PROTOCOL_BINARY_RESPONSE_SUCCESS if success,
 *         PROTOCOL_BINARY_RESPONSE_ENOMEM if we failed to allocate memory
 */
static protocol_binary_response_status spool_output(struct memcached_protocol_client_st *client,
                                                    const void *data,
                                                    size_t length)
{
  if (client->mute)
  {
    return PROTOCOL_BINARY_RESPONSE_SUCCESS;
  }

  size_t offset= 0;

  struct chunk_st *chunk= client->output;
  while (offset < length)
  {
    if (chunk == NULL || (chunk->size - chunk->nbytes) == 0)
    {
      if ((chunk= allocate_output_chunk(client)) == NULL)
      {
        return PROTOCOL_BINARY_RESPONSE_ENOMEM;
      }
    }

    size_t bulk= length - offset;
    if (bulk > chunk->size - chunk->nbytes)
    {
      bulk= chunk->size - chunk->nbytes;
    }

    memcpy(chunk->data + chunk->nbytes, data, bulk);
    chunk->nbytes += bulk;
    offset += bulk;
  }

  return PROTOCOL_BINARY_RESPONSE_SUCCESS;
}
/**
 * Spool data into the send-buffer for a client.
 *
 * @param client the client to spool the data for
 * @param data the data to spool
 * @param length the number of bytes of data to spool
 * @return PROTOCOL_BINARY_RESPONSE_SUCCESS if success,
 *         PROTOCOL_BINARY_RESPONSE_ENOMEM if we failed to allocate memory
 */
static protocol_binary_response_status spool_output(struct memcached_protocol_client_st *client,
                                                    const void *data,
                                                    size_t length)
{
  if (client->is_verbose)
  {
    fprintf(stderr, "%s:%d %s mute:%d length:%d\n", __FILE__, __LINE__, __func__, (int)client->mute, (int)length);
  }

  if (client->mute)
  {
    return PROTOCOL_BINARY_RESPONSE_SUCCESS;
  }

  size_t offset= 0;

  struct chunk_st *chunk= client->output;
  while (offset < length)
  {
    if (chunk == NULL || (chunk->size - chunk->nbytes) == 0)
    {
      if ((chunk= allocate_output_chunk(client)) == NULL)
      {
        return PROTOCOL_BINARY_RESPONSE_ENOMEM;
      }
    }

    size_t bulk= length - offset;
    if (bulk > chunk->size - chunk->nbytes)
    {
      bulk= chunk->size - chunk->nbytes;
    }

    memcpy(chunk->data + chunk->nbytes, data, bulk);
    chunk->nbytes += bulk;
    offset += bulk;
  }

  return PROTOCOL_BINARY_RESPONSE_SUCCESS;
}