flare-core 1.0.0

A high-performance, reliable long-connection communication toolkit for Rust, supporting WebSocket and QUIC protocols with features like authentication, device management, serialization negotiation, and protocol racing.
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
use super::*;

#[async_trait]
impl ConnectionManagerTrait for ConnectionManager {
    fn as_any(&self) -> &dyn std::any::Any {
        self
    }

    async fn add_connection(
        &self,
        connection_id: String,
        connection: Arc<Mutex<Box<dyn Connection>>>,
        user_id: Option<String>,
    ) -> Result<()> {
        // 注意:trait 方法不能直接传递 requires_auth,我们需要从 ServerCore 获取
        // 但这里我们暂时使用 true(需要认证),实际值应该在调用时通过 ServerCore 的 auth_enabled() 获取
        // 由于 ConnectionManager 不知道 ServerCore,我们暂时使用 true
        // 实际应用中,连接会在 CONNECT 消息处理时被标记为已验证
        let requires_auth = true; // 默认需要认证,如果不需要认证,连接会在 CONNECT 消息处理时被标记为已验证

        // 将 Arc<Mutex<Box<dyn Connection>>> 转换为 Box<dyn Connection>
        // 注意:这需要从 Arc 中取出,但 Arc 可能被多个地方引用
        // 对于默认实现,我们需要一个不同的方式
        // 由于 ConnectionManager 内部使用 Arc<Mutex<Box<dyn Connection>>>,
        // 我们需要保持一致性
        self.reserve_connection_slot(usize::MAX)?;

        let mut shard = match self.connection_shard(&connection_id).write() {
            Ok(shard) => shard,
            Err(_) => {
                self.release_connection_slot();
                return Err(FlareError::general_error("Failed to lock connection shard"));
            }
        };

        if shard.contains_key(&connection_id) {
            self.release_connection_slot();
            return Err(FlareError::protocol_error(format!(
                "Connection {} already exists",
                connection_id
            )));
        }

        let mut info = ConnectionInfo::new(connection_id.clone(), requires_auth);
        info.user_id = user_id.clone();

        let entry = self.new_connection_entry(&connection_id, Arc::clone(&connection), info);
        shard.insert(connection_id.clone(), entry);

        // 如果提供了用户 ID,添加到用户连接映射
        if let Some(user_id) = user_id
            && let Err(err) = self.insert_user_connection(user_id, &connection_id)
        {
            shard.remove(&connection_id);
            self.release_connection_slot();
            return Err(err);
        }

        Ok(())
    }

    async fn remove_connection(&self, connection_id: &str) -> Result<()> {
        ConnectionManager::remove_connection(self, connection_id)
    }

