syslog_rs/a_sync/
async_socket.rs

1/*-
2 * syslog-rs - a syslog client translated from libc to rust
3 * 
4 * Copyright 2025 Aleksandr Morozov
5 * 
6 * The syslog-rs crate can be redistributed and/or modified
7 * under the terms of either of the following licenses:
8 *
9 *   1. the Mozilla Public License Version 2.0 (the “MPL”) OR
10 *                     
11 *   2. EUROPEAN UNION PUBLIC LICENCE v. 1.2 EUPL © the European Union 2007, 2016
12 */
13
14use std::fmt;
15
16use crate::{error::SyRes, AsyncSyslogDestination, TapType};
17
18/// A trait which implements the necessary functions for syslog internals.
19/// It `taps` the connection and provides the functionality for the syslog client
20/// to use the communication channel.
21#[allow(async_fn_in_trait)]
22pub trait AsyncSyslogTap<D: AsyncSyslogDestination + fmt::Display>: fmt::Debug + Send
23{
24    /// Proves the new instance of the syslog communication.
25    /// 
26    /// # Arguments
27    /// 
28    /// `req_tap` - a connection endpoint setup information i.e address, path...
29    /// 
30    /// # Returns
31    /// 
32    /// In case of any error a [SyRes] i.e [Result::Err] should be returned.
33    fn new(req_tap: D) -> SyRes<Self> where Self: Sized;
34
35    /// Opens a connection to target.
36    async fn connectlog(&mut self) -> SyRes<()>;
37
38    /// Writes provided data to socket
39    async fn send(&mut self, msg: &[u8]) -> std::io::Result<usize>;
40
41    /// Disconnects the connection.
42    async fn disconnectlog(&mut self) -> std::io::Result<()>;
43
44    /// Tells if connection was opened.
45    fn is_connected(&self) -> bool;
46
47    /// Returns the exact type of the connection.
48    fn get_type(&self) -> TapType;
49
50    /// Returns (in bytes) the maximum message size (including headers).
51    fn get_max_msg_size() -> usize;
52
53    /// Updates the connection information without breaking tap.
54    fn update_tap_data(&mut self, tap_data: D);
55}
56
57/// A structure which holds a socket instance and all necessary information
58/// which can be used to reestablish the connection. This is for async only.
59#[derive(Debug)]
60pub struct AsyncTap<T, D: AsyncSyslogDestination>
61{
62    /// Channel
63    pub(crate) sock: Option<T>,
64
65    /// Tap settings and state
66    pub(crate) tap_data: D,
67
68    pub(crate) cur_tap_type: TapType,
69}
70
71/// An implementation for the external executor setup.
72#[cfg(feature = "build_async_interface")]
73impl<T, D: AsyncSyslogDestination> AsyncTap<T, D>
74{
75    /// Creates the new socket instance which is `tapped`.
76    pub 
77    fn new(req_tap: D) -> SyRes<AsyncTap<T, D>>
78    {
79        return Ok(
80            Self
81            {
82                sock: None, 
83                tap_data: req_tap,
84                cur_tap_type: TapType::None,
85            }
86        );
87    }
88
89    /// Returns the information about the `tap` connection.
90    pub 
91    fn get_tap_data(&self) -> &D
92    {
93        return &self.tap_data;
94    }
95
96    /// Updates the current connection information on the intances records. It
97    /// does not performs reconnect or whatever IO.
98    pub 
99    fn update_tap_data(&mut self, tap_data: D)
100    {
101        self.tap_data = tap_data;
102    }
103
104    /// Returns the connection type identification.
105    pub 
106    fn get_tap_type(&self) -> TapType
107    {
108        return self.cur_tap_type;
109    }
110
111    /// Returns the access to connection.
112    pub 
113    fn get_sock(&self) -> Option<&T>
114    {
115        return self.sock.as_ref();
116    }
117
118    /// Returns the mutable access to connection.
119    pub 
120    fn get_sock_mut(&mut self) -> Option<&mut T>
121    {
122        return self.sock.as_mut();
123    }
124
125    /// Consumes the connections and returns to caller.
126    pub 
127    fn take_sock(&mut self) -> Option<T>
128    {
129        return self.sock.take();
130    }
131
132    /// Sets the connection socket of type `T`.
133    pub 
134    fn set_sock(&mut self, sock: T)
135    {
136        self.sock.replace(sock);
137
138        return;
139    }
140
141    /// Sets the type of the connection.
142    pub 
143    fn set_cur_tap_type(&mut self, cur_tap_type: TapType)
144    {
145        return self.cur_tap_type = cur_tap_type;
146    }
147}