rzmq 0.5.5

A high performance, fully asynchronous, safe pure-Rust implementation of ZeroMQ (ØMQ) messaging with io_uring and tcp cork acceleration on Linux.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
use crate::delegate_to_core;
use crate::error::ZmqError;
use crate::message::{Blob, Msg, MsgFlags};
use crate::runtime::{Command, MailboxSender};
use crate::socket::core::SocketCore;
use crate::socket::patterns::incoming_orchestrator::IncomingMessageOrchestrator;
use crate::socket::patterns::LoadBalancer;
use crate::socket::ISocket;
use super::patterns::WritePipeCoordinator;

use std::collections::{HashMap, VecDeque};
use std::sync::Arc;
use std::time::Duration;

use async_trait::async_trait;
use parking_lot::RwLock as ParkingLotRwLock;
use tokio::sync::{Mutex as TokioMutex, Notify};
use tokio::task::JoinHandle;
use tokio::time::timeout as tokio_timeout;


const MAX_DEALER_SEND_BUFFER_PARTS: usize = 10240;

#[derive(Debug)]
enum DealerSendTransaction {
  Idle,
  Buffering {
    parts: Vec<Msg>,
    completion_notifier: Arc<Notify>,
  },
}

#[derive(Debug)]
struct DealerSocketOutgoingProcessor {
  core_handle: usize,
  pending_queue: Arc<TokioMutex<VecDeque<Vec<Msg>>>>,
  load_balancer: Arc<LoadBalancer>,
  core_accessor: Arc<SocketCore>,
  queue_activity_notifier: Arc<Notify>,
  peer_availability_notifier: Arc<Notify>,
  stop_signal: Arc<Notify>,
  pipe_send_coordinator: Arc<WritePipeCoordinator>,
}

