listen function

bool listen(
  1. Function onData
)

Starts listening on the socket for packets sent by the RCON server. Returns a boolean that specifies if the socket has started listening. Note: onData must accept a Uint8List as the only parameter. The payload of the message starts at element 12.

Implementation

bool listen(Function onData) {
  if (rconSck == null) {
    return false;
  }

  rconSck!.listen(
    (Uint8List data) {
      Uint8List respReqIDInts = Uint8List(4)
        ..[0] = data[4]
        ..[1] = data[5]
        ..[2] = data[6]
        ..[3] = data[7];
      var blob = ByteData.sublistView(respReqIDInts);
      int respReqID = blob.getInt32(0, Endian.little);
      if (kDebugMode) {
        print("response id: $respReqID");
      }

      if (respReqID == requestID) {
        if (kDebugMode) {
          print("mc_rcon: Good packet received.");
        }
        onData(data);
      } else if (respReqID == -1) {
        if (kDebugMode) {
          print(
              "mc_rcon: Bad authentication. Incorrect password or you haven't logged in yet.");
        }
      } else {
        if (kDebugMode) {
          print("mc_rcon: Received unknown request ID.");
        }
      }
    },
    onError: (error) {
      if (kDebugMode) {
        print('mc_rcon: Error with the connection to the server: $error');
      }
      rconSck!.destroy();
    },
    onDone: () {
      if (kDebugMode) {
        print('mc_rcon: The server has ended the connection.');
      }
      rconSck!.destroy();
    },
    cancelOnError: false,
  );

  return true;
}