TServerSocket.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. *
  20. * @package thrift.transport
  21. */
  22. namespace Thrift\Server;
  23. use Thrift\Transport\TSocket;
  24. /**
  25. * Socket implementation of a server agent.
  26. *
  27. * @package thrift.transport
  28. */
  29. class TServerSocket extends TServerTransport
  30. {
  31. /**
  32. * Handle for the listener socket
  33. *
  34. * @var resource
  35. */
  36. protected $listener_;
  37. /**
  38. * Port for the listener to listen on
  39. *
  40. * @var int
  41. */
  42. protected $port_;
  43. /**
  44. * Timeout when listening for a new client
  45. *
  46. * @var int
  47. */
  48. protected $acceptTimeout_ = 30000;
  49. /**
  50. * Host to listen on
  51. *
  52. * @var string
  53. */
  54. protected $host_;
  55. /**
  56. * ServerSocket constructor
  57. *
  58. * @param string $host Host to listen on
  59. * @param int $port Port to listen on
  60. * @return void
  61. */
  62. public function __construct($host = 'localhost', $port = 9090)
  63. {
  64. $this->host_ = $host;
  65. $this->port_ = $port;
  66. }
  67. /**
  68. * Sets the accept timeout
  69. *
  70. * @param int $acceptTimeout
  71. * @return void
  72. */
  73. public function setAcceptTimeout($acceptTimeout)
  74. {
  75. $this->acceptTimeout_ = $acceptTimeout;
  76. }
  77. /**
  78. * Opens a new socket server handle
  79. *
  80. * @return void
  81. */
  82. public function listen()
  83. {
  84. $this->listener_ = stream_socket_server('tcp://' . $this->host_ . ':' . $this->port_);
  85. }
  86. /**
  87. * Closes the socket server handle
  88. *
  89. * @return void
  90. */
  91. public function close()
  92. {
  93. @fclose($this->listener_);
  94. $this->listener_ = null;
  95. }
  96. /**
  97. * Implementation of accept. If not client is accepted in the given time
  98. *
  99. * @return TSocket
  100. */
  101. protected function acceptImpl()
  102. {
  103. $handle = @stream_socket_accept($this->listener_, $this->acceptTimeout_ / 1000.0);
  104. if(!$handle) return null;
  105. $socket = new TSocket();
  106. $socket->setHandle($handle);
  107. return $socket;
  108. }
  109. }