PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/SourceQuery/Socket.class.php

https://gitlab.com/jean-pierre/SN_Online_Servers
PHP | 179 lines | 128 code | 36 blank | 15 comment | 13 complexity | 9041374e92d126b9019c2416b8f6d76c MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * Class written by xPaw
  4. *
  5. * Website: https://xpaw.me
  6. * GitHub: https://github.com/xPaw/PHP-Source-Query-Class
  7. */
  8. use xPaw\SourceQuery\Exception\InvalidPacketException;
  9. use xPaw\SourceQuery\Exception\SocketException;
  10. class SourceQuerySocket
  11. {
  12. public $Socket;
  13. public $Engine;
  14. public $Ip;
  15. public $Port;
  16. public $Timeout;
  17. /**
  18. * Points to buffer class
  19. *
  20. * @var SourceQueryBuffer
  21. */
  22. private $Buffer;
  23. public function __construct( $Buffer )
  24. {
  25. $this->Buffer = $Buffer;
  26. }
  27. public function Close( )
  28. {
  29. if( $this->Socket )
  30. {
  31. FClose( $this->Socket );
  32. $this->Socket = null;
  33. }
  34. }
  35. public function Open( $Ip, $Port, $Timeout, $Engine )
  36. {
  37. $this->Timeout = $Timeout;
  38. $this->Engine = $Engine;
  39. $this->Port = $Port;
  40. $this->Ip = $Ip;
  41. $this->Socket = @FSockOpen( 'udp://' . $Ip, $Port, $ErrNo, $ErrStr, $Timeout );
  42. if( $ErrNo || $this->Socket === false )
  43. {
  44. throw new SocketException( 'Could not create socket: ' . $ErrStr, SocketException::COULD_NOT_CREATE_SOCKET );
  45. }
  46. Stream_Set_Timeout( $this->Socket, $Timeout );
  47. Stream_Set_Blocking( $this->Socket, true );
  48. return true;
  49. }
  50. public function Write( $Header, $String = '' )
  51. {
  52. $Command = Pack( 'ccccca*', 0xFF, 0xFF, 0xFF, 0xFF, $Header, $String );
  53. $Length = StrLen( $Command );
  54. return $Length === FWrite( $this->Socket, $Command, $Length );
  55. }
  56. public function Read( $Length = 1400 )
  57. {
  58. $this->ReadBuffer( FRead( $this->Socket, $Length ), $Length );
  59. }
  60. protected function ReadBuffer( $Buffer, $Length )
  61. {
  62. $this->Buffer->Set( $Buffer );
  63. if( $this->Buffer->Remaining( ) === 0 )
  64. {
  65. // TODO: Should we throw an exception here?
  66. return;
  67. }
  68. $Header = $this->Buffer->GetLong( );
  69. if( $Header === -1 ) // Single packet
  70. {
  71. // We don't have to do anything
  72. }
  73. else if( $Header === -2 ) // Split packet
  74. {
  75. $Packets = Array( );
  76. $IsCompressed = false;
  77. $ReadMore = false;
  78. do
  79. {
  80. $RequestID = $this->Buffer->GetLong( );
  81. switch( $this->Engine )
  82. {
  83. case SourceQuery :: GOLDSOURCE:
  84. {
  85. $PacketCountAndNumber = $this->Buffer->GetByte( );
  86. $PacketCount = $PacketCountAndNumber & 0xF;
  87. $PacketNumber = $PacketCountAndNumber >> 4;
  88. break;
  89. }
  90. case SourceQuery :: SOURCE:
  91. {
  92. $IsCompressed = ( $RequestID & 0x80000000 ) !== 0;
  93. $PacketCount = $this->Buffer->GetByte( );
  94. $PacketNumber = $this->Buffer->GetByte( ) + 1;
  95. if( $IsCompressed )
  96. {
  97. $this->Buffer->GetLong( ); // Split size
  98. $PacketChecksum = $this->Buffer->GetUnsignedLong( );
  99. }
  100. else
  101. {
  102. $this->Buffer->GetShort( ); // Split size
  103. }
  104. break;
  105. }
  106. }
  107. $Packets[ $PacketNumber ] = $this->Buffer->Get( );
  108. $ReadMore = $PacketCount > sizeof( $Packets );
  109. }
  110. while( $ReadMore && $this->Sherlock( $Length ) );
  111. $Buffer = Implode( $Packets );
  112. // TODO: Test this
  113. if( $IsCompressed )
  114. {
  115. // Let's make sure this function exists, it's not included in PHP by default
  116. if( !Function_Exists( 'bzdecompress' ) )
  117. {
  118. throw new RuntimeException( 'Received compressed packet, PHP doesn\'t have Bzip2 library installed, can\'t decompress.' );
  119. }
  120. $Buffer = bzdecompress( $Buffer );
  121. if( CRC32( $Buffer ) !== $PacketChecksum )
  122. {
  123. throw new InvalidPacketException( 'CRC32 checksum mismatch of uncompressed packet data.', InvalidPacketException::CHECKSUM_MISMATCH );
  124. }
  125. }
  126. $this->Buffer->Set( SubStr( $Buffer, 4 ) );
  127. }
  128. else
  129. {
  130. throw new InvalidPacketException( 'Socket read: Raw packet header mismatch. (0x' . DecHex( $Header ) . ')', InvalidPacketException::PACKET_HEADER_MISMATCH );
  131. }
  132. }
  133. private function Sherlock( $Length )
  134. {
  135. $Data = FRead( $this->Socket, $Length );
  136. if( StrLen( $Data ) < 4 )
  137. {
  138. return false;
  139. }
  140. $this->Buffer->Set( $Data );
  141. return $this->Buffer->GetLong( ) === -2;
  142. }
  143. }