PEAR2_Net_RouterOS-1.0.0b2 › PEAR2_Net_RouterOS-1.0.0b2/src/PEAR2/Net/Transmitter/TcpClient.php
- PEAR2_Net_RouterOS-1.0.0b2/
- docs/
- docblox.xml
- docblox.xsd
- doxygen.ini
- tutorials/
- PEAR2_Net_RouterOS/
- examples/
- src/
- PEAR2/
- Net/
- RouterOS/
- Transmitter/
- Net/
- PEAR2/
- tests/
- docs/
- package.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
/**
* ~~summary~~
*
* ~~description~~
*
* PHP version 5
*
* @category Net
* @package PEAR2_Net_Transmitter
* @author Vasil Rangelov <boen.robot@gmail.com>
* @copyright 2011 Vasil Rangelov
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version SVN: $WCREV$
* @link http://pear2.php.net/PEAR2_Net_Transmitter
*/
/**
* The namespace declaration.
*/
namespace PEAR2\Net\Transmitter;
/**
* A socket transmitter.
*
* This is a convinience wrapper for socket functionality. Used to ensure data
* integrity.
*
* @category Net
* @package PEAR2_Net_Transmitter
* @author Vasil Rangelov <boen.robot@gmail.com>
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @link http://pear2.php.net/PEAR2_Net_Transmitter
*/
class TcpClient extends NetworkStream
{
/**
* @var int The error code of the last error on the socket.
*/
protected $error_no = null;
/**
* @var string The error message of the last error on the socket.
*/
protected $error_str = null;
/**
* Creates a new connection with the specified options.
*
* @param string $host Hostname (IP or domain) of the server.
* @param int $port The port on the server.
* @param bool $persist Whether or not the connection should be a
* persistent one.
* @param float $timeout The timeout for the connection.
* @param string $key A string that uniquely identifies the
* connection.
* @param resource $context A context for the socket.
*/
public function __construct($host, $port, $persist = false,
$timeout = null, $key = '', $context = null
) {
if (strpos($host, ':') !== false) {
$host = "[{$host}]";
}
$flags = STREAM_CLIENT_CONNECT;
if ($persist) {
$flags |= STREAM_CLIENT_PERSISTENT;
}
$timeout
= null == $timeout ? ini_get('default_socket_timeout') : $timeout;
$key = rawurlencode($key);
if (null === $context) {
$context = stream_context_get_default();
} elseif (
(!is_resource($context))
|| ('stream-context' !== get_resource_type($context))
) {
throw $this->createException('Invalid context supplied.', 6);
}
try {
parent::__construct(
@stream_socket_client(
"tcp://{$host}:{$port}/{$key}", $this->error_no,
$this->error_str, $timeout, $flags, $context
)
);
} catch (\Exception $e) {
throw $this->createException('Failed to initialize socket.', 7);
}
}
/**
* Creates a new exception.
*
* Creates a new exception. Used by the rest of the functions in this class.
*
* @param string $message The exception message.
* @param int $code The exception code.
*
* @return SocketException The exception to then be thrown.
*/
protected function createException($message, $code = 0)
{
return new SocketException(
$message, $code, null, $this->error_no, $this->error_str
);
}
}EOF
