1 /* -*- Mode: C; tab-width: 2; c-basic-offset: 2; indent-tabs-mode: nil -*- */
3 * What is a library without an example to show you how to use the library?
4 * This example use both interfaces to implement a small memcached server.
5 * Please note that this is an exemple on how to use the library, not
6 * an implementation of a scalable memcached server. If you look closely
7 * at the example it isn't even multithreaded ;-)
9 * With that in mind, let me give you some pointers into the source:
10 * storage.c/h - Implements the item store for this server and not really
11 * interesting for this example.
12 * interface_v0.c - Shows an implementation of the memcached server by using
13 * the "raw" access to the packets as they arrive
14 * interface_v1.c - Shows an implementation of the memcached server by using
15 * the more "logical" interface.
16 * memcached_light.c - This file sets up all of the sockets and run the main
20 * config.h is included so that I can use the ntohll/htonll on platforms that
21 * doesn't have that (this is a private function inside libmemcached, so you
22 * cannot use it directly from libmemcached without special modifications to
28 #include <sys/types.h>
37 #include <libmemcached/protocol_handler.h>
38 #include <example/byteorder.h>
40 #include "memcached_light.h"
42 extern memcached_binary_protocol_callback_st interface_v0_impl
;
43 extern memcached_binary_protocol_callback_st interface_v1_impl
;
45 static memcached_socket_t server_sockets
[1024];
46 static int num_server_sockets
= 0;
54 /* The default maximum number of connections... (change with -c) */
55 static int maxconns
= 1024;
57 static struct connection
*socket_userdata_map
;
58 static bool verbose
= false;
59 static struct event_base
*event_base
;
67 typedef struct options_st options_st
;
70 * Callback for driving a client connection
71 * @param fd the socket for the client socket
72 * @param which identifying the event that occurred (not used)
73 * @param arg the connection structure for the client
75 static void drive_client(memcached_socket_t fd
, short which
, void *arg
)
78 struct connection
*client
= arg
;
79 struct memcached_protocol_client_st
* c
= client
->userdata
;
82 memcached_protocol_event_t events
= memcached_protocol_client_work(c
);
83 if (events
& MEMCACHED_PROTOCOL_ERROR_EVENT
)
85 memcached_protocol_client_destroy(c
);
89 if (events
& MEMCACHED_PROTOCOL_WRITE_EVENT
)
94 if (events
& MEMCACHED_PROTOCOL_READ_EVENT
)
99 event_set(&client
->event
, (intptr_t)fd
, flags
, drive_client
, client
);
100 event_base_set(event_base
, &client
->event
);
102 if (event_add(&client
->event
, 0) == -1)
104 (void)fprintf(stderr
, "Failed to add event for %d\n", fd
);
105 memcached_protocol_client_destroy(c
);
112 * Callback for accepting new connections
113 * @param fd the socket for the server socket
114 * @param which identifying the event that occurred (not used)
115 * @param arg the connection structure for the server
117 static void accept_handler(memcached_socket_t fd
, short which
, void *arg
)
120 struct connection
*server
= arg
;
121 /* accept new client */
122 struct sockaddr_storage addr
;
123 socklen_t addrlen
= sizeof(addr
);
124 memcached_socket_t sock
= accept(fd
, (struct sockaddr
*)&addr
, &addrlen
);
126 if (sock
== INVALID_SOCKET
)
128 perror("Failed to accept client");
133 if (sock
>= maxconns
)
135 (void)fprintf(stderr
, "Client outside socket range (specified with -c)\n");
141 struct memcached_protocol_client_st
* c
;
142 c
= memcached_protocol_create_client(server
->userdata
, sock
);
145 (void)fprintf(stderr
, "Failed to create client\n");
150 struct connection
*client
= &socket_userdata_map
[sock
];
153 event_set(&client
->event
, (intptr_t)sock
, EV_READ
, drive_client
, client
);
154 event_base_set(event_base
, &client
->event
);
155 if (event_add(&client
->event
, 0) == -1)
157 (void)fprintf(stderr
, "Failed to add event for %d\n", sock
);
158 memcached_protocol_client_destroy(c
);
165 * Create a socket and bind it to a specific port number
166 * @param port the port number to bind to
168 static int server_socket(const char *port
)
171 struct addrinfo hints
= { .ai_flags
= AI_PASSIVE
,
172 .ai_family
= AF_UNSPEC
,
173 .ai_socktype
= SOCK_STREAM
};
175 int error
= getaddrinfo("127.0.0.1", port
, &hints
, &ai
);
178 if (error
!= EAI_SYSTEM
)
179 fprintf(stderr
, "getaddrinfo(): %s\n", gai_strerror(error
));
181 perror("getaddrinfo()");
186 struct linger ling
= {0, 0};
188 for (struct addrinfo
*next
= ai
; next
; next
= next
->ai_next
)
190 memcached_socket_t sock
= socket(ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
);
191 if (sock
== INVALID_SOCKET
)
193 perror("Failed to create socket");
200 if (ioctlsocket(sock
, FIONBIO
, &arg
) == SOCKET_ERROR
)
202 perror("Failed to set nonblocking io");
207 flags
= fcntl(sock
, F_GETFL
, 0);
210 perror("Failed to get socket flags");
215 if ((flags
& O_NONBLOCK
) != O_NONBLOCK
)
217 if (fcntl(sock
, F_SETFL
, flags
| O_NONBLOCK
) == -1)
219 perror("Failed to set socket to nonblocking mode");
227 if (setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, (void *)&flags
, sizeof(flags
)) != 0)
228 perror("Failed to set SO_REUSEADDR");
230 if (setsockopt(sock
, SOL_SOCKET
, SO_KEEPALIVE
, (void *)&flags
, sizeof(flags
)) != 0)
231 perror("Failed to set SO_KEEPALIVE");
233 if (setsockopt(sock
, SOL_SOCKET
, SO_LINGER
, (void *)&ling
, sizeof(ling
)) != 0)
234 perror("Failed to set SO_LINGER");
236 if (setsockopt(sock
, IPPROTO_TCP
, TCP_NODELAY
, (void *)&flags
, sizeof(flags
)) != 0)
237 perror("Failed to set TCP_NODELAY");
239 if (bind(sock
, next
->ai_addr
, next
->ai_addrlen
) == SOCKET_ERROR
)
241 if (get_socket_errno() != EADDRINUSE
)
250 if (listen(sock
, 1024) == SOCKET_ERROR
)
257 server_sockets
[num_server_sockets
++]= sock
;
262 return (num_server_sockets
> 0) ? 0 : 1;
266 * Convert a command code to a textual string
267 * @param cmd the comcode to convert
268 * @return a textual string with the command or NULL for unknown commands
270 static const char* comcode2str(uint8_t cmd
)
272 static const char * const text
[] = {
273 "GET", "SET", "ADD", "REPLACE", "DELETE",
274 "INCREMENT", "DECREMENT", "QUIT", "FLUSH",
275 "GETQ", "NOOP", "VERSION", "GETK", "GETKQ",
276 "APPEND", "PREPEND", "STAT", "SETQ", "ADDQ",
277 "REPLACEQ", "DELETEQ", "INCREMENTQ", "DECREMENTQ",
278 "QUITQ", "FLUSHQ", "APPENDQ", "PREPENDQ"
281 if (cmd
<= PROTOCOL_BINARY_CMD_PREPENDQ
)
288 * Print out the command we are about to execute
290 static void pre_execute(const void *cookie
,
291 protocol_binary_request_header
*header
)
295 const char *cmd
= comcode2str(header
->request
.opcode
);
297 fprintf(stderr
, "pre_execute from %p: %s\n", cookie
, cmd
);
299 fprintf(stderr
, "pre_execute from %p: 0x%02x\n", cookie
, header
->request
.opcode
);
304 * Print out the command we just executed
306 static void post_execute(const void *cookie
,
307 protocol_binary_request_header
*header
)
311 const char *cmd
= comcode2str(header
->request
.opcode
);
313 fprintf(stderr
, "post_execute from %p: %s\n", cookie
, cmd
);
315 fprintf(stderr
, "post_execute from %p: 0x%02x\n", cookie
, header
->request
.opcode
);
320 * Callback handler for all unknown commands.
321 * Send an unknown command back to the client
323 static protocol_binary_response_status
unknown(const void *cookie
,
324 protocol_binary_request_header
*header
,
325 memcached_binary_protocol_raw_response_handler response_handler
)
327 protocol_binary_response_no_extras response
= {
330 .magic
= PROTOCOL_BINARY_RES
,
331 .opcode
= header
->request
.opcode
,
332 .status
= htons(PROTOCOL_BINARY_RESPONSE_UNKNOWN_COMMAND
),
333 .opaque
= header
->request
.opaque
338 return response_handler(cookie
, header
, (void*)&response
);
342 * Program entry point. Bind to the specified port(s) and serve clients
344 * @param argc number of items in the argument vector
345 * @param argv argument vector
346 * @return EXIT_SUCCESS on success, 1 otherwise
348 int main(int argc
, char **argv
)
351 memcached_binary_protocol_callback_st
*interface
= &interface_v0_impl
;
353 memset(&global_options
, 0, sizeof(global_options
));
355 event_base
= event_init();
356 if (event_base
== NULL
)
358 fprintf(stderr
, "Failed to create an instance of libevent\n");
363 * We need to initialize the handlers manually due to a bug in the
364 * warnings generated by struct initialization in gcc (all the way up to 4.4)
366 initialize_interface_v0_handler();
368 while ((cmd
= getopt(argc
, argv
, "v1p:P:?hc:")) != EOF
)
372 interface
= &interface_v1_impl
;
375 global_options
.pid_file
= strdup(optarg
);
378 global_options
.has_port
= true;
379 (void)server_socket(optarg
);
385 maxconns
= atoi(optarg
);
387 case 'h': /* FALLTHROUGH */
388 case '?': /* FALLTHROUGH */
390 (void)fprintf(stderr
, "Usage: %s [-p port] [-v] [-1] [-c #clients] [-P pidfile]\n",
396 if (! initialize_storage())
398 /* Error message already printed */
402 if (! global_options
.has_port
)
403 (void)server_socket("9999");
405 if (global_options
.pid_file
)
410 pid_file
= fopen(global_options
.pid_file
, "w+");
412 if (pid_file
== NULL
)
414 perror(strerror(get_socket_errno()));
418 pid
= (uint32_t)getpid();
419 fprintf(pid_file
, "%u\n", pid
);
423 if (num_server_sockets
== 0)
425 fprintf(stderr
, "I don't have any server sockets\n");
430 * Create and initialize the handles to the protocol handlers. I want
431 * to be able to trace the traffic throught the pre/post handlers, and
432 * set up a common handler for unknown messages
434 interface
->pre_execute
= pre_execute
;
435 interface
->post_execute
= post_execute
;
436 interface
->unknown
= unknown
;
438 struct memcached_protocol_st
*protocol_handle
;
439 if ((protocol_handle
= memcached_protocol_create_instance()) == NULL
)
441 fprintf(stderr
, "Failed to allocate protocol handle\n");
445 socket_userdata_map
= calloc((size_t)(maxconns
), sizeof(struct connection
));
446 if (socket_userdata_map
== NULL
)
448 fprintf(stderr
, "Failed to allocate room for connections\n");
452 memcached_binary_protocol_set_callbacks(protocol_handle
, interface
);
453 memcached_binary_protocol_set_pedantic(protocol_handle
, true);
455 for (int xx
= 0; xx
< num_server_sockets
; ++xx
)
457 struct connection
*conn
= &socket_userdata_map
[server_sockets
[xx
]];
458 conn
->userdata
= protocol_handle
;
459 event_set(&conn
->event
, (intptr_t)server_sockets
[xx
], EV_READ
| EV_PERSIST
,
460 accept_handler
, conn
);
461 event_base_set(event_base
, &conn
->event
);
462 if (event_add(&conn
->event
, 0) == -1)
464 fprintf(stderr
, "Failed to add event for %d\n", server_sockets
[xx
]);
465 closesocket(server_sockets
[xx
]);
469 /* Serve all of the clients */
470 event_base_loop(event_base
, 0);