| 1 |
|
| 2 |
|
| 3 |
use polling::os::iocp::PollerIocpExt; |
| 4 |
use polling::{Event, PollMode, Poller}; |
| 5 |
use std::fmt; |
| 6 |
use std::io::Result; |
| 7 |
use std::os::windows::io::{ |
| 8 |
AsRawHandle, AsRawSocket, BorrowedHandle, BorrowedSocket, RawHandle, RawSocket, |
| 9 |
}; |
| 10 |
|
| 11 |
|
| 12 |
#[doc(hidden)] |
| 13 |
pub enum Registration { |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
Socket(RawSocket), |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
Handle(RawHandle), |
| 29 |
} |
| 30 |
|
| 31 |
unsafe impl Send for Registration {} |
| 32 |
unsafe impl Sync for Registration {} |
| 33 |
|
| 34 |
impl fmt::Debug for Registration { |
| 35 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 36 |
match self { |
| 37 |
Self::Socket(raw) => fmt::Debug::fmt(raw, f), |
| 38 |
Self::Handle(handle) => fmt::Debug::fmt(handle, f), |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
impl Registration { |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
pub(crate) unsafe fn new(f: BorrowedSocket<'_>) -> Self { |
| 50 |
Self::Socket(f.as_raw_socket()) |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
pub(crate) unsafe fn new_waitable(f: BorrowedHandle<'_>) -> Self { |
| 59 |
Self::Handle(f.as_raw_handle()) |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
#[inline] |
| 64 |
pub(crate) fn add(&self, poller: &Poller, token: usize) -> Result<()> { |
| 65 |
|
| 66 |
unsafe { |
| 67 |
match self { |
| 68 |
Self::Socket(raw) => poller.add(*raw, Event::none(token)), |
| 69 |
Self::Handle(handle) => { |
| 70 |
poller.add_waitable(*handle, Event::none(token), PollMode::Oneshot) |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
#[inline] |
| 78 |
pub(crate) fn modify(&self, poller: &Poller, interest: Event) -> Result<()> { |
| 79 |
|
| 80 |
match self { |
| 81 |
Self::Socket(raw) => { |
| 82 |
poller.modify(unsafe { BorrowedSocket::borrow_raw(*raw) }, interest) |
| 83 |
} |
| 84 |
Self::Handle(handle) => poller.modify_waitable( |
| 85 |
unsafe { BorrowedHandle::borrow_raw(*handle) }, |
| 86 |
interest, |
| 87 |
PollMode::Oneshot, |
| 88 |
), |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
#[inline] |
| 94 |
pub(crate) fn delete(&self, poller: &Poller) -> Result<()> { |
| 95 |
|
| 96 |
match self { |
| 97 |
Self::Socket(raw) => poller.delete(unsafe { BorrowedSocket::borrow_raw(*raw) }), |
| 98 |
Self::Handle(handle) => { |
| 99 |
poller.remove_waitable(unsafe { BorrowedHandle::borrow_raw(*handle) }) |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
} |