@@ -2362,8 +2362,6 @@ class monitor_t
2362
2362
{
2363
2363
assert (_monitor_socket);
2364
2364
2365
- zmq::message_t eventMsg;
2366
-
2367
2365
zmq::pollitem_t items[] = {
2368
2366
{_monitor_socket.handle (), 0 , ZMQ_POLLIN, 0 },
2369
2367
};
@@ -2374,106 +2372,7 @@ class monitor_t
2374
2372
zmq::poll (&items[0 ], 1 , timeout);
2375
2373
#endif
2376
2374
2377
- if (items[0 ].revents & ZMQ_POLLIN) {
2378
- int rc = zmq_msg_recv (eventMsg.handle (), _monitor_socket.handle (), 0 );
2379
- if (rc == -1 && zmq_errno () == ETERM)
2380
- return false ;
2381
- assert (rc != -1 );
2382
-
2383
- } else {
2384
- return false ;
2385
- }
2386
-
2387
- #if ZMQ_VERSION_MAJOR >= 4
2388
- const char *data = static_cast <const char *>(eventMsg.data ());
2389
- zmq_event_t msgEvent;
2390
- memcpy (&msgEvent.event , data, sizeof (uint16_t ));
2391
- data += sizeof (uint16_t );
2392
- memcpy (&msgEvent.value , data, sizeof (int32_t ));
2393
- zmq_event_t *event = &msgEvent;
2394
- #else
2395
- zmq_event_t *event = static_cast <zmq_event_t *>(eventMsg.data ());
2396
- #endif
2397
-
2398
- #ifdef ZMQ_NEW_MONITOR_EVENT_LAYOUT
2399
- zmq::message_t addrMsg;
2400
- int rc = zmq_msg_recv (addrMsg.handle (), _monitor_socket.handle (), 0 );
2401
- if (rc == -1 && zmq_errno () == ETERM) {
2402
- return false ;
2403
- }
2404
-
2405
- assert (rc != -1 );
2406
- std::string address = addrMsg.to_string ();
2407
- #else
2408
- // Bit of a hack, but all events in the zmq_event_t union have the same layout so this will work for all event types.
2409
- std::string address = event->data .connected .addr ;
2410
- #endif
2411
-
2412
- #ifdef ZMQ_EVENT_MONITOR_STOPPED
2413
- if (event->event == ZMQ_EVENT_MONITOR_STOPPED) {
2414
- return false ;
2415
- }
2416
-
2417
- #endif
2418
-
2419
- switch (event->event ) {
2420
- case ZMQ_EVENT_CONNECTED:
2421
- on_event_connected (*event, address.c_str ());
2422
- break ;
2423
- case ZMQ_EVENT_CONNECT_DELAYED:
2424
- on_event_connect_delayed (*event, address.c_str ());
2425
- break ;
2426
- case ZMQ_EVENT_CONNECT_RETRIED:
2427
- on_event_connect_retried (*event, address.c_str ());
2428
- break ;
2429
- case ZMQ_EVENT_LISTENING:
2430
- on_event_listening (*event, address.c_str ());
2431
- break ;
2432
- case ZMQ_EVENT_BIND_FAILED:
2433
- on_event_bind_failed (*event, address.c_str ());
2434
- break ;
2435
- case ZMQ_EVENT_ACCEPTED:
2436
- on_event_accepted (*event, address.c_str ());
2437
- break ;
2438
- case ZMQ_EVENT_ACCEPT_FAILED:
2439
- on_event_accept_failed (*event, address.c_str ());
2440
- break ;
2441
- case ZMQ_EVENT_CLOSED:
2442
- on_event_closed (*event, address.c_str ());
2443
- break ;
2444
- case ZMQ_EVENT_CLOSE_FAILED:
2445
- on_event_close_failed (*event, address.c_str ());
2446
- break ;
2447
- case ZMQ_EVENT_DISCONNECTED:
2448
- on_event_disconnected (*event, address.c_str ());
2449
- break ;
2450
- #if ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 3, 0) || (defined(ZMQ_BUILD_DRAFT_API) && ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 3))
2451
- case ZMQ_EVENT_HANDSHAKE_FAILED_NO_DETAIL:
2452
- on_event_handshake_failed_no_detail (*event, address.c_str ());
2453
- break ;
2454
- case ZMQ_EVENT_HANDSHAKE_FAILED_PROTOCOL:
2455
- on_event_handshake_failed_protocol (*event, address.c_str ());
2456
- break ;
2457
- case ZMQ_EVENT_HANDSHAKE_FAILED_AUTH:
2458
- on_event_handshake_failed_auth (*event, address.c_str ());
2459
- break ;
2460
- case ZMQ_EVENT_HANDSHAKE_SUCCEEDED:
2461
- on_event_handshake_succeeded (*event, address.c_str ());
2462
- break ;
2463
- #elif defined(ZMQ_BUILD_DRAFT_API) && ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 1)
2464
- case ZMQ_EVENT_HANDSHAKE_FAILED:
2465
- on_event_handshake_failed (*event, address.c_str ());
2466
- break ;
2467
- case ZMQ_EVENT_HANDSHAKE_SUCCEED:
2468
- on_event_handshake_succeed (*event, address.c_str ());
2469
- break ;
2470
- #endif
2471
- default :
2472
- on_event_unknown (*event, address.c_str ());
2473
- break ;
2474
- }
2475
-
2476
- return true ;
2375
+ return process_event (items[0 ].revents );
2477
2376
}
2478
2377
2479
2378
#ifdef ZMQ_EVENT_MONITOR_STOPPED
@@ -2583,6 +2482,115 @@ class monitor_t
2583
2482
(void ) addr_;
2584
2483
}
2585
2484
2485
+ protected:
2486
+ bool process_event (short events)
2487
+ {
2488
+ zmq::message_t eventMsg;
2489
+
2490
+ if (events & ZMQ_POLLIN) {
2491
+ int rc = zmq_msg_recv (eventMsg.handle (), _monitor_socket.handle (), 0 );
2492
+ if (rc == -1 && zmq_errno () == ETERM)
2493
+ return false ;
2494
+ assert (rc != -1 );
2495
+
2496
+ } else {
2497
+ return false ;
2498
+ }
2499
+
2500
+ #if ZMQ_VERSION_MAJOR >= 4
2501
+ const char *data = static_cast <const char *>(eventMsg.data ());
2502
+ zmq_event_t msgEvent;
2503
+ memcpy (&msgEvent.event , data, sizeof (uint16_t ));
2504
+ data += sizeof (uint16_t );
2505
+ memcpy (&msgEvent.value , data, sizeof (int32_t ));
2506
+ zmq_event_t *event = &msgEvent;
2507
+ #else
2508
+ zmq_event_t *event = static_cast <zmq_event_t *>(eventMsg.data ());
2509
+ #endif
2510
+
2511
+ #ifdef ZMQ_NEW_MONITOR_EVENT_LAYOUT
2512
+ zmq::message_t addrMsg;
2513
+ int rc = zmq_msg_recv (addrMsg.handle (), _monitor_socket.handle (), 0 );
2514
+ if (rc == -1 && zmq_errno () == ETERM) {
2515
+ return false ;
2516
+ }
2517
+
2518
+ assert (rc != -1 );
2519
+ std::string address = addrMsg.to_string ();
2520
+ #else
2521
+ // Bit of a hack, but all events in the zmq_event_t union have the same layout so this will work for all event types.
2522
+ std::string address = event->data .connected .addr ;
2523
+ #endif
2524
+
2525
+ #ifdef ZMQ_EVENT_MONITOR_STOPPED
2526
+ if (event->event == ZMQ_EVENT_MONITOR_STOPPED) {
2527
+ return false ;
2528
+ }
2529
+
2530
+ #endif
2531
+
2532
+ switch (event->event ) {
2533
+ case ZMQ_EVENT_CONNECTED:
2534
+ on_event_connected (*event, address.c_str ());
2535
+ break ;
2536
+ case ZMQ_EVENT_CONNECT_DELAYED:
2537
+ on_event_connect_delayed (*event, address.c_str ());
2538
+ break ;
2539
+ case ZMQ_EVENT_CONNECT_RETRIED:
2540
+ on_event_connect_retried (*event, address.c_str ());
2541
+ break ;
2542
+ case ZMQ_EVENT_LISTENING:
2543
+ on_event_listening (*event, address.c_str ());
2544
+ break ;
2545
+ case ZMQ_EVENT_BIND_FAILED:
2546
+ on_event_bind_failed (*event, address.c_str ());
2547
+ break ;
2548
+ case ZMQ_EVENT_ACCEPTED:
2549
+ on_event_accepted (*event, address.c_str ());
2550
+ break ;
2551
+ case ZMQ_EVENT_ACCEPT_FAILED:
2552
+ on_event_accept_failed (*event, address.c_str ());
2553
+ break ;
2554
+ case ZMQ_EVENT_CLOSED:
2555
+ on_event_closed (*event, address.c_str ());
2556
+ break ;
2557
+ case ZMQ_EVENT_CLOSE_FAILED:
2558
+ on_event_close_failed (*event, address.c_str ());
2559
+ break ;
2560
+ case ZMQ_EVENT_DISCONNECTED:
2561
+ on_event_disconnected (*event, address.c_str ());
2562
+ break ;
2563
+ #if ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 3, 0) || (defined(ZMQ_BUILD_DRAFT_API) && ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 3))
2564
+ case ZMQ_EVENT_HANDSHAKE_FAILED_NO_DETAIL:
2565
+ on_event_handshake_failed_no_detail (*event, address.c_str ());
2566
+ break ;
2567
+ case ZMQ_EVENT_HANDSHAKE_FAILED_PROTOCOL:
2568
+ on_event_handshake_failed_protocol (*event, address.c_str ());
2569
+ break ;
2570
+ case ZMQ_EVENT_HANDSHAKE_FAILED_AUTH:
2571
+ on_event_handshake_failed_auth (*event, address.c_str ());
2572
+ break ;
2573
+ case ZMQ_EVENT_HANDSHAKE_SUCCEEDED:
2574
+ on_event_handshake_succeeded (*event, address.c_str ());
2575
+ break ;
2576
+ #elif defined(ZMQ_BUILD_DRAFT_API) && ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 1)
2577
+ case ZMQ_EVENT_HANDSHAKE_FAILED:
2578
+ on_event_handshake_failed (*event, address.c_str ());
2579
+ break ;
2580
+ case ZMQ_EVENT_HANDSHAKE_SUCCEED:
2581
+ on_event_handshake_succeed (*event, address.c_str ());
2582
+ break ;
2583
+ #endif
2584
+ default :
2585
+ on_event_unknown (*event, address.c_str ());
2586
+ break ;
2587
+ }
2588
+
2589
+ return true ;
2590
+ }
2591
+
2592
+ socket_ref monitor_socket () {return _monitor_socket;}
2593
+
2586
2594
private:
2587
2595
monitor_t (const monitor_t &) ZMQ_DELETED_FUNCTION;
2588
2596
void operator =(const monitor_t &) ZMQ_DELETED_FUNCTION;
0 commit comments