-
Notifications
You must be signed in to change notification settings - Fork 0
/
Connection.php
143 lines (120 loc) · 3.95 KB
/
Connection.php
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
namespace MiksIr\proxy;
abstract class Connection {
const READ = 1;
const WRITE = 2;
const READ_BUFFER = 4096;
/** @var ConnectionsCollection */
protected $collection;
protected $socket;
protected $logger;
protected $write_buffer = '';
protected $buffer = '';
protected $name;
public $id;
private static $idcount = 1;
public function __construct(ConnectionsCollection $collection, LoggerAbstract $logger)
{
$this->collection = $collection;
$this->logger = $logger;
$this->id = self::$idcount++;
$this->logger->log(LoggerAbstract::DEBUG, __CLASS__."::__construct ID#{$this->id}");
}
/**
* @return mixed
*/
public function getSocket()
{
return $this->socket;
}
/**
* @param mixed $socket
*/
public function setSocket($socket)
{
$this->socket = $socket;
}
/**
* Создание сокета
* @return mixed
*/
public function create() {
$this->collection->add($this);
}
/**
* Операция чтения сокета, вызывается после select()
* @return mixed
*/
public function readSocket()
{
$this->logger->log(LoggerAbstract::DEBUG, __CLASS__."::readSocket #{$this->name}");
$new_data = socket_read($this->socket, self::READ_BUFFER - count($this->buffer), PHP_BINARY_READ);
if ($new_data === '') {
$this->logger->log(LoggerAbstract::INFO, "#{$this->name} connection closed: ".socket_strerror(socket_last_error($this->socket)), ['caller' => __CLASS__.":".__LINE__]);
$this->close();
return;
}
if ($new_data === false) {
$this->logger->log(LoggerAbstract::INFO, "#{$this->name} socket_read error: ".socket_strerror(socket_last_error($this->socket)), ['caller' => __CLASS__.":".__LINE__]);
$this->close();
return;
}
$this->buffer .= $new_data;
$this->logger->log(LoggerAbstract::DEBUG, "#{$this->name} read {$new_data}", ['caller' => __CLASS__.":".__LINE__]);
}
/**
* Операция записи в сокет, вызывается после select()
* @return mixed
*/
public function writeSocket()
{
$this->logger->log(LoggerAbstract::DEBUG, __CLASS__."::writeSocket #{$this->name}");
if (strlen($this->write_buffer) !== 0) {
$size = socket_write($this->socket, $this->write_buffer);
if ($size === false || socket_last_error($this->socket)) {
$this->logger->log(LoggerAbstract::WARNING, "#{$this->name} write fail: " . socket_strerror(socket_last_error($this->socket)), ['caller' => __CLASS__ . ":" . __LINE__]);
$this->close();
return;
}
$this->write_buffer = substr($this->write_buffer, $size);
$this->logger->log(LoggerAbstract::DEBUG, "#{$this->name} write {$size} bytes", ['caller' => __CLASS__.":".__LINE__]);
}
}
/**
* Забрать уже полученные данные (накапливаются в классе)
* @param bool $clean
* @return mixed
*/
public function getData($clean = true)
{
$buffer = $this->buffer;
if ($clean) {
$this->buffer = '';
}
return $buffer;
}
/**
* Отправить
* @param $buffer
* @return mixed
*/
public function putData($buffer)
{
$this->write_buffer .= $buffer;
}
/**
* Закрыть соединение
* @return mixed
*/
public function close()
{
$this->logger->log(LoggerAbstract::DEBUG, __CLASS__."::close #{$this->name}");
$this->buffer = '';
$this->write_buffer = '';
$this->collection->remove($this);
if ($this->socket) {
socket_close($this->socket);
}
}
abstract public function mode();
}