12
12
import sys
13
13
import errno
14
14
import codecs
15
- import Queue
15
+ from collections import deque
16
16
from BaseHTTPServer import BaseHTTPRequestHandler
17
17
from StringIO import StringIO
18
18
from select import select
@@ -81,7 +81,7 @@ def __init__(self, server, sock, address):
81
81
self .frag_buffer = None
82
82
self .frag_decoder = codecs .getincrementaldecoder ('utf-8' )(errors = 'strict' )
83
83
self .closed = False
84
- self .sendq = Queue . Queue ()
84
+ self .sendq = deque ()
85
85
86
86
self .state = HEADERB1
87
87
@@ -151,7 +151,7 @@ def _handlePacket(self):
151
151
status = 1002
152
152
153
153
self .close (status , reason )
154
- raise Exception ("received client close" )
154
+ # raise Exception("received client close")
155
155
156
156
if self .fin == 0 :
157
157
if self .opcode != STREAM :
@@ -245,7 +245,7 @@ def _handleData(self):
245
245
if self .request .headers .has_key ('Sec-WebSocket-Key' .lower ()):
246
246
key = self .request .headers ['Sec-WebSocket-Key' .lower ()]
247
247
hStr = HANDSHAKE_STR % { 'acceptstr' : base64 .b64encode (hashlib .sha1 (key + GUID_STR ).digest ()) }
248
- self ._sendBuffer ( hStr )
248
+ self .sendq . append (( BINARY , hStr ) )
249
249
self .handshaked = True
250
250
self .headerbuffer = ''
251
251
self .handleConnected ()
@@ -280,7 +280,7 @@ def close(self, status = 1000, reason = u''):
280
280
else :
281
281
close_msg .extend (reason )
282
282
283
- self ._sendMessage (False , CLOSE , str (close_msg ), False )
283
+ self ._sendMessage (False , CLOSE , str (close_msg ))
284
284
285
285
finally :
286
286
self .closed = True
@@ -289,24 +289,27 @@ def close(self, status = 1000, reason = u''):
289
289
def _sendBuffer (self , buff ):
290
290
size = len (buff )
291
291
tosend = size
292
- index = 0
292
+ already_sent = 0
293
293
294
294
while tosend > 0 :
295
295
try :
296
296
# i should be able to send a bytearray
297
- sent = self .client .send (buff [index : size ])
297
+ sent = self .client .send (buff [already_sent : ])
298
298
if sent == 0 :
299
299
raise RuntimeError ("socket connection broken" )
300
300
301
- index += sent
301
+ already_sent += sent
302
302
tosend -= sent
303
303
304
304
except socket .error as e :
305
305
# if we have full buffers then wait for them to drain and try again
306
306
if e .errno == errno .EAGAIN :
307
- time .sleep (0.001 )
307
+ #time.sleep(0.001)
308
+ return buff [already_sent :]
308
309
else :
309
310
raise e
311
+
312
+ return None
310
313
311
314
def sendFragmentStart (self , data ):
312
315
"""
@@ -353,7 +356,7 @@ def sendMessage(self, data):
353
356
self ._sendMessage (False , opcode , data )
354
357
355
358
356
- def _sendMessage (self , fin , opcode , data , useq = True ):
359
+ def _sendMessage (self , fin , opcode , data ):
357
360
header = bytearray ()
358
361
b1 = 0
359
362
b2 = 0
@@ -387,10 +390,8 @@ def _sendMessage(self, fin, opcode, data, useq = True):
387
390
else :
388
391
payload = str (header )
389
392
390
- if useq is True :
391
- self .sendq .put (payload )
392
- else :
393
- self ._sendBuffer (payload )
393
+ self .sendq .append ((opcode , payload ))
394
+
394
395
395
396
def _parseMessage (self , byte ):
396
397
# read in the header
@@ -580,15 +581,24 @@ def close(self):
580
581
581
582
def serveforever (self ):
582
583
while True :
583
- rList , wList , xList = select (self .listeners , self .listeners , self .listeners , 1 )
584
+ rList , wList , xList = select (self .listeners , self .listeners , self .listeners , 3 )
584
585
585
586
for ready in wList :
586
587
client = None
587
588
try :
588
589
client = self .connections [ready ]
589
- while not client .sendq .empty ():
590
- client ._sendBuffer (client .sendq .get ())
590
+ while client .sendq :
591
+ opcode , payload = client .sendq .popleft ()
592
+ remaining = client ._sendBuffer (payload )
593
+ if remaining is not None :
594
+ client .sendq .appendleft ((opcode , remaining ))
595
+ break
596
+ else :
597
+ if opcode == CLOSE :
598
+ raise Exception ("received client close" )
599
+
591
600
except Exception as n :
601
+
592
602
if client :
593
603
client .client .close ()
594
604
0 commit comments