Server.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace Thrift\Service;
  3. use Dever;
  4. use Thrift\Exception\TException;
  5. use Thrift\Factory\TFramedTransportFactory;
  6. use Thrift\Factory\TBinaryProtocolFactory;
  7. use Thrift\Factory\TTransportFactory;
  8. use Thrift\Factory\TBufferedTransportFactory;
  9. use Thrift\Server\TServerSocket;
  10. use Thrift\Server\TSimpleServer;
  11. use Thrift\Server\TForkingServer;
  12. use Thrift\Protocol\TBinaryProtocol;
  13. use Thrift\Transport\TPhpStream;
  14. use Thrift\Transport\TBufferedTransport;
  15. header('Content-Type','application/x-thrift');
  16. class Server extends Base
  17. {
  18. protected $processor = null;
  19. public function start($processor, $handle)
  20. {
  21. $this->processor = new $processor($handle);
  22. $this->{$this->type}();
  23. }
  24. # socket
  25. private function socket()
  26. {
  27. $transportFactory = new TBufferedTransportFactory();
  28. $protocolFactory = new TBinaryProtocolFactory(true, true);
  29. $transport = new TServerSocket($this->host, $this->port);
  30. $server = new TForkingServer($this->processor, $transport, $transportFactory, $transportFactory, $protocolFactory, $protocolFactory);
  31. $server->serves($this->process);
  32. }
  33. # php stream
  34. public function stream()
  35. {
  36. if (php_sapi_name() == 'cli') {
  37. echo PHP_EOL;
  38. }
  39. $transport = new TBufferedTransport(new TPhpStream(TPhpStream::MODE_R | TPhpStream::MODE_W));
  40. $protocol = new TBinaryProtocol($transport,true,true);
  41. $transport->open();
  42. $this->processor->process($protocol, $protocol);
  43. $transport->close();
  44. }
  45. }