Advertisement
Guest User

Example App

a guest
Jun 11th, 2013
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.19 KB | None | 0 0
  1. function export($addr, $port, $str) {
  2.    
  3.     // All queries must begin with a question mark (ie "?players")
  4.     if($str{0} != '?') $str = ('?' . $str);
  5.    
  6.     /* --- Prepare a packet to send to the server (based on a reverse-engineered packet structure) --- */
  7.     $querysize = pack('n', strlen($str) + 6);
  8.    
  9.     $query = "\x00\x83" . pack('n', strlen($str) + 6) . "\x00\x00\x00\x00\x00" . $str . "\x00";
  10.    
  11.     /* --- Create a socket and connect it to the server --- */
  12.     $server = socket_create(AF_INET,SOCK_STREAM,SOL_TCP) or die('Unable to create export socket.');
  13.     if(!socket_connect($server,$addr,$port)) {
  14.         return "ERROR";
  15.     }
  16.  
  17.    
  18.     /* --- Send bytes to the server. Loop until all bytes have been sent --- */
  19.     $bytestosend = strlen($query);
  20.     $bytessent = 0;
  21.     while ($bytessent < $bytestosend) {
  22.         //echo $bytessent.'<br>';
  23.         $result = socket_write($server,substr($query,$bytessent),$bytestosend-$bytessent);
  24.         //echo 'Sent '.$result.' bytes<br>';
  25.         if ($result===FALSE) die(socket_strerror(socket_last_error()));
  26.         $bytessent += $result;
  27.     }
  28.    
  29.     /* --- Idle for a while until recieved bytes from game server --- */
  30.     $result = socket_read($server, 10000, PHP_BINARY_READ);
  31.     socket_close($server); // we don't need this anymore
  32.    
  33.     if($result != "") {
  34.         if($result{0} == "\x00" || $result{1} == "\x83") { // make sure it's the right packet format
  35.            
  36.             // Actually begin reading the output:
  37.             $sizebytes = unpack('n', $result{2} . $result{3}); // array size of the type identifier and content
  38.             $size = $sizebytes[1] - 1; // size of the string/floating-point (minus the size of the identifier byte)
  39.            
  40.             if($result{4} == "\x2a") { // 4-byte big-endian floating-point
  41.                 $unpackint = unpack('f', $result{5} . $result{6} . $result{7} . $result{8}); // 4 possible bytes: add them up together, unpack them as a floating-point
  42.                 return $unpackint[1];
  43.             }
  44.             else if($result{4} == "\x06") { // ASCII string
  45.                 $unpackstr = ""; // result string
  46.                 $index = 5; // string index
  47.                
  48.                 while($size > 0) { // loop through the entire ASCII string
  49.                     $size--;
  50.                     $unpackstr = $unpackstr . $result{$index}; // add the string position to return string
  51.                     $index++;
  52.                 }
  53.                 return $unpackstr;
  54.             }
  55.         }
  56.     }      
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement