handleMessage method
Handles incoming message data and converts it to AMI messages.
Implementation
@override
void handleMessage(dynamic message) {
// Convert bytes to string once
final data = String.fromCharCodes(message);
// Split by EOL - we expect most messages to have few lines
final lines = data.split(eol);
// Remove trailing empty line (AMI messages end with double CRLF)
if (lines.isNotEmpty && lines.last.isEmpty) {
lines.removeLast();
}
// Process each line
final lineCount = lines.length;
for (var i = 0; i < lineCount; i++) {
final line = lines[i];
if (line.isNotEmpty) {
// Prevent unbounded buffer growth from malformed messages
if (_buffer.length >= _maxBufferLines) {
print('WARNING: Buffer overflow detected (${_buffer.length} lines). Clearing buffer.');
_buffer.clear();
_status = HandleStatus.idle;
return;
}
_status = HandleStatus.reading;
_buffer.add(line);
} else {
// Empty line marks end of message
_status = HandleStatus.done;
_handleStatus();
}
}
// Handle truncated messages (no empty line at end)
if (_status == HandleStatus.reading) {
_status = HandleStatus.truncated;
_handleStatus();
}
}