semver: 1.0 -> 1
[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/handler.h"
22 #include "libmemcachedprotocol/cache.h"
23 #include "libmemcached/byteorder.h"
24
25 /*
26 * I don't really need the following two functions as function pointers
27 * in the instance handle, but I don't want to put them in the global
28 * namespace for those linking statically (personally I don't like that,
29 * but some people still do). If it ever shows up as a performance thing
30 * I'll look into optimizing this ;-)
31 */
32 typedef bool (*drain_func)(memcached_protocol_client_st *client);
33 typedef protocol_binary_response_status (*spool_func)(memcached_protocol_client_st *client,
34 const void *data, size_t length);
35
36 /**
37 * Definition of the per instance structure.
38 */
39 struct memcached_protocol_st {
40 memcached_binary_protocol_callback_st *callback;
41 memcached_protocol_recv_func recv;
42 memcached_protocol_send_func send;
43
44 /*
45 * I really don't need these as funciton pointers, but I don't want
46 * to clutter the namespace if someone links statically.
47 */
48 drain_func drain;
49 spool_func spool;
50
51 /*
52 * To avoid keeping a buffer in each client all the time I have a
53 * bigger buffer in the instance that I read to initially, and then
54 * I try to parse and execute as much from the buffer. If I wasn't able
55 * to process all data I'll keep that in a per-connection buffer until
56 * the next time I can read from the socket.
57 */
58 uint8_t *input_buffer;
59 size_t input_buffer_size;
60
61 bool pedantic;
62 /* @todo use multiple sized buffers */
63 cache_t *buffer_cache;
64 };
65
66 struct chunk_st {
67 /* Pointer to the data */
68 char *data;
69 /* The offset to the first byte into the buffer that is used */
70 size_t offset;
71 /* The offset into the buffer for the first free byte */
72 size_t nbytes;
73 /* The number of bytes in the buffer */
74 size_t size;
75 /* Pointer to the next buffer in the chain */
76 struct chunk_st *next;
77 };
78
79 #define CHUNK_BUFFERSIZE 2048
80
81 typedef memcached_protocol_event_t (*process_data)(struct memcached_protocol_client_st *client,
82 ssize_t *length, void **endptr);
83
84 enum ascii_cmd {
85 GET_CMD,
86 GETS_CMD,
87 SET_CMD,
88 ADD_CMD,
89 REPLACE_CMD,
90 CAS_CMD,
91 APPEND_CMD,
92 PREPEND_CMD,
93 DELETE_CMD,
94 INCR_CMD,
95 DECR_CMD,
96 STATS_CMD,
97 FLUSH_ALL_CMD,
98 VERSION_CMD,
99 QUIT_CMD,
100 VERBOSITY_CMD,
101 UNKNOWN_CMD
102 };
103
104 struct memcached_protocol_client_st {
105 bool is_verbose;
106 memcached_protocol_st *root;
107 memcached_socket_t sock;
108 int error;
109
110 /* Linked list of data to send */
111 struct chunk_st *output;
112 struct chunk_st *output_tail;
113
114 /*
115 * While we process input data, this is where we spool incomplete commands
116 * if we need to receive more data....
117 * @todo use the buffercace
118 */
119 uint8_t *input_buffer;
120 size_t input_buffer_size;
121 size_t input_buffer_offset;
122
123 /* The callback to the protocol handler to use (ascii or binary) */
124 process_data work;
125
126 /*
127 * Should the spool data discard the data to send or not? (aka noreply in
128 * the ascii protocol..
129 */
130 bool mute;
131
132 /* Members used by the binary protocol */
133 protocol_binary_request_header *current_command;
134
135 /* Members used by the ascii protocol */
136 enum ascii_cmd ascii_command;
137 };
138
139 #include "ascii_handler.h"
140 #include "binary_handler.h"