ugly_smart_lib/
net_pub.rs1use super::smart_pub::{MpscAsyncSender, StdArc, StdResult};
12
13pub const SERVER_KEY: &'static str = "Server";
14pub const CLIENT_KEY: &'static str = "Client";
15
16#[derive(Debug)]
18pub enum EConnectionState<T> {
19 Connecting,
21 Running(MpscAsyncSender<T>),
23 Interrupt,
26 Stoped,
28}
29
30impl Default for EConnectionState<Vec<u8>> {
31 fn default() -> Self {
32 EConnectionState::Stoped
33 }
34}
35
36impl Default for EConnectionState<(String, Vec<u8>)> {
37 fn default() -> Self {
38 EConnectionState::Stoped
39 }
40}
41
42#[derive(Clone, Copy, PartialEq)]
44pub enum ServiceType {
45 Server,
47 Client,
49}
50
51impl Default for ServiceType {
52 fn default() -> Self {
53 Self::Client
54 }
55}
56
57impl From<i32> for ServiceType {
58 fn from(value: i32) -> Self {
59 match value {
60 0 => ServiceType::Server,
61 1 => ServiceType::Client,
62 _ => Self::Client,
63 }
64 }
65}
66
67impl From<String> for ServiceType {
68 fn from(value: String) -> Self {
69 match &value as &str {
70 SERVER_KEY => ServiceType::Server,
71 CLIENT_KEY => ServiceType::Client,
72 _ => ServiceType::Client,
73 }
74 }
75}
76
77impl std::fmt::Display for ServiceType {
78 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
79 match self {
80 Self::Server => {
81 write!(f, "{}", SERVER_KEY)
82 }
83 Self::Client => {
84 write!(f, "{}", CLIENT_KEY)
85 }
86 }
87 }
88}
89
90impl std::fmt::Debug for ServiceType {
91 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
92 match self {
93 Self::Server => {
94 write!(f, "{}", SERVER_KEY)
95 }
96 Self::Client => {
97 write!(f, "{}", CLIENT_KEY)
98 }
99 }
100 }
101}
102
103pub trait INetCallBack<T>
105where
106 Self: Send + Sync,
107{
108 fn data_callback(self: StdArc<Self>, data: Vec<u8>, handle: String, serv_type: ServiceType);
119
120 fn state_callback(
131 self: StdArc<Self>,
132 handle: String,
134 state: EConnectionState<T>,
135 serv_type: ServiceType,
136 );
137}
138
139impl std::fmt::Display for dyn INetCallBack<Vec<u8>> {
140 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
141 write!(f, "CallBackTrait Display")
142 }
143}
144
145impl std::fmt::Debug for dyn INetCallBack<Vec<u8>> {
146 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
147 write!(f, "CallBackTrait Debug")
148 }
149}
150
151impl std::fmt::Display for dyn INetCallBack<(String, Vec<u8>)> {
152 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
153 write!(f, "CallBackTrait Display")
154 }
155}
156
157impl std::fmt::Debug for dyn INetCallBack<(String, Vec<u8>)> {
158 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
159 write!(f, "CallBackTrait Debug")
160 }
161}
162
163pub trait INetwork {
165 fn start(self: StdArc<Self>) -> impl std::future::Future<Output = StdResult<()>> + Send;
175
176 fn stop(self: StdArc<Self>) -> impl std::future::Future<Output = StdResult<()>> + Send;
184}
185
186pub fn spawn_and_log_error<F>(fut: F) -> tokio::task::JoinHandle<()>
218where
219 F: std::future::Future<Output = StdResult<()>> + Send + 'static,
220{
221 tokio::spawn(async move {
222 if let Err(e) = fut.await {
223 tracing::error!(e)
224 }
225 })
226}