msgtrans 1.0.10

Support for a variety of communication protocols such as TCP / QUIC / WebSocket, easy to create server and client network library.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
use crate::{error::TransportError, packet::Packet, SessionId};
use tokio::sync::oneshot;

/// Unified abstraction for transport layer commands
#[derive(Debug)]
pub enum TransportCommand {
    /// Send packet
    Send {
        session_id: SessionId,
        packet: Packet,
        response_tx: oneshot::Sender<Result<(), TransportError>>,
    },

    /// Close connection
    Close {
        session_id: SessionId,
        response_tx: oneshot::Sender<Result<(), TransportError>>,
    },

    /// Configuration update
    Configure {
        config: ConfigUpdate,
        response_tx: oneshot::Sender<Result<(), TransportError>>,
    },

    /// Get statistics information
    GetStats {
        response_tx: oneshot::Sender<TransportStats>,
    },

    /// Get connection information
    GetConnectionInfo {
        session_id: SessionId,
        response_tx: oneshot::Sender<Result<ConnectionInfo, TransportError>>,
    },

    /// Get all active sessions
    GetActiveSessions {
        response_tx: oneshot::Sender<Vec<SessionId>>,
    },

    /// Force disconnect session
    ForceDisconnect {
        session_id: SessionId,
        reason: String,
        response_tx: oneshot::Sender<Result<(), TransportError>>,
    },

    /// Pause session
    PauseSession {
        session_id: SessionId,
        response_tx: oneshot::Sender<Result<(), TransportError>>,
    },

    /// Resume session
    ResumeSession {
        session_id: SessionId,
        response_tx: oneshot::Sender<Result<(), TransportError>>,
    },
}

/// Configuration update types
#[derive(Debug)]
pub enum ConfigUpdate {
    /// Set buffer size
    BufferSize(usize),
    /// Set timeout duration
    Timeout(std::time::Duration),
    /// Set maximum connections
    MaxConnections(usize),
    /// Protocol-specific configuration
    Protocol(Box<dyn std::any::Any + Send>),
}

/// Transport statistics information
#[derive(Debug, Clone)]
pub struct TransportStats {
    /// Total packets sent
    pub packets_sent: u64,
    /// Total packets received
    pub packets_received: u64,
    /// Total bytes sent
    pub bytes_sent: u64,
    /// Total bytes received
    pub bytes_received: u64,
    /// Active connections
    pub active_connections: u64,
    /// Total connections (historical)
    pub total_connections: u64,
    /// Error count
    pub errors: u64,
    /// Start time
    pub start_time: std::time::SystemTime,
    /// Last activity time
    pub last_activity: std::time::SystemTime,
}

impl Default for TransportStats {
    fn default() -> Self {
        let now = std::time::SystemTime::now();
        Self {
            packets_sent: 0,
            packets_received: 0,
            bytes_sent: 0,
            bytes_received: 0,
            active_connections: 0,
            total_connections: 0,
            errors: 0,
            start_time: now,
            last_activity: now,
        }
    }
}

impl TransportStats {
    pub fn new() -> Self {
        Default::default()
    }

    pub fn update_activity(&mut self) {
        self.last_activity = std::time::SystemTime::now();
    }

    pub fn record_packet_sent(&mut self, size: usize) {
        self.packets_sent += 1;
        self.bytes_sent += size as u64;
        self.update_activity();
    }

    pub fn record_packet_received(&mut self, size: usize) {
        self.packets_received += 1;
        self.bytes_received += size as u64;
        self.update_activity();
    }

    pub fn record_connection_opened(&mut self) {
        self.active_connections += 1;
        self.total_connections += 1;
        self.update_activity();
    }

    pub fn record_connection_closed(&mut self) {
        if self.active_connections > 0 {
            self.active_connections -= 1;
        }
        self.update_activity();
    }

    pub fn record_error(&mut self) {
        self.errors += 1;
        self.update_activity();
    }

    pub fn uptime(&self) -> std::time::Duration {
        self.start_time.elapsed().unwrap_or_default()
    }

    pub fn idle_time(&self) -> std::time::Duration {
        self.last_activity.elapsed().unwrap_or_default()
    }
}

/// Connection information
#[derive(Debug, Clone)]
pub struct ConnectionInfo {
    /// Session ID
    pub session_id: SessionId,
    /// Local address
    pub local_addr: std::net::SocketAddr,
    /// Remote address
    pub peer_addr: std::net::SocketAddr,
    /// Protocol type
    pub protocol: String,
    /// Connection state
    pub state: ConnectionState,
    /// Established time
    pub established_at: std::time::SystemTime,
    /// Closed time
    pub closed_at: Option<std::time::SystemTime>,
    /// Last activity time
    pub last_activity: std::time::SystemTime,
    /// Packets sent count
    pub packets_sent: u64,
    /// Packets received count
    pub packets_received: u64,
    /// Bytes sent count
    pub bytes_sent: u64,
    /// Bytes received count
    pub bytes_received: u64,
}

impl Default for ConnectionInfo {
    fn default() -> Self {
        let now = std::time::SystemTime::now();
        Self {
            session_id: SessionId::new(0),
            local_addr: "0.0.0.0:0".parse().unwrap(),
            peer_addr: "0.0.0.0:0".parse().unwrap(),
            protocol: "tcp".to_string(),
            state: ConnectionState::Connecting,
            established_at: now,
            closed_at: None,
            last_activity: now,
            packets_sent: 0,
            packets_received: 0,
            bytes_sent: 0,
            bytes_received: 0,
        }
    }
}

