Skip to content

Commit 6f9b01f

Browse files
PeterGRutherfordscasino983
authored andcommitted
feat: centralize MTU constants and fragmentation logic
- Created lib-network/src/mtu.rs with centralized MTU constants for all protocols - BLE: 23/247/512, chunk=200 - Bluetooth Classic: 1000 - LoRaWAN: 242 - WiFi Direct: 1400 - UDP: 1400, QUIC: 1200, Mesh: 65536 - Added Protocol enum with mtu(), chunk_size(), negotiated_mtu() methods - Created lib-network/src/fragmentation.rs with unified fragmentation logic - fragment_message() for splitting payloads - reassemble_message() for combining fragments - FragmentReassembler for stateful streaming reassembly - Handles out-of-order fragments, duplicates, concurrent messages - Updated 10 files to use centralized constants: - lib-network: blockchain_sync, bluetooth (gatt/mod/classic), lorawan, lorawan_hardware, discovery - lib-storage: dht/transport - zhtp: bluetooth_le, web4_stub - Removed all hardcoded MTU values (247, 242, 1400, 1000, 512, 23) - Added backward-compatible deprecated aliases in blockchain_sync - All 16 tests passing, code compiles successfully
1 parent 35c263a commit 6f9b01f

File tree

11 files changed

+816
-115
lines changed

11 files changed

+816
-115
lines changed

lib-network/src/blockchain_sync/mod.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,20 @@ use anyhow::Result;
4646
use lib_crypto::PublicKey;
4747
use crate::types::mesh_message::ZhtpMeshMessage;
4848
use crate::protocols::NetworkProtocol;
49+
use crate::mtu::{
50+
BLE_CHUNK_SIZE, BLUETOOTH_CLASSIC_CHUNK_SIZE, WIFI_DIRECT_CHUNK_SIZE, DEFAULT_CHUNK_SIZE,
51+
};
4952
use std::time::Duration;
5053

51-
/// Chunk sizes based on protocol capabilities
52-
pub const BLE_CHUNK_SIZE: usize = 200; // Conservative for BLE GATT (247-byte MTU)
53-
pub const CLASSIC_CHUNK_SIZE: usize = 1000; // Bluetooth Classic RFCOMM (larger MTU)
54-
pub const WIFI_CHUNK_SIZE: usize = 1400; // WiFi Direct (can handle more)
55-
pub const DEFAULT_CHUNK_SIZE: usize = 200; // Safe fallback
54+
/// Re-export chunk sizes for backward compatibility
55+
#[deprecated(since = "0.1.0", note = "Use crate::mtu::BLE_CHUNK_SIZE instead")]
56+
pub const BLE_CHUNK_SIZE_COMPAT: usize = BLE_CHUNK_SIZE;
57+
58+
#[deprecated(since = "0.1.0", note = "Use crate::mtu::BLUETOOTH_CLASSIC_CHUNK_SIZE instead")]
59+
pub const CLASSIC_CHUNK_SIZE: usize = BLUETOOTH_CLASSIC_CHUNK_SIZE;
60+
61+
#[deprecated(since = "0.1.0", note = "Use crate::mtu::WIFI_DIRECT_CHUNK_SIZE instead")]
62+
pub const WIFI_CHUNK_SIZE: usize = WIFI_DIRECT_CHUNK_SIZE;
5663

5764
/// Security constraints - Original limits
5865
pub const MAX_CHUNK_BUFFER_SIZE: usize = 10_000_000; // 10MB max buffer per request
@@ -82,11 +89,13 @@ pub const MAX_REQUESTS_PER_PEER: usize = 10;
8289

8390
/// Get optimal chunk size for protocol
8491
pub fn get_chunk_size_for_protocol(protocol: &NetworkProtocol) -> usize {
92+
use crate::mtu::Protocol;
93+
8594
match protocol {
86-
NetworkProtocol::BluetoothLE => BLE_CHUNK_SIZE,
87-
NetworkProtocol::BluetoothClassic => CLASSIC_CHUNK_SIZE,
88-
NetworkProtocol::WiFiDirect => WIFI_CHUNK_SIZE,
89-
NetworkProtocol::TCP | NetworkProtocol::UDP => WIFI_CHUNK_SIZE,
95+
NetworkProtocol::BluetoothLE => Protocol::BluetoothLE.chunk_size(),
96+
NetworkProtocol::BluetoothClassic => Protocol::BluetoothClassic.chunk_size(),
97+
NetworkProtocol::WiFiDirect => Protocol::WiFiDirect.chunk_size(),
98+
NetworkProtocol::TCP | NetworkProtocol::UDP => Protocol::Udp.chunk_size(),
9099
_ => DEFAULT_CHUNK_SIZE,
91100
}
92101
}

