22
33namespace Bolt ;
44
5- use Bolt \PackStream \IUnpacker ;
6- use Exception ;
7-
85/**
96 * Static class Socket
107 *
@@ -23,7 +20,7 @@ final class Socket
2320 * @param string $ip
2421 * @param int $port
2522 * @param int $timeout
26- * @throws Exception
23+ * @throws \ Exception
2724 */
2825 public function __construct (string $ ip , int $ port , int $ timeout )
2926 {
@@ -56,9 +53,9 @@ public function __construct(string $ip, int $port, int $timeout)
5653 }
5754
5855 /**
59- * Write to socket
56+ * Write buffer to socket
6057 * @param string $buffer
61- * @throws Exception
58+ * @throws \ Exception
6259 */
6360 public function write (string $ buffer )
6461 {
@@ -71,7 +68,7 @@ public function write(string $buffer)
7168 $ sent = 0 ;
7269
7370 if (Bolt::$ debug )
74- Bolt:: printHex ($ buffer );
71+ $ this -> printHex ($ buffer );
7572
7673 while ($ sent < $ size ) {
7774 $ sent = socket_write ($ this ->socket , $ buffer , $ size );
@@ -86,58 +83,21 @@ public function write(string $buffer)
8683 }
8784 }
8885
89- /**
90- * Read unpacked from socket
91- * @param IUnpacker $unpacker
92- * @return mixed
93- * @throws Exception
94- */
95- public function read (IUnpacker $ unpacker )
96- {
97- if (!is_resource ($ this ->socket )) {
98- Bolt::error ('Not initialized socket ' );
99- return ;
100- }
101-
102- $ msg = '' ;
103- while (true ) {
104- $ header = $ this ->readBuffer (2 );
105- if (ord ($ header [0 ]) == 0x00 && ord ($ header [1 ]) == 0x00 )
106- break ;
107- $ length = unpack ('n ' , $ header )[1 ] ?? 0 ;
108- $ msg .= $ this ->readBuffer ($ length );
109- }
110-
111- $ output = null ;
112- $ signature = 0 ;
113- if (!empty ($ msg )) {
114- if (Bolt::$ debug )
115- Bolt::printHex ($ msg , false );
116-
117- try {
118- $ output = $ unpacker ->unpack ($ msg , $ signature );
119- } catch (Exception $ ex ) {
120- Bolt::error ($ ex ->getMessage ());
121- }
122- }
123-
124- return [$ signature , $ output ];
125- }
126-
12786 /**
12887 * Read buffer from socket
12988 * @param int $length
13089 * @return string
131- * @throws Exception
90+ * @throws \ Exception
13291 */
133- public function readBuffer (int $ length = 2048 ): string
92+ public function read (int $ length = 2048 ): string
13493 {
94+ $ output = '' ;
95+
13596 if (!is_resource ($ this ->socket )) {
13697 Bolt::error ('Not initialized socket ' );
137- return ;
98+ return $ output ;
13899 }
139100
140- $ output = '' ;
141101 do {
142102 $ readed = socket_read ($ this ->socket , $ length - mb_strlen ($ output , '8bit ' ), PHP_BINARY_READ );
143103 if ($ readed === false ) {
@@ -147,9 +107,30 @@ public function readBuffer(int $length = 2048): string
147107 $ output .= $ readed ;
148108 }
149109 } while (mb_strlen ($ output , '8bit ' ) < $ length );
110+
111+ if (Bolt::$ debug )
112+ $ this ->printHex ($ output , false );
113+
150114 return $ output ;
151115 }
152116
117+ /**
118+ * Print buffer as HEX
119+ * @param string $str
120+ * @param bool $write
121+ */
122+ private function printHex (string $ str , bool $ write = true )
123+ {
124+ $ str = implode (unpack ('H* ' , $ str ));
125+ echo '<pre> ' ;
126+ echo $ write ? '> ' : '< ' ;
127+ foreach (str_split ($ str , 8 ) as $ chunk ) {
128+ echo implode (' ' , str_split ($ chunk , 2 ));
129+ echo ' ' ;
130+ }
131+ echo '</pre> ' ;
132+ }
133+
153134 /**
154135 * Close socket
155136 */
0 commit comments