Segmentation
System for fragmenting and rebuilding large packets to optimize network transport.
The Segmentation message allows splitting and rebuilding large packets into smaller segments.
This mechanism is essential to handle network packet size limitations and to ensure reliable transmission of large data.
Priority
Critical - This message has the highest priority to guarantee correct and fast assembly of the segments.
Use Cases
Fragmentation of Large Data
Allows transmitting data exceeding the maximum packet size (defined during the handshake).
Network Optimization
Prevents transmission failures due to the network MTU (Maximum Transmission Unit) limitations.
Reliable Transmission
Guarantees data integrity by allowing the retransmission of individual segments in case of failure.
Client Request → Server (0x02)
Payload Format
[segment_id: ushort]
[total_segments: ushort]
[current_segment: ushort]
[segment_data: byte[]]Detailed Structure
| Type | Name | Description |
|---|---|---|
| ushort | SegmentID | Unique identifier of the segmentation session |
| ushort | TotalSegments | Total number of segments for this packet |
| ushort | CurrentSegment | Index of the current segment (0-based) |
| byte[] | SegmentData | Data of this specific segment |
Payload Example
[0x00, 0x2A] // SegmentID = 42
[0x00, 0x03] // TotalSegments = 3
[0x00, 0x00] // CurrentSegment = 0 (first segment)
[binary_data...] // SegmentDataProcessing
Server Side
- Segment reception: Extraction of the segmentation metadata
- Expiration check: Test whether the existing session has not expired
- Session creation/retrieval: Session handling based on SegmentID + client hash
- Segment addition: Ordered storage of the segment in the session
- Completeness check: Test whether all segments have been received
- Rebuilding: Assembly of the segments into the original packet if complete
- Cleanup: Removal of the session after rebuilding
Unique Identification
Each segmentation session is identified by:
Key = HashCode.Combine(SegmentID, ClientAddress.GetHashCode())Limitations and Considerations
Session Timeout
- Segmentation sessions have an automatic timeout to prevent memory leaks.
Segment Order
- Segments may arrive in any order
- The system automatically rebuilds the correct order
ID Collision
- Use unique SegmentIDs to avoid collisions
- The client address hash adds a layer of differentiation
Performance
- Memory impact: Handled automatically by the timeout system
- Cleanup frequency: Fixed 10-second timer (not configurable)
- Expiration check: Minimal cost on reception
Usage Example
Sending a Large Packet (1500 bytes)
Segment 1: [0x2A] [3] [0] [500 bytes]
Segment 2: [0x2A] [3] [1] [500 bytes]
Segment 3: [0x2A] [3] [2] [500 bytes]Rebuilding
Once all segments are received, the server:
- Merges the 1500 bytes in the correct order
- Processes the rebuilt packet as a normal packet
Error Codes
Expired Segments
Segments belonging to expired sessions are automatically rejected. The client must retransmit all the segments to create a new session.
Invalid SegmentID
SegmentIDs must be consistent for all segments of the same session.