    async fn get_connection(
        &self,
        connection_id: &str,
    ) -> Option<(
        Arc<Mutex<Box<dyn Connection>>>,
        crate::server::connection::r#trait::ConnectionInfo,
    )> {
        ConnectionManager::get_connection(self, connection_id).map(|(conn, info)| {
            // 转换 ConnectionInfo 格式(从 Instant 转换为 Unix 时间戳)
            let now = std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap_or_default()
                .as_secs();
            let created_at_secs = now.saturating_sub(info.created_at.elapsed().as_secs());
            let last_active_secs = now.saturating_sub(info.last_active.elapsed().as_secs());

            let trait_info = crate::server::connection::r#trait::ConnectionInfo {
                connection_id: info.connection_id,
                user_id: info.user_id,
                created_at: created_at_secs,
                last_active: last_active_secs,
                metadata: info.metadata,
                device_info: info.device_info.clone(),
                serialization_format: info.serialization_format,
                compression: info.compression,
                encryption: info.encryption,
                authenticated: info.authenticated,
                authenticated_at: info.authenticated_at,
                negotiation_completed: info.negotiation_completed,
                negotiation_confirmed: info.negotiation_confirmed,
                cached_parser: info.cached_parser.clone(),
                cached_pipeline: info.cached_pipeline.clone(),
            };
            (conn, trait_info)
        })
    }

    async fn get_user_connections(&self, user_id: &str) -> Vec<String> {
        ConnectionManager::get_user_connections(self, user_id)
    }

    async fn bind_user(&self, connection_id: &str, user_id: String) -> Result<()> {
        ConnectionManager::bind_user(self, connection_id, user_id)
    }

    async fn update_connection_active(&self, connection_id: &str) -> Result<()> {
        ConnectionManager::update_connection_active(self, connection_id)
    }

    async fn set_connection_authenticated(
        &self,
        connection_id: &str,
        user_id: Option<String>,
    ) -> Result<()> {
        // ConnectionManager::set_connection_authenticated 是同步方法,直接调用
        ConnectionManager::set_connection_authenticated(self, connection_id, user_id)
    }

    async fn list_connections(&self) -> Vec<String> {
        ConnectionManager::list_connections(self)
    }

    async fn connection_count(&self) -> usize {
        ConnectionManager::connection_count(self)
    }

    fn connection_count_snapshot(&self) -> usize {
        ConnectionManager::connection_count(self)
    }

    fn user_count_snapshot(&self) -> usize {
        ConnectionManager::user_count(self)
    }

    async fn cleanup_timeout_connections(&self, timeout: Duration) -> Vec<String> {
        let timeout_connections = self.timeout_connection_snapshots(timeout);

        for (_, connection, _) in &timeout_connections {
            let mut conn = connection.lock().await;
            let _ = conn.close().await;
        }

        self.remove_connection_snapshots(
            timeout_connections
                .iter()
                .map(|(connection_id, _, _)| connection_id.clone()),
        )
    }

    async fn send_to_connection(&self, connection_id: &str, data: &[u8]) -> Result<()> {
        let (_, connection, _) = self.get_connection_snapshot(connection_id).ok_or_else(|| {
            FlareError::protocol_error(format!("Connection {} not found", connection_id))
        })?;

        self.send_to_connection_handle(connection_id, connection, data)
            .await
    }

    async fn send_to_user(&self, user_id: &str, data: &[u8]) -> Result<()> {
        let connections =
            self.connection_handles_for_ids(ConnectionManager::get_user_connections(self, user_id));

        stream::iter(connections)
            .for_each_concurrent(
                self.fanout_concurrency,
                |(connection_id, connection)| async move {
                    if let Err(e) = self
                        .send_to_connection_handle(&connection_id, connection, data)
                        .await
                    {
                        tracing::warn!("Failed to send to connection {}: {:?}", connection_id, e);
                    }
                },
            )
            .await;

        Ok(())
    }

    async fn broadcast(&self, data: &[u8]) -> Result<()> {
        let connections = self.connection_handles();

        stream::iter(connections)
            .for_each_concurrent(
                self.fanout_concurrency,
                |(connection_id, connection)| async move {
                    if let Err(e) = self
                        .send_to_connection_handle(&connection_id, connection, data)
                        .await
                    {
                        tracing::warn!(
                            "Failed to broadcast to connection {}: {:?}",
                            connection_id,
                            e
                        );
                    }
                },
            )
            .await;

        Ok(())
    }

    async fn broadcast_except(&self, data: &[u8], exclude_connection_id: &str) -> Result<()> {
        let connections = self.connection_handles_except(exclude_connection_id);

        stream::iter(connections)
            .for_each_concurrent(
                self.fanout_concurrency,
                |(connection_id, connection)| async move {
                    if let Err(e) = self
                        .send_to_connection_handle(&connection_id, connection, data)
                        .await
                    {
                        tracing::warn!(
                            "Failed to broadcast to connection {}: {:?}",
                            connection_id,
                            e
                        );
                    }
                },
            )
            .await;

        Ok(())
    }

    async fn send_frame_to(
        &self,
        connection_id: &str,
        frame: &crate::common::protocol::Frame,
        parser: Option<&crate::common::MessageParser>,
    ) -> Result<()> {
        let snapshot = self.get_connection_snapshot(connection_id).ok_or_else(|| {
            FlareError::connection_failed(format!("连接 {} 不存在", connection_id))
        })?;

        self.send_frame_to_snapshot(snapshot, frame, parser).await
    }

    async fn send_frame_to_user(
        &self,
        user_id: &str,
        frame: &crate::common::protocol::Frame,
        parser: Option<&crate::common::MessageParser>,
    ) -> Result<()> {
        let connection_ids = ConnectionManager::get_user_connections(self, user_id);

        if let Some(parser) = parser {
            let connections = self.connection_auth_snapshots_for_ids(connection_ids);
            let data = match parser.serialize(frame) {
                Ok(data) => data,
                Err(e) => {
                    tracing::warn!("Failed to serialize frame for user {}: {:?}", user_id, e);
                    return Ok(());
                }
            };

            let successful_ids = Arc::new(std::sync::Mutex::new(Vec::new()));
            stream::iter(connections)
                .for_each_concurrent(self.fanout_concurrency, |snapshot| {
                    let successful_ids = Arc::clone(&successful_ids);
                    let data = data.as_slice();
                    async move {
                        let connection_id = snapshot.0.clone();
                        let result = self
                            .send_serialized_frame_to_auth_snapshot_without_active(
                                snapshot, frame, data,
                            )
                            .await;
                        match result {
                            Ok(connection_id) => {
                                Self::record_successful_connection_id(
                                    &successful_ids,
                                    connection_id,
                                );
                            }
                            Err(e) => {
                                tracing::warn!(
                                    "Failed to send frame to connection {}: {:?}",
                                    connection_id,
                                    e
                                );
                            }
                        }
                    }
                })
                .await;
            self.update_connections_active(Self::take_successful_connection_ids(successful_ids));

            return Ok(());
        }

        let connections = self.connection_snapshots_for_ids(connection_ids);
        let successful_ids = Arc::new(std::sync::Mutex::new(Vec::new()));
        stream::iter(connections)
            .for_each_concurrent(self.fanout_concurrency, |snapshot| {
                let successful_ids = Arc::clone(&successful_ids);
                async move {
                    let connection_id = snapshot.0.clone();
                    let result = self
                        .send_frame_to_snapshot_without_active(snapshot, frame, parser)
                        .await;
                    match result {
                        Ok(connection_id) => {
                            Self::record_successful_connection_id(&successful_ids, connection_id);
                        }
                        Err(e) => {
                            tracing::warn!(
                                "Failed to send frame to connection {}: {:?}",
                                connection_id,
                                e
                            );
                        }
                    }
                }
            })
            .await;
        self.update_connections_active(Self::take_successful_connection_ids(successful_ids));

        Ok(())
    }

    async fn broadcast_frame(
        &self,
        frame: &crate::common::protocol::Frame,
        parser: Option<&crate::common::MessageParser>,
    ) -> Result<()> {
        if let Some(parser) = parser {
            let connections = self.connection_auth_snapshots();
            let data = match parser.serialize(frame) {
                Ok(data) => data,
                Err(e) => {
                    tracing::warn!("Failed to serialize broadcast frame: {:?}", e);
                    return Ok(());
                }
            };

            let successful_ids = Arc::new(std::sync::Mutex::new(Vec::new()));
            stream::iter(connections)
                .for_each_concurrent(self.fanout_concurrency, |snapshot| {
                    let successful_ids = Arc::clone(&successful_ids);
                    let data = data.as_slice();
                    async move {
                        let connection_id = snapshot.0.clone();
                        let result = self
                            .send_serialized_frame_to_auth_snapshot_without_active(
                                snapshot, frame, data,
                            )
                            .await;
                        match result {
                            Ok(connection_id) => {
                                Self::record_successful_connection_id(
                                    &successful_ids,
                                    connection_id,
                                );
                            }
                            Err(e) => {
                                tracing::warn!(
                                    "Failed to broadcast frame to connection {}: {:?}",
                                    connection_id,
                                    e
                                );
                            }
                        }
                    }
                })
                .await;
            self.update_connections_active(Self::take_successful_connection_ids(successful_ids));

            return Ok(());
        }

        let connections = self.connection_snapshots();
        let successful_ids = Arc::new(std::sync::Mutex::new(Vec::new()));
        stream::iter(connections)
            .for_each_concurrent(self.fanout_concurrency, |snapshot| {
                let successful_ids = Arc::clone(&successful_ids);
                async move {
                    let connection_id = snapshot.0.clone();
                    let result = self
                        .send_frame_to_snapshot_without_active(snapshot, frame, parser)
                        .await;
                    match result {
                        Ok(connection_id) => {
                            Self::record_successful_connection_id(&successful_ids, connection_id);
                        }
                        Err(e) => {
                            tracing::warn!(
                                "Failed to broadcast frame to connection {}: {:?}",
                                connection_id,
                                e
                            );
                        }
                    }
                }
            })
            .await;
        self.update_connections_active(Self::take_successful_connection_ids(successful_ids));

        Ok(())
    }

    async fn broadcast_frame_except(
        &self,
        frame: &crate::common::protocol::Frame,
        exclude_connection_id: &str,
        parser: Option<&crate::common::MessageParser>,
    ) -> Result<()> {
        if let Some(parser) = parser {
            let connections = self.connection_auth_snapshots_except(exclude_connection_id);
            let data = match parser.serialize(frame) {
                Ok(data) => data,
                Err(e) => {
                    tracing::warn!("Failed to serialize broadcast frame: {:?}", e);
                    return Ok(());
                }
            };

            let successful_ids = Arc::new(std::sync::Mutex::new(Vec::new()));
            stream::iter(connections)
                .for_each_concurrent(self.fanout_concurrency, |snapshot| {
                    let successful_ids = Arc::clone(&successful_ids);
                    let data = data.as_slice();
                    async move {
                        let connection_id = snapshot.0.clone();
                        let result = self
                            .send_serialized_frame_to_auth_snapshot_without_active(
                                snapshot, frame, data,
                            )
                            .await;
                        match result {
                            Ok(connection_id) => {
                                Self::record_successful_connection_id(
                                    &successful_ids,
                                    connection_id,
                                );
                            }
                            Err(e) => {
                                tracing::warn!(
                                    "Failed to broadcast frame to connection {}: {:?}",
                                    connection_id,
                                    e
                                );
                            }
                        }
                    }
                })
                .await;
            self.update_connections_active(Self::take_successful_connection_ids(successful_ids));

            return Ok(());
        }

        let connections = self.connection_snapshots_except(exclude_connection_id);
        let successful_ids = Arc::new(std::sync::Mutex::new(Vec::new()));
        stream::iter(connections)
            .for_each_concurrent(self.fanout_concurrency, |snapshot| {
                let successful_ids = Arc::clone(&successful_ids);
                async move {
                    let connection_id = snapshot.0.clone();
                    let result = self
                        .send_frame_to_snapshot_without_active(snapshot, frame, parser)
                        .await;
                    match result {
                        Ok(connection_id) => {
                            Self::record_successful_connection_id(&successful_ids, connection_id);
                        }
                        Err(e) => {
                            tracing::warn!(
                                "Failed to broadcast frame to connection {}: {:?}",
                                connection_id,
                                e
                            );
                        }
                    }
                }
            })
            .await;
        self.update_connections_active(Self::take_successful_connection_ids(successful_ids));

        Ok(())
    }
}