ipmb 0.8.5

Inter-process message bus
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
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
use crate::{
    decode, encode,
    message::{ConnectMessage, ConnectMessageAck},
    util::Align4,
    version, EndpointID, Error, Label, LabelOp, MemoryRegion, Message, MessageBox, Selector,
    Version,
};
pub(crate) use memory_region::page_mask;
use security::SecurityAttr;
use std::{
    io, mem,
    mem::MaybeUninit,
    ops::Deref,
    os::windows::prelude::{
        AsRawHandle, FromRawHandle, HandleOrInvalid, IntoRawHandle, OwnedHandle, RawHandle,
    },
    ptr, slice,
    sync::{
        mpsc,
        mpsc::{Receiver, Sender, TryRecvError},
        Arc,
    },
    time::Duration,
};
use type_uuid::*;
use windows::{
    core::HSTRING,
    Win32::{
        Foundation::{self, WIN32_ERROR},
        Storage::FileSystem,
        System::{Pipes, Threading, IO},
    },
};

mod memory_region;
mod pipe;
mod security;
pub(crate) mod util;

#[derive(Debug, PartialEq)]
pub struct Remote {
    pipe: Handle,
    // Empty only during the connecting phase
    process: Option<Handle>,
}

impl Remote {
    pub fn is_dead(&self) -> bool {
        // Compatible with Windows 7
        let mut written = 0;
        unsafe {
            !FileSystem::WriteFile(self.pipe.as_raw_windows(), None, Some(&mut written), None)
                .as_bool()
        }
    }
}

pub(crate) fn look_up(
    identifier: &str,
    label: Label,
    token: String,
    im: Arc<IoMultiplexing>,
) -> Result<(IoHub, Remote, EndpointID), Error> {
    // A empty identifier will successfully open the pipe , but fail with `GetNamedPipeServerProcessId`
    if identifier.is_empty() {
        return Err(Error::PermissionDenied);
    }

    unsafe {
        let identifier_h: HSTRING = format!("\\\\.\\pipe\\{}", identifier).into();

        let pipe_handle: Foundation::HANDLE = FileSystem::CreateFileW(
            &identifier_h,
            FileSystem::FILE_GENERIC_WRITE.0,
            FileSystem::FILE_SHARE_NONE,
            None,
            FileSystem::OPEN_EXISTING,
            // We write synchronously, otherwise may incorrectly report that the write operation is complete
            FileSystem::FILE_FLAGS_AND_ATTRIBUTES::default(),
            None,
        )
        .map_err(|err| match WIN32_ERROR::from_error(&err) {
            Some(Foundation::ERROR_PIPE_BUSY) => Error::WinError(err), // TODO
            Some(Foundation::ERROR_FILE_NOT_FOUND) => Error::IdentifierNotInUse,
            Some(Foundation::ERROR_ACCESS_DENIED) => Error::PermissionDenied,
            _ => Error::WinError(err),
        })?;

        let remote_pipe = Handle(OwnedHandle::from_raw_handle(pipe_handle.0 as _));

        let mut server_pid = 0;
        let r = Pipes::GetNamedPipeServerProcessId(remote_pipe.as_raw_windows(), &mut server_pid);
        if !r.as_bool() {
            return Err(Error::WinError(windows::core::Error::from_win32()));
        }

        let mut remote = Remote {
            pipe: remote_pipe,
            process: None,
        };

        let server_process =
            match Threading::OpenProcess(Threading::PROCESS_DUP_HANDLE, false, server_pid) {
                Ok(server_process) => OwnedHandle::from_raw_handle(server_process.0 as _),
                Err(err) => {
                    if Foundation::GetLastError() == Foundation::ERROR_ACCESS_DENIED {
                        util::fetch_remote_process_handle(&remote, &im.sa)?
                    } else {
                        return Err(Error::WinError(err));
                    }
                }
            };
        remote.process = Some(Handle(server_process));

        let (read_pipe, write_pipe) = pipe::anon_pipe(identifier, &im.sa)?;
        let read_pipe = NamedPipe::new(read_pipe, NamedPipeStatus::Readable);

        let mut msg = Message::new(
            Selector::unicast(LabelOp::True),
            ConnectMessage {
                version: version(),
                token,
                label,
            },
        );
        msg.objects.push(Handle(OwnedHandle::from_raw_handle(
            Threading::GetCurrentProcess().0 as _,
        )));
        msg.objects.push(write_pipe);

        let mut encoded_msg = msg.into_encoded();
        encoded_msg.send(&remote)?;

        let mut io_hub: IoHub = IoHub::for_endpoint(im, identifier_h, read_pipe);
        // Wait ack
        let encoded_msg = io_hub.recv(Some(Duration::from_secs(2)), None)?;
        let ack = ConnectMessageAck::decode(encoded_msg.selector.uuid, encoded_msg.payload_data)?;

        match ack {
            ConnectMessageAck::Ok(endpoint_id) => Ok((io_hub, remote, endpoint_id)),
            ConnectMessageAck::ErrVersion(v) => Err(Error::VersionMismatch(v, None)),
            ConnectMessageAck::ErrToken => Err(Error::TokenMismatch),
        }
    }
}

pub(crate) fn register(
    identifier: &str,
    im: Arc<IoMultiplexing>,
) -> Result<(IoHub, Sender<EncodedMessage>, EndpointID), Error> {
    unsafe {
        let identifier: HSTRING = format!("\\\\.\\pipe\\{}", identifier).into();

        let pipe_handle = Pipes::CreateNamedPipeW(
            &identifier,
            FileSystem::PIPE_ACCESS_INBOUND
                | FileSystem::FILE_FLAG_OVERLAPPED
                | FileSystem::FILE_FLAG_FIRST_PIPE_INSTANCE,
            Pipes::PIPE_TYPE_MESSAGE | Pipes::PIPE_READMODE_MESSAGE | Pipes::PIPE_WAIT,
            Pipes::PIPE_UNLIMITED_INSTANCES,
            pipe::DEFAULT_BUFFER_SIZE,
            pipe::DEFAULT_BUFFER_SIZE,
            0,
            Some(im.sa.attr()),
        );
        let pipe_handle: OwnedHandle = HandleOrInvalid::from_raw_handle(pipe_handle.0 as _)
            .try_into()
            .map_err(|_| match Foundation::GetLastError() {
                Foundation::ERROR_ALREADY_EXISTS => Error::IdentifierInUse,
                Foundation::ERROR_ACCESS_DENIED => Error::PermissionDenied,
                err => Error::WinError(err.into()),
            })?;

        let local = NamedPipe::new(Handle(pipe_handle), NamedPipeStatus::Free);
        let (bus_sender, bus_receiver) = mpsc::channel();

        Ok((
            IoHub::for_bus_controller(im, identifier, bus_receiver, local),
            bus_sender,
            EndpointID::new(),
        ))
    }
}

pub(crate) struct IoHub {
    identifier: HSTRING,
    bus_receiver: Option<Receiver<EncodedMessage>>,
    waker: usize,
    im: Arc<IoMultiplexing>,
    pipes: Vec<NamedPipe>,
    listening_pipe: Option<NamedPipe>,
}

impl IoHub {
    fn for_bus_controller(
        im: Arc<IoMultiplexing>,
        identifier: HSTRING,
        bus_receiver: Receiver<EncodedMessage>,
        listening_pipe: NamedPipe,
    ) -> Self {
        im.register_handle(&listening_pipe);

        let io_hub = Self {
            identifier,
            bus_receiver: Some(bus_receiver),
            waker: im.waker,
            im,
            pipes: vec![],
            listening_pipe: Some(listening_pipe),
        };

        io_hub
    }

    fn for_endpoint(im: Arc<IoMultiplexing>, identifier: HSTRING, pipe: NamedPipe) -> Self {
        im.register_handle(&pipe);

        Self {
            identifier,
            bus_receiver: None,
            waker: im.waker,
            im,
            pipes: vec![pipe],
            listening_pipe: None,
        }
    }

    fn listen(&mut self) {
        if let Some(listening_pipe) = &mut self.listening_pipe {
            loop {
                listening_pipe.listen();

                match listening_pipe.status {
                    NamedPipeStatus::Readable => {
                        self.pipes.push(mem::replace(
                            listening_pipe,
                            self.im.create_instance(&self.identifier),
                        ));
                    }
                    NamedPipeStatus::Pending => break,
                    NamedPipeStatus::Offline => {
                        *listening_pipe = self.im.create_instance(&self.identifier);
                    }
                    NamedPipeStatus::Free | NamedPipeStatus::Ready => unreachable!(),
                }
            }
        }
    }

    pub fn recv(
        &mut self,
        timeout: Option<Duration>,
        _remote: Option<&Remote>,
    ) -> Result<EncodedMessage, Error> {
        'ret: loop {
            // Listen new instance
            self.listen();

            // Receive message from bus_receiver
            if let Some(bus_receiver) = &self.bus_receiver {
                match bus_receiver.try_recv() {
                    Ok(message) => {
                        break Ok(message);
                    }
                    Err(TryRecvError::Empty) => {}
                    Err(TryRecvError::Disconnected) => {
                        self.bus_receiver = None;
                    }
                }
            }

            // Read message from pipes
            let mut i = 0;
            while i < self.pipes.len() {
                let pipe = &mut self.pipes[i];
                match pipe.read() {
                    Some((pipe_msg, msg_size)) => {
                        break 'ret EncodedMessage::new(pipe_msg, msg_size)
                    }
                    None => match pipe.status {
                        NamedPipeStatus::Pending => i += 1,
                        NamedPipeStatus::Offline => {
                            let _ = self.pipes.swap_remove(i);
                        }
                        NamedPipeStatus::Free
                        | NamedPipeStatus::Readable
                        | NamedPipeStatus::Ready => unreachable!(),
                    },
                }
            }

            if self.bus_receiver.is_none() && self.listening_pipe.is_none() && self.pipes.is_empty()
            {
                break Err(Error::Disconnect);
            }

            // Wait
            let mut key = 0;
            let mut n = 0;
            let r = self.im.wait(&mut key, &mut n, timeout);
            if r {
                if key == self.waker {
                    continue;
                }

                if let Some(listening_pipe) = &mut self.listening_pipe {
                    if listening_pipe.as_raw() as usize == key {
                        listening_pipe.status = NamedPipeStatus::Readable;
                        self.pipes.push(mem::replace(
                            listening_pipe,
                            self.im.create_instance(&self.identifier),
                        ));
                        continue;
                    }
                }

                if let Some(pipe) = self
                    .pipes
                    .iter_mut()
                    .find(|pipe| pipe.as_raw() as usize == key)
                {
                    pipe.message_size += n as usize;
                    pipe.status = NamedPipeStatus::Ready;

                    continue;
                }
            } else {
                let err = unsafe { Foundation::GetLastError() };

                match err {
                    Foundation::ERROR_ABANDONED_WAIT_0 => continue, // Handle is closed
                    Foundation::ERROR_TIMEOUT | Foundation::WAIT_TIMEOUT => {
                        break Err(Error::Timeout)
                    }
                    _ => {}
                }

                if key == self.waker {
                    continue;
                }

                if let Some(listening_pipe) = &mut self.listening_pipe {
                    if listening_pipe.as_raw() as usize == key {
                        match err {
                            Foundation::ERROR_PIPE_CONNECTED => {
                                listening_pipe.status = NamedPipeStatus::Readable;
                                self.pipes.push(mem::replace(
                                    listening_pipe,
                                    self.im.create_instance(&self.identifier),
                                ));
                            }
                            Foundation::ERROR_PIPE_LISTENING | Foundation::ERROR_IO_PENDING => {}
                            _ => {
                                log::error!("Listening: {:?}", err);
                                *listening_pipe = self.im.create_instance(&self.identifier);
                            }
                        }
                        continue;
                    }
                }

                if let Some(pipe) = self
                    .pipes
                    .iter_mut()
                    .find(|pipe| pipe.as_raw() as usize == key)
                {
                    match err {
                        Foundation::ERROR_MORE_DATA => {
                            pipe.message_size += n as usize;
                            pipe.buffer.reserve(pipe.buffer.capacity() * 2);
                            pipe.status = NamedPipeStatus::Readable;
                        }
                        Foundation::ERROR_IO_PENDING | Foundation::ERROR_IO_INCOMPLETE => continue,
                        _ => {
                            pipe.status = NamedPipeStatus::Offline;
                            continue;
                        }
                    }
                }
            }
        }
    }

    pub fn io_multiplexing(&self) -> Arc<IoMultiplexing> {
        self.im.clone()
    }
}

fn send_helper(
    remote: &Remote,
    pipe_msg: &mut [u8],
    msg_size: usize,
    objects: &[Handle],
    memory_regions: &[MemoryRegion],
) -> Result<(), Error> {
    unsafe {
        let mut remote_objects = Vec::with_capacity(objects.len() + memory_regions.len());

        let object_count_ptr = (pipe_msg.as_mut_ptr() as *mut u32).offset(1);
        let mut object_ptr = object_count_ptr.add(1) as *mut u64;

        for object in objects
            .iter()
            .chain(memory_regions.iter().map(|r| r.object()))
        {
            let remote_object = object
                .to_remote(remote.process.as_ref().unwrap())
                .map_err(|_| Error::Disconnect)?;
            ptr::write_unaligned(
                object_ptr,
                remote_object.pseudo_handle.unwrap() as isize as u64,
            );
            remote_objects.push(remote_object);
            object_ptr = object_ptr.add(1);
        }

        let mut n = 0;

        // Add memory region's ref count
        for r in memory_regions {
            r.ref_count_inner(1);
        }

        let r = FileSystem::WriteFile(
            remote.pipe.as_raw_windows(),
            Some(&pipe_msg[..msg_size]),
            Some(&mut n),
            None,
        );
        if !r.as_bool() {
            for r in memory_regions {
                r.ref_count_inner(-1);
            }
            return Err(Error::Disconnect);
        }

        assert_eq!(n, msg_size as _);

        // r = FlushFileBuffers(remote.pipe.as_raw());
        // if !r.as_bool() {
        //     return Err(Error::Disconnect);
        // }

        for mut remote_object in remote_objects {
            remote_object.drain();
        }
    }
    Ok(())
}

/// Message layout
/// | version (magic__major__minor__patch)
/// | object_count (u32)
/// | object (u64) * N
/// | selector_size (u32)
/// | selector
/// | selector_padding
/// | payload_size (u32)
/// | payload
/// | payload_padding
///
pub(crate) struct EncodedMessage {
    pub selector: Selector,
    pub payload_data: &'static [u8],
    pipe_msg: Vec<u8>,
    msg_size: usize,
    pub objects: Vec<Handle>,
    pub memory_regions: Vec<MemoryRegion>,
}

impl EncodedMessage {
    pub fn extract_remote(&mut self) -> Option<Remote> {
        debug_assert_eq!(self.selector.uuid, <ConnectMessage as TypeUuid>::UUID);

        let pipe = self.objects.pop()?;
        let process = self.objects.pop()?;
        Some(Remote {
            pipe,
            process: Some(process),
        })
    }

    pub fn send(&mut self, remote: &Remote) -> Result<(), Error> {
        send_helper(
            remote,
            self.pipe_msg.as_mut_slice(),
            self.msg_size,
            self.objects.as_slice(),
            self.memory_regions.as_slice(),
        )
    }

    // TODO: Check size
    fn new(mut pipe_msg: Vec<u8>, msg_size: usize) -> Result<Self, Error> {
        unsafe {
            pipe_msg.set_len(msg_size);

            // Version
            let version_ptr = pipe_msg.as_mut_ptr() as *mut u32;
            let [magic, major, minor, patch]: [u8; 4] = u32::to_ne_bytes(*version_ptr);
            let remote_version = Version((major, minor, patch));

            if magic != 0xFF {
                return Err(Error::VersionMismatch(Version((0, 0, 0)), None));
            }

            let object_count_ptr = version_ptr.offset(1);
            let object_count = *object_count_ptr;

            let mut object_ptr = object_count_ptr.add(1) as *mut u64;
            let mut objects = Vec::with_capacity(object_count as _);
            for _ in 0..object_count {
                objects.push(Handle(OwnedHandle::from_raw_handle(
                    ptr::read_unaligned(object_ptr) as isize as _,
                )));
                object_ptr = object_ptr.add(1);
            }

            if !version().compatible(remote_version) {
                return Err(Error::VersionMismatch(remote_version, None));
            }

            let selector_size_ptr = object_ptr as *mut u32;
            let selector_size = *selector_size_ptr;

            let selector_ptr = selector_size_ptr.add(1) as *mut u8;
            // TODO: Check size
            let selector: Selector =
                decode(slice::from_raw_parts(selector_ptr, selector_size as _))?;

            // Pop memory regions
            let memory_regions: Vec<_> = objects
                .drain((objects.len() - selector.memory_region_count as usize)..)
                .map(|obj| {
                    let r = MemoryRegion::from_object(obj);
                    r.ref_count_inner(-1);
                    r
                })
                .collect();

            let payload_size_ptr = selector_ptr.add(selector_size.align4() as _) as *mut u32;
            let payload_size = *payload_size_ptr;

            let payload_ptr = payload_size_ptr.add(1) as *mut u8;

            Ok(Self {
                selector,
                payload_data: slice::from_raw_parts(payload_ptr, payload_size as _),
                pipe_msg,
                msg_size,
                objects,
                memory_regions,
            })
        }
    }
}

impl<T: MessageBox> Message<T> {
    fn encode_inner(&self) -> (&'static [u8], Vec<u8>, usize) {
        unsafe {
            // TODO: How can we hint size
            let selector_bytes = encode(&self.selector).unwrap();
            let payload_bytes = self.payload.encode().unwrap();

            let msg_size = 4
                + 4
                + (self.objects.len() + self.memory_regions.len()) * 8
                + 4
                + selector_bytes.len().align4()
                + 4
                + payload_bytes.len().align4();

            let mut pipe_msg: Vec<u8> = Vec::with_capacity(msg_size);
            pipe_msg.set_len(msg_size);

            // Version
            let version_ptr = pipe_msg.as_mut_ptr() as *mut u32;
            {
                let v = version();
                *version_ptr = u32::from_ne_bytes([0xFF, v.major(), v.minor(), v.patch()]);
            }

            let object_count_ptr = version_ptr.offset(1);
            *object_count_ptr = (self.objects.len() + self.memory_regions.len()) as _;

            let object_ptr = object_count_ptr.add(1) as *mut u64;
            // Skip remote objects
            let selector_size_ptr =
                object_ptr.add(self.objects.len() + self.memory_regions.len()) as *mut u32;
            *selector_size_ptr = selector_bytes.len() as _;

            let selector_ptr = selector_size_ptr.add(1) as *mut u8;
            ptr::copy_nonoverlapping(selector_bytes.as_ptr(), selector_ptr, selector_bytes.len());

            let payload_size_ptr = selector_ptr.add(selector_bytes.len().align4()) as *mut u32;
            *payload_size_ptr = payload_bytes.len() as _;

            let payload_ptr = payload_size_ptr.add(1) as *mut u8;
            ptr::copy_nonoverlapping(payload_bytes.as_ptr(), payload_ptr, payload_bytes.len());

            (
                slice::from_raw_parts(payload_ptr, payload_bytes.len()),
                pipe_msg,
                msg_size,
            )
        }
    }

    pub(crate) fn into_encoded(self) -> EncodedMessage {
        let (payload_data, pipe_msg, msg_size) = self.encode_inner();

        EncodedMessage {
            selector: self.selector,
            payload_data,
            pipe_msg,
            msg_size,
            objects: self.objects,
            memory_regions: self.memory_regions,
        }
    }
}

#[repr(transparent)]
#[derive(Debug)]
pub struct Handle(OwnedHandle);

impl Handle {
    pub unsafe fn from_raw(raw: RawHandle) -> Self {
        Self(OwnedHandle::from_raw_handle(raw))
    }

    pub fn into_raw(self) -> RawHandle {
        self.0.into_raw_handle()
    }

    pub fn as_raw(&self) -> RawHandle {
        self.0.as_raw_handle()
    }

    pub(crate) fn as_raw_windows(&self) -> Foundation::HANDLE {
        Foundation::HANDLE(self.as_raw() as _)
    }

    fn to_remote<'a>(&self, remote: &'a Self) -> Result<RemoteHandle<'a>, Error> {
        let mut pseudo_handle = Foundation::HANDLE::default();

        unsafe {
            let r = Foundation::DuplicateHandle(
                // pseudo handle, no leak here
                // https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getcurrentprocess
                Threading::GetCurrentProcess(),
                self.as_raw_windows(),
                remote.as_raw_windows(),
                &mut pseudo_handle,
                0,
                false,
                Foundation::DUPLICATE_SAME_ACCESS,
            );
            if !r.as_bool() {
                let err = match Foundation::GetLastError() {
                    Foundation::ERROR_ACCESS_DENIED => Error::Disconnect, // TODO
                    err => Error::WinError(err.into()),
                };

                return Err(err);
            }
        }

        Ok(RemoteHandle {
            pseudo_handle: Some(pseudo_handle.0 as _),
            remote,
        })
    }
}

impl PartialEq for Handle {
    fn eq(&self, other: &Self) -> bool {
        self.0.as_raw_handle() == other.0.as_raw_handle()
    }
}

impl Handle {
    pub fn clone(&self) -> io::Result<Self> {
        self.0.try_clone().map(Self)
    }
}

unsafe impl Send for Handle {}
unsafe impl Sync for Handle {}

struct RemoteHandle<'a> {
    pseudo_handle: Option<RawHandle>,
    remote: &'a Handle,
}

impl<'a> RemoteHandle<'a> {
    fn drain(&mut self) {
        self.pseudo_handle = None;
    }
}

impl<'a> Drop for RemoteHandle<'a> {
    fn drop(&mut self) {
        if let Some(pseudo_handle) = self.pseudo_handle.take() {
            unsafe {
                let _ = Foundation::DuplicateHandle(
                    self.remote.as_raw_windows(),
                    Foundation::HANDLE(pseudo_handle as _),
                    None,
                    ptr::null_mut(),
                    0,
                    false,
                    Foundation::DUPLICATE_CLOSE_SOURCE,
                );
            }
        }
    }
}

// Free -> Pending -> Readable -> Pending -> Ready -> Readable -> ..
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
enum NamedPipeStatus {
    Free,
    Readable,
    Pending,
    Ready,
    Offline,
}

pub struct NamedPipe {
    handle: Handle,
    status: NamedPipeStatus,
    buffer: Vec<u8>,
    message_size: usize,
    overlapped: Box<IO::OVERLAPPED>,
}

unsafe impl Send for NamedPipe {}

impl NamedPipe {
    fn new(handle: Handle, status: NamedPipeStatus) -> Self {
        unsafe {
            let overlapped: Box<IO::OVERLAPPED> = Box::new(MaybeUninit::zeroed().assume_init());

            Self {
                handle,
                status,
                buffer: Vec::with_capacity(64),
                message_size: 0,
                overlapped,
            }
        }
    }

    fn listen(&mut self) {
        unsafe {
            match self.status {
                NamedPipeStatus::Free => {}
                NamedPipeStatus::Pending | NamedPipeStatus::Offline => return,
                NamedPipeStatus::Readable | NamedPipeStatus::Ready => unreachable!(),
            }

            let r = Pipes::ConnectNamedPipe(self.as_raw_windows(), Some(self.overlapped.as_mut()));

            if r.as_bool() {
                // self.status = NamedPipeStatus::Readable;
                self.status = NamedPipeStatus::Pending;
            } else {
                match Foundation::GetLastError() {
                    Foundation::ERROR_PIPE_CONNECTED => {
                        self.status = NamedPipeStatus::Readable;
                    }
                    Foundation::ERROR_PIPE_LISTENING | Foundation::ERROR_IO_PENDING => {
                        self.status = NamedPipeStatus::Pending;
                    }
                    err @ _ => {
                        log::error!("Listening: {:?}", err);
                        self.status = NamedPipeStatus::Offline;
                    }
                }
            }
        }
    }

    fn read(&mut self) -> Option<(Vec<u8>, usize)> {
        match self.status {
            NamedPipeStatus::Readable => {}
            NamedPipeStatus::Pending => return None,
            NamedPipeStatus::Ready => {
                self.status = NamedPipeStatus::Readable;
                if self.message_size > 0 {
                    let pipe_msg = mem::replace(&mut self.buffer, Vec::with_capacity(64));
                    let msg_size = mem::replace(&mut self.message_size, 0);
                    return Some((pipe_msg, msg_size));
                }
            }
            NamedPipeStatus::Offline => return None,
            NamedPipeStatus::Free => unreachable!(),
        }

        let mut n = 0;
        // loop {
        unsafe {
            let r = FileSystem::ReadFile(
                self.as_raw_windows(),
                Some(self.buffer.as_mut_ptr().add(self.message_size) as _),
                (self.buffer.capacity() - self.message_size) as _,
                Some(&mut n),
                Some(self.overlapped.as_mut()),
            );

            if r.as_bool() {
                // self.message_size += n as usize;
                //
                // let pipe_msg = mem::replace(&mut self.buffer, Vec::with_capacity(64));
                // let msg_size = mem::replace(&mut self.message_size, 0);
                //
                // break Some((pipe_msg, msg_size));

                self.status = NamedPipeStatus::Pending;
                return None;
            }

            match Foundation::GetLastError() {
                Foundation::ERROR_MORE_DATA => {
                    // self.message_size += n as usize;
                    // self.buffer.reserve(self.buffer.capacity() * 2);

                    self.status = NamedPipeStatus::Pending;
                    None
                }
                Foundation::ERROR_IO_PENDING | Foundation::ERROR_IO_INCOMPLETE => {
                    self.status = NamedPipeStatus::Pending;
                    None
                }
                err @ _ => {
                    log::error!("ReadFile: {:?}", err);
                    self.status = NamedPipeStatus::Offline;
                    None
                }
            }
        }
        // }
    }
}

impl Drop for NamedPipe {
    fn drop(&mut self) {
        unsafe {
            let _ = Pipes::DisconnectNamedPipe(self.as_raw_windows());
        }
    }
}

impl Deref for NamedPipe {
    type Target = Handle;

    fn deref(&self) -> &Self::Target {
        &self.handle
    }
}

pub(crate) struct IoMultiplexing {
    completion_port: Handle,
    waker: usize,
    sa: SecurityAttr,
}

impl IoMultiplexing {
    pub fn new() -> Self {
        unsafe {
            let completion_port: Foundation::HANDLE =
                IO::CreateIoCompletionPort(Foundation::INVALID_HANDLE_VALUE, None, 0, 0)
                    .expect("CreateIoCompletionPort failed");
            let completion_port = OwnedHandle::from_raw_handle(completion_port.0 as _);

            Self {
                completion_port: Handle(completion_port),
                waker: 0, // We use 0 as waker key, because a valid handle will never be 0
                sa: SecurityAttr::allow_everyone().unwrap(),
            }
        }
    }

    fn create_instance(&self, identifier: &HSTRING) -> NamedPipe {
        unsafe {
            let pipe_handle = Pipes::CreateNamedPipeW(
                identifier,
                FileSystem::PIPE_ACCESS_INBOUND | FileSystem::FILE_FLAG_OVERLAPPED,
                Pipes::PIPE_TYPE_MESSAGE | Pipes::PIPE_READMODE_MESSAGE | Pipes::PIPE_WAIT,
                Pipes::PIPE_UNLIMITED_INSTANCES,
                pipe::DEFAULT_BUFFER_SIZE,
                pipe::DEFAULT_BUFFER_SIZE,
                0,
                Some(self.sa.attr()),
            );

            let pipe_handle: OwnedHandle = HandleOrInvalid::from_raw_handle(pipe_handle.0 as _)
                .try_into()
                .expect("CreateNamedPipeW");

            let pipe = NamedPipe::new(Handle(pipe_handle), NamedPipeStatus::Free);

            self.register_handle(&pipe);

            pipe
        }
    }

    fn register_handle(&self, handle: &Handle) {
        unsafe {
            let _ = IO::CreateIoCompletionPort(
                handle.as_raw_windows(),
                self.completion_port.as_raw_windows(),
                handle.as_raw() as _,
                0,
            )
            .expect("CreateIoCompletionPort failed");
        }
    }

    fn wait(&self, key: &mut usize, n: &mut u32, timeout: Option<Duration>) -> bool {
        let mut overlapped = ptr::null_mut();

        unsafe {
            IO::GetQueuedCompletionStatus(
                self.completion_port.as_raw_windows(),
                n,
                key,
                &mut overlapped,
                timeout
                    .map(|timeout| timeout.as_millis() as _)
                    .unwrap_or(Threading::INFINITE),
            )
            .as_bool()
        }
    }

    pub fn wake(&self) {
        unsafe {
            let r = IO::PostQueuedCompletionStatus(
                self.completion_port.as_raw_windows(),
                0,
                self.waker,
                None,
            );
            assert!(r.as_bool());
        }
    }
}