blob: 8f38fb23f720b42be60f6f97522ffe4ed539809a [file] [log] [blame]
Roger Meier21c0a852012-09-05 19:47:14 +00001<?php
jfarrell311c9842016-09-20 13:30:16 -04002/*
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 */
Roger Meier21c0a852012-09-05 19:47:14 +000022
23namespace Thrift\Server;
24
Roger Meier21c0a852012-09-05 19:47:14 +000025use Thrift\Transport\TSocket;
26
27/**
28 * Socket implementation of a server agent.
29 *
30 * @package thrift.transport
31 */
Roger Thomas6fb59232014-11-04 10:09:23 +000032class TServerSocket extends TServerTransport
33{
Robert Lub03ca012018-01-18 19:06:39 +080034 /**
35 * Handle for the listener socket
36 *
37 * @var resource
38 */
39 protected $listener_;
Roger Meier21c0a852012-09-05 19:47:14 +000040
Robert Lub03ca012018-01-18 19:06:39 +080041 /**
42 * Port for the listener to listen on
43 *
44 * @var int
45 */
46 protected $port_;
Roger Meier21c0a852012-09-05 19:47:14 +000047
Robert Lub03ca012018-01-18 19:06:39 +080048 /**
49 * Timeout when listening for a new client
50 *
51 * @var int
52 */
53 protected $acceptTimeout_ = 30000;
Roger Meier21c0a852012-09-05 19:47:14 +000054
Robert Lub03ca012018-01-18 19:06:39 +080055 /**
56 * Host to listen on
57 *
58 * @var string
59 */
60 protected $host_;
Roger Meier21c0a852012-09-05 19:47:14 +000061
Robert Lub03ca012018-01-18 19:06:39 +080062 /**
63 * ServerSocket constructor
64 *
65 * @param string $host Host to listen on
66 * @param int $port Port to listen on
67 * @return void
68 */
69 public function __construct($host = 'localhost', $port = 9090)
70 {
71 $this->host_ = $host;
72 $this->port_ = $port;
73 }
Roger Meier21c0a852012-09-05 19:47:14 +000074
Robert Lub03ca012018-01-18 19:06:39 +080075 /**
76 * Sets the accept timeout
77 *
78 * @param int $acceptTimeout
79 * @return void
80 */
81 public function setAcceptTimeout($acceptTimeout)
82 {
83 $this->acceptTimeout_ = $acceptTimeout;
84 }
Roger Meier21c0a852012-09-05 19:47:14 +000085
Robert Lub03ca012018-01-18 19:06:39 +080086 /**
87 * Opens a new socket server handle
88 *
89 * @return void
90 */
91 public function listen()
92 {
93 $this->listener_ = stream_socket_server('tcp://' . $this->host_ . ':' . $this->port_);
94 }
Roger Meier21c0a852012-09-05 19:47:14 +000095
Robert Lub03ca012018-01-18 19:06:39 +080096 /**
97 * Closes the socket server handle
98 *
99 * @return void
100 */
101 public function close()
102 {
103 @fclose($this->listener_);
104 $this->listener_ = null;
105 }
Roger Meier21c0a852012-09-05 19:47:14 +0000106
Robert Lub03ca012018-01-18 19:06:39 +0800107 /**
108 * Implementation of accept. If not client is accepted in the given time
109 *
110 * @return TSocket
111 */
112 protected function acceptImpl()
113 {
114 $handle = @stream_socket_accept($this->listener_, $this->acceptTimeout_ / 1000.0);
115 if (!$handle) {
116 return null;
117 }
Roger Meier21c0a852012-09-05 19:47:14 +0000118
Robert Lub03ca012018-01-18 19:06:39 +0800119 $socket = new TSocket();
120 $socket->setHandle($handle);
Roger Meier21c0a852012-09-05 19:47:14 +0000121
Robert Lub03ca012018-01-18 19:06:39 +0800122 return $socket;
123 }
Roger Meier21c0a852012-09-05 19:47:14 +0000124}