pub struct ServiceStatusHandle(/* private fields */);
Expand description
A struct that holds a unique token for updating the status of the corresponding service.
Implementations§
source§impl ServiceStatusHandle
impl ServiceStatusHandle
sourcepub fn set_service_status(&self, service_status: ServiceStatus) -> Result<()>
pub fn set_service_status(&self, service_status: ServiceStatus) -> Result<()>
Report the new service status to the system.
Examples found in repository?
examples/ping_service.rs (lines 105-113)
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
pub fn run_service() -> Result<()> {
// Create a channel to be able to poll a stop event from the service worker loop.
let (shutdown_tx, shutdown_rx) = mpsc::channel();
// Define system service event handler that will be receiving service events.
let event_handler = move |control_event| -> ServiceControlHandlerResult {
match control_event {
// Notifies a service to report its current status information to the service
// control manager. Always return NoError even if not implemented.
ServiceControl::Interrogate => ServiceControlHandlerResult::NoError,
// Handle stop
ServiceControl::Stop => {
shutdown_tx.send(()).unwrap();
ServiceControlHandlerResult::NoError
}
// treat the UserEvent as a stop request
ServiceControl::UserEvent(code) => {
if code.to_raw() == 130 {
shutdown_tx.send(()).unwrap();
}
ServiceControlHandlerResult::NoError
}
_ => ServiceControlHandlerResult::NotImplemented,
}
};
// Register system service event handler.
// The returned status handle should be used to report service status changes to the system.
let status_handle = service_control_handler::register(SERVICE_NAME, event_handler)?;
// Tell the system that service is running
status_handle.set_service_status(ServiceStatus {
service_type: SERVICE_TYPE,
current_state: ServiceState::Running,
controls_accepted: ServiceControlAccept::STOP,
exit_code: ServiceExitCode::Win32(0),
checkpoint: 0,
wait_hint: Duration::default(),
process_id: None,
})?;
// For demo purposes this service sends a UDP packet once a second.
let loopback_ip = IpAddr::from(LOOPBACK_ADDR);
let sender_addr = SocketAddr::new(loopback_ip, 0);
let receiver_addr = SocketAddr::new(loopback_ip, RECEIVER_PORT);
let msg = PING_MESSAGE.as_bytes();
let socket = UdpSocket::bind(sender_addr).unwrap();
loop {
let _ = socket.send_to(msg, receiver_addr);
// Poll shutdown event.
match shutdown_rx.recv_timeout(Duration::from_secs(1)) {
// Break the loop either upon stop or channel disconnect
Ok(_) | Err(mpsc::RecvTimeoutError::Disconnected) => break,
// Continue work if no events were received within the timeout
Err(mpsc::RecvTimeoutError::Timeout) => (),
};
}
// Tell the system that service has stopped.
status_handle.set_service_status(ServiceStatus {
service_type: SERVICE_TYPE,
current_state: ServiceState::Stopped,
controls_accepted: ServiceControlAccept::empty(),
exit_code: ServiceExitCode::Win32(0),
checkpoint: 0,
wait_hint: Duration::default(),
process_id: None,
})?;
Ok(())
}
Trait Implementations§
source§impl AsRawHandle for ServiceStatusHandle
impl AsRawHandle for ServiceStatusHandle
source§fn as_raw_handle(&self) -> RawHandle
fn as_raw_handle(&self) -> RawHandle
Get access to the raw handle to use in other Windows APIs
source§impl Clone for ServiceStatusHandle
impl Clone for ServiceStatusHandle
source§fn clone(&self) -> ServiceStatusHandle
fn clone(&self) -> ServiceStatusHandle
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moresource§impl Debug for ServiceStatusHandle
impl Debug for ServiceStatusHandle
impl Copy for ServiceStatusHandle
impl Send for ServiceStatusHandle
impl Sync for ServiceStatusHandle
Auto Trait Implementations§
impl Freeze for ServiceStatusHandle
impl RefUnwindSafe for ServiceStatusHandle
impl Unpin for ServiceStatusHandle
impl UnwindSafe for ServiceStatusHandle
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more