impl DealerSocketOutgoingProcessor {
  pub async fn run(self) {
    tracing::debug!(
      "[DealerProc {}] Outgoing queue processor task started.",
      self.core_handle
    );

    loop {
      let mut current_message_to_send_option: Option<Vec<Msg>> = None;

      tokio::select! {
        biased;
        _ = self.stop_signal.notified() => {
          tracing::debug!("[DealerProc {}] Stop signal received. Exiting processor loop.", self.core_handle);
          break;
        }
        _ = async {
          tokio::select! {
            _ = self.queue_activity_notifier.notified() => {
              tracing::trace!("[DealerProc {}] Woke on queue_activity_notifier.", self.core_handle);
            },
            _ = self.peer_availability_notifier.notified() => {
              tracing::trace!("[DealerProc {}] Woke on peer_availability_notifier.", self.core_handle);
            },
          }
        } => {
          let mut queue_guard = self.pending_queue.lock().await;
          if !queue_guard.is_empty() && self.load_balancer.has_connections() {
            current_message_to_send_option = queue_guard.pop_front();
          }
        }
      }

      if let Some(zmtp_frames_for_logical_message) = current_message_to_send_option {
        tracing::trace!(
          "[DealerProc {}] Processing message from outgoing queue ({} parts).",
          self.core_handle,
          zmtp_frames_for_logical_message.len()
        );

        let snd_timeout_opt: Option<Duration> = { self.core_accessor.core_state.read().options.sndtimeo };

        let mut message_successfully_sent_to_a_peer = false;
        let mut attempts_this_message_cycle = 0;
        let max_attempts = self.load_balancer.connection_count().await.max(1);

        'peer_send_loop: while !message_successfully_sent_to_a_peer && attempts_this_message_cycle < max_attempts {
          if !self.core_accessor.is_running().await {
            tracing::warn!(
              "[DealerProc {}] Socket closing, stopping send attempts for current message from queue.",
              self.core_handle
            );
            self
              .pending_queue
              .lock()
              .await
              .push_front(zmtp_frames_for_logical_message.clone());
            self.queue_activity_notifier.notify_one();
            break 'peer_send_loop;
          }
          attempts_this_message_cycle += 1;

          if let Some(endpoint_uri) = self.load_balancer.get_next_connection_uri() {
            let (connection_iface_opt, connection_id_for_coord_opt) = {
              let core_s = self.core_accessor.core_state.read();
              core_s.endpoints.get(&endpoint_uri).map_or((None, None), |ep_info| {
                (Some(ep_info.connection_iface.clone()), Some(ep_info.handle_id))
              })
            };

            if let (Some(conn_iface), Some(conn_id_for_coord)) = (connection_iface_opt, connection_id_for_coord_opt) {
              match self
                .pipe_send_coordinator
                .acquire_send_permit(conn_id_for_coord, snd_timeout_opt)
                .await
              {
                Ok(_send_permit) => match conn_iface.send_multipart(zmtp_frames_for_logical_message.clone()).await {
                  Ok(()) => {
                    message_successfully_sent_to_a_peer = true;
                  }
                  Err(ZmqError::ResourceLimitReached) | Err(ZmqError::Timeout) => {
                    tracing::warn!(
                      "[DealerProc {}] HWM/Timeout on send_multipart to conn_id {}. Trying next peer.",
                      self.core_handle,
                      conn_id_for_coord
                    );
                  }
                  Err(e @ ZmqError::ConnectionClosed) | Err(e @ ZmqError::HostUnreachable(_)) => {
                    tracing::warn!(
                      "[DealerProc {}] Conn {} (URI {}) closed/unreachable for send_multipart: {}. Removing.",
                      self.core_handle,
                      conn_id_for_coord,
                      endpoint_uri,
                      e
                    );
                    self.load_balancer.remove_connection(&endpoint_uri);
                    self.pipe_send_coordinator.remove_pipe(conn_id_for_coord).await;
                    self.peer_availability_notifier.notify_waiters();
                  }
                  Err(e) => {
                    tracing::error!(
                        "[DealerProc {}] Unexpected error on send_multipart to conn_id {}: {}. Re-queuing and stopping this message cycle.",
                        self.core_handle, conn_id_for_coord, e
                      );
                    message_successfully_sent_to_a_peer = false;
                    attempts_this_message_cycle = max_attempts;
                  }
                },
                Err(e) => {
                  tracing::warn!(
                    "[DealerProc {}] Failed to acquire send permit for conn_id {} (URI {}): {}. Trying next peer.",
                    self.core_handle,
                    conn_id_for_coord,
                    endpoint_uri,
                    e
                  );
                  if matches!(e, ZmqError::HostUnreachable(_)) {
                    self.load_balancer.remove_connection(&endpoint_uri);
                    self.peer_availability_notifier.notify_waiters();
                  }
                }
              }
            } else {
              tracing::warn!(
                "[DealerProc {}] URI {} from LB was stale (no EndpointInfo). Removing and trying next peer.",
                self.core_handle,
                endpoint_uri
              );
              self.load_balancer.remove_connection(&endpoint_uri);
              self.peer_availability_notifier.notify_waiters();
            }
          } else {
            tracing::trace!(
              "[DealerProc {}] No (more) peers available from LB for this message cycle.",
              self.core_handle
            );
            break 'peer_send_loop;
          }
        }

        if !message_successfully_sent_to_a_peer {
          tracing::debug!(
            "[DealerProc {}] Message could not be sent in this cycle after {} attempts. Re-queuing.",
            self.core_handle,
            attempts_this_message_cycle
          );
          self
            .pending_queue
            .lock()
            .await
            .push_front(zmtp_frames_for_logical_message);
          self.queue_activity_notifier.notify_one();
        }
      } else {
        tracing::trace!(
          "[DealerProc {}] No message popped from queue (or conditions not met). Continuing to wait.",
          self.core_handle
        );
      }
    }
    tracing::debug!(
      "[DealerProc {}] Outgoing queue processor task finished execution.",
      self.core_handle
    );
  }
}

