PEAR2_Net_RouterOS-1.0.0b3 › PEAR2_Net_RouterOS-1.0.0b3/src/PEAR2/Net/Transmitter/TcpServerConnection.php
- PEAR2_Net_RouterOS-1.0.0b3/
- docs/
- doxygen.ini
- phpdoc.dist.xml
- tutorials/
- PEAR2_Net_RouterOS/
- examples/
- src/
- PEAR2/
- Autoload.php
- Cache/
- Net/
- RouterOS/
- Transmitter/
- 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
115
116
117
118
119
120
121
122
123
<?php
/**
* Wrapper for network stream functionality.
*
* PHP has built in support for various types of network streams, such as HTTP and TCP sockets. One problem that arises with them is the fact that a single fread/fwrite call might not read/write all the data you intended, regardless of whether you're in blocking mode or not. While the PHP manual offers a workaround in the form of a loop with a few variables, using it every single time you want to read/write can be tedious.
This package abstracts this away, so that when you want to get exactly N amount of bytes, you can be sure the upper levels of your app will be dealing with N bytes. Oh, and the functionality is nicely wrapped in an object (but that's just the icing on the cake).
*
* 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 1.0.0a3
* @link http://pear2.php.net/PEAR2_Net_Transmitter
*/
/**
* The namespace declaration.
*/
namespace PEAR2\Net\Transmitter;
/**
* A transmitter for connections to a socket server.
*
* This is a convinience wrapper for functionality of socket server connections.
* Used to ensure data integrity. Server handling is not part of the class in
* order to allow its usage as part of various server implementations (e.g. fork
* and/or sequential).
*
* @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 TcpServerConnection extends NetworkStream
{
/**
* @var string The IP address of the connected client.
*/
protected $peerIP;
/**
* @var int The port of the connected client.
*/
protected $peerPort;
/**
* Creates a new connection with the specified options.
*
* @param resource $server A socket server, created with
* {@link stream_socket_server()}.
* @param float $timeout The timeout for the connection.
*/
public function __construct($server, $timeout = null)
{
if (!self::isStream($server)) {
throw $this->createException('Invalid server supplied.', 8);
}
$timeout
= null == $timeout ? ini_get('default_socket_timeout') : $timeout;
try {
parent::__construct(
@stream_socket_accept($server, $timeout, $peername)
);
$portString = strrchr($peername, ':');
$this->peerPort = (int) substr($portString, 1);
$ipString = substr(
$peername, 0, strlen($peername) - strlen($portString)
);
if (strpos($ipString, '[') === 0
&& strpos(strrev($ipString), ']') === 0
) {
$ipString = substr($ipString, 1, strlen($ipString) - 2);
}
$this->peerIP = $ipString;
} catch (Exception $e) {
throw $this->createException('Failed to initialize connection.', 9);
}
}
/**
* Gets the IP address of the connected client.
*
* @return string The IP address of the connected client.
*/
public function getPeerIP()
{
return $this->peerIP;
}
/**
* Gets the port of the connected client.
*
* @return int The port of the connected client.
*/
public function getPeerPort()
{
return $this->peerPort;
}
/**
* 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);
}
}EOF
