sendBytes method
Send a message.
data should be a list of bytes, or a string (which will then be encoded with utf-8.)
Throws exception on socket failure.
//DUMMY Doesn't throw. Just keeps going. Should probably fix that.
If not important, and another message is currently being sent, returns false instead of sending the message.
Otherwise, returns true once message has been added to the sock.
Implementation
Future<bool> sendBytes(Uint8List data, {bool important = true}) async {
if (mSendLock.locked && !mSendLock.inLock && !important) {
return false;
}
return await mSendLock.synchronized(() async {
try {
List<int> bb = [];
bb.addAll(int64BigEndianBytes(data.length));
// Send inverse, for validation
Uint8List inv = int64BigEndianBytes(data.length);
for (int i = 0; i < inv.length; i++) {
inv[i] ^= 0xFF;
}
inv = Uint8List.fromList(inv.reversed.toList());
bb.addAll(inv);
bb.addAll(data);
sock.add(bb);
await sock.flush();
return true;
} catch (e) {
await close();
rethrow;
}
});
}