listener.js
// Given a whole packet, read the header, and do stuff!
Given a whole packet, read the header, and do stuff!
Given a whole packet read the header and do stuff!
receiveMessage(m, rinfo) {
    this.emit("raw_message", {message: m, sender: rinfo });
    if (!m || m.length < 20) {
      this.emit("invalid_packet", {reason: "too short", message: m});
    }
    var offset = 0,
        mlength = m.length;
    var indicator = m.slice(0,8);
    if (indicator.compare(packetMagicBytes) !== 0) {
      this.emit("invalid_packet", {reason: "magic bytes mismatch", message: m});
      return;
    }
    offset += 8;
    var headerLength = m.readUInt32LE(offset);
    offset += 4; 
    if (mlength < offset + headerLength) return;
    var header = m.slice(offset, offset + headerLength);
    var headerContent =  messageTypes.CMsgRemoteClientBroadcastHeader.decode(header);
    offset += headerLength;
    if (mlength < offset + 4) return;
    var bodyLength = m.readUInt32LE(offset);
    offset += 4; 
    if (mlength < offset + bodyLength) return;
    if (headerContent.msg_type == messageTypes.ERemoteClientBroadcastMsg.k_ERemoteClientBroadcastMsgStatus) {
      var bodyData = m.slice(offset, offset + bodyLength);
      var bodyParsed = this.parseBody(bodyData, bodyLength);
      if (bodyParsed) {
        bodyParsed["ip_address"] = rinfo.address;
        this.emit("client_seen", bodyParsed);
      }
    }
    else
    {
    }
  }
receiveMessage ( m , rinfo ) { this . emit ( "raw_message" , { message : m , sender : rinfo } ) ; if ( ! m || m . length < 20 ) { this . emit ( "invalid_packet" , { reason : "too short" , message : m } ) ; } var offset = 0 , mlength = m . length ; var indicator = m . slice ( 0 , 8 ) ; if ( indicator . compare ( packetMagicBytes ) !== 0 ) { this . emit ( "invalid_packet" , { reason : "magic bytes mismatch" , message : m } ) ; return ; } offset += 8 ; var headerLength = m . readUInt32LE ( offset ) ; offset += 4 ; if ( mlength < offset + headerLength ) return ; var header = m . slice ( offset , offset + headerLength ) ; var headerContent = messageTypes . CMsgRemoteClientBroadcastHeader . decode ( header ) ; offset += headerLength ; if ( mlength < offset + 4 ) return ; var bodyLength = m . readUInt32LE ( offset ) ; offset += 4 ; if ( mlength < offset + bodyLength ) return ; if ( headerContent . msg_type == messageTypes . ERemoteClientBroadcastMsg . k_ERemoteClientBroadcastMsgStatus ) { var bodyData = m . slice ( offset , offset + bodyLength ) ; var bodyParsed = this . parseBody ( bodyData , bodyLength ) ; if ( bodyParsed ) { bodyParsed [ "ip_address" ] = rinfo . address ; this . emit ( "client_seen" , bodyParsed ) ; } } else { } }
Given a whole packet, read the header, and do stuff!
Given a whole packet read the header and do stuff!
// 32 bit // 32 bit // Useful for debugging - type 0 is a discovery packet from another client //if (headerContent.msg_type === 0) { //  var body_data = m.slice(offset, offset + bodyLength); //  var discoveryPacket = messageTypes.CMsgRemoteClientBroadcastDiscovery.decode(body_data); //}