247
247
248
248
.. _box_box_iproto_send:
249
249
250
- .. c:function:: int box_iproto_send(uint64_t sid, char *header, char *header_end, char *body, char *body_end)
250
+ .. c:function:: int box_iproto_send(uint64_t sid, char *header, char *header_end[ , char *body, char *body_end] )
251
251
252
252
Since version :doc: `2.11.0 </release/2.11.0 >`.
253
253
Send an :ref: `IPROTO <internals-iproto-format >` packet over the session's socket with the given MsgPack header
258
258
:param uint32_t sid: IPROTO session identifier (see :ref: `box_session_id() <box_box_session_id >`)
259
259
:param char* header: a MsgPack-encoded header
260
260
:param char* header_end: end of a header encoded as MsgPack
261
- :param char* body: a MsgPack-encoded body
261
+ :param char* body: a MsgPack-encoded body. If the ``body`` and ``body_end`` parameters are omitted, the packet
262
+ consists of the header only.
262
263
:param char* body_end: end of a body encoded as MsgPack
263
264
264
265
:return: 0 on success
276
277
277
278
For details, see `src/box/errcode.h <https:// github.com/tarantool/tarantool/blob/master/src/box/errcode.h>`__.
278
279
280
+ **Example**
281
+
282
+ .. code-block:: c
283
+
284
+ /* IPROTO constants are not exported to C.
285
+ * That is, the user encodes them by himself.
286
+ */
287
+ #define IPROTO_REQUEST_TYPE 0x00
288
+ #define IPROTO_OK 0x00
289
+ #define IPROTO_SYNC 0x01
290
+ #define IPROTO_SCHEMA_VERSION 0x05
291
+ #define IPROTO_DATA 0x30
292
+
293
+ char buf[256 ] = {};
294
+ char *header = buf;
295
+ char *header_end = header;
296
+ header_end = mp_encode_map(header_end, 3 );
297
+ header_end = mp_encode_uint(header_end, IPROTO_REQUEST_TYPE);
298
+ header_end = mp_encode_uint(header_end, IPROTO_OK);
299
+ header_end = mp_encode_uint(header_end, IPROTO_SYNC);
300
+ header_end = mp_encode_uint(header_end, 10 );
301
+ header_end = mp_encode_uint(header_end, IPROTO_SCHEMA_VERSION);
302
+ header_end = mp_encode_uint(header_end, box_schema_version());
303
+
304
+ char *body = header_end;
305
+ char *body_end = body;
306
+ body_end = mp_encode_map(body_end, 1 );
307
+ body_end = mp_encode_uint(body_end, IPROTO_DATA);
308
+ body_end = mp_encode_uint(body_end, 1 );
309
+
310
+ /* The packet contains both the header and body. */
311
+ box_iproto_send (box_session_id(), header, header_end, body, body_end);
312
+
313
+ /* The packet contains the header only. */
314
+ box_iproto_send (box_session_id(), header, header_end, NULL, NULL);
315
+
279
316
.. _box_box_iproto_override :
280
317
281
318
.. c :function :: void box_iproto_override (uint32_t request_type, iproto_handler_t handler, iproto_handler_destroy_t destroy, void *ctx)
282
319
283
320
Since version :doc: `2.11.0 </release/2.11.0 >`.
284
- Sets an IPROTO request handler with the provided context for the given request type.
321
+ Set a new IPROTO request handler with the provided context for the given request type.
285
322
The function yields.
286
323
287
- Possible values:
324
+ :param uint32_t request_type: IPROTO request type code (for example, ``IPROTO_SELECT ``).
325
+ For details, check :ref:`Client-server requests and responses <internals-requests_responses>`.
326
+
327
+ To override the handler of an unknown request type, use the :ref:`IPROTO_UNKNOWN <internals-iproto-keys-unknown>` type code.
288
328
289
- * a type code from the :ref: ` box.iproto.type < reference_lua-box_iproto_type >` (except
290
- `` box.iproto.type.UNKNOWN ``) -- override the existing request type .
329
+ :param iproto_handler_t handler: IPROTO request handler. To reset the request handler, set the ``handler`` parameter to ``NULL``.
330
+ See the full parameter description in the :ref:`Handler function <box_box_iproto_override-handler>` section .
291
331
292
- * ``box.iproto.type.UNKNOWN`` -- override an unknown request type.
332
+ :param iproto_handler_destroy_t destroy: IPROTO request handler destructor. The destructor is called when the
333
+ corresponding handler is removed. See the full parameter description
334
+ in the :ref:`Handler destructor function <box_box_iproto_override-destroy>` section.
293
335
294
- :param uint32_t request_type: request type code from the ``iproto_type`` enumeration
295
- :param iproto_handler_t handler: IPROTO request handler
296
- :param iproto_handler_destroy_t destroy: IPROTO request handler destructor
297
336
:param void* ctx: a context passed to the ``handler`` and ``destroy`` callbacks
298
337
299
338
:return: 0 on success
@@ -304,7 +343,64 @@ Possible values:
304
343
305
344
**Possible errors:**
306
345
346
+ If the Lua handler throws an exception, the behavior is similar to a remote procedure call.
347
+ The following errors are returned to the client over IPROTO (see `src/lua/utils.h <https://github.com/tarantool/tarantool/blob/dec0e0221e183fa972efa65bb0fb658112f2196f/src/lua/utils.h#L366-L371 >`__):
348
+
307
349
* :errcode:`ER_PROC_LUA` -- the exception is thrown, diagnostic is not set.
308
350
* diagnostics from ``src/box/errcode.h`` -- the exception is thrown, diagnostic is set.
309
351
310
- For details, see `src/box/errcode.h <https:// github.com/tarantool/tarantool/blob/master/src/box/errcode.h>`__.
352
+ For details, see `src/box/errcode.h <https:// github.com/tarantool/tarantool/blob/master/src/box/errcode.h>`__.
353
+
354
+ .. _box_box_iproto_override-handler:
355
+
356
+ **Handler function**
357
+
358
+ The signature of a handler function (the ``handler `` parameter):
359
+
360
+ .. code-block:: c
361
+
362
+ enum iproto_handler_status {
363
+ IPROTO_HANDLER_OK,
364
+ IPROTO_HANDLER_ERROR,
365
+ IPROTO_HANDLER_FALLBACK,
366
+ }
367
+
368
+ typedef enum iproto_handler_status
369
+ (*iproto_handler_t )(const char *header, const char *header_end,
370
+ const char *body, const char *body_end, void *ctx);
371
+
372
+ where:
373
+
374
+ * ``header``(const char*) -- a MsgPack-encoded header
375
+ * ``header_end``(const char*) -- end of a header encoded as MsgPack
376
+ * ``body``(const char*) -- a MsgPack-encoded body
377
+ * ``header_end``(const char*) -- end of a body encoded as MsgPack
378
+
379
+ The handler returns a status code. Possible statuses:
380
+
381
+ * ``IPROTO_REQUEST_HANDLER_OK `` -- success
382
+ * ``IPROTO_REQUEST_HANDLER_ERROR `` -- error, diagnostic must be set by handler
383
+ * ``IPROTO_REQUEST_HANDLER_FALLBACK `` -- fallback to the system handler
384
+
385
+ .. _box_box_iproto_override-destroy :
386
+
387
+ **Handler destructor function **
388
+
389
+ The signature of a destructor function (the ``destroy `` parameter):
390
+
391
+ .. code-block:: c
392
+
393
+ typedef void (*iproto_handler_destroy_t)(void *ctx);
394
+
395
+ where:
396
+
397
+ * ``ctx `` (void*): the context provided by ``box_iproto_override()`` function.
398
+
399
+ **Examples**
400
+
401
+ .. code-block:: c
402
+
403
+ box_iproto_override(1000, iproto_request_handler_с, NULL)
404
+ box_iproto_override(IPROTO_SELECT, iproto_request_handler_с, (uintptr_t)23)
405
+ box_iproto_override(IPROTO_SELECT, NULL, NULL)
406
+ box_iproto_override(IPROTO_UNKNOWN, iproto_unknown_request_handler_с, &ctx)
0 commit comments