Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 230 #238

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions test/test_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ def test_closing_message_received(self):
ws = WebSocket(sock=m)
with patch.multiple(ws, close=c):
ws.stream = s
s.parser.send = lambda p:1
ws.stream.closing = CloseControlMessage(code=1000, reason='test closing')
ws.process(b'unused for this test')
c.assert_called_once_with(1000, b'test closing')
Expand Down
2 changes: 1 addition & 1 deletion ws4py/framing.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def _parsing(self):
some_bytes = buf[:self.payload_length]

self.body = some_bytes
yield
yield min(0,len(some_bytes)-len(buf))

def mask(self, data):
"""
Expand Down
12 changes: 10 additions & 2 deletions ws4py/streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,20 @@ def receiver(self):
utf8validator = Utf8Validator()
running = True
frame = None
remaining = 0
while running:
frame = Frame()
while 1:
try:
some_bytes = (yield next(frame.parser))
frame.parser.send(some_bytes)
if remaining<0:
#another message left in the pipeline
some_bytes = (yield remaining)
else:
some_bytes = (yield next(frame.parser))
remaining = frame.parser.send(some_bytes)
if remaining < 0:
#another message is waiting, but we can finish this one
next(frame.parser)
except GeneratorExit:
running = False
break
Expand Down
64 changes: 35 additions & 29 deletions ws4py/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,39 +455,45 @@ def process(self, bytes):
if not bytes and self.reading_buffer_size > 0:
return False

self.reading_buffer_size = s.parser.send(bytes) or DEFAULT_READING_SIZE
while bytes:
self.reading_buffer_size = s.parser.send(bytes) or DEFAULT_READING_SIZE

if s.closing is not None:
logger.debug("Closing message received (%d) '%s'" % (s.closing.code, s.closing.reason))
if not self.server_terminated:
self.close(s.closing.code, s.closing.reason)
if self.reading_buffer_size < 0:
bytes = bytes[self.reading_buffer_size:]
else:
self.client_terminated = True
return False
bytes = b''

if s.closing is not None:
logger.debug("Closing message received (%d) '%s'" % (s.closing.code, s.closing.reason))
if not self.server_terminated:
self.close(s.closing.code, s.closing.reason)
else:
self.client_terminated = True
return False

if s.errors:
for error in s.errors:
logger.debug("Error message received (%d) '%s'" % (error.code, error.reason))
self.close(error.code, error.reason)
s.errors = []
return False
if s.errors:
for error in s.errors:
logger.debug("Error message received (%d) '%s'" % (error.code, error.reason))
self.close(error.code, error.reason)
s.errors = []
return False

if s.has_message:
self.received_message(s.message)
if s.message is not None:
s.message.data = None
s.message = None
return True

if s.pings:
for ping in s.pings:
self._write(s.pong(ping.data))
s.pings = []

if s.pongs:
for pong in s.pongs:
self.ponged(pong)
s.pongs = []
if s.has_message:
self.received_message(s.message)
if s.message is not None:
s.message.data = None
s.message = None
continue

if s.pings:
for ping in s.pings:
self._write(s.pong(ping.data))
s.pings = []

if s.pongs:
for pong in s.pongs:
self.ponged(pong)
s.pongs = []

return True

Expand Down