内容纲要
Rcon.prototype._tcpSocketOnData = function(data) {
  if (this.outstandingData != null) {
    data = Buffer.concat([this.outstandingData, data], this.outstandingData.length + data.length);
    this.outstandingData = null;
  }

  while (data.length) {
    var len = data.readInt32LE(0);
    if (!len) return;

    var id = data.readInt32LE(4);
    var type = data.readInt32LE(8);

    if (len >= 10 && data.length >= len + 4) {
      if (id == this.rconId) {
        if (!this.hasAuthed && type == PacketType.RESPONSE_AUTH) {
          this.hasAuthed = true;
          this.emit('auth');
        } else if (type == PacketType.RESPONSE_VALUE) {
          // Read just the body of the packet (truncate the last null byte)
          // See https://developer.valvesoftware.com/wiki/Source_RCON_Protocol for details
          var str = data.toString('utf8', 12, 12 + len - 10);

          if (str.charAt(str.length - 1) === '\n') {
            // Emit the response without the newline.
            str = str.substring(0, str.length - 1);
          }

          this.emit('response', str);
        }
      } else if (id == -1) {
        this.emit('error', new Error("Authentication failed"));
      } else {
        // ping/pong likely
        var str = data.toString('utf8', 12, 12 + len - 10);

        if (str.charAt(str.length - 1) === '\n') {
          // Emit the response without the newline.
          str = str.substring(0, str.length - 1);
        }

        this.emit('server', str);
      }

      data = data.slice(12 + len - 8);
    } else {
      // Keep a reference to the chunk if it doesn't represent a full packet
      this.outstandingData = data;
      break;
    }
  }
};

https://github.com/pushrax/node-rcon/blob/master/node-rcon.js

发表评论

电子邮件地址不会被公开。 必填项已用*标注