MCPcopy Index your code
hub / github.com/python-websockets/websockets / close_expected

Method close_expected

src/websockets/protocol.py:517–558  ·  view source on GitHub ↗

Tell if the TCP connection is expected to close soon. Call this method immediately after any of the ``receive_*()``, ``send_close()``, or :meth:`fail` methods. If it returns :obj:`True`, schedule closing the TCP connection after a short timeout if the other

(self)

Source from the content-addressed store, hash-verified

515 return writes
516
517 def close_expected(self) -> bool:
518 """
519 Tell if the TCP connection is expected to close soon.
520
521 Call this method immediately after any of the ``receive_*()``,
522 ``send_close()``, or :meth:`fail` methods.
523
524 If it returns :obj:`True`, schedule closing the TCP connection after a
525 short timeout if the other side hasn't already closed it.
526
527 Returns:
528 Whether the TCP connection is expected to close soon.
529
530 """
531 # During the opening handshake, when our state is CONNECTING, we expect
532 # a TCP close if and only if the hansdake fails. When it does, we start
533 # the TCP closing handshake by sending EOF with send_eof().
534
535 # Once the opening handshake completes successfully, we expect a TCP
536 # close if and only if we sent a close frame, meaning that our state
537 # progressed to CLOSING:
538
539 # * Normal closure: once we send a close frame, we expect a TCP close:
540 # server waits for client to complete the TCP closing handshake;
541 # client waits for server to initiate the TCP closing handshake.
542
543 # * Abnormal closure: we always send a close frame and the same logic
544 # applies, except on EOFError where we don't send a close frame
545 # because we already received the TCP close, so we don't expect it.
546
547 # If our state is CLOSED, we already received a TCP close so we don't
548 # expect it anymore.
549
550 # Micro-optimization: put the most common case first
551 if self.state is OPEN:
552 return False
553 if self.state is CLOSING:
554 return True
555 if self.state is CLOSED:
556 return False
557 assert self.state is CONNECTING
558 return self.eof_sent
559
560 # Private methods for receiving data.
561

Calls

no outgoing calls