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
|
|
|
* Server class |
15
|
|
|
* |
16
|
|
|
* Server socket implementation |
17
|
|
|
*/ |
18
|
|
|
class Server extends AbstractSocket |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Reads a message from client |
22
|
|
|
* |
23
|
|
|
* @param resource |
24
|
|
|
* @param mixed $socket |
25
|
|
|
* |
26
|
|
|
* @throws RuntimeException |
27
|
|
|
* |
28
|
|
|
* @return string |
29
|
|
|
*/ |
30
|
|
|
public function read($socket) |
31
|
|
|
{ |
32
|
|
|
if (($message = @socket_read($socket, 1024)) === false) { |
33
|
|
|
$errno = socket_last_error(); |
34
|
|
|
$this->error($errno, socket_strerror($errno)); |
|
|
|
|
35
|
|
|
|
36
|
|
|
throw new \RuntimeException("Could not read message from client socket"); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return $message; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Sends a message to client socket |
44
|
|
|
* |
45
|
|
|
* @param resource $socket |
46
|
|
|
* @param string $message |
47
|
|
|
* |
48
|
|
|
* @throws RuntimeException |
49
|
|
|
* |
50
|
|
|
* @return integer |
51
|
|
|
*/ |
52
|
|
|
public function send($socket, $message) |
53
|
|
|
{ |
54
|
|
|
if (($bytes = @socket_write($socket, $message, strlen($message))) === false) { |
55
|
|
|
$errno = socket_last_error(); |
56
|
|
|
$this->error($errno, socket_strerror($errno)); |
57
|
|
|
|
58
|
|
|
throw new \RuntimeException("Could not send message to the client socket"); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $bytes; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Sets socket to listening |
66
|
|
|
* |
67
|
|
|
* @param array $eventHandlers |
68
|
|
|
* |
69
|
|
|
* @throws RuntimeException |
70
|
|
|
* |
71
|
|
|
* @return boolean |
72
|
|
|
*/ |
73
|
|
|
public function listen(array $eventHandlers = []) |
74
|
|
|
{ |
75
|
|
|
$event = $eventHandlers; |
76
|
|
|
$clousure = function () { |
77
|
|
|
}; |
78
|
|
|
|
79
|
|
|
if (!array_key_exists('success', $event)) { |
80
|
|
|
$event["success"] = $clousure; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if (!array_key_exists('error', $event)) { |
84
|
|
|
$event["error"] = $clousure; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$listener = false; |
|
|
|
|
88
|
|
|
|
89
|
|
|
if (!($listener = @socket_listen($this->socket, 30))) { |
90
|
|
|
$errno = socket_last_error(); |
91
|
|
|
$this->error($errno, socket_strerror($errno)); |
92
|
|
|
|
93
|
|
|
throw new \RuntimeException("Could not set socket to listen"); |
94
|
|
|
} else { |
95
|
|
|
echo "\n"; |
96
|
|
|
echo "Server Started : " . date('Y-m-d H:i:s') . "\n"; |
97
|
|
|
echo "Master socket : " . $this->socket . "\n"; |
|
|
|
|
98
|
|
|
echo "Listening on : " . $this->host . " port " . $this->port . "\n\n"; |
99
|
|
|
|
100
|
|
|
if (!($spawn = @socket_accept($this->socket))) { |
101
|
|
|
$errno = socket_last_error(); |
102
|
|
|
$this->error($errno, socket_strerror($errno)); |
103
|
|
|
|
104
|
|
|
throw new \RuntimeException("Could not accept incoming connection"); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$input = $this->read($spawn); |
108
|
|
|
|
109
|
|
|
$input = trim($input); |
110
|
|
|
call_user_func($event["success"], $input, $this, $spawn); |
111
|
|
|
|
112
|
|
|
socket_close($spawn); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if (!$listener) { |
|
|
|
|
116
|
|
|
call_user_func($event["error"], $this->getLastError()); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $listener; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|