Skip to content

Commit 83a5c1c

Browse files
committed
fix: wrong error code on stream error, improved write speed for small frames
1 parent d002705 commit 83a5c1c

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

PhpAmqpLib/Wire/IO/SocketIO.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,12 @@ public function write($data)
189189
try {
190190
$result = 0;
191191
if ($this->select_write()) {
192-
$buffer = mb_substr($data, $written, self::BUFFER_SIZE, 'ASCII');
192+
// if data is smaller than buffer - no need to cut part of it
193+
if ($len <= self::BUFFER_SIZE) {
194+
$buffer = $data;
195+
} else {
196+
$buffer = mb_substr($data, $written, self::BUFFER_SIZE, 'ASCII');
197+
}
193198
$result = socket_write($this->sock, $buffer);
194199
}
195200
$this->throwOnError();

PhpAmqpLib/Wire/IO/StreamIO.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -265,16 +265,17 @@ public function write($data)
265265
// check stream and prevent from high CPU usage
266266
$result = 0;
267267
if ($this->select_write()) {
268-
$buffer = mb_substr($data, $written, self::BUFFER_SIZE, 'ASCII');
268+
// if data is smaller than buffer - no need to cut part of it
269+
if ($len <= self::BUFFER_SIZE) {
270+
$buffer = $data;
271+
} else {
272+
$buffer = mb_substr($data, $written, self::BUFFER_SIZE, 'ASCII');
273+
}
269274
$result = fwrite($this->sock, $buffer);
270275
}
271276
$this->throwOnError();
272277
} catch (\ErrorException $e) {
273-
$code = 999;
274-
if ($this->last_error != null)
275-
{
276-
$code = $this->last_error->getCode();
277-
}
278+
$code = $e->getCode();
278279
$constants = SocketConstants::getInstance();
279280
switch ($code) {
280281
case $constants->SOCKET_EPIPE:

0 commit comments

Comments
 (0)