@@ -605,74 +605,76 @@ def _handleClose(self, client):
605
605
except :
606
606
pass
607
607
608
- def serveforever (self ):
609
- while True :
610
- writers = []
611
- for fileno in self .listeners :
612
- if fileno == self .serversocket :
613
- continue
614
- client = self .connections [fileno ]
615
- if client .sendq :
616
- writers .append (fileno )
608
+ def serveonce (self ):
609
+ writers = []
610
+ for fileno in self .listeners :
611
+ if fileno == self .serversocket :
612
+ continue
613
+ client = self .connections [fileno ]
614
+ if client .sendq :
615
+ writers .append (fileno )
616
+
617
+ if self .selectInterval :
618
+ rList , wList , xList = select (self .listeners , writers , self .listeners , self .selectInterval )
619
+ else :
620
+ rList , wList , xList = select (self .listeners , writers , self .listeners )
617
621
618
- if self .selectInterval :
619
- rList , wList , xList = select (self .listeners , writers , self .listeners , self .selectInterval )
620
- else :
621
- rList , wList , xList = select (self .listeners , writers , self .listeners )
622
+ for ready in wList :
623
+ client = self .connections [ready ]
624
+ try :
625
+ while client .sendq :
626
+ opcode , payload = client .sendq .popleft ()
627
+ remaining = client ._sendBuffer (payload )
628
+ if remaining is not None :
629
+ client .sendq .appendleft ((opcode , remaining ))
630
+ break
631
+ else :
632
+ if opcode == CLOSE :
633
+ raise Exception ('received client close' )
634
+
635
+ except Exception as n :
636
+ self ._handleClose (client )
637
+ del self .connections [ready ]
638
+ self .listeners .remove (ready )
622
639
623
- for ready in wList :
640
+ for ready in rList :
641
+ if ready == self .serversocket :
642
+ try :
643
+ sock , address = self .serversocket .accept ()
644
+ newsock = self ._decorateSocket (sock )
645
+ newsock .setblocking (0 )
646
+ fileno = newsock .fileno ()
647
+ self .connections [fileno ] = self ._constructWebSocket (newsock , address )
648
+ self .listeners .append (fileno )
649
+ except Exception as n :
650
+ if sock is not None :
651
+ sock .close ()
652
+ else :
653
+ if ready not in self .connections :
654
+ continue
624
655
client = self .connections [ready ]
625
656
try :
626
- while client .sendq :
627
- opcode , payload = client .sendq .popleft ()
628
- remaining = client ._sendBuffer (payload )
629
- if remaining is not None :
630
- client .sendq .appendleft ((opcode , remaining ))
631
- break
632
- else :
633
- if opcode == CLOSE :
634
- raise Exception ('received client close' )
635
-
657
+ client ._handleData ()
636
658
except Exception as n :
637
659
self ._handleClose (client )
638
660
del self .connections [ready ]
639
661
self .listeners .remove (ready )
640
662
641
- for ready in rList :
642
- if ready == self .serversocket :
643
- try :
644
- sock , address = self .serversocket .accept ()
645
- newsock = self ._decorateSocket (sock )
646
- newsock .setblocking (0 )
647
- fileno = newsock .fileno ()
648
- self .connections [fileno ] = self ._constructWebSocket (newsock , address )
649
- self .listeners .append (fileno )
650
- except Exception as n :
651
- if sock is not None :
652
- sock .close ()
653
- else :
654
- if ready not in self .connections :
655
- continue
656
- client = self .connections [ready ]
657
- try :
658
- client ._handleData ()
659
- except Exception as n :
660
- self ._handleClose (client )
661
- del self .connections [ready ]
662
- self .listeners .remove (ready )
663
-
664
- for failed in xList :
665
- if failed == self .serversocket :
666
- self .close ()
667
- raise Exception ('server socket failed' )
668
- else :
669
- if failed not in self .connections :
670
- continue
671
- client = self .connections [failed ]
672
- self ._handleClose (client )
673
- del self .connections [failed ]
674
- self .listeners .remove (failed )
663
+ for failed in xList :
664
+ if failed == self .serversocket :
665
+ self .close ()
666
+ raise Exception ('server socket failed' )
667
+ else :
668
+ if failed not in self .connections :
669
+ continue
670
+ client = self .connections [failed ]
671
+ self ._handleClose (client )
672
+ del self .connections [failed ]
673
+ self .listeners .remove (failed )
675
674
675
+ def serveforever (self ):
676
+ while True :
677
+ self .serveonce ()
676
678
677
679
class SimpleSSLWebSocketServer (SimpleWebSocketServer ):
678
680
0 commit comments