TCP::onBound()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
nc 2
nop 0
1
<?php
2
namespace PHPDaemon\BoundSocket;
3
4
use PHPDaemon\Core\Daemon;
5
6
/**
7
 * TCP
8
 *
9
 * @package Core
10
 *
11
 * @author  Vasily Zorin <[email protected]>
12
 */
13
class TCP extends Generic
14
{
15
    /**
16
     * Hostname
17
     * @var string
18
     */
19
    protected $host;
20
21
    /**
22
     * Port
23
     * @var integer
24
     */
25
    protected $port;
26
27
    /**
28
     * Default port
29
     * @var integer
30
     */
31
    protected $defaultPort;
32
33
    /**
34
     * Sets default port
35
     * @param integer Port
36
     * @return void
37
     */
38
    public function setDefaultPort($port)
39
    {
40
        $this->defaultPort = $port;
41
    }
42
43
    /**
44
     * Bind the socket
45
     * @return null|boolean Success.
46
     */
47
    public function bindSocket()
48
    {
49
        if ($this->erroneous) {
50
            return false;
51
        }
52
        $port = $this->getPort();
53 View Code Duplication
        if (!is_int($port)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54
            Daemon::log(get_class($this) . ' (' . get_class($this->pool) . '): no port defined for \'' . $this->uri['uri'] . '\'');
55
            return;
56
        }
57
        if (($port < 1024) && Daemon::$config->user->value !== 'root') {
58
            $this->listenerMode = false;
59
        }
60
        if ($this->listenerMode) {
61
            $this->setFd($this->host . ':' . $port);
62
            return true;
63
        }
64
        $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
65 View Code Duplication
        if (!$sock) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
66
            $errno = socket_last_error();
67
            Daemon::$process->log(get_class($this->pool) . ': Couldn\'t create TCP-socket (' . $errno . ' - ' . socket_strerror($errno) . ').');
68
            return false;
69
        }
70 View Code Duplication
        if ($this->reuse) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
            if (!socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1)) {
72
                $errno = socket_last_error();
73
                Daemon::$process->log(get_class($this->pool) . ': Couldn\'t set option REUSEADDR to socket (' . $errno . ' - ' . socket_strerror($errno) . ').');
74
                return false;
75
            }
76
            if (defined('SO_REUSEPORT') && !@socket_set_option($sock, SOL_SOCKET, SO_REUSEPORT, 1)) {
77
                $errno = socket_last_error();
78
                Daemon::$process->log(get_class($this->pool) . ': Couldn\'t set option REUSEPORT to socket (' . $errno . ' - ' . socket_strerror($errno) . ').');
79
                return false;
80
            }
81
        }
82 View Code Duplication
        if (!@socket_bind($sock, $this->host, $port)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
            $errno = socket_last_error();
84
            Daemon::$process->log(get_class($this->pool) . ': Couldn\'t bind TCP-socket \'' . $this->host . ':' . $port . '\' (' . $errno . ' - ' . socket_strerror($errno) . ').');
85
            return false;
86
        }
87
        socket_getsockname($sock, $this->host, $this->port);
88
        socket_set_nonblock($sock);
89 View Code Duplication
        if (!$this->listenerMode) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90
            if (!socket_listen($sock, SOMAXCONN)) {
91
                $errno = socket_last_error();
92
                Daemon::$process->log(get_class($this->pool) . ': Couldn\'t listen TCP-socket \'' . $this->host . ':' . $port . '\' (' . $errno . ' - ' . socket_strerror($errno) . ')');
93
                return false;
94
            }
95
        }
96
        $this->setFd($sock);
97
        return true;
98
    }
99
100
    /**
101
     * Called when socket is bound
102
     * @return boolean|null Success
103
     */
104
    protected function onBound()
105
    {
106
        if ($this->ev) {
107
            $this->ev->getSocketName($this->locHost, $this->locPort);
0 ignored issues
show
Bug introduced by
The property locHost does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
The property locPort does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
108
        } else {
109
            Daemon::log('Unable to bind TCP-socket ' . $this->host . ':' . $this->getPort());
110
        }
111
    }
112
113
    /**
114
     * @return int
115
     */
116
    public function getPort()
117
    {
118
        return isset($this->port) ? $this->port : $this->defaultPort;
119
    }
120
}
121