Also, this version includes an update to the server startup so that we don't try to shutdown anything we didn't start.
cmd->plain.message.header.request.bodylen=
ntohl(cmd->plain.message.header.request.bodylen);
cmd->plain.message.header.request.cas=
- ntohll(cmd->plain.message.header.request.cas);
+ memcached_ntohll(cmd->plain.message.header.request.cas);
execute(resend_packet(cmd));
return TEST_PASS;
rsp->plain.message.header.response.bodylen=
ntohl(rsp->plain.message.header.response.bodylen);
rsp->plain.message.header.response.cas=
- ntohll(rsp->plain.message.header.response.cas);
+ memcached_ntohll(rsp->plain.message.header.response.cas);
size_t bodysz= rsp->plain.message.header.response.bodylen;
if (bodysz > 0)
cmd->incr.message.header.request.extlen= 20;
cmd->incr.message.header.request.bodylen= (uint32_t)(keylen + 20);
cmd->incr.message.header.request.opaque= 0xdeadbeef;
- cmd->incr.message.body.delta= htonll(delta);
- cmd->incr.message.body.initial= htonll(initial);
+ cmd->incr.message.body.delta= memcached_htonll(delta);
+ cmd->incr.message.body.initial= memcached_htonll(initial);
cmd->incr.message.body.expiration= htonl(exptime);
off_t key_offset= sizeof (protocol_binary_request_no_extras) + 20;
}
/* try to set with the correct CAS value */
- cmd.plain.message.header.request.cas=
- htonll(rsp.plain.message.header.response.cas);
+ cmd.plain.message.header.request.cas= memcached_htonll(rsp.plain.message.header.response.cas);
execute(resend_packet(&cmd));
if (cc == PROTOCOL_BINARY_CMD_SET)
{
execute(test_binary_noop());
/* try to set with an incorrect CAS value */
- cmd.plain.message.header.request.cas=
- htonll(rsp.plain.message.header.response.cas - 1);
+ cmd.plain.message.header.request.cas= memcached_htonll(rsp.plain.message.header.response.cas - 1);
execute(resend_packet(&cmd));
execute(send_binary_noop());
execute(recv_packet(&rsp));
}
/* verify that replace with CAS value works! */
- cmd.plain.message.header.request.cas=
- htonll(rsp.plain.message.header.response.cas);
+ cmd.plain.message.header.request.cas= memcached_htonll(rsp.plain.message.header.response.cas);
execute(resend_packet(&cmd));
if (cc == PROTOCOL_BINARY_CMD_REPLACE)
execute(test_binary_noop());
/* try to set with an incorrect CAS value */
- cmd.plain.message.header.request.cas=
- htonll(rsp.plain.message.header.response.cas - 1);
+ cmd.plain.message.header.request.cas= memcached_htonll(rsp.plain.message.header.response.cas - 1);
execute(resend_packet(&cmd));
execute(send_binary_noop());
execute(recv_packet(&rsp));
{
execute(recv_packet(&rsp));
verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
- verify(ntohll(rsp.incr.message.body.value) == ii);
+ verify(memcached_ntohll(rsp.incr.message.body.value) == ii);
}
else
execute(test_binary_noop());
{
execute(recv_packet(&rsp));
verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
- verify(ntohll(rsp.decr.message.body.value) == (uint64_t)ii);
+ verify(memcached_ntohll(rsp.decr.message.body.value) == (uint64_t)ii);
}
else
execute(test_binary_noop());
{
execute(recv_packet(&rsp));
verify(validate_response_header(&rsp, cc, PROTOCOL_BINARY_RESPONSE_SUCCESS));
- verify(ntohll(rsp.decr.message.body.value) == 0);
+ verify(memcached_ntohll(rsp.decr.message.body.value) == 0);
}
else
{
msg.response.message.body.flags= htonl(item->flags);
char *ptr= (char*)(msg.response.bytes + sizeof(*header) + 4);
uint32_t bodysize= 4;
- msg.response.message.header.response.cas= htonll(item->cas);
+ msg.response.message.header.response.cas= memcached_htonll(item->cas);
if (opcode == PROTOCOL_BINARY_CMD_GETK || opcode == PROTOCOL_BINARY_CMD_GETKQ)
{
memcpy(ptr, item->key, item->nkey);
};
uint16_t keylen= ntohs(header->request.keylen);
- uint64_t initial= ntohll(req->message.body.initial);
- uint64_t delta= ntohll(req->message.body.delta);
+ uint64_t initial= memcached_ntohll(req->message.body.initial);
+ uint64_t delta= memcached_ntohll(req->message.body.delta);
uint32_t expiration= ntohl(req->message.body.expiration);
uint32_t flags= 0;
void *key= req->bytes + sizeof(req->bytes);
if (rval == PROTOCOL_BINARY_RESPONSE_SUCCESS)
{
response.message.header.response.bodylen= ntohl(8);
- response.message.body.value= ntohll((*(uint64_t*)item->data));
- response.message.header.response.cas= ntohll(item->cas);
+ response.message.body.value= memcached_ntohll((*(uint64_t*)item->data));
+ response.message.header.response.cas= memcached_ntohll(item->cas);
release_item(item);
if (header->request.opcode == PROTOCOL_BINARY_CMD_INCREMENTQ ||
.opcode= PROTOCOL_BINARY_CMD_VERSION,
.status= htons(PROTOCOL_BINARY_RESPONSE_SUCCESS),
.opaque= header->request.opaque,
+ .cas= 0,
.bodylen= htonl((uint32_t)strlen(versionstring))
}
};
{
protocol_binary_response_status rval= PROTOCOL_BINARY_RESPONSE_SUCCESS;
uint16_t keylen= ntohs(header->request.keylen);
- uint64_t cas= ntohll(header->request.cas);
+ uint64_t cas= memcached_ntohll(header->request.cas);
void *key= header + 1;
uint32_t vallen= ntohl(header->request.bodylen) - keylen;
void *val= (char*)key + keylen;
.opcode= header->request.opcode,
.status= htons(rval),
.opaque= header->request.opaque,
- .cas= htonll(cas),
+ .cas= memcached_htonll(cas),
}
}
};
struct item* item= get_item(key, keylen);
if (item != NULL)
{
- if (item->cas != ntohll(header->request.cas))
+ if (item->cas != memcached_ntohll(header->request.cas))
{
release_item(item);
response.message.header.response.status= htons(PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS);
/* SETQ shouldn't return a message */
if (header->request.opcode == PROTOCOL_BINARY_CMD_SET)
{
- response.message.header.response.cas= htonll(item->cas);
+ response.message.header.response.cas= memcached_htonll(item->cas);
release_item(item);
return response_handler(cookie, header, (void*)&response);
}
/* ADDQ shouldn't return a message */
if (header->request.opcode == PROTOCOL_BINARY_CMD_ADD)
{
- response.message.header.response.cas= htonll(item->cas);
+ response.message.header.response.cas= memcached_htonll(item->cas);
release_item(item);
return response_handler(cookie, header, (void*)&response);
}
struct item* item= get_item(key, keylen);
if (item == NULL)
+ {
response.message.header.response.status= htons(PROTOCOL_BINARY_RESPONSE_KEY_ENOENT);
- else if (header->request.cas == 0 || ntohll(header->request.cas) == item->cas)
+ }
+ else if (header->request.cas == 0 || memcached_ntohll(header->request.cas) == item->cas)
{
release_item(item);
delete_item(key, keylen);
item= create_item(key, keylen, data, datalen, flags, timeout);
+
if (item == NULL)
+ {
response.message.header.response.status= htons(PROTOCOL_BINARY_RESPONSE_ENOMEM);
+ }
else
{
put_item(item);
/* REPLACEQ shouldn't return a message */
if (header->request.opcode == PROTOCOL_BINARY_CMD_REPLACE)
{
- response.message.header.response.cas= htonll(item->cas);
+ response.message.header.response.cas= memcached_htonll(item->cas);
release_item(item);
return response_handler(cookie, header, (void*)&response);
}
request.message.header.request.extlen= 20;
request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
request.message.header.request.bodylen= htonl((uint32_t)(key_length + memcached_array_size(ptr->prefix_key) +request.message.header.request.extlen));
- request.message.body.delta= htonll(offset);
- request.message.body.initial= htonll(initial);
+ request.message.body.delta= memcached_htonll(offset);
+ request.message.body.initial= memcached_htonll(initial);
request.message.body.expiration= htonl((uint32_t) expiration);
struct libmemcached_io_vector_st vector[]=
*
*/
-#include <libmemcached/common.h>
+#include <config.h>
+
+#include <sys/types.h>
+
+#include <libmemcached/visibility.h>
+#include <libmemcached/byteorder.h>
/* Byte swap a 64-bit number. */
#ifndef swap64
}
#endif
+#ifdef HAVE_HTONLL
+
+uint64_t memcached_ntohll(uint64_t value)
+{
+ return ntohll(value);
+}
+
+uint64_t memcached_htonll(uint64_t value)
+{
+ return htonll(value);
+}
+
+#else // HAVE_HTONLL
+
uint64_t memcached_ntohll(uint64_t value)
{
return swap64(value);
{
return swap64(value);
}
+
+#endif // HAVE_HTONLL
#pragma once
-#if HAVE_SYS_TYPES_H
-#include <sys/types.h>
-#endif
-
-#ifndef HAVE_HTONLL
-#define ntohll(a) memcached_ntohll(a)
-#define htonll(a) memcached_htonll(a)
-
#ifdef __cplusplus
extern "C" {
#endif
LIBMEMCACHED_LOCAL
uint64_t memcached_ntohll(uint64_t);
+
LIBMEMCACHED_LOCAL
uint64_t memcached_htonll(uint64_t);
+
#ifdef __cplusplus
}
#endif
-
-#endif
-
-#ifdef linux
-/* /usr/include/netinet/in.h defines macros from ntohs() to _bswap_nn to
- * optimize the conversion functions, but the prototypes generate warnings
- * from gcc. The conversion methods isn't the bottleneck for my app, so
- * just remove the warnings by undef'ing the optimization ..
- */
-#undef ntohs
-#undef ntohl
-#undef htons
-#undef htonl
-#endif
.opcode= opcode,
.status= htons(PROTOCOL_BINARY_RESPONSE_SUCCESS),
.opaque= client->current_command->request.opaque,
- .cas= htonll(cas),
+ .cas= memcached_htonll(cas),
.keylen= htons(keylen),
.extlen= 4,
.bodylen= htonl(bodylen + keylen + 4),
* @param body the length of the body
* @param bodylen the length of the body
*/
-static protocol_binary_response_status
-stat_response_handler(const void *cookie,
- const void *key,
- uint16_t keylen,
- const void *body,
- uint32_t bodylen) {
+static protocol_binary_response_status stat_response_handler(const void *cookie,
+ const void *key,
+ uint16_t keylen,
+ const void *body,
+ uint32_t bodylen)
+{
memcached_protocol_client_st *client= (void*)cookie;
.opaque= client->current_command->request.opaque,
.keylen= htons(keylen),
.bodylen= htonl(bodylen + keylen),
+ .cas= 0
},
};
.status= htons(PROTOCOL_BINARY_RESPONSE_SUCCESS),
.opaque= client->current_command->request.opaque,
.bodylen= htonl(textlen),
+ .cas= 0
},
};
.opcode= PROTOCOL_BINARY_CMD_ADD,
.status= htons(PROTOCOL_BINARY_RESPONSE_SUCCESS),
.opaque= header->request.opaque,
- .cas= ntohll(cas)
+ .cas= memcached_ntohll(cas)
}
}
};
{
uint16_t keylen= ntohs(header->request.keylen);
protocol_binary_request_decr *request= (void*)header;
- uint64_t init= ntohll(request->message.body.initial);
- uint64_t delta= ntohll(request->message.body.delta);
+ uint64_t init= memcached_ntohll(request->message.body.initial);
+ uint64_t delta= memcached_ntohll(request->message.body.delta);
uint32_t timeout= ntohl(request->message.body.expiration);
void *key= request->bytes + sizeof(request->bytes);
uint64_t result;
.opcode= PROTOCOL_BINARY_CMD_DECREMENT,
.status= htons(PROTOCOL_BINARY_RESPONSE_SUCCESS),
.opaque= header->request.opaque,
- .cas= ntohll(cas),
+ .cas= memcached_ntohll(cas),
.bodylen= htonl(8)
},
- .body.value= htonll(result)
+ .body.value= memcached_htonll(result)
}
};
rval= response_handler(cookie, header, (void*)&response);
if (client->root->callback->interface.v1.delete != NULL)
{
uint16_t keylen= ntohs(header->request.keylen);
- void *key= (header + 1);
- uint64_t cas= ntohll(header->request.cas);
+ void *key= (header +1);
+ uint64_t cas= memcached_ntohll(header->request.cas);
rval= client->root->callback->interface.v1.delete(cookie, key, keylen, cas);
if (rval == PROTOCOL_BINARY_RESPONSE_SUCCESS &&
header->request.opcode == PROTOCOL_BINARY_CMD_DELETE)
{
uint16_t keylen= ntohs(header->request.keylen);
protocol_binary_request_incr *request= (void*)header;
- uint64_t init= ntohll(request->message.body.initial);
- uint64_t delta= ntohll(request->message.body.delta);
+ uint64_t init= memcached_ntohll(request->message.body.initial);
+ uint64_t delta= memcached_ntohll(request->message.body.delta);
uint32_t timeout= ntohl(request->message.body.expiration);
void *key= request->bytes + sizeof(request->bytes);
uint64_t cas;
.opcode= PROTOCOL_BINARY_CMD_INCREMENT,
.status= htons(PROTOCOL_BINARY_RESPONSE_SUCCESS),
.opaque= header->request.opaque,
- .cas= ntohll(cas),
+ .cas= memcached_ntohll(cas),
.bodylen= htonl(8)
},
- .body.value= htonll(result)
+ .body.value= memcached_htonll(result)
}
};
{
uint16_t keylen= ntohs(header->request.keylen);
uint32_t datalen= ntohl(header->request.bodylen) - keylen;
- char *key= (void*)(header + 1);
- char *data= key + keylen;
- uint64_t cas= ntohll(header->request.cas);
+ char *key= (void*)(header +1);
+ char *data= key +keylen;
+ uint64_t cas= memcached_ntohll(header->request.cas);
uint64_t result_cas;
rval= client->root->callback->interface.v1.append(cookie, key, keylen,
.opcode= PROTOCOL_BINARY_CMD_APPEND,
.status= htons(PROTOCOL_BINARY_RESPONSE_SUCCESS),
.opaque= header->request.opaque,
- .cas= ntohll(result_cas),
+ .cas= memcached_ntohll(result_cas),
},
}
};
uint32_t datalen= ntohl(header->request.bodylen) - keylen;
char *key= (char*)(header + 1);
char *data= key + keylen;
- uint64_t cas= ntohll(header->request.cas);
+ uint64_t cas= memcached_ntohll(header->request.cas);
uint64_t result_cas;
rval= client->root->callback->interface.v1.prepend(cookie, key, keylen,
data, datalen, cas,
.opcode= PROTOCOL_BINARY_CMD_PREPEND,
.status= htons(PROTOCOL_BINARY_RESPONSE_SUCCESS),
.opaque= header->request.opaque,
- .cas= ntohll(result_cas),
+ .cas= memcached_ntohll(result_cas),
},
}
};
uint32_t timeout= ntohl(request->message.body.expiration);
char *key= ((char*)header) + sizeof(*header) + 8;
char *data= key + keylen;
- uint64_t cas= ntohll(header->request.cas);
+ uint64_t cas= memcached_ntohll(header->request.cas);
uint64_t result_cas;
rval= client->root->callback->interface.v1.replace(cookie, key, keylen,
.opcode= PROTOCOL_BINARY_CMD_REPLACE,
.status= htons(PROTOCOL_BINARY_RESPONSE_SUCCESS),
.opaque= header->request.opaque,
- .cas= ntohll(result_cas),
+ .cas= memcached_ntohll(result_cas),
},
}
};
uint32_t timeout= ntohl(request->message.body.expiration);
char *key= ((char*)header) + sizeof(*header) + 8;
char *data= key + keylen;
- uint64_t cas= ntohll(header->request.cas);
+ uint64_t cas= memcached_ntohll(header->request.cas);
uint64_t result_cas;
.opcode= PROTOCOL_BINARY_CMD_SET,
.status= htons(PROTOCOL_BINARY_RESPONSE_SUCCESS),
.opaque= header->request.opaque,
- .cas= ntohll(result_cas),
+ .cas= memcached_ntohll(result_cas),
},
}
};
header.response.keylen= ntohs(header.response.keylen);
header.response.status= ntohs(header.response.status);
header.response.bodylen= ntohl(header.response.bodylen);
- header.response.cas= ntohll(header.response.cas);
+ header.response.cas= memcached_ntohll(header.response.cas);
uint32_t bodylen= header.response.bodylen;
if (header.response.status == PROTOCOL_BINARY_RESPONSE_SUCCESS ||
return MEMCACHED_UNKNOWN_READ_FAILURE;
}
- val= ntohll(val);
+ val= memcached_ntohll(val);
memcpy(buffer, &val, sizeof(val));
}
break;
request.message.header.request.extlen));
if (cas)
- request.message.header.request.cas= htonll(cas);
+ request.message.header.request.cas= memcached_htonll(cas);
flush= (bool) ((server->root->flags.buffer_requests && verb == SET_OP) ? 0 : 1);
if (libmemcached_util_flush("localhost", construct->port[x], NULL))
{
fprintf(stderr, "Found server on port %d, flushed it!\n", (int)construct->port[x]);
+ construct->is_used[x]= true;
} // If we can flush it, we will just use it
else
{
MEMCACHED_BINARY, construct->pid_file[x], construct->port[x], construct->port[x]);
}
- if (libmemcached_util_ping("localhost", construct->port[x], NULL))
- {
- fprintf(stderr, "Server on port %u already exists\n", construct->port[x]);
- }
- else
- {
- status= system(buffer);
- fprintf(stderr, "STARTING SERVER: %s status:%d\n", buffer, status);
- }
+ if (construct->is_used[x])
+ {
+ fprintf(stderr, "USING SERVER: %s\n", buffer);
+ }
+ else
+ {
+ if (libmemcached_util_ping("localhost", construct->port[x], NULL))
+ {
+ fprintf(stderr, "Server on port %u already exists\n", construct->port[x]);
+ }
+ else
+ {
+ status= system(buffer);
+ fprintf(stderr, "STARTING SERVER: %s status:%d\n", buffer, status);
+ }
+ }
int count;
size_t remaining_length= sizeof(server_string_buffer) - (size_t)(end_ptr -server_string_buffer);
for (uint32_t x= 0; x < construct->count; x++)
{
uint32_t counter= 3000; // Absurd, just to catch run away process
+
+ if (construct->is_used[x])
+ continue;
+
while (construct->pids[x] <= 0 && --counter)
{
FILE *file= fopen(construct->pid_file[x], "r");
}
fclose(file);
}
+
switch (errno)
{
default:
- fprintf(stderr, "Could not open pid file %s -> fopen(%s) -> %s:%d\n", construct->pid_file[x], strerror(errno),
- __FILE__, __LINE__);
+ fprintf(stderr, "Could not open pid file %s -> fopen(%s) -> %s:%d\n", construct->pid_file[x], strerror(errno), __FILE__, __LINE__);
abort();
+
case ENOENT:
case EINTR:
case EACCES:
{
for (uint32_t x= 0; x < construct->count; x++)
{
+ if (construct->is_used[x])
+ continue;
+
kill_file(construct->pid_file[x]);
}
char pid_file[SERVERS_TO_CREATE][FILENAME_MAX];
in_port_t port[SERVERS_TO_CREATE];
int pids[SERVERS_TO_CREATE];
+ bool is_used[SERVERS_TO_CREATE]; // Did we start it, or was it just sitting there?
};
void server_startup(server_startup_st *construct);
static test_return_t user_supplied_bug10(memcached_st *memc)
{
const char *key= "foo";
- char *value;
size_t value_length= 512;
- unsigned int x;
size_t key_len= 3;
unsigned int set= 1;
memcached_st *mclone= memcached_clone(NULL, memc);
- int32_t timeout;
memcached_behavior_set(mclone, MEMCACHED_BEHAVIOR_NO_BLOCK, set);
memcached_behavior_set(mclone, MEMCACHED_BEHAVIOR_TCP_NODELAY, set);
- timeout= 2;
- memcached_behavior_set(mclone, MEMCACHED_BEHAVIOR_POLL_TIMEOUT,
- (uint64_t)timeout);
+ int32_t timeout= 0;
+ memcached_behavior_set(mclone, MEMCACHED_BEHAVIOR_POLL_TIMEOUT, (uint64_t)timeout);
- value = (char*)malloc(value_length * sizeof(char));
+ char *value= (char*)malloc(value_length * sizeof(char));
- for (x= 0; x < value_length; x++)
+ for (unsigned int x= 0; x < value_length; x++)
+ {
value[x]= (char) (x % 127);
+ }
- for (x= 1; x <= 100000; ++x)
+ for (unsigned int x= 1; x <= 100000; ++x)
{
memcached_return_t rc= memcached_set(mclone, key, key_len,value, value_length, 0, 0);
- test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_WRITE_FAILURE ||
- rc == MEMCACHED_BUFFERED || rc == MEMCACHED_TIMEOUT);
+ test_true_got(rc == MEMCACHED_SUCCESS or rc == MEMCACHED_WRITE_FAILURE or rc == MEMCACHED_BUFFERED or rc == MEMCACHED_TIMEOUT or rc == MEMCACHED_CONNECTION_FAILURE,
+ memcached_strerror(NULL, rc));
- if (rc == MEMCACHED_WRITE_FAILURE || rc == MEMCACHED_TIMEOUT)
+ if (rc == MEMCACHED_WRITE_FAILURE or rc == MEMCACHED_TIMEOUT)
+ {
x--;
+ }
}
free(value);
static test_return_t user_supplied_bug11(memcached_st *memc)
{
const char *key= "foo";
- char *value;
size_t value_length= 512;
- unsigned int x;
size_t key_len= 3;
- memcached_return_t rc;
unsigned int set= 1;
- int32_t timeout;
memcached_st *mclone= memcached_clone(NULL, memc);
memcached_behavior_set(mclone, MEMCACHED_BEHAVIOR_NO_BLOCK, set);
memcached_behavior_set(mclone, MEMCACHED_BEHAVIOR_TCP_NODELAY, set);
- timeout= -1;
- memcached_behavior_set(mclone, MEMCACHED_BEHAVIOR_POLL_TIMEOUT,
- (size_t)timeout);
+ int32_t timeout= -1;
+ memcached_behavior_set(mclone, MEMCACHED_BEHAVIOR_POLL_TIMEOUT, (size_t)timeout);
timeout= (int32_t)memcached_behavior_get(mclone, MEMCACHED_BEHAVIOR_POLL_TIMEOUT);
test_true(timeout == -1);
- value = (char*)malloc(value_length * sizeof(char));
+ char *value= (char*)malloc(value_length * sizeof(char));
- for (x= 0; x < value_length; x++)
+ for (unsigned int x= 0; x < value_length; x++)
+ {
value[x]= (char) (x % 127);
+ }
- for (x= 1; x <= 100000; ++x)
+ for (unsigned int x= 1; x <= 100000; ++x)
{
- rc= memcached_set(mclone, key, key_len,value, value_length, 0, 0);
+ memcached_return_t rc= memcached_set(mclone, key, key_len,value, value_length, 0, 0);
+ (void)rc;
}
free(value);
static test_return_t poll_timeout(memcached_st *memc)
{
- size_t timeout;
-
- timeout= 100;
+ size_t timeout= 100; // Not using, just checking that it sets
memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_POLL_TIMEOUT, timeout);
timeout= (size_t)memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_POLL_TIMEOUT);
- test_true(timeout == 100);
+ test_compare(100, timeout);
return TEST_SUCCESS;
}