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