@@ -473,9 +473,9 @@ def check(self):
473473
474474class SocketService (SimpleService ):
475475 def __init__ (self , configuration = None , name = None ):
476+ self ._sock = None
476477 self .host = "localhost"
477478 self .port = None
478- self .sock = None
479479 self .unix_socket = None
480480 self .request = ""
481481 SimpleService .__init__ (self , configuration = configuration , name = name )
@@ -485,61 +485,84 @@ def _get_raw_data(self):
485485 Get raw data with low-level "socket" module.
486486 :return: str
487487 """
488- if self .sock is None :
488+ if self ._sock is None :
489489 try :
490490 if self .unix_socket is None :
491- sock = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
492- sock .setsockopt (socket .SOL_SOCKET , socket .SO_REUSEADDR , 1 )
491+ self . _sock = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
492+ self . _sock .setsockopt (socket .SOL_SOCKET , socket .SO_REUSEADDR , 1 )
493493 #sock.setsockopt(socket.SOL_SOCKET, socket.TCP_NODELAY, 1)
494494 #sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
495495 #sock.settimeout(self.update_every)
496- sock .settimeout (0.5 )
497- sock .connect ((self .host , self .port ))
498- sock .settimeout (0.5 ) # Just to be sure
496+ self . _sock .settimeout (0.5 )
497+ self . _sock .connect ((self .host , self .port ))
498+ self . _sock .settimeout (0.5 ) # Just to be sure
499499 else :
500- sock = socket .socket (socket .AF_UNIX , socket .SOCK_DGRAM )
501- #sock .settimeout(self.update_every)
502- sock .settimeout (0.05 )
503- sock .connect (self .unix_socket )
504- sock .settimeout (0.05 ) # Just to be sure
500+ self . _sock = socket .socket (socket .AF_UNIX , socket .SOCK_DGRAM )
501+ #self._sock .settimeout(self.update_every)
502+ self . _sock .settimeout (0.05 )
503+ self . _sock .connect (self .unix_socket )
504+ self . _sock .settimeout (0.05 ) # Just to be sure
505505
506506 except Exception as e :
507507 self .error (str (e ), "used configuration: host:" , str (self .host ), "port:" , str (self .port ), "socket:" , str (self .unix_socket ))
508- self .sock = None
508+ self ._sock = None
509509 return None
510510
511511 if self .request != "" .encode ():
512512 try :
513- sock .send (self .request )
513+ self . _sock .send (self .request )
514514 except Exception as e :
515515 try :
516- sock .shutdown (1 )
517- sock .close ()
516+ self . _sock .shutdown (1 )
517+ self . _sock .close ()
518518 except :
519519 pass
520- self .sock = None
520+ self ._sock = None
521521 self .error (str (e ), "used configuration: host:" , str (self .host ), "port:" , str (self .port ), "socket:" , str (self .unix_socket ))
522522 return None
523+ # data = ""
524+ # import select
525+ # while True:
526+ # try:
527+ # ready_to_read, _, in_error = select.select([sock, ], [], [], 0.01)
528+ # except Exception as e:
529+ # self.debug("SELECT", str(e))
530+ # sock.shutdown(0)
531+ # break
532+ # if len(ready_to_read) > 0:
533+ # buf = sock.recv(1024)
534+ # if len(buf) == 0 or buf is None:
535+ # break
536+ # data += buf.decode()
537+ # if data == "":
538+ # return None
539+ # return data
523540
524541 size = 2
525542 try :
526- data = sock .recv (size ).decode ()
543+ data = self . _sock .recv (size ).decode ()
527544 except Exception as e :
528545 self .error (str (e ), "used configuration: host:" , str (self .host ), "port:" , str (self .port ), "socket:" , str (self .unix_socket ))
529- sock .close ()
546+ self . _sock .close ()
530547 return None
531548
532549 while True :
533550 # implement something like TCP Window Scaling
534551 if size < 4096 :
535552 size *= 2
536- buf = sock .recv (size )
553+ buf = self . _sock .recv (size )
537554 data += buf .decode ()
538555 if len (buf ) < size :
539- break
556+ if self ._more_data_available ():
557+ size = 2
558+ else :
559+ break
540560
541561 return data
542562
563+ def _more_data_available (self ):
564+ return False
565+
543566 def _parse_config (self ):
544567 """
545568 Parse configuration data
0 commit comments