1 | <?php namespace Comodojo\Daemon\Socket; |
||
21 | abstract class AbstractSocket { |
||
22 | |||
23 | const VERSION = '1.0'; |
||
24 | |||
25 | const DEFAULT_READ_BUFFER = 1024; |
||
26 | |||
27 | const DEFAULT_SOCKET_PORT = 10042; |
||
28 | |||
29 | protected $socket; |
||
30 | |||
31 | protected $handler; |
||
32 | |||
33 | protected $socket_domain; |
||
34 | |||
35 | protected $socket_type = SOCK_STREAM; |
||
36 | |||
37 | protected $socket_protocol = 0; |
||
38 | |||
39 | protected $socket_resource; |
||
40 | |||
41 | protected $socket_port = 0; |
||
42 | |||
43 | protected $read_buffer; |
||
44 | |||
45 | 5 | public function __construct($handler, $read_buffer = null) { |
|
46 | |||
47 | 5 | $this->setHandler($handler); |
|
48 | |||
49 | 5 | $this->read_buffer = is_null($read_buffer) |
|
50 | 5 | ? self::DEFAULT_READ_BUFFER |
|
51 | 5 | : DataFilter::filterInteger($read_buffer, 1024, PHP_INT_MAX, self::DEFAULT_READ_BUFFER); |
|
52 | |||
53 | 5 | } |
|
54 | |||
55 | public function getSocket() { |
||
60 | |||
61 | abstract public function connect(); |
||
62 | |||
63 | abstract public function close(); |
||
64 | |||
65 | protected function setSocket($socket) { |
||
72 | |||
73 | 5 | protected function setHandler($handler) { |
|
74 | |||
75 | 5 | $this->handler = $handler; |
|
76 | |||
77 | 5 | list($domain, $resource) = preg_split( '@(:\/\/)@', $handler ); |
|
78 | |||
79 | 5 | $domain = strtolower($domain); |
|
80 | |||
81 | 5 | if ( $domain == 'unix' ) { |
|
82 | $this->socket_domain = AF_UNIX; |
||
83 | $this->socket_resource = $resource; |
||
84 | } else { |
||
85 | 5 | $this->socket_domain = AF_INET; |
|
86 | 5 | $this->socket_protocol = $domain == 'udp' ? SOL_UDP : SOL_TCP; |
|
87 | 5 | if (strpos($resource, ':') !== false) { |
|
88 | 5 | list($this->socket_resource, $this->socket_port) = explode(":", $resource); |
|
89 | 5 | } else { |
|
90 | $this->socket_resource = $resource; |
||
91 | $this->socket_port = self::DEFAULT_SOCKET_PORT; |
||
92 | } |
||
93 | |||
94 | } |
||
95 | |||
96 | 5 | } |
|
97 | |||
98 | public static function getSocketError($socket = null) { |
||
103 | |||
104 | } |
||
105 |