send method

bool send(
  1. Uint8List bytes
)

Send bytes to the Python handler registered for this bridge's port.

Returns true on successful delivery, false if delivery isn't yet possible: either the Python interpreter hasn't finished Py_Initialize yet, or Python hasn't called dart_bridge.set_enqueue_handler_func for this port. Both states are transient on app startup — the caller typically retries until true.

Implementation

bool send(Uint8List bytes) {
  if (_closed) {
    throw StateError('PythonBridge is closed');
  }
  final len = bytes.length;
  final buf = malloc<Uint8>(len == 0 ? 1 : len);
  try {
    if (len > 0) {
      buf.asTypedList(len).setAll(0, bytes);
    }
    // rc: 0=delivered, -1=no handler yet, -2=Py_Initialize not finished.
    // Both negative cases are transient retry signals on app startup.
    final rc = _bridge.enqueueMessage(port, buf, len);
    return rc == 0;
  } finally {
    malloc.free(buf);
  }
}