TSimpleServer.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace Thrift\Server;
  3. use Thrift\Exception\TTransportException;
  4. /**
  5. * Simple implemtation of a Thrift server.
  6. *
  7. * @package thrift.server
  8. */
  9. class TSimpleServer extends TServer
  10. {
  11. /**
  12. * Flag for the main serving loop
  13. *
  14. * @var bool
  15. */
  16. private $stop_ = false;
  17. /**
  18. * Listens for new client using the supplied
  19. * transport. It handles TTransportExceptions
  20. * to avoid timeouts etc killing it
  21. *
  22. * @return void
  23. */
  24. public function serve()
  25. {
  26. $this->transport_->listen();
  27. while (!$this->stop_) {
  28. try {
  29. $transport = $this->transport_->accept();
  30. if ($transport != null) {
  31. $inputTransport = $this->inputTransportFactory_->getTransport($transport);
  32. $outputTransport = $this->outputTransportFactory_->getTransport($transport);
  33. $inputProtocol = $this->inputProtocolFactory_->getProtocol($inputTransport);
  34. $outputProtocol = $this->outputProtocolFactory_->getProtocol($outputTransport);
  35. while ($this->processor_->process($inputProtocol, $outputProtocol)) { }
  36. }
  37. } catch (TTransportException $e) { }
  38. }
  39. }
  40. /**
  41. * Stops the server running. Kills the transport
  42. * and then stops the main serving loop
  43. *
  44. * @return void
  45. */
  46. public function stop()
  47. {
  48. $this->transport_->close();
  49. $this->stop_ = true;
  50. }
  51. }