blob: 4c1dda5a5ceb34c273f0b2d3026513e9cde502de [file] [log] [blame]
Roger Meier21c0a852012-09-05 19:47:14 +00001<?php
2
3namespace Thrift\Server;
4
Roger Meier21c0a852012-09-05 19:47:14 +00005use Thrift\Exception\TTransportException;
6
7/**
8 * Simple implemtation of a Thrift server.
9 *
10 * @package thrift.server
11 */
Roger Thomas6fb59232014-11-04 10:09:23 +000012class TSimpleServer extends TServer
13{
Robert Lub03ca012018-01-18 19:06:39 +080014 /**
15 * Flag for the main serving loop
16 *
17 * @var bool
18 */
19 private $stop_ = false;
Roger Meier21c0a852012-09-05 19:47:14 +000020
Robert Lub03ca012018-01-18 19:06:39 +080021 /**
22 * Listens for new client using the supplied
23 * transport. It handles TTransportExceptions
24 * to avoid timeouts etc killing it
25 *
26 * @return void
27 */
28 public function serve()
29 {
30 $this->transport_->listen();
Roger Meier21c0a852012-09-05 19:47:14 +000031
Robert Lub03ca012018-01-18 19:06:39 +080032 while (!$this->stop_) {
33 try {
34 $transport = $this->transport_->accept();
Roger Meier21c0a852012-09-05 19:47:14 +000035
Robert Lub03ca012018-01-18 19:06:39 +080036 if ($transport != null) {
37 $inputTransport = $this->inputTransportFactory_->getTransport($transport);
38 $outputTransport = $this->outputTransportFactory_->getTransport($transport);
39 $inputProtocol = $this->inputProtocolFactory_->getProtocol($inputTransport);
40 $outputProtocol = $this->outputProtocolFactory_->getProtocol($outputTransport);
41 while ($this->processor_->process($inputProtocol, $outputProtocol)) {
42 }
43 }
44 } catch (TTransportException $e) {
45 }
Roger Meier21c0a852012-09-05 19:47:14 +000046 }
Roger Meier21c0a852012-09-05 19:47:14 +000047 }
Roger Meier21c0a852012-09-05 19:47:14 +000048
Robert Lub03ca012018-01-18 19:06:39 +080049 /**
50 * Stops the server running. Kills the transport
51 * and then stops the main serving loop
52 *
53 * @return void
54 */
55 public function stop()
56 {
57 $this->transport_->close();
58 $this->stop_ = true;
59 }
Roger Meier21c0a852012-09-05 19:47:14 +000060}