#[derive(Debug)]
pub(crate) struct DealerSocket {
  core: Arc<SocketCore>,
  load_balancer: Arc<LoadBalancer>,
  incoming_orchestrator: IncomingMessageOrchestrator<Vec<Msg>>,
  pipe_read_to_endpoint_uri: ParkingLotRwLock<HashMap<usize, String>>,
  pending_outgoing_queue: Arc<TokioMutex<VecDeque<Vec<Msg>>>>,
  outgoing_queue_activity_notifier: Arc<Notify>,
  peer_availability_notifier: Arc<Notify>,
  processor_task_handle: TokioMutex<Option<JoinHandle<()>>>,
  processor_stop_signal: Arc<Notify>,
  current_send_transaction: TokioMutex<DealerSendTransaction>,
  pipe_send_coordinator: Arc<WritePipeCoordinator>,
}

impl DealerSocket {
  pub fn new(core: Arc<SocketCore>) -> Self {
    let pending_queue_arc = Arc::new(TokioMutex::new(VecDeque::new()));
    let lb_arc = Arc::new(LoadBalancer::new());
    let queue_notifier_arc = Arc::new(Notify::new());
    let peer_notifier_arc = Arc::new(Notify::new());
    let stop_signal_arc = Arc::new(Notify::new());
    let pipe_send_coordinator_arc = Arc::new(WritePipeCoordinator::new());

    let processor = DealerSocketOutgoingProcessor {
      core_handle: core.handle,
      pending_queue: pending_queue_arc.clone(),
      load_balancer: lb_arc.clone(),
      core_accessor: core.clone(),
      queue_activity_notifier: queue_notifier_arc.clone(),
      peer_availability_notifier: peer_notifier_arc.clone(),
      stop_signal: stop_signal_arc.clone(),
      pipe_send_coordinator: pipe_send_coordinator_arc.clone(),
    };

    let processor_jh = tokio::spawn(processor.run());
    let rcvhwm = { core.core_state.read().options.rcvhwm };
    let orchestrator = IncomingMessageOrchestrator::new(core.handle, rcvhwm);

    Self {
      core,
      load_balancer: lb_arc,
      incoming_orchestrator: orchestrator,
      pipe_read_to_endpoint_uri: ParkingLotRwLock::new(HashMap::new()),
      pending_outgoing_queue: pending_queue_arc,
      outgoing_queue_activity_notifier: queue_notifier_arc,
      peer_availability_notifier: peer_notifier_arc,
      processor_task_handle: TokioMutex::new(Some(processor_jh)),
      processor_stop_signal: stop_signal_arc,
      current_send_transaction: TokioMutex::new(DealerSendTransaction::Idle),
      pipe_send_coordinator: pipe_send_coordinator_arc,
    }
  }

