| Volodymyr Panivko | 68139d1 | 2024-03-19 23:14:07 +0100 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | error_reporting(E_ALL); |
| 4 | |
| 5 | require_once __DIR__ . '/../../vendor/autoload.php'; |
| 6 | |
| Volodymyr Panivko | 76113eb | 2026-03-26 21:57:29 +0100 | [diff] [blame] | 7 | class TBinaryProtocolAcceleratedFactory implements \Thrift\Factory\TProtocolFactory |
| 8 | { |
| 9 | public function getProtocol($trans) |
| 10 | { |
| 11 | return new \Thrift\Protocol\TBinaryProtocolAccelerated($trans, false, true); |
| 12 | } |
| 13 | } |
| 14 | |
| Volodymyr Panivko | 68139d1 | 2024-03-19 23:14:07 +0100 | [diff] [blame] | 15 | $opts = getopt( |
| 16 | 'h::', |
| 17 | [ |
| 18 | 'port::', |
| 19 | 'domain-socket::', |
| 20 | 'pipe::', |
| 21 | 'server-type::', |
| 22 | 'transport::', |
| 23 | 'protocol::', |
| 24 | 'multiplex::', |
| 25 | 'abstract-namespace::', |
| 26 | 'ssl::', |
| 27 | 'zlib::', |
| 28 | 'processor-events::', |
| 29 | 'workers::', |
| 30 | ] |
| 31 | ); |
| 32 | if (isset($opts['h'])) { |
| 33 | echo <<<HELP |
| 34 | -h | --help produce help message |
| 35 | --port=arg (9090) Port number to listen |
| 36 | --domain-socket=arg Unix Domain Socket (e.g. /tmp/ThriftTest.thrift) |
| 37 | --pipe=arg Windows Named Pipe (e.g. MyThriftPipe) |
| 38 | --server-type=arg (simple) type of server, "simple", "thread-pool", |
| 39 | "threaded", or "nonblocking" |
| 40 | --transport=arg (buffered) transport: buffered, framed, http, anonpipe, zlib |
| Volodymyr Panivko | 76113eb | 2026-03-26 21:57:29 +0100 | [diff] [blame] | 41 | --protocol=arg (binary) protocol: binary, compact, json, accel |
| Volodymyr Panivko | 68139d1 | 2024-03-19 23:14:07 +0100 | [diff] [blame] | 42 | --multiplex Add TMultiplexedProtocol service name "ThriftTest" |
| 43 | --abstract-namespace Create the domain socket in the Abstract Namespace |
| 44 | (no connection with filesystem pathnames) |
| 45 | --ssl Encrypted Transport using SSL |
| 46 | --zlib Wrapped Transport using Zlib |
| 47 | --processor-events processor-events |
| 48 | -n=arg | --workers=arg (=4) Number of thread pools workers. Only valid for |
| 49 | thread-pool server type |
| 50 | HELP; |
| 51 | exit(0); |
| 52 | } |
| 53 | |
| 54 | $port = $opts['port'] ?? 9090; |
| 55 | $transport = $opts['transport'] ?? 'buffered'; |
| Volodymyr Panivko | 76113eb | 2026-03-26 21:57:29 +0100 | [diff] [blame] | 56 | $protocol = $opts['protocol'] ?? 'binary'; |
| Volodymyr Panivko | 68139d1 | 2024-03-19 23:14:07 +0100 | [diff] [blame] | 57 | |
| 58 | |
| 59 | $loader = new Thrift\ClassLoader\ThriftClassLoader(); |
| Volodymyr Panivko | 76113eb | 2026-03-26 21:57:29 +0100 | [diff] [blame] | 60 | $loader->registerDefinition('ThriftTest', __DIR__ . '/gen-php-classmap'); |
| Volodymyr Panivko | 68139d1 | 2024-03-19 23:14:07 +0100 | [diff] [blame] | 61 | $loader->register(); |
| 62 | |
| 63 | $sslOptions = \stream_context_create( |
| 64 | [ |
| 65 | 'ssl' => [ |
| 66 | 'verify_peer' => false, |
| 67 | 'verify_peer_name' => false, |
| 68 | ], |
| 69 | ] |
| 70 | ); |
| 71 | |
| 72 | require_once __DIR__ . '/Handler.php'; |
| 73 | |
| 74 | switch ($transport) { |
| 75 | case 'framed': |
| 76 | $serverTransportFactory = new \Thrift\Factory\TFramedTransportFactory(); |
| 77 | break; |
| 78 | default: |
| 79 | $serverTransportFactory = new \Thrift\Factory\TTransportFactory(); |
| 80 | } |
| 81 | |
| Volodymyr Panivko | 76113eb | 2026-03-26 21:57:29 +0100 | [diff] [blame] | 82 | switch ($protocol) { |
| 83 | case 'binary': |
| 84 | $protocolFactory = new \Thrift\Factory\TBinaryProtocolFactory(false, true); |
| 85 | break; |
| 86 | case 'accel': |
| 87 | if (!function_exists('thrift_protocol_write_binary')) { |
| 88 | fwrite(STDERR, "Acceleration extension is not loaded\n"); |
| 89 | exit(1); |
| 90 | } |
| 91 | $protocolFactory = new TBinaryProtocolAcceleratedFactory(); |
| 92 | break; |
| 93 | case 'compact': |
| 94 | $protocolFactory = new \Thrift\Factory\TCompactProtocolFactory(); |
| 95 | break; |
| 96 | case 'json': |
| 97 | $protocolFactory = new \Thrift\Factory\TJSONProtocolFactory(); |
| 98 | break; |
| 99 | default: |
| 100 | fwrite(STDERR, "--protocol must be one of {binary|compact|json|accel}\n"); |
| 101 | exit(1); |
| 102 | } |
| 103 | |
| Volodymyr Panivko | 381db97 | 2026-04-02 10:22:48 +0200 | [diff] [blame] | 104 | // `localhost` may resolve to an IPv6-only listener in newer PHP/runtime combinations, |
| 105 | // while some cross-test clients still connect via 127.0.0.1. Bind explicitly to IPv4. |
| 106 | $serverTransport = new \Thrift\Server\TServerSocket('127.0.0.1', $port); |
| Volodymyr Panivko | 68139d1 | 2024-03-19 23:14:07 +0100 | [diff] [blame] | 107 | $handler = new Handler(); |
| 108 | $processor = new ThriftTest\ThriftTestProcessor($handler); |
| 109 | |
| 110 | $server = new \Thrift\Server\TSimpleServer( |
| 111 | $processor, |
| 112 | $serverTransport, |
| 113 | $serverTransportFactory, |
| 114 | $serverTransportFactory, |
| Volodymyr Panivko | 76113eb | 2026-03-26 21:57:29 +0100 | [diff] [blame] | 115 | $protocolFactory, |
| 116 | $protocolFactory |
| Volodymyr Panivko | 68139d1 | 2024-03-19 23:14:07 +0100 | [diff] [blame] | 117 | ); |
| 118 | |
| 119 | echo "Starting the Test server...\n"; |
| 120 | $server->serve(); |