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