ee27ab4a721074064bc1df8b0a32bfbb645b78e6
[m6w6/libmemcached] / src / libmemcachedprotocol / common.h
1 /*
2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
14 */
15
16 #pragma once
17
18 #include "mem_config.h"
19 #include <assert.h>
20
21 #include "libmemcachedprotocol-0.0/handler.h"
22 #include "libmemcachedprotocol/cache.h"
23 #include "libmemcached/byteorder.h"
24 #include "libmemcached/socket.hpp"
25
26 /*
27 * I don't really need the following two functions as function pointers
28 * in the instance handle, but I don't want to put them in the global
29 * namespace for those linking statically (personally I don't like that,
30 * but some people still do). If it ever shows up as a performance thing
31 * I'll look into optimizing this ;-)
32 */
33 typedef bool (*drain_func)(memcached_protocol_client_st *client);
34 typedef protocol_binary_response_status (*spool_func)(memcached_protocol_client_st *client,
35 const void *data, size_t length);
36
37 /**
38 * Definition of the per instance structure.
39 */
40 struct memcached_protocol_st {
41 memcached_binary_protocol_callback_st *callback;
42 memcached_protocol_recv_func recv;
43 memcached_protocol_send_func send;
44
45 /*
46 * I really don't need these as funciton pointers, but I don't want
47 * to clutter the namespace if someone links statically.
48 */
49 drain_func drain;
50 spool_func spool;
51
52 /*
53 * To avoid keeping a buffer in each client all the time I have a
54 * bigger buffer in the instance that I read to initially, and then
55 * I try to parse and execute as much from the buffer. If I wasn't able
56 * to process all data I'll keep that in a per-connection buffer until
57 * the next time I can read from the socket.
58 */
59 uint8_t *input_buffer;
60 size_t input_buffer_size;
61
62 bool pedantic;
63 /* @todo use multiple sized buffers */
64 cache_t *buffer_cache;
65 };
66
67 struct chunk_st {
68 /* Pointer to the data */
69 char *data;
70 /* The offset to the first byte into the buffer that is used */
71 size_t offset;
72 /* The offset into the buffer for the first free byte */
73 size_t nbytes;
74 /* The number of bytes in the buffer */
75 size_t size;
76 /* Pointer to the next buffer in the chain */
77 struct chunk_st *next;
78 };
79
80 #define CHUNK_BUFFERSIZE 2048
81
82 typedef memcached_protocol_event_t (*process_data)(struct memcached_protocol_client_st *client,
83 ssize_t *length, void **endptr);
84
85 enum ascii_cmd {
86 GET_CMD,
87 GETS_CMD,
88 SET_CMD,
89 ADD_CMD,
90 REPLACE_CMD,
91 CAS_CMD,
92 APPEND_CMD,
93 PREPEND_CMD,
94 DELETE_CMD,
95 INCR_CMD,
96 DECR_CMD,
97 STATS_CMD,
98 FLUSH_ALL_CMD,
99 VERSION_CMD,
100 QUIT_CMD,
101 VERBOSITY_CMD,
102 UNKNOWN_CMD
103 };
104
105 struct memcached_protocol_client_st {
106 bool is_verbose;
107 memcached_protocol_st *root;
108 memcached_socket_t sock;
109 int error;
110
111 /* Linked list of data to send */
112 struct chunk_st *output;
113 struct chunk_st *output_tail;
114
115 /*
116 * While we process input data, this is where we spool incomplete commands
117 * if we need to receive more data....
118 * @todo use the buffercace
119 */
120 uint8_t *input_buffer;
121 size_t input_buffer_size;
122 size_t input_buffer_offset;
123
124 /* The callback to the protocol handler to use (ascii or binary) */
125 process_data work;
126
127 /*
128 * Should the spool data discard the data to send or not? (aka noreply in
129 * the ascii protocol..
130 */
131 bool mute;
132
133 /* Members used by the binary protocol */
134 protocol_binary_request_header *current_command;
135
136 /* Members used by the ascii protocol */
137 enum ascii_cmd ascii_command;
138 };
139
140 #include "ascii_handler.h"
141 #include "binary_handler.h"