2 * Summary: Definition of the callback interface to the protocol handler
4 * Copy: See Copyright for the status of this software.
8 #ifndef MEMCACHED_PROTOCOL_H
9 #define MEMCACHED_PROTOCOL_H
11 #include <sys/types.h>
14 #include <libmemcached/memcached/protocol_binary.h>
15 #include <libmemcached/visibility.h>
16 #include <libmemcached/protocol/callback.h>
18 /* Forward declarations */
20 * You should only access memcached_protocol_st from one thread!,
21 * and never assume anything about the internal layout / sizes of the
24 typedef struct memcached_protocol_st memcached_protocol_st
;
25 typedef struct memcached_protocol_client_st memcached_protocol_client_st
;
28 * Function the protocol handler should call to receive data.
29 * This function should behave exactly like read(2)
31 * @param cookie a cookie used to represent a given client
32 * @param fd the filedescriptor associated with the client
33 * @param buf destination buffer
34 * @param nbuf number of bytes to receive
35 * @return the number of bytes copied into buf
36 * or -1 upon error (errno should contain more information)
38 typedef ssize_t (*memcached_protocol_recv_func
)(const void *cookie
,
44 * Function the protocol handler should call to send data.
45 * This function should behave exactly like write(2)
47 * @param cookie a cookie used to represent a given client
48 * @param fd the filedescriptor associated with the client
49 * @param buf the source buffer
50 * @param nbuf number of bytes to send
51 * @return the number of bytes sent
52 * or -1 upon error (errno should contain more information)
54 typedef ssize_t (*memcached_protocol_send_func
)(const void *cookie
,
60 * Create an instance of the protocol handler
62 * @return NULL if allocation of an instance fails
65 memcached_protocol_st
*memcached_protocol_create_instance(void);
68 * Get the callbacks associated with a protocol handler instance
69 * @return the callbacks currently used
72 memcached_binary_protocol_callback_st
*memcached_binary_protocol_get_callbacks(memcached_protocol_st
*instance
);
75 * Set the callbacks to be used by the given protocol handler instance
76 * @param instance the instance to update
77 * @param callback the callbacks to use
80 void memcached_binary_protocol_set_callbacks(memcached_protocol_st
*instance
, memcached_binary_protocol_callback_st
*callback
);
83 * Should the library inspect the packages being sent and received and verify
84 * that they are according to the specification? If it encounters an invalid
85 * packet, it will return an EINVAL packet.
87 * @param instance the instance to update
88 * @param enable true if you want the library to check packages, false otherwise
91 void memcached_binary_protocol_set_pedantic(memcached_protocol_st
*instance
, bool enable
);
94 * Is the library inpecting each package?
95 * @param instance the instance to check
96 * @return true it the library is inspecting each package, false otherwise
99 bool memcached_binary_protocol_get_pedantic(memcached_protocol_st
*instance
);
102 * Destroy an instance of the protocol handler
104 * @param instance The instance to destroy
107 void memcached_protocol_destroy_instance(memcached_protocol_st
*instance
);
110 * Set the IO functions used by the instance to send and receive data. The
111 * functions should behave like recv(3socket) and send(3socket).
113 * @param instance the instance to specify the IO functions for
114 * @param recv the function to call for reciving data
115 * @param send the function to call for sending data
118 void memached_protocol_set_io_functions(memcached_protocol_st
*instance
,
119 memcached_protocol_recv_func recv
,
120 memcached_protocol_send_func send
);
124 * Create a new client instance and associate it with a socket
125 * @param instance the protocol instance to bind the client to
126 * @param sock the client socket
127 * @return NULL if allocation fails, otherwise an instance
130 memcached_protocol_client_st
*memcached_protocol_create_client(memcached_protocol_st
*instance
, int sock
);
133 * Destroy a client handle.
134 * The caller needs to close the socket accociated with the client
135 * <b>before</b> calling this function. This function invalidates the
136 * client memory area.
138 * @param client the client to destroy
141 void memcached_protocol_client_destroy(memcached_protocol_client_st
*client
);
144 * Error event means that the client encountered an error with the
145 * connection so you should shut it down
147 #define MEMCACHED_PROTOCOL_ERROR_EVENT 1
149 * Please notify when there is more data available to read
151 #define MEMCACHED_PROTOCOL_READ_EVENT 2
153 * Please notify when it is possible to send more data
155 #define MEMCACHED_PROTOCOL_WRITE_EVENT 4
157 * Backed paused the execution for this client
159 #define MEMCACHED_PROTOCOL_PAUSE_EVENT 8
162 * The different events the client is interested in. This is a bitmask of
163 * the constants defined above.
165 typedef uint32_t memcached_protocol_event_t
;
168 * Let the client do some work. This might involve reading / sending data
169 * to/from the client, or perform callbacks to execute a command.
170 * @param client the client structure to work on
171 * @return The next event the protocol handler will be notified for
174 memcached_protocol_event_t
memcached_protocol_client_work(memcached_protocol_client_st
*client
);
177 * Get the socket attached to a client handle
178 * @param client the client to query
179 * @return the socket handle
182 int memcached_protocol_client_get_socket(memcached_protocol_client_st
*client
);
185 * Get the error id socket attached to a client handle
186 * @param client the client to query for an error code
187 * @return the OS error code from the client
190 int memcached_protocol_client_get_errno(memcached_protocol_client_st
*client
);
193 * Get a raw response handler for the given cookie
194 * @param cookie the cookie passed along into the callback
195 * @return the raw reponse handler you may use if you find
196 * the generic callback too limiting
199 memcached_binary_protocol_raw_response_handler
memcached_binary_protocol_get_raw_response_handler(const void *cookie
);