OSDP.Net 5.2.1
A .NET library for implementing the Open Supervised Device Protocol (OSDP)
Loading...
Searching...
No Matches
PacketBuffer.cs
1#nullable enable
2
3using System;
5
6namespace OSDP.Net.Tracing;
7
13public class PacketBuffer
14{
15 private const int DefaultBufferSize = 4096;
16 private const int MinPacketLength = 6;
17 private const int MaxPacketLength = 1024;
18
19 private readonly byte[] _buffer;
20 private int _position;
21
25 public PacketBuffer() : this(DefaultBufferSize)
26 {
27 }
28
33 public PacketBuffer(int bufferSize)
34 {
35 _buffer = new byte[bufferSize];
36 }
37
43 public bool TryExtractPacket(out byte[]? packet)
44 {
45 packet = null;
46
47 // Find SOM (Start of Message)
48 int somIndex = Array.IndexOf(_buffer, Message.StartOfMessage, 0, _position);
49 if (somIndex == -1) return false;
50
51 // Discard any bytes before SOM
52 if (somIndex > 0)
53 {
54 int remaining = _position - somIndex;
55 Array.Copy(_buffer, somIndex, _buffer, 0, remaining);
56 _position = remaining;
57 }
58
59 // Need at least minimum header: SOM + Addr + Len(2) + Ctrl + Type
60 if (_position < MinPacketLength) return false;
61
62 // Parse length field (LSB first)
63 int length = _buffer[2] | (_buffer[3] << 8);
64
65 // Validate length (sanity check)
66 if (length < MinPacketLength || length > MaxPacketLength)
67 {
68 // Invalid length, skip this SOM and look for next
69 Array.Copy(_buffer, 1, _buffer, 0, _position - 1);
70 _position--;
71 return false;
72 }
73
74 // Check if we have complete packet
75 if (_position < length) return false;
76
77 // Extract packet
78 packet = new byte[length];
79 Array.Copy(_buffer, 0, packet, 0, length);
80
81 // Remove from buffer
82 int remainingAfter = _position - length;
83 if (remainingAfter > 0)
84 Array.Copy(_buffer, length, _buffer, 0, remainingAfter);
85 _position = remainingAfter;
86
87 return true;
88 }
89
95 public void Append(byte[] data, int count)
96 {
97 if (_position + count > _buffer.Length)
98 {
99 // Buffer full, try to find and keep only data after last SOM
100 int lastSom = Array.LastIndexOf(_buffer, Message.StartOfMessage, _position - 1);
101 if (lastSom > 0)
102 {
103 Array.Copy(_buffer, lastSom, _buffer, 0, _position - lastSom);
104 _position -= lastSom;
105 }
106 else
107 {
108 _position = 0; // Discard all if no SOM found
109 }
110 }
111
112 Array.Copy(data, 0, _buffer, _position, count);
113 _position += count;
114 }
115
119 public void Clear()
120 {
121 _position = 0;
122 }
123
127 public int Length => _position;
128}
A base message class for sending or replying.
Definition Message.cs:12
const byte StartOfMessage
OSDP message SOM (start of the message) byte.
Definition Message.cs:21
int Length
Gets the number of bytes currently in the buffer.
void Clear()
Clears all data from the buffer.
void Append(byte[] data, int count)
Appends data to the buffer.
PacketBuffer()
Creates a new PacketBuffer with the default buffer size (4096 bytes).
PacketBuffer(int bufferSize)
Creates a new PacketBuffer with a specified buffer size.
bool TryExtractPacket(out byte[]? packet)
Attempts to extract a complete OSDP packet from the buffer.