updateServerNonce method

void updateServerNonce(
  1. int uint8Lsb
)

Implementation

void updateServerNonce(int uint8Lsb) {
  int difference = uint8Lsb - serverNonce[0];
  if (difference != 1) {
    //packets were received out of order
    if (difference > 30) {
      //30 seems to be arbitrary; it's used in the official client source, so I use it here
      //we wrapped around back to zero, they are still on high numbers
      //solution: we copy their lsb and undo the wraping
      serverNonce[0] = uint8Lsb;
      _passBack(serverNonce, 1);
    } else if (difference < -30) {
      //they wrapped around back to zero, we are still on high numbers
      //solution: we copy their lsb and wrap too
      serverNonce[0] = uint8Lsb;
      _passOn(serverNonce, 1);
    } else if (difference == 0) {
      // we either missed some multiple of exactly 255 packets, or they sent two packets with the same nonce
      // this should pretty much never happen unless the server screws up, or the network is REALLY bad
      throw new NonceOutOfSyncException();
    } else {
      //messages might be out of order here but its not so bad that we need wraping
      //solution: just copy their lsb and hope thats enough...
      serverNonce[0] = uint8Lsb;
    }
  } else {
    //no wrapping here, all messages in order, just copy their lsb
    serverNonce[0] = uint8Lsb;
  }
}