TSSLServerSocket.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. */
  21. namespace Thrift\Server;
  22. use Thrift\Transport\TSSLSocket;
  23. /**
  24. * Socket implementation of a server agent.
  25. *
  26. * @package thrift.transport
  27. */
  28. class TSSLServerSocket extends TServerSocket
  29. {
  30. /**
  31. * Remote port
  32. *
  33. * @var resource
  34. */
  35. protected $context_ = null;
  36. /**
  37. * ServerSocket constructor
  38. *
  39. * @param string $host Host to listen on
  40. * @param int $port Port to listen on
  41. * @param resource $context Stream context
  42. * @return void
  43. */
  44. public function __construct($host = 'localhost', $port = 9090, $context = null)
  45. {
  46. $ssl_host = $this->getSSLHost($host);
  47. parent::__construct($ssl_host, $port);
  48. $this->context_ = $context;
  49. }
  50. public function getSSLHost($host)
  51. {
  52. $transport_protocol_loc = strpos($host, "://");
  53. if ($transport_protocol_loc === false) {
  54. $host = 'ssl://'.$host;
  55. }
  56. return $host;
  57. }
  58. /**
  59. * Opens a new socket server handle
  60. *
  61. * @return void
  62. */
  63. public function listen()
  64. {
  65. $this->listener_ = @stream_socket_server(
  66. $this->host_ . ':' . $this->port_,
  67. $errno,
  68. $errstr,
  69. STREAM_SERVER_BIND|STREAM_SERVER_LISTEN,
  70. $this->context_);
  71. }
  72. /**
  73. * Implementation of accept. If not client is accepted in the given time
  74. *
  75. * @return TSocket
  76. */
  77. protected function acceptImpl()
  78. {
  79. $handle = @stream_socket_accept($this->listener_, $this->acceptTimeout_ / 1000.0);
  80. if(!$handle) return null;
  81. $socket = new TSSLSocket();
  82. $socket->setHandle($handle);
  83. return $socket;
  84. }
  85. }