Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions lib/php/lib/Factory/TBinaryProtocolAcceleratedFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* @package thrift.protocol
*/

declare(strict_types=1);

namespace Thrift\Factory;

use Thrift\Protocol\TBinaryProtocolAccelerated;
use Thrift\Transport\TTransport;

/**
* Accelerated Binary Protocol Factory.
*
* Produces TBinaryProtocolAccelerated, which dispatches read/write through
* the thrift_protocol C extension when available and falls back to
* TBinaryProtocol otherwise.
*/
class TBinaryProtocolAcceleratedFactory implements TProtocolFactory
{
public function __construct(
private bool $strictRead = false,
private bool $strictWrite = true,
) {
}

public function getProtocol(TTransport $trans): TBinaryProtocolAccelerated
{
return new TBinaryProtocolAccelerated($trans, $this->strictRead, $this->strictWrite);
}
}
6 changes: 1 addition & 5 deletions lib/php/lib/Factory/TBinaryProtocolFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,7 @@ public function __construct(
) {
}

/**
* @param TTransport $trans
* @return TBinaryProtocol
*/
public function getProtocol($trans)
public function getProtocol(TTransport $trans): TBinaryProtocol
{
return new TBinaryProtocol($trans, $this->strictRead, $this->strictWrite);
}
Expand Down
6 changes: 1 addition & 5 deletions lib/php/lib/Factory/TCompactProtocolFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@
*/
class TCompactProtocolFactory implements TProtocolFactory
{
/**
* @param TTransport $trans
* @return TCompactProtocol
*/
public function getProtocol($trans)
public function getProtocol(TTransport $trans): TCompactProtocol
{
return new TCompactProtocol($trans);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/php/lib/Factory/TFramedTransportFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

class TFramedTransportFactory implements TTransportFactoryInterface
{
public function getTransport(TTransport $transport)
public function getTransport(TTransport $transport): TFramedTransport
{
return new TFramedTransport($transport);
}
Expand Down
6 changes: 1 addition & 5 deletions lib/php/lib/Factory/TJSONProtocolFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@
*/
class TJSONProtocolFactory implements TProtocolFactory
{
/**
* @param TTransport $trans
* @return TJSONProtocol
*/
public function getProtocol($trans)
public function getProtocol(TTransport $trans): TJSONProtocol
{
return new TJSONProtocol($trans);
}
Expand Down
5 changes: 2 additions & 3 deletions lib/php/lib/Factory/TProtocolFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
namespace Thrift\Factory;

use Thrift\Protocol\TProtocol;
use Thrift\Transport\TTransport;

/**
* Protocol factory creates protocol objects from transports
Expand All @@ -34,8 +35,6 @@ interface TProtocolFactory
{
/**
* Build a protocol from the base transport
*
* @return TProtocol protocol
*/
public function getProtocol($trans);
public function getProtocol(TTransport $trans): TProtocol;
}
6 changes: 1 addition & 5 deletions lib/php/lib/Factory/TTransportFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@

class TTransportFactory implements TTransportFactoryInterface
{
/**
* @param TTransport $transport
* @return TTransport
*/
public function getTransport(TTransport $transport)
public function getTransport(TTransport $transport): TTransport
{
return $transport;
}
Expand Down
6 changes: 1 addition & 5 deletions lib/php/lib/Factory/TTransportFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,5 @@

interface TTransportFactoryInterface
{
/**
* @param TTransport $transport
* @return TTransport
*/
public function getTransport(TTransport $transport);
public function getTransport(TTransport $transport): TTransport;
}
43 changes: 12 additions & 31 deletions lib/php/lib/Server/TForkingServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,22 @@ class TForkingServer extends TServer
* Listens for new client using the supplied
* transport. We fork when a new connection
* arrives.
*
* @return void
*/
public function serve()
public function serve(): void
{
$this->transport->listen();

while (!$this->stop) {
try {
$transport = $this->transport->accept();
$pid = pcntl_fork();

if ($transport != null) {
$pid = pcntl_fork();

if ($pid > 0) {
$this->handleParent($transport, $pid);
} elseif ($pid === 0) {
$this->handleChild($transport);
} else {
throw new TException('Failed to fork');
}
if ($pid > 0) {
$this->handleParent($transport, $pid);
} elseif ($pid === 0) {
$this->handleChild($transport);
} else {
throw new TException('Failed to fork');
}
} catch (TTransportException $e) {
}
Expand All @@ -62,23 +57,16 @@ public function serve()

/**
* Code run by the parent
*
* @param TTransport $transport
* @param int $pid
* @return void
*/
private function handleParent(TTransport $transport, $pid)
private function handleParent(TTransport $transport, int $pid): void
{
$this->children[$pid] = $transport;
}

/**
* Code run by the child.
*
* @param TTransport $transport
* @return void
*/
private function handleChild(TTransport $transport)
private function handleChild(TTransport $transport): void
{
try {
$inputTransport = $this->inputTransportFactory->getTransport($transport);
Expand All @@ -94,12 +82,7 @@ private function handleChild(TTransport $transport)
exit(0);
}

/**
* Collects any children we may have
*
* @return void
*/
private function collectChildren()
private function collectChildren(): void
{
$status = null;
foreach ($this->children as $pid => $transport) {
Expand All @@ -115,10 +98,8 @@ private function collectChildren()
/**
* Stops the server running. Kills the transport
* and then stops the main serving loop
*
* @return void
*/
public function stop()
public function stop(): void
{
$this->transport->close();
$this->stop = true;
Expand Down
14 changes: 2 additions & 12 deletions lib/php/lib/Server/TSSLServerSocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,7 @@ public function __construct(
$this->context = $context ?? stream_context_create();
}

/**
* Opens a new socket server handle
*
* @return void
*/
public function listen()
public function listen(): void
{
$this->listener = @stream_socket_server(
$this->host . ':' . $this->port,
Expand All @@ -70,12 +65,7 @@ public function listen()
);
}

/**
* Implementation of accept. If not client is accepted in the given time
*
* @return TSocket
*/
protected function acceptImpl()
protected function acceptImpl(): ?TSSLSocket
{
$handle = @stream_socket_accept($this->listener, $this->acceptTimeout / 1000.0);
if (!$handle) {
Expand Down
18 changes: 2 additions & 16 deletions lib/php/lib/Server/TServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@
*/
abstract class TServer
{
/**
* Sets up all the factories, etc
*
* @param TProcessor $processor
*/
public function __construct(
protected $processor,
protected TServerTransport $transport,
Expand All @@ -33,17 +28,8 @@ public function __construct(
* Serves the server. This should never return
* unless a problem permits it to do so or it
* is interrupted intentionally
*
* @abstract
* @return void
*/
abstract public function serve();
abstract public function serve(): void;

/**
* Stops the server serving
*
* @abstract
* @return void
*/
abstract public function stop();
abstract public function stop(): void;
}
29 changes: 4 additions & 25 deletions lib/php/lib/Server/TServerSocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,44 +55,23 @@ public function __construct(
) {
}

/**
* Sets the accept timeout
*
* @param int $acceptTimeout
* @return void
*/
public function setAcceptTimeout($acceptTimeout)
public function setAcceptTimeout(int $acceptTimeout): void
{
$this->acceptTimeout = $acceptTimeout;
}

/**
* Opens a new socket server handle
*
* @return void
*/
public function listen()
public function listen(): void
{
$this->listener = stream_socket_server('tcp://' . $this->host . ':' . $this->port);
}

/**
* Closes the socket server handle
*
* @return void
*/
public function close()
public function close(): void
{
@fclose($this->listener);
$this->listener = null;
}

/**
* Implementation of accept. If not client is accepted in the given time
*
* @return TSocket
*/
protected function acceptImpl()
protected function acceptImpl(): ?TSocket
{
$handle = @stream_socket_accept($this->listener, $this->acceptTimeout / 1000.0);
if (!$handle) {
Expand Down
34 changes: 7 additions & 27 deletions lib/php/lib/Server/TServerTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,23 @@
*/
abstract class TServerTransport
{
/**
* List for new clients
*
* @abstract
* @return void
*/
abstract public function listen();
abstract public function listen(): void;

/**
* Close the server
*
* @abstract
* @return void
*/
abstract public function close();
abstract public function close(): void;

/**
* Subclasses should use this to implement
* accept.
*
* @abstract
* @return TTransport
* Subclasses implement accept here.
*/
abstract protected function acceptImpl();
abstract protected function acceptImpl(): ?TTransport;

/**
* Uses the accept implemtation. If null is returned, an
* exception is thrown.
*
* @throws TTransportException
* @return TTransport
* @throws TTransportException when no transport is available
*/
public function accept()
public function accept(): TTransport
{
$transport = $this->acceptImpl();

if ($transport == null) {
if ($transport === null) {
throw new TTransportException("accept() may not return NULL");
}

Expand Down
Loading
Loading