lib-network/src/discovery/lorawan_hardware.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ async fn test_serial_lorawan_module(port: &str, chip_type: &str) -> Result<LoRaW
346346
class_a: true,
347347
otaa_support: true,
348348
abp_support: true,
349-
max_payload_size: 242,
349+
max_payload_size: crate::mtu::LORAWAN_MAX_PAYLOAD,
350350
spreading_factors: vec![7, 8, 9, 10, 11, 12],
351351
..Default::default()
352352
},
@@ -376,7 +376,7 @@ async fn detect_i2c_lorawan(address: &str) -> Result<LoRaWANHardware> {
376376
class_a: true,
377377
otaa_support: true,
378378
abp_support: true,
379-
max_payload_size: 242,
379+
max_payload_size: crate::mtu::LORAWAN_MAX_PAYLOAD,
380380
spreading_factors: vec![7, 8, 9, 10, 11, 12],
381381
..Default::default()
382382
},
@@ -414,7 +414,7 @@ async fn detect_raspberry_pi_lorawan_hat() -> Result<LoRaWANHardware> {
414414
class_c: true,
415415
otaa_support: true,
416416
abp_support: true,
417-
max_payload_size: 242,
417+
max_payload_size: crate::mtu::LORAWAN_MAX_PAYLOAD,
418418
spreading_factors: vec![7, 8, 9, 10, 11, 12],
419419
..Default::default()
420420
},
@@ -445,7 +445,7 @@ fn create_sx127x_hardware(chip_name: &str, spi_path: &str) -> LoRaWANHardware {
445445
class_c: true,
446446
otaa_support: true,
447447
abp_support: true,
448-
max_payload_size: 242,
448+
max_payload_size: crate::mtu::LORAWAN_MAX_PAYLOAD,
449449
spreading_factors: vec![6, 7, 8, 9, 10, 11, 12],
450450
..Default::default()
451451
},
@@ -466,7 +466,7 @@ fn create_sx130x_hardware(chip_name: &str, spi_path: &str) -> LoRaWANHardware {
466466
class_c: true,
467467
otaa_support: true,
468468
abp_support: true,
469-
max_payload_size: 242,
469+
max_payload_size: crate::mtu::LORAWAN_MAX_PAYLOAD,
470470
spreading_factors: vec![7, 8, 9, 10, 11, 12],
471471
..Default::default()
472472
},
@@ -509,7 +509,7 @@ async fn detect_windows_lorawan() -> Result<LoRaWANHardware> {
509509
class_a: true,
510510
otaa_support: true,
511511
abp_support: true,
512-
max_payload_size: 242,
512+
max_payload_size: crate::mtu::LORAWAN_MAX_PAYLOAD,
513513
spreading_factors: vec![7, 8, 9, 10],
514514
..Default::default()
515515
},
@@ -559,7 +559,7 @@ async fn test_windows_com_lorawan(port_name: &str) -> Result<LoRaWANHardware> {
559559
class_a: true,
560560
otaa_support: true,
561561
abp_support: true,
562-
max_payload_size: 242,
562+
max_payload_size: crate::mtu::LORAWAN_MAX_PAYLOAD,
563563
spreading_factors: vec![7, 8, 9, 10],
564564
..Default::default()
565565
},
@@ -632,7 +632,7 @@ async fn test_macos_usb_lorawan(device_path: &str) -> Result<LoRaWANHardware> {
632632
class_a: true,
633633
otaa_support: true,
634634
abp_support: true,
635-
max_payload_size: 242,
635+
max_payload_size: crate::mtu::LORAWAN_MAX_PAYLOAD,
636636
spreading_factors: vec![7, 8, 9, 10, 11, 12],
637637
..Default::default()
638638
},

0 commit comments

Comments
 (0)