ensureInitiaLized method

Future<void> ensureInitiaLized({
  1. required RawDatagramSocket socket,
  2. required NetworkInterface networkInterface,
  3. required FutureOr<void> onMessage(
    1. GeneralSocketMessageData generalSocketMessageData
    ),
  4. required void onDone(),
})

Implementation

Future<void> ensureInitiaLized({
  required RawDatagramSocket socket,
  required NetworkInterface networkInterface,
  required FutureOr<void> Function(
          GeneralSocketMessageData generalSocketMessageData)
      onMessage,
  required void Function() onDone,
}) async {
  if (is_initialized) {
    return;
  }
  _socket = socket;

  socket.joinMulticast(
    InternetAddress(multiCastGroup),
    networkInterface,
  );

  socket.listen(
    (e) async {
      final Datagram? datagram = socket.receive();
      if (datagram == null) {
        return;
      }
      onMessage(GeneralSocketMessageData(
        socket_id: socket_id,
        ipAddres: datagram.address.address,
        data: datagram.data,
      ));
    },
    onDone: () {
      onDone();
    },
  );
  is_initialized = true;
}