  fn process_incoming_zmtp_message_for_dealer(
    &self,
    pipe_read_id: usize,
    mut frames: Vec<Msg>,
  ) -> Result<Vec<Msg>, ZmqError> {
    tracing::trace!(
      handle = self.core.handle,
      pipe_id = pipe_read_id,
      num_raw_frames = frames.len(),
      "Dealer processing incoming ZMTP message"
    );

    if frames.is_empty() {
      tracing::warn!(
        handle = self.core.handle,
        pipe_id = pipe_read_id,
        "Dealer received empty ZMTP message. Returning as empty payload."
      );
      return Ok(frames);
    }

    // A DEALER socket, when receiving from a ROUTER, expects the ZMTP message to be:
    // [identity_of_this_dealer_M, empty_delimiter_M, app_payload_frames...]
    // The DEALER application should only see the app_payload_frames.
    //
    // If receiving from another DEALER, the ZMTP message is:
    // [empty_delimiter_M, app_payload_frames...]
    // The DEALER application should see app_payload_frames.

    // We need to determine if the first frame is an identity (non-empty) or a delimiter (empty).
    // This is inherently ambiguous for a DEALER as it doesn't know its peer type.
    // However, the ZMQ spec implies that the DEALER socket itself handles stripping
    // its own identity if present, and always strips the first empty delimiter before payload.

    // For rzmq, the 'identity_of_this_dealer' is a ZMTP routing detail.
    // The ISocketConnection would have received it. If it's part of the `frames` here,
    // it means it was part of the ZMTP message on the wire for this specific connection.
    // Let's assume if the first frame is NOT empty, it's an identity from a ROUTER and should be stripped.
    if frames[0].size() != 0 {
      // Assume it's an identity frame sent by a ROUTER, discard it.
      tracing::trace!(
        handle = self.core.handle,
        pipe_id = pipe_read_id,
        "Dealer: Discarding first non-empty frame (assumed identity from ROUTER)."
      );
      frames.remove(0);
      // After discarding the identity, the next frame *must* be an empty delimiter.
      if frames.is_empty() || frames[0].size() != 0 {
        tracing::warn!(handle = self.core.handle, pipe_id = pipe_read_id, "Dealer: Expected empty delimiter after presumed identity frame, but not found or no frames left. Message might be malformed from peer ROUTER.");
        // Return what's left, which might be an error for the app or an unexpected payload.
        return Ok(frames);
      }
      // Now frames[0] is the empty delimiter, discard it.
      frames.remove(0);
      tracing::trace!(
        handle = self.core.handle,
        pipe_id = pipe_read_id,
        "Dealer: Stripped empty delimiter after presumed identity."
      );
    }
    // If the first frame *was* empty (or became empty after stripping identity and the check above)
    else if !frames.is_empty() && frames[0].size() == 0 {
      // This handles the case of DEALER-to-DEALER (first frame is delimiter)
      // OR the delimiter that followed an identity from a ROUTER (already handled by the `else if` above if identity was present).
      // So, this specifically catches the DEALER-DEALER case now.
      tracing::trace!(
        handle = self.core.handle,
        pipe_id = pipe_read_id,
        "Dealer: Stripped leading empty ZMTP delimiter (likely from peer DEALER)."
      );
      frames.remove(0);
    }
    // `frames` now contains only the application payload parts.
    Ok(frames)
  }

  fn prepare_full_multipart_send_sequence(&self, user_frames: Vec<Msg>) -> Vec<Msg> {
    if user_frames.is_empty() {
      let mut delimiter = Msg::new();
      delimiter.set_flags(MsgFlags::MORE);
      let mut last_empty = Msg::new();
      last_empty.set_flags(last_empty.flags() & !MsgFlags::MORE);
      return vec![delimiter, last_empty];
    }
    let mut frames_to_send = Vec::with_capacity(user_frames.len() + 1);
    let mut delimiter = Msg::new();
    delimiter.set_flags(MsgFlags::MORE);
    frames_to_send.push(delimiter);
    let num_user_frames = user_frames.len();
    for (i, mut frame) in user_frames.into_iter().enumerate() {
      if i < num_user_frames - 1 {
        frame.set_flags(frame.flags() | MsgFlags::MORE);
      } else {
        frame.set_flags(frame.flags() & !MsgFlags::MORE);
      }
      frames_to_send.push(frame);
    }
    frames_to_send
  }
}

#[async_trait]
impl ISocket for DealerSocket {
  fn core(&self) -> &Arc<SocketCore> {
    &self.core
  }
  fn mailbox(&self) -> MailboxSender {
    self.core.command_sender()
  }
  async fn bind(&self, endpoint: &str) -> Result<(), ZmqError> {
    delegate_to_core!(self, UserBind, endpoint: endpoint.to_string())
  }
  async fn connect(&self, endpoint: &str) -> Result<(), ZmqError> {
    let res = delegate_to_core!(self, UserConnect, endpoint: endpoint.to_string());
    if res.is_ok() {
      self.peer_availability_notifier.notify_one();
    }
    res
  }
  async fn disconnect(&self, endpoint: &str) -> Result<(), ZmqError> {
    delegate_to_core!(self, UserDisconnect, endpoint: endpoint.to_string())
  }
  async fn unbind(&self, endpoint: &str) -> Result<(), ZmqError> {
    delegate_to_core!(self, UserUnbind, endpoint: endpoint.to_string())
  }
  async fn set_option(&self, option: i32, value: &[u8]) -> Result<(), ZmqError> {
    delegate_to_core!(self, UserSetOpt, option: option, value: value.to_vec())
  }
  async fn get_option(&self, option: i32) -> Result<Vec<u8>, ZmqError> {
    delegate_to_core!(self, UserGetOpt, option: option)
  }
  async fn close(&self) -> Result<(), ZmqError> {
    delegate_to_core!(self, UserClose,)
  }

  async fn send(&self, msg: Msg) -> Result<(), ZmqError> {
    if !self.core.is_running().await {
      return Err(ZmqError::InvalidState("Socket is closing".into()));
    }

    let mut transaction_guard = self.current_send_transaction.lock().await;

    if msg.is_more() {
      match &mut *transaction_guard {
        DealerSendTransaction::Idle => {
          let notifier = Arc::new(Notify::new());
          *transaction_guard = DealerSendTransaction::Buffering {
            parts: vec![msg],
            completion_notifier: notifier,
          };
        }
        DealerSendTransaction::Buffering { parts, .. } => {
          if parts.len() >= MAX_DEALER_SEND_BUFFER_PARTS {
            let notifier_to_signal = match std::mem::replace(&mut *transaction_guard, DealerSendTransaction::Idle) {
              DealerSendTransaction::Buffering {
                completion_notifier: cn,
                ..
              } => Some(cn),
              _ => None,
            };
            drop(transaction_guard);
            if let Some(notifier_arc) = notifier_to_signal {
              notifier_arc.notify_waiters();
            }
            return Err(ZmqError::ResourceLimitReached);
          }
          parts.push(msg);
        }
      }
      Ok(())
    } else {
      let (parts_to_send_app_level, notifier_opt) =
        match std::mem::replace(&mut *transaction_guard, DealerSendTransaction::Idle) {
          DealerSendTransaction::Idle => (vec![msg], None),
          DealerSendTransaction::Buffering {
            mut parts,
            completion_notifier,
          } => {
            parts.push(msg);
            (parts, Some(completion_notifier))
          }
        };
      drop(transaction_guard);

      let full_message_for_wire = self.prepare_full_multipart_send_sequence(parts_to_send_app_level);
      let result = self.send_logical_message(full_message_for_wire).await;

      if let Some(notifier) = notifier_opt {
        notifier.notify_waiters();
      }
      result
    }
  }

  async fn send_multipart(&self, user_frames: Vec<Msg>) -> Result<(), ZmqError> {
    
    if !self.core.is_running().await {
      return Err(ZmqError::InvalidState("Socket is closing".into()));
    }
    
    let sndtimeo_opt = { self.core.core_state.read().options.sndtimeo };

    loop {
      
      let mut transaction_guard = self.current_send_transaction.lock().await;
      match &*transaction_guard {
        DealerSendTransaction::Idle => {
          drop(transaction_guard);
          
          let zmtp_wire_frames = self.prepare_full_multipart_send_sequence(user_frames);
          let res = self.send_logical_message(zmtp_wire_frames).await;

          
      return res;
        }
        DealerSendTransaction::Buffering {
          completion_notifier, ..
        } => {
          
          let notifier_clone = completion_notifier.clone();
          drop(transaction_guard);

          let closing_signal_future = async {
            loop {
              if !self.core.is_running().await {
                break;
              }
              tokio::time::sleep(Duration::from_millis(50)).await;
            }
          };
          
          match sndtimeo_opt {
            Some(duration) if duration.is_zero() => return Err(ZmqError::ResourceLimitReached),
            Some(duration) => {
              tokio::select! {
                biased;
                _ = closing_signal_future => return Err(ZmqError::InvalidState("Socket is closing while waiting for prior send tx".into())),
                res = tokio_timeout(duration, notifier_clone.notified()) => {
                  
                  if res.is_err() { return Err(ZmqError::Timeout); }
                }
              }
            }
            None => {
              tokio::select! {
                biased;
                _ = closing_signal_future => return Err(ZmqError::InvalidState("Socket is closing while waiting for prior send tx".into())),
                _ = notifier_clone.notified() => { }
              }
            }
          }
        }
      }
    }
  }

  async fn recv(&self) -> Result<Msg, ZmqError> {
    if !self.core.is_running().await {
      return Err(ZmqError::InvalidState("Socket is closing".into()));
    }
    let rcvtimeo_opt: Option<Duration> = { self.core.core_state.read().options.rcvtimeo };
    // For DEALER, QItem is Vec<Msg> (payload parts).
    // The transform_fn tells orchestrator.recv_message how to convert QItem into app_frames.
    // For DEALER, the QItem (payload Vec<Msg>) IS the app_frames for the orchestrator's buffer.
    let transform_fn = |q_item: Vec<Msg>| q_item;
    self
      .incoming_orchestrator
      .recv_message(rcvtimeo_opt, transform_fn)
      .await
  }

  async fn recv_multipart(&self) -> Result<Vec<Msg>, ZmqError> {
    if !self.core.is_running().await {
      return Err(ZmqError::InvalidState("Socket is closing".into()));
    }

    let rcvtimeo_opt: Option<Duration> = { self.core.core_state.read().options.rcvtimeo };
    // For DEALER, QItem is Vec<Msg> (the payload parts). Transform is identity.
    let transform_fn = |q_item: Vec<Msg>| q_item;
    self
      .incoming_orchestrator
      .recv_logical_message(rcvtimeo_opt, transform_fn)
      .await
  }

  async fn set_pattern_option(&self, option: i32, _value: &[u8]) -> Result<(), ZmqError> {
    Err(ZmqError::UnsupportedOption(option))
  }
  async fn get_pattern_option(&self, option: i32) -> Result<Vec<u8>, ZmqError> {
    Err(ZmqError::UnsupportedOption(option))
  }

  async fn process_command(&self, command: Command) -> Result<bool, ZmqError> {

    match command {
      Command::Stop => {
        self.incoming_orchestrator.close().await;
        self.processor_stop_signal.notify_one();
        if let Some(handle) = self.processor_task_handle.lock().await.take() {
          if let Err(e) = tokio_timeout(Duration::from_millis(100), handle).await {
            tracing::warn!(
              "[Dealer {}] Timeout or error waiting for processor task on close: {:?}",
              self.core.handle,
              e
            );
          }
        }
        self.outgoing_queue_activity_notifier.notify_waiters();
        self.peer_availability_notifier.notify_waiters();
      }
      _ => return Ok(false),
    }
    
    Ok(true)
  }

  async fn handle_pipe_event(&self, pipe_read_id: usize, event: Command) -> Result<(), ZmqError> {
    match event {
      Command::PipeMessageReceived { msg, .. } => {
        if let Some(raw_zmtp_message_vec) = self.incoming_orchestrator.accumulate_pipe_frame(pipe_read_id, msg)? {
          match self.process_incoming_zmtp_message_for_dealer(pipe_read_id, raw_zmtp_message_vec) {
            Ok(payload_parts_vec) => {
              self
                .incoming_orchestrator
                .queue_item(pipe_read_id, payload_parts_vec)
                .await?;
            }
            Err(e) => {
              tracing::error!(
                handle = self.core.handle,
                pipe_id = pipe_read_id,
                "DEALER: Error processing raw ZMTP message: {}. Message dropped.",
                e
              );
            }
          }
        }
      }
      _ => {}
    }
    Ok(())
  }

  async fn pipe_attached(&self, pipe_read_id: usize, _pipe_write_id: usize, _peer_identity: Option<&[u8]>) {
    let (endpoint_uri_opt, connection_id_opt) = {
      let core_s_read = self.core.core_state.read();
      let uri = core_s_read.pipe_read_id_to_endpoint_uri.get(&pipe_read_id).cloned();
      let conn_id = if let Some(ref u) = uri {
        core_s_read.endpoints.get(u).map(|ep_info| ep_info.handle_id)
      } else {
        None
      };
      (uri, conn_id)
    };

    if let Some(endpoint_uri) = endpoint_uri_opt {
      tracing::debug!(handle = self.core.handle, pipe_read_id, uri = %endpoint_uri, conn_id = ?connection_id_opt, "DEALER attaching connection");
      self
        .pipe_read_to_endpoint_uri
        .write()
        .insert(pipe_read_id, endpoint_uri.clone());
      self.load_balancer.add_connection(endpoint_uri.clone());

      if let Some(conn_id) = connection_id_opt {
        self.pipe_send_coordinator.add_pipe(conn_id).await;
      } else {
        tracing::warn!(handle = self.core.handle, pipe_read_id, uri = %endpoint_uri, "DEALER pipe_attached: Could not find connection_id for URI. WritePipeCoordinator not updated.");
      }
      self.peer_availability_notifier.notify_one();
      self.outgoing_queue_activity_notifier.notify_one();
    } else {
      tracing::warn!(
        handle = self.core.handle,
        pipe_read_id,
        "DEALER pipe_attached: Could not find endpoint_uri for pipe_read_id. Cannot update internal maps."
      );
    }
  }

  async fn update_peer_identity(&self, _pipe_read_id: usize, _identity: Option<Blob>) {
    /* DEALER ignores ZMTP identity from peer for its core logic */
  }

  async fn pipe_detached(&self, pipe_read_id: usize) {
    tracing::debug!(
      "[Dealer {}] Detaching connection via pipe_read_id: {}",
      self.core.handle,
      pipe_read_id
    );
    let maybe_endpoint_uri = self.pipe_read_to_endpoint_uri.write().remove(&pipe_read_id);

    if let Some(endpoint_uri) = maybe_endpoint_uri {
      self.load_balancer.remove_connection(&endpoint_uri);
      let connection_id_to_remove_opt = {
        let core_s_read = self.core.core_state.read();
        core_s_read
          .endpoints
          .get(&endpoint_uri)
          .map(|ep_info| ep_info.handle_id)
      };
      if let Some(conn_id) = connection_id_to_remove_opt {
        self.pipe_send_coordinator.remove_pipe(conn_id).await;
      }
    }
    self.incoming_orchestrator.clear_pipe_state(pipe_read_id).await;
    self.peer_availability_notifier.notify_waiters();
  }
}

impl DealerSocket {
  async fn send_logical_message(&self, zmtp_wire_frames: Vec<Msg>) -> Result<(), ZmqError> {
    if !self.core.is_running().await {
      return Err(ZmqError::InvalidState("Socket is closing or terminated".into()));
    }
    let (global_sndtimeo, global_sndhwm) = {
      let core_s_read = self.core.core_state.read();
      (core_s_read.options.sndtimeo, core_s_read.options.sndhwm.max(1))
    };

    let mut attempts_this_message_cycle = 0;
    let max_direct_send_attempts = self.load_balancer.connection_count().await.max(1);

    'direct_send_attempt_loop: loop {
      if attempts_this_message_cycle >= max_direct_send_attempts {
        return self
          .queue_message_or_error(zmtp_wire_frames, global_sndhwm, global_sndtimeo)
          .await;
      }
      attempts_this_message_cycle += 1;

      if !self.core.is_running().await {
        return Err(ZmqError::InvalidState("Socket closing mid-send_logical_message".into()));
      }

      let endpoint_uri_to_send_to = match self.load_balancer.get_next_connection_uri() {
        Some(uri) => {
          let uri_exists = { self.core.core_state.read().endpoints.contains_key(&uri) };
          if uri_exists {
            uri
          } else {
            self.load_balancer.remove_connection(&uri);
            self.peer_availability_notifier.notify_waiters();
            continue 'direct_send_attempt_loop;
          }
        }
        None => {
          return self
            .queue_message_or_error(zmtp_wire_frames, global_sndhwm, global_sndtimeo)
            .await;
        }
      };

      let (connection_iface_arc, connection_id_for_coord) = {
        let core_s = self.core.core_state.read();
        match core_s.endpoints.get(&endpoint_uri_to_send_to) {
          Some(ep_info) => (ep_info.connection_iface.clone(), ep_info.handle_id),
          None => {
            drop(core_s);
            self.load_balancer.remove_connection(&endpoint_uri_to_send_to);
            self.peer_availability_notifier.notify_waiters();
            continue 'direct_send_attempt_loop;
          }
        }
      };

      match self
        .pipe_send_coordinator
        .acquire_send_permit(connection_id_for_coord, global_sndtimeo)
        .await
      {
        Ok(_send_permit) => match connection_iface_arc.send_multipart(zmtp_wire_frames.clone()).await {
          Ok(()) => {
            return Ok(());
          }
          Err(ZmqError::ResourceLimitReached) | Err(ZmqError::Timeout) => {
            tracing::warn!(
              "[Dealer {}] HWM/Timeout on send_multipart to conn_id {}. Trying next peer.",
              self.core.handle,
              connection_id_for_coord
            );
            continue 'direct_send_attempt_loop;
          }
          Err(e @ ZmqError::ConnectionClosed) | Err(e @ ZmqError::HostUnreachable(_)) => {
            self.load_balancer.remove_connection(&endpoint_uri_to_send_to);
            self.pipe_send_coordinator.remove_pipe(connection_id_for_coord).await;
            self.peer_availability_notifier.notify_waiters();
            tracing::warn!(
              "[Dealer {}] Conn {} (URI {}) closed/unreachable for send_multipart: {}. Removed.",
              self.core.handle,
              connection_id_for_coord,
              endpoint_uri_to_send_to,
              e
            );
            continue 'direct_send_attempt_loop;
          }
          Err(e) => {
            return Err(e);
          }
        },
        Err(ZmqError::ResourceLimitReached) | Err(ZmqError::Timeout) => {
          return self
            .queue_message_or_error(zmtp_wire_frames, global_sndhwm, global_sndtimeo)
            .await;
        }
        Err(ZmqError::HostUnreachable(_)) => {
          self.load_balancer.remove_connection(&endpoint_uri_to_send_to);
          self.peer_availability_notifier.notify_waiters();
          continue 'direct_send_attempt_loop;
        }
        Err(e) => return Err(e),
      }
    }
  }

  async fn queue_message_or_error(
    &self,
    full_message_parts: Vec<Msg>,
    global_sndhwm: usize,
    global_sndtimeo: Option<Duration>,
  ) -> Result<(), ZmqError> {
    tracing::trace!(
      "[Dealer {}] Attempting to queue message ({} parts).",
      self.core.handle,
      full_message_parts.len()
    );
    loop {
      if !self.core.is_running().await {
        return Err(ZmqError::InvalidState("Socket is closing while trying to queue".into()));
      }
      if self.pending_outgoing_queue.lock().await.len() < global_sndhwm {
        self.pending_outgoing_queue.lock().await.push_back(full_message_parts);
        self.outgoing_queue_activity_notifier.notify_one();
        return Ok(());
      }
      match global_sndtimeo {
        Some(duration) if duration.is_zero() => return Err(ZmqError::ResourceLimitReached),
        Some(duration) => {
          let queue_wait_fut = self.outgoing_queue_activity_notifier.notified();
          if tokio_timeout(duration, queue_wait_fut).await.is_err() {
            return Err(ZmqError::Timeout);
          }
        }
        None => {
          tokio::select! {
            biased;
             _ = async { if !self.core.is_running().await { futures::future::pending().await } else { futures::future::pending().await } } => {
                return Err(ZmqError::InvalidState("Socket is closing while waiting for queue space".into()));
            }
            _ = self.outgoing_queue_activity_notifier.notified() => {}
            _ = self.peer_availability_notifier.notified() => {}
          }
        }
      }
    }
  }
}

impl Drop for DealerSocket {
  fn drop(&mut self) {
    self.processor_stop_signal.notify_one();
    if let Ok(mut guard) = self.processor_task_handle.try_lock() {
      if let Some(handle) = guard.take() {
        handle.abort();
      }
    }
    if let Ok(mut transaction_guard) = self.current_send_transaction.try_lock() {
      if let DealerSendTransaction::Buffering {
        completion_notifier, ..
      } = std::mem::replace(&mut *transaction_guard, DealerSendTransaction::Idle)
      {
        completion_notifier.notify_waiters();
      }
    }
  }
}