|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* DronePHP (http://www.dronephp.com) |
|
4
|
|
|
* |
|
5
|
|
|
* @link http://github.com/Pleets/DronePHP |
|
6
|
|
|
* @copyright Copyright (c) 2016-2018 Pleets. (http://www.pleets.org) |
|
7
|
|
|
* @license http://www.dronephp.com/license |
|
8
|
|
|
* @author Darío Rivera <[email protected]> |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Drone\Network\Socket; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* AbstractSocket class |
|
15
|
|
|
* |
|
16
|
|
|
* This class defines some standard functions to interact with sockets. |
|
17
|
|
|
* Client and Server behaviors must be implemented. |
|
18
|
|
|
*/ |
|
19
|
|
|
abstract class AbstractSocket |
|
20
|
|
|
{ |
|
21
|
|
|
use \Drone\Error\ErrorTrait; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Host |
|
25
|
|
|
* |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $host; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Port |
|
32
|
|
|
* |
|
33
|
|
|
* @var integer |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $port; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Socket resource |
|
39
|
|
|
* |
|
40
|
|
|
* @var resource |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $socket; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Returns the host attribute |
|
46
|
|
|
* |
|
47
|
|
|
* @return string |
|
48
|
|
|
*/ |
|
49
|
|
|
public function getHost() |
|
50
|
|
|
{ |
|
51
|
|
|
return $this->host; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Returns the port attribute |
|
56
|
|
|
* |
|
57
|
|
|
* @return integer |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getPort() |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->port; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Returns the socket attribute |
|
66
|
|
|
* |
|
67
|
|
|
* @return resource|boolean |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getSocket() |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->socket; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Sets host attribute |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $host |
|
78
|
|
|
* |
|
79
|
|
|
* @return null |
|
80
|
|
|
*/ |
|
81
|
|
|
public function setHost($host) |
|
82
|
|
|
{ |
|
83
|
|
|
$this->host = $host; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Sets port attribute |
|
88
|
|
|
* |
|
89
|
|
|
* @param integer $port |
|
90
|
|
|
* |
|
91
|
|
|
* @return null |
|
92
|
|
|
*/ |
|
93
|
|
|
public function setPort($port) |
|
94
|
|
|
{ |
|
95
|
|
|
$this->port = $port; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Driver Constructor (here socket will be created!) |
|
100
|
|
|
* |
|
101
|
|
|
* All modifiable attributes (i.e. with setter method) can be passed as key |
|
102
|
|
|
* |
|
103
|
|
|
* |
|
104
|
|
|
* @param array $options |
|
105
|
|
|
* @throws RuntimeException |
|
106
|
|
|
*/ |
|
107
|
|
|
public function __construct($options) |
|
108
|
|
|
{ |
|
109
|
|
|
foreach ($options as $option => $value) { |
|
110
|
|
|
if (property_exists(__CLASS__, strtolower($option)) && method_exists($this, 'set'.$option)) { |
|
111
|
|
|
$this->{'set'.$option}($value); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
if (!($socket = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP))) { |
|
116
|
|
|
$errno = socket_last_error(); |
|
117
|
|
|
$this->error($errno, socket_strerror($errno)); |
|
|
|
|
|
|
118
|
|
|
|
|
119
|
|
|
throw new \RuntimeException("Could not create the socket"); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
$this->socket = $socket; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Binds the socket |
|
127
|
|
|
* |
|
128
|
|
|
* @return bool |
|
129
|
|
|
*/ |
|
130
|
|
|
public function bind() |
|
131
|
|
|
{ |
|
132
|
|
|
if (!($bind = @socket_bind($this->socket, $this->host, $this->port))) { |
|
133
|
|
|
$errno = socket_last_error(); |
|
134
|
|
|
$this->error($errno, socket_strerror($errno)); |
|
135
|
|
|
|
|
136
|
|
|
return false; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
return $bind; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Closes the socket |
|
144
|
|
|
* |
|
145
|
|
|
* @return void |
|
146
|
|
|
*/ |
|
147
|
|
|
public function close() |
|
148
|
|
|
{ |
|
149
|
|
|
return socket_close($this->socket); |
|
|
|
|
|
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|