Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
W
wspy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Taddeüs Kroes
wspy
Commits
57af2bf1
Commit
57af2bf1
authored
Jul 25, 2013
by
Taddeüs Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improved CLOSE handshake implementation, it should work now...
parent
f44555a8
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
10 additions
and
36 deletions
+10
-36
connection.py
connection.py
+10
-36
No files found.
connection.py
View file @
57af2bf1
...
@@ -21,9 +21,7 @@ class Connection(object):
...
@@ -21,9 +21,7 @@ class Connection(object):
"""
"""
self
.
sock
=
sock
self
.
sock
=
sock
self
.
close_frame_sent
=
False
self
.
close_frame_received
=
False
self
.
close_frame_received
=
False
self
.
ping_sent
=
False
self
.
ping_sent
=
False
self
.
ping_payload
=
None
self
.
ping_payload
=
None
...
@@ -72,18 +70,10 @@ class Connection(object):
...
@@ -72,18 +70,10 @@ class Connection(object):
Handle a control frame as defined by RFC 6455.
Handle a control frame as defined by RFC 6455.
"""
"""
if
frame
.
opcode
==
OPCODE_CLOSE
:
if
frame
.
opcode
==
OPCODE_CLOSE
:
# Handle a close message by sending a response close message if no
# Close the connection from this end as well
# CLOSE frame was sent before, and closing the connection. The
# onclose() handler is called afterwards.
self
.
close_frame_received
=
True
self
.
close_frame_received
=
True
code
,
reason
=
frame
.
unpack_close
()
code
,
reason
=
frame
.
unpack_close
()
if
not
self
.
close_frame_sent
:
payload
=
''
if
code
is
None
else
struct
.
pack
(
'!H'
,
code
)
self
.
sock
.
send
(
ControlFrame
(
OPCODE_CLOSE
,
payload
))
self
.
sock
.
close
()
# No more receiving data after a close message
# No more receiving data after a close message
raise
SocketClosed
(
code
,
reason
)
raise
SocketClosed
(
code
,
reason
)
...
@@ -114,19 +104,12 @@ class Connection(object):
...
@@ -114,19 +104,12 @@ class Connection(object):
try
:
try
:
self
.
onmessage
(
self
.
receive
())
self
.
onmessage
(
self
.
receive
())
except
SocketClosed
as
e
:
except
SocketClosed
as
e
:
self
.
onclose
(
e
.
code
,
e
.
reason
)
self
.
close
()
#self.onclose(e.code, e.reason)
break
break
except
Exception
as
e
:
except
Exception
as
e
:
self
.
onerror
(
e
)
self
.
onerror
(
e
)
def
send_close
(
self
,
code
,
reason
):
"""
Send a CLOSE control frame.
"""
payload
=
''
if
code
is
None
else
struct
.
pack
(
'!H'
,
code
)
+
reason
self
.
sock
.
send
(
ControlFrame
(
OPCODE_CLOSE
,
payload
))
self
.
close_frame_sent
=
True
def
send_ping
(
self
,
payload
=
''
):
def
send_ping
(
self
,
payload
=
''
):
"""
"""
Send a PING control frame with an optional payload.
Send a PING control frame with an optional payload.
...
@@ -136,27 +119,18 @@ class Connection(object):
...
@@ -136,27 +119,18 @@ class Connection(object):
self
.
ping_sent
=
True
self
.
ping_sent
=
True
self
.
onping
(
payload
)
self
.
onping
(
payload
)
def
handle_close
(
self
,
code
=
None
,
reason
=
''
):
"""
Handle a close message by sending a response close message if no CLOSE
frame was sent before, and closing the connection. The onclose()
handler is called afterwards.
"""
if
not
self
.
close_frame_sent
:
payload
=
''
if
code
is
None
else
struct
.
pack
(
'!H'
,
code
)
self
.
sock
.
send
(
ControlFrame
(
OPCODE_CLOSE
,
payload
))
self
.
sock
.
close
()
self
.
onclose
(
code
,
reason
)
def
close
(
self
,
code
=
None
,
reason
=
''
):
def
close
(
self
,
code
=
None
,
reason
=
''
):
"""
"""
Close the socket by sending a CLOSE frame and waiting for a response
Close the socket by sending a CLOSE frame and waiting for a response
close message. The onclose() handler is called after the CLOSE frame
close message, unless such a message has already been received earlier
has been sent, but before the response has been received.
(prior to calling this function, for example). The onclose() handler is
called after the response has been received.
"""
"""
self
.
send_close
(
code
,
reason
)
# Send CLOSE frame
payload
=
''
if
code
is
None
else
struct
.
pack
(
'!H'
,
code
)
+
reason
self
.
sock
.
send
(
ControlFrame
(
OPCODE_CLOSE
,
payload
))
# Receive CLOSE frame
if
not
self
.
close_frame_received
:
if
not
self
.
close_frame_received
:
frame
=
self
.
sock
.
recv
()
frame
=
self
.
sock
.
recv
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment