| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | namespace GameQ\Query; |
| 20: | |
| 21: | use GameQ\Exception\Query as Exception; |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | class Native extends Core |
| 29: | { |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | public function get() |
| 37: | { |
| 38: | |
| 39: | if (is_null($this->socket)) { |
| 40: | $this->create(); |
| 41: | } |
| 42: | |
| 43: | return $this->socket; |
| 44: | } |
| 45: | |
| 46: | |
| 47: | |
| 48: | |
| 49: | |
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: | public function write($data) |
| 55: | { |
| 56: | try { |
| 57: | |
| 58: | if (is_null($this->socket)) { |
| 59: | $this->create(); |
| 60: | } |
| 61: | |
| 62: | |
| 63: | return fwrite($this->socket, $data); |
| 64: | } catch (\Exception $e) { |
| 65: | throw new Exception($e->getMessage(), $e->getCode(), $e); |
| 66: | } |
| 67: | } |
| 68: | |
| 69: | |
| 70: | |
| 71: | |
| 72: | public function close() |
| 73: | { |
| 74: | if ($this->socket) { |
| 75: | fclose($this->socket); |
| 76: | $this->socket = null; |
| 77: | } |
| 78: | } |
| 79: | |
| 80: | |
| 81: | |
| 82: | |
| 83: | |
| 84: | |
| 85: | protected function create() |
| 86: | { |
| 87: | |
| 88: | $remote_addr = sprintf("%s://%s:%d", $this->transport, $this->ip, $this->port); |
| 89: | |
| 90: | |
| 91: | $context = stream_context_create([ |
| 92: | 'socket' => [ |
| 93: | 'bindto' => '0:0', |
| 94: | ], |
| 95: | ]); |
| 96: | |
| 97: | |
| 98: | $errno = null; |
| 99: | $errstr = null; |
| 100: | |
| 101: | |
| 102: | if (($this->socket = |
| 103: | @stream_socket_client($remote_addr, $errno, $errstr, $this->timeout, STREAM_CLIENT_CONNECT, $context)) |
| 104: | !== false |
| 105: | ) { |
| 106: | |
| 107: | stream_set_timeout($this->socket, $this->timeout); |
| 108: | |
| 109: | |
| 110: | stream_set_blocking($this->socket, $this->blocking); |
| 111: | |
| 112: | |
| 113: | stream_set_read_buffer($this->socket, 0); |
| 114: | |
| 115: | |
| 116: | stream_set_write_buffer($this->socket, 0); |
| 117: | } else { |
| 118: | |
| 119: | $this->socket = null; |
| 120: | |
| 121: | |
| 122: | throw new Exception( |
| 123: | __METHOD__ . " - Error creating socket to server {$this->ip}:{$this->port}. Error: " . $errstr, |
| 124: | $errno |
| 125: | ); |
| 126: | } |
| 127: | } |
| 128: | |
| 129: | |
| 130: | |
| 131: | |
| 132: | |
| 133: | |
| 134: | |
| 135: | |
| 136: | |
| 137: | |
| 138: | |
| 139: | |
| 140: | |
| 141: | public function getResponses(array $sockets, $timeout, $stream_timeout) |
| 142: | { |
| 143: | |
| 144: | $loop_active = true; |
| 145: | |
| 146: | |
| 147: | $responses = []; |
| 148: | |
| 149: | |
| 150: | $sockets_tmp = []; |
| 151: | |
| 152: | |
| 153: | foreach ($sockets as $socket_id => $socket_data) { |
| 154: | |
| 155: | |
| 156: | $socket = $socket_data['socket']; |
| 157: | |
| 158: | |
| 159: | $sockets_tmp[$socket_id] = $socket->get(); |
| 160: | |
| 161: | unset($socket); |
| 162: | } |
| 163: | |
| 164: | |
| 165: | $read = $sockets_tmp; |
| 166: | $write = null; |
| 167: | $except = null; |
| 168: | |
| 169: | |
| 170: | if (empty($read)) { |
| 171: | return $responses; |
| 172: | } |
| 173: | |
| 174: | |
| 175: | $time_stop = microtime(true) + $timeout; |
| 176: | |
| 177: | |
| 178: | while ($loop_active && microtime(true) < $time_stop) { |
| 179: | |
| 180: | if (empty($read)) { |
| 181: | break; |
| 182: | } |
| 183: | |
| 184: | |
| 185: | $streams = stream_select($read, $write, $except, 0, $stream_timeout); |
| 186: | |
| 187: | |
| 188: | if ($streams === false || ($streams <= 0)) { |
| 189: | break; |
| 190: | } |
| 191: | |
| 192: | |
| 193: | foreach ($read as $socket) { |
| 194: | |
| 195: | |
| 196: | |
| 197: | if (($response = fread($socket, 32768)) === false) { |
| 198: | continue; |
| 199: | } |
| 200: | |
| 201: | |
| 202: | if (strlen($response) == 0) { |
| 203: | |
| 204: | unset($sockets_tmp[(int)$socket]); |
| 205: | continue; |
| 206: | } |
| 207: | |
| 208: | |
| 209: | $responses[(int)$socket][] = $response; |
| 210: | } |
| 211: | |
| 212: | |
| 213: | $read = $sockets_tmp; |
| 214: | } |
| 215: | |
| 216: | |
| 217: | unset($streams, $read, $write, $except, $sockets_tmp, $time_stop, $response); |
| 218: | |
| 219: | |
| 220: | return $responses; |
| 221: | } |
| 222: | } |
| 223: | |