writeMessage method
Generate the next handshake message with optional payload. For XX pattern: msg1 has no payload, msg2 has responder payload, msg3 has initiator payload.
Implementation
Future<Uint8List> writeMessage(List<int> payload) async {
if (_state.state == XXHandshakeState.error) {
throw StateError('Cannot write message in error state');
}
try {
_validateWriteState();
final result = await switch (_state.state) {
XXHandshakeState.initial => _writeInitialMessage(),
XXHandshakeState.sentE => _writeSecondMessage(payload),
XXHandshakeState.sentEES => _writeFinalMessage(payload),
_ => throw StateError('Cannot write message in state: ${_state.state}'),
};
_state = result.$2; // Update state
return result.$1; // Return message
} catch (e) {
// Only set error state for non-validation errors
if (e is! StateError) {
_state = _state.copyWith(state: XXHandshakeState.error);
}
rethrow;
}
}