예제 #1
0
 /**
  * Process the response for the StarMade server
  *
  * @return array
  * @throws \GameQ\Exception\Protocol
  */
 public function processResponse()
 {
     // Holds the results sent back
     $results = [];
     // Holds the processed packets after having been reassembled
     $processed = [];
     // Start up the index for the processed
     $sequence_id_last = 0;
     foreach ($this->packets_response as $packet) {
         // Create a new buffer
         $buffer = new Buffer($packet);
         // Each "good" packet begins with sequence_id (32-bit)
         $sequence_id = $buffer->readInt32();
         // Sequence id is a response
         if (array_key_exists($sequence_id, $this->responses)) {
             $processed[$sequence_id] = $buffer->getBuffer();
             $sequence_id_last = $sequence_id;
         } else {
             // This is a continuation of the previous packet, reset the buffer and append
             $buffer->jumpto(0);
             // Append
             $processed[$sequence_id_last] .= $buffer->getBuffer();
         }
     }
     unset($buffer, $sequence_id_last, $sequence_id);
     // Iterate over the combined packets and do some work
     foreach ($processed as $sequence_id => $data) {
         // Create a new buffer
         $buffer = new Buffer($data);
         // Get the length of the packet
         $packetLength = $buffer->getLength();
         // Check to make sure the expected length matches the real length
         // Subtract 4 for the sequence_id pulled out earlier
         if ($packetLength != $buffer->readInt32() - 4) {
             throw new Exception(__METHOD__ . " packet length does not match expected length!");
         }
         // Now we need to call the proper method
         $results = array_merge($results, call_user_func_array([$this, $this->responses[$sequence_id]], [$buffer]));
     }
     return $results;
 }