@@ -77,15 +77,15 @@ public function getConfig(): CommonConfig
7777
7878    public  function  isConnected (): bool 
7979    {
80-         return  null  !==  $ this socket ;
80+         return  $ this -> socket  &&  $ this socket -> isConnected () ;
8181    }
8282
8383    public  function  connect (): void 
8484    {
8585        $ config$ this config ;
8686        $ clientnew  Client ($ this getClientType ());
8787        $ clientset ($ this getClientConfig ());
88-         if  ($ clientconnect ($ this host , $ this port )) {
88+         if  ($ clientconnect ($ this host , $ this port ,  $ config -> getConnectTimeout () )) {
8989            $ this socket  = $ client
9090        } else  {
9191            throw  new  ConnectionException (sprintf ('Could not connect to tcp://%s:%s (%s [%d]) ' , $ this host , $ this port , $ clienterrMsg , $ clienterrCode ));
@@ -96,7 +96,6 @@ public function close(): bool
9696    {
9797        if  ($ this socket ) {
9898            $ this socket ->close ();
99-             $ this socket  = null ;
10099            $ this receivedBuffer  = '' ;
101100
102101            return  true ;
@@ -109,6 +108,7 @@ public function send(string $data, ?float $timeout = null): int
109108    {
110109        $ result$ this socket ->send ($ data
111110        if  (false  === $ result
111+             $ this close ();
112112            throw  new  SocketException (sprintf ('Could not write data to stream, %s [%d] ' , $ this socket ->errMsg , $ this socket ->errCode ));
113113        }
114114
@@ -125,6 +125,7 @@ public function recv(int $length, ?float $timeout = null): string
125125        while  ($ this socket  && !isset ($ this receivedBuffer [$ length1 ]) && (-1  == $ timeout$ leftTime0 )) {
126126            $ buffer$ this socket ->recv ($ timeout
127127            if  (''  === $ bufferfalse  === $ buffer
128+                 $ this close ();
128129                throw  new  SocketException (sprintf ('Could not recv data from stream, %s [%d] ' , $ this socket ->errMsg , $ this socket ->errCode ));
129130            }
130131            $ this receivedBuffer  .= $ buffer
@@ -140,7 +141,8 @@ public function recv(int $length, ?float $timeout = null): string
140141            return  $ result
141142        }
142143
143-         if  ($ this socket ) {
144+         if  ($ this socket ->isConnected ()) {
145+             $ this close ();
144146            throw  new  SocketException ('Could not recv data from stream ' );
145147        }
146148
0 commit comments