|
6 | 6 | * - - - - - - - - - - - - - BEGIN LICENSE BLOCK - - - - - - - - - - - - - |
7 | 7 | * The MIT License (MIT) |
8 | 8 | * |
9 | | - * Copyright (c) 2020 Kevin Meijer |
| 9 | + * Copyright (c) 2021 Kevin Meijer |
10 | 10 | * |
11 | 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
12 | 12 | * of this software and associated documentation files (the "Software"), to deal |
@@ -131,7 +131,7 @@ abstract class AConnection implements IStreamContainer, LoggerAwareInterface, IT |
131 | 131 | /** |
132 | 132 | * The timestamp since when this connection has been opened |
133 | 133 | * |
134 | | - * @var int |
| 134 | + * @var float|null |
135 | 135 | */ |
136 | 136 | protected $_openedTimestamp = NULL; |
137 | 137 |
|
@@ -215,7 +215,7 @@ abstract class AConnection implements IStreamContainer, LoggerAwareInterface, IT |
215 | 215 | /** |
216 | 216 | * The resource stream |
217 | 217 | * |
218 | | - * @var resource |
| 218 | + * @var resource|null |
219 | 219 | */ |
220 | 220 | protected $_stream = NULL; |
221 | 221 |
|
@@ -478,9 +478,10 @@ protected function _handlePacket(string $newData) : \Generator { |
478 | 478 |
|
479 | 479 | if ($res === FALSE) { |
480 | 480 |
|
481 | | - $this->close(); |
482 | 481 | yield new Update\Error(Update\Error::C_WRITE_INVALID_TARGET_STREAM, $this); |
483 | 482 |
|
| 483 | + $this->close(); |
| 484 | + |
484 | 485 | } |
485 | 486 |
|
486 | 487 | } else { |
@@ -540,10 +541,10 @@ protected function _handlePacket(string $newData) : \Generator { |
540 | 541 |
|
541 | 542 | $this->_isClosed = TRUE; |
542 | 543 |
|
543 | | - $this->close(); |
544 | | - |
545 | 544 | yield new Update\Read(Update\Read::C_SOCK_DISCONNECT, $this); |
546 | 545 |
|
| 546 | + $this->close(); |
| 547 | + |
547 | 548 | } elseif (!$this->_weSentDisconnect) { |
548 | 549 |
|
549 | 550 | $this->_log(LogLevel::DEBUG, ' Remote initiated the disconnect, echo disconnect'); |
@@ -717,7 +718,18 @@ public function writeMultiFramed(string $data, int $opcode = \PHPWebSockets::OPC |
717 | 718 | throw new \LogicException('FrameSize should be at least 1'); |
718 | 719 | } |
719 | 720 |
|
720 | | - $frames = str_split($data, $frameSize); |
| 721 | + $dataLen = strlen($data); |
| 722 | + if (strlen($data) > $frameSize) { |
| 723 | + |
| 724 | + $frames = str_split($data, $frameSize); |
| 725 | + if ($frames === FALSE) { |
| 726 | + throw new \UnexpectedValueException('Unable to split ' . $dataLen . ' bytes into frames of ' . $frameSize . ' bytes'); |
| 727 | + } |
| 728 | + |
| 729 | + } else { |
| 730 | + $frames = [$data]; |
| 731 | + } |
| 732 | + |
721 | 733 | end($frames); |
722 | 734 | $lastKey = key($frames); |
723 | 735 |
|
@@ -757,6 +769,10 @@ protected function _getStreamForNewMessage(array $headers) { |
757 | 769 | */ |
758 | 770 | public function writeRaw(string $data, bool $priority) : void { |
759 | 771 |
|
| 772 | + if (!$this->isOpen()) { |
| 773 | + throw new \LogicException('Unable to write: Connection is closed'); |
| 774 | + } |
| 775 | + |
760 | 776 | if ($priority) { |
761 | 777 | $this->_priorityFramesBuffer[] = $data; |
762 | 778 | } else { |
@@ -859,7 +875,7 @@ public function setNewMessageStreamCallback(callable $callable = NULL) : void { |
859 | 875 | /** |
860 | 876 | * Returns the stream object for this connection |
861 | 877 | * |
862 | | - * @return resource |
| 878 | + * @return resource|null |
863 | 879 | */ |
864 | 880 | public function getStream() { |
865 | 881 | return $this->_stream; |
@@ -960,10 +976,31 @@ public function close() : void { |
960 | 976 | $this->_isClosed = TRUE; |
961 | 977 |
|
962 | 978 | if (is_resource($this->_stream)) { |
963 | | - fclose($this->_stream); |
| 979 | + |
| 980 | + $stream = $this->_stream; |
964 | 981 | $this->_stream = NULL; |
| 982 | + |
| 983 | + fclose($stream); |
| 984 | + |
965 | 985 | } |
966 | 986 |
|
| 987 | + // To prevent the warning that there was still data to write |
| 988 | + $this->_clearBuffers(); |
| 989 | + $this->_resetFrameData(); |
| 990 | + |
| 991 | + } |
| 992 | + |
| 993 | + /** |
| 994 | + * Clears all buffers |
| 995 | + */ |
| 996 | + private function _clearBuffers() : void { |
| 997 | + |
| 998 | + $this->_currentFrameRemainingBytes = 0; |
| 999 | + $this->_priorityFramesBuffer = []; |
| 1000 | + $this->_framesBuffer = []; |
| 1001 | + $this->_writeBuffer = NULL; |
| 1002 | + $this->_readBuffer = NULL; |
| 1003 | + |
967 | 1004 | } |
968 | 1005 |
|
969 | 1006 | /** |
|
0 commit comments