Skip to content

Commit 5ed67e0

Browse files
committed
Add set custom decode frame length function for C++ native only
1 parent f569017 commit 5ed67e0

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/masio.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,10 @@ io_service::io_service() : stopping_(false), thread_started_(false), interrupter
293293
ares_outstanding_work_ = 0;
294294
#endif
295295
ipsv_state_ = 0;
296+
297+
this->xdec_len_ = [](io_service *service, void *ptr, int len) {
298+
return service->builtin_decode_frame_length(ptr, len);
299+
};
296300
}
297301

298302
io_service::~io_service() { stop_service(); }
@@ -383,6 +387,9 @@ void io_service::set_option(int option, ...)
383387
case MASIO_OPT_IO_EVENT_CALLBACK:
384388
this->on_event_ = std::move(*va_arg(ap, io_event_callback_t *));
385389
break;
390+
case MASIO_OPT_DECODE_FRAME_LENGTH_FUNCTION:
391+
this->xdec_len_ = std::move(*va_arg(ap, decode_frame_length_fn_t *));
392+
break;
386393
}
387394

388395
va_end(ap);
@@ -1174,7 +1181,7 @@ bool io_service::do_read(transport_ptr transport)
11741181
#endif
11751182
if (transport->receiving_pdu_elen_ == -1)
11761183
{ // decode length
1177-
int length = decode_frame_length(transport->buffer_, transport->offset_ + n);
1184+
int length = this->xdec_len_(this, transport->buffer_, transport->offset_ + n);
11781185
if (length > 0)
11791186
{
11801187
transport->receiving_pdu_elen_ = length;
@@ -1556,7 +1563,7 @@ void io_service::handle_ares_work_finish(channel *)
15561563
}
15571564
#endif
15581565

1559-
int io_service::decode_frame_length(void *ud, int n)
1566+
int io_service::builtin_decode_frame_length(void *ud, int n)
15601567
{
15611568
if (options_.lfib.length_field_offset >= 0)
15621569
{

src/masio.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ enum
118118
MASIO_OPT_LOG_FILE,
119119
MASIO_OPT_LFIB_PARAMS,
120120
MASIO_OPT_IO_EVENT_CALLBACK,
121+
MASIO_OPT_DECODE_FRAME_LENGTH_FUNCTION, // Native C++ ONLY
121122
};
122123

123124
typedef std::chrono::high_resolution_clock highp_clock_t;
@@ -309,6 +310,7 @@ class deadline_timer;
309310

310311
typedef std::function<void(error_number)> send_pdu_callback_t;
311312
typedef std::function<void(event_ptr)> io_event_callback_t;
313+
typedef std::function<int(io_service *service, void *ptr, int len)> decode_frame_length_fn_t;
312314

313315
class io_service
314316
{
@@ -448,7 +450,7 @@ class io_service
448450
void do_nonblocking_accept_completion(fd_set *fds_array, io_channel *);
449451

450452
// -1 indicate failed, connection will be closed
451-
int decode_frame_length(void *ptr, int len);
453+
int builtin_decode_frame_length(void *ptr, int len);
452454

453455
private:
454456
bool stopping_;
@@ -519,8 +521,12 @@ class io_service
519521
int max_frame_length = SZ(10, M);
520522
} lfib;
521523
FILE *outf = nullptr;
524+
522525
} options_;
523526

527+
// The decode frame length function
528+
decode_frame_length_fn_t xdec_len_;
529+
524530
// The resolve function
525531
resolv_fn_t xresolv_;
526532

0 commit comments

Comments
 (0)