UserEventCode

Struct UserEventCode 

Source
pub struct UserEventCode(/* private fields */);
Expand description

Struct describing a user-defined control code (128 to 255)

Implementations§

Source§

impl UserEventCode

Source

pub const unsafe fn from_unchecked(raw: u32) -> Self

Mainly for declaring user events as constants:

const MY_EVENT: UserEventCode = unsafe { UserEventCode::from_unchecked(130) };
§Safety

raw should be a valid user control code in the range of 128 to 255.

Examples found in repository?
examples/notify_service.rs (line 28)
28    const NO_OP: UserEventCode = unsafe { UserEventCode::from_unchecked(128) };
29    const CUSTOM_STOP: UserEventCode = unsafe { UserEventCode::from_unchecked(130) };
Source

pub fn from_raw(raw: u32) -> Result<UserEventCode, ParseRawError>

Source

pub fn to_raw(&self) -> u32

Examples found in repository?
examples/ping_service.rs (line 90)
71    pub fn run_service() -> Result<()> {
72        // Create a channel to be able to poll a stop event from the service worker loop.
73        let (shutdown_tx, shutdown_rx) = mpsc::channel();
74
75        // Define system service event handler that will be receiving service events.
76        let event_handler = move |control_event| -> ServiceControlHandlerResult {
77            match control_event {
78                // Notifies a service to report its current status information to the service
79                // control manager. Always return NoError even if not implemented.
80                ServiceControl::Interrogate => ServiceControlHandlerResult::NoError,
81
82                // Handle stop
83                ServiceControl::Stop => {
84                    shutdown_tx.send(()).unwrap();
85                    ServiceControlHandlerResult::NoError
86                }
87
88                // treat the UserEvent as a stop request
89                ServiceControl::UserEvent(code) => {
90                    if code.to_raw() == 130 {
91                        shutdown_tx.send(()).unwrap();
92                    }
93                    ServiceControlHandlerResult::NoError
94                }
95
96                _ => ServiceControlHandlerResult::NotImplemented,
97            }
98        };
99
100        // Register system service event handler.
101        // The returned status handle should be used to report service status changes to the system.
102        let status_handle = service_control_handler::register(SERVICE_NAME, event_handler)?;
103
104        // Tell the system that service is running
105        status_handle.set_service_status(ServiceStatus {
106            service_type: SERVICE_TYPE,
107            current_state: ServiceState::Running,
108            controls_accepted: ServiceControlAccept::STOP,
109            exit_code: ServiceExitCode::Win32(0),
110            checkpoint: 0,
111            wait_hint: Duration::default(),
112            process_id: None,
113        })?;
114
115        // For demo purposes this service sends a UDP packet once a second.
116        let loopback_ip = IpAddr::from(LOOPBACK_ADDR);
117        let sender_addr = SocketAddr::new(loopback_ip, 0);
118        let receiver_addr = SocketAddr::new(loopback_ip, RECEIVER_PORT);
119        let msg = PING_MESSAGE.as_bytes();
120        let socket = UdpSocket::bind(sender_addr).unwrap();
121
122        loop {
123            let _ = socket.send_to(msg, receiver_addr);
124
125            // Poll shutdown event.
126            match shutdown_rx.recv_timeout(Duration::from_secs(1)) {
127                // Break the loop either upon stop or channel disconnect
128                Ok(_) | Err(mpsc::RecvTimeoutError::Disconnected) => break,
129
130                // Continue work if no events were received within the timeout
131                Err(mpsc::RecvTimeoutError::Timeout) => (),
132            };
133        }
134
135        // Tell the system that service has stopped.
136        status_handle.set_service_status(ServiceStatus {
137            service_type: SERVICE_TYPE,
138            current_state: ServiceState::Stopped,
139            controls_accepted: ServiceControlAccept::empty(),
140            exit_code: ServiceExitCode::Win32(0),
141            checkpoint: 0,
142            wait_hint: Duration::default(),
143            process_id: None,
144        })?;
145
146        Ok(())
147    }

Trait Implementations§

Source§

impl Clone for UserEventCode

Source§

fn clone(&self) -> UserEventCode

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for UserEventCode

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Hash for UserEventCode

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for UserEventCode

Source§

fn eq(&self, other: &UserEventCode) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for UserEventCode

Source§

impl Eq for UserEventCode

Source§

impl StructuralPartialEq for UserEventCode

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.