impl ConnectionInfo {
    pub fn update_activity(&mut self) {
        self.last_activity = std::time::SystemTime::now();
    }

    pub fn record_packet_sent(&mut self, size: usize) {
        self.packets_sent += 1;
        self.bytes_sent += size as u64;
        self.update_activity();
    }

    pub fn record_packet_received(&mut self, size: usize) {
        self.packets_received += 1;
        self.bytes_received += size as u64;
        self.update_activity();
    }

    pub fn connection_duration(&self) -> std::time::Duration {
        self.established_at.elapsed().unwrap_or_default()
    }

    pub fn idle_duration(&self) -> std::time::Duration {
        self.last_activity.elapsed().unwrap_or_default()
    }
}

/// Connection state
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ConnectionState {
    /// Connecting
    Connecting,
    /// Connected
    Connected,
    /// Closing
    Closing,
    /// Closed
    Closed,
    /// Paused
    Paused,
    /// Error state
    Error,
}

impl std::fmt::Display for ConnectionState {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ConnectionState::Connecting => write!(f, "Connecting"),
            ConnectionState::Connected => write!(f, "Connected"),
            ConnectionState::Closing => write!(f, "Closing"),
            ConnectionState::Closed => write!(f, "Closed"),
            ConnectionState::Paused => write!(f, "Paused"),
            ConnectionState::Error => write!(f, "Error"),
        }
    }
}

/// Protocol-specific command trait
///
/// This trait allows each protocol to define its own specific command types
pub trait ProtocolCommand: Send + std::fmt::Debug + 'static {
    type Response: Send;

    /// Convert to generic transport command
    fn into_transport_command(self) -> TransportCommand;

    /// Execute command
    fn execute(self) -> impl std::future::Future<Output = Self::Response> + Send;
}

/// Command builder
pub struct CommandBuilder;

impl CommandBuilder {
    /// Create send command
    pub fn send(
        session_id: SessionId,
        packet: Packet,
    ) -> (
        TransportCommand,
        oneshot::Receiver<Result<(), TransportError>>,
    ) {
        let (response_tx, response_rx) = oneshot::channel();
        let command = TransportCommand::Send {
            session_id,
            packet,
            response_tx,
        };
        (command, response_rx)
    }

    /// Create close command
    pub fn close(
        session_id: SessionId,
    ) -> (
        TransportCommand,
        oneshot::Receiver<Result<(), TransportError>>,
    ) {
        let (response_tx, response_rx) = oneshot::channel();
        let command = TransportCommand::Close {
            session_id,
            response_tx,
        };
        (command, response_rx)
    }

    /// Create get statistics command
    pub fn get_stats() -> (TransportCommand, oneshot::Receiver<TransportStats>) {
        let (response_tx, response_rx) = oneshot::channel();
        let command = TransportCommand::GetStats { response_tx };
        (command, response_rx)
    }

    /// Create get connection info command
    pub fn get_connection_info(
        session_id: SessionId,
    ) -> (
        TransportCommand,
        oneshot::Receiver<Result<ConnectionInfo, TransportError>>,
    ) {
        let (response_tx, response_rx) = oneshot::channel();
        let command = TransportCommand::GetConnectionInfo {
            session_id,
            response_tx,
        };
        (command, response_rx)
    }

    /// Create get active sessions command
    pub fn get_active_sessions() -> (TransportCommand, oneshot::Receiver<Vec<SessionId>>) {
        let (response_tx, response_rx) = oneshot::channel();
        let command = TransportCommand::GetActiveSessions { response_tx };
        (command, response_rx)
    }

    /// Create force disconnect command
    pub fn force_disconnect(
        session_id: SessionId,
        reason: String,
    ) -> (
        TransportCommand,
        oneshot::Receiver<Result<(), TransportError>>,
    ) {
        let (response_tx, response_rx) = oneshot::channel();
        let command = TransportCommand::ForceDisconnect {
            session_id,
            reason,
            response_tx,
        };
        (command, response_rx)
    }
}

/// Command executor
pub struct CommandExecutor;

impl CommandExecutor {
    /// Execute send command and wait for result
    pub async fn send_and_wait(
        command_tx: &tokio::sync::mpsc::Sender<TransportCommand>,
        session_id: SessionId,
        packet: Packet,
    ) -> Result<(), TransportError> {
        let (command, response_rx) = CommandBuilder::send(session_id, packet);

        command_tx
            .send(command)
            .await
            .map_err(|_| TransportError::connection_error("Channel closed", false))?;

        response_rx
            .await
            .map_err(|_| TransportError::connection_error("Channel closed", false))?
    }

    /// Close connection and wait for result
    pub async fn close_and_wait(
        command_tx: &tokio::sync::mpsc::Sender<TransportCommand>,
        session_id: SessionId,
    ) -> Result<(), TransportError> {
        let (command, response_rx) = CommandBuilder::close(session_id);

        command_tx
            .send(command)
            .await
            .map_err(|_| TransportError::connection_error("Channel closed", false))?;

        response_rx
            .await
            .map_err(|_| TransportError::connection_error("Channel closed", false))?
    }

    /// Get statistics information
    pub async fn get_stats(
        command_tx: &tokio::sync::mpsc::Sender<TransportCommand>,
    ) -> Result<TransportStats, TransportError> {
        let (command, response_rx) = CommandBuilder::get_stats();

        command_tx
            .send(command)
            .await
            .map_err(|_| TransportError::connection_error("Channel closed", false))?;

        response_rx
            .await
            .map_err(|_| TransportError::connection_error("Channel closed", false))
    }
}