�
Ϫ�fkO � � � d Z ddlmZ ddlZddlZddlZddlmZmZm Z ddl
mZmZ ddl
mZmZmZmZmZ ddlmZmZ ddlmZmZ dd lmZ dd
lmZmZ ddlm Z m!Z!m"Z"m#Z#m$Z$ ddl%m&Z& dd
l'm(Z( ddl)m*Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1 ddl2m3Z3m4Z4m5Z5 ddl6m7Z8 e8Z9erddl;m<Z< e ejz e"ej| iZ? ee&ej� ej� � G d� dej� e1e,� � ZCe9� eeCej� � G d� de-e.eC� ZE G d� deC� ZF G d� de+� Z* eej� � G d� de1e� � ZHy# e:$ r dZ9Y ��w xY w)z
TCP support for IOCP reactor
� )�annotationsN)�
TYPE_CHECKING�Optional�Union)�classImplements�implementer)�address�defer�error�
interfaces�main)� _LogOwner�
isIPv6Address)�IPv4Address�IPv6Address)� IProtocol)�abstract�iocpsupport)�ERROR_CONNECTION_REFUSED�ERROR_IO_PENDING�ERROR_NETWORK_UNREACHABLE�SO_UPDATE_ACCEPT_CONTEXT�SO_UPDATE_CONNECT_CONTEXT)�IReadWriteHandle)�Protocol)� Connector�_AbortingMixin�_BaseBaseClient�_BaseTCPClient�_getsockname�_resolveIPv6�
_SocketCloser)�failure�log�reflect)�startTLS)�IOCPReactorc � � e Zd ZdZdZdd�Zd� Zd� Zd� Zd� Z d � Z
d
� Zd� Zd� Z
d
� Zd� Zd� Zd� Ze�dd�Zd� Zd� Zdd�Zd� Zd� Zd� Zd� Zy)�
Connectionz�
@ivar TLS: C{False} to indicate the connection is in normal TCP mode,
C{True} to indicate that TLS has been started and that operations must
be routed through the L{TLSMemoryBIOProtocol} instance.
FNc � � t j j | |� || _ |j | _ || _ y �N)r �
FileHandle�__init__�socket�fileno�
getFileHandle�protocol)�self�sock�proto�reactors �B/usr/lib/python3/dist-packages/twisted/internet/iocpreactor/tcp.pyr- zConnection.__init__F s2 � ����$�$�T�7�3����!�[�[�����
� c � � | j S r+ )r. �r2 s r6 � getHandlezConnection.getHandleL s � ��{�{�r7 c �� � t |t � rn3t |t � rt |� }nt dt |� z � �| j
j
|� y)z`
@param rbuffer: Data received.
@type rbuffer: L{bytes} or L{bytearray}
z%data must be bytes or bytearray, not N)�
isinstance�bytes� bytearray� TypeError�typer1 �dataReceived)r2 �rbuffers r6 rA zConnection.dataReceivedO sK � �
�g�u�%��
���
+��G�n�G��C�d�7�m�S�T�T��
�
�"�"�7�+r7 c �L � t j | j � ||� S r+ )�_iocp�recvr0 )r2 �bufflist�evts r6 �readFromHandlezConnection.readFromHandle^ s � ��z�z�$�,�,�.��#�>�>r7 c � � t |� }t j | j � |d| j j � |� S )z�
Send C{buff} to current file handle using C{_iocp.send}. The buffer
sent is limited to a size of C{self.SEND_LIMIT}.
r )�
memoryviewrD �sendr0 �
SEND_LIMIT�tobytes)r2 �buffrG � writeViews r6 �
writeToHandlezConnection.writeToHandlea sC � �
�t�$� ��z�z���� �)�A����"@�"H�"H�"J�C�
�
r7 c �R � | j j d� t j | j
d � }|r |j
� y y # t $ r Y �@w xY w# t $ r< t j � }t j � | j |� Y y w xY w)N� )
r. �shutdown�OSErrorr �IHalfCloseableProtocolr1 �writeConnectionLost�
BaseExceptionr# �Failurer$ �err�connectionLost)r2 �p�fs r6 �_closeWriteConnectionz Connection._closeWriteConnectionk s� � � ��K�K� � ��#�
�-�-�d�m�m�T�B���
'��%�%�'�
�� � �� �� !�
'��O�O�%����� ��#�#�A�&�
'�s$ �A � A! � A�A�!AB&�%B&c � � t j | j d � }|r |j � y | j |� y # t $ r: t j � | j t j � � Y y w xY wr+ )
r rU r1 �readConnectionLostrW r$ rY rZ r# rX )r2 �reasonr[ s r6 r_ zConnection.readConnectionLosty sk � ��-�-�d�m�m�T�B���
7��$�$�&�
����'�� !�
7���� ��#�#�G�O�O�$5�6�
7�s �A �A B
� B
c � � | j ry t j j | |� |d u xs |j t
j � }| j |� | j }| `| ` | `
|j |� y r+ )�disconnectedr r, rZ �checkr �ConnectionAborted�_closeSocketr1 r. r0 )r2 r` �isCleanr1 s r6 rZ zConnection.connectionLost� sy � ��������*�*�4��8��D�.�M����U�5L�5L�(M�$M�����'�"��=�=���M��K�������'r7 c � � | j S )zN
Return the prefix to log with when I own the logging thread.
)�logstrr9 s r6 � logPrefixzConnection.logPrefix� � � � �{�{�r7 c � � t | j j t j t j � � S r+ )�boolr. �
getsockopt�IPPROTO_TCP�TCP_NODELAYr9 s r6 �
getTcpNoDelayzConnection.getTcpNoDelay� s* � ��D�K�K�*�*�6�+=�+=�v�?Q�?Q�R�S�Sr7 c �v � | j j t j t j |� y r+ )r. �
setsockoptrn ro �r2 �enableds r6 �
setTcpNoDelayzConnection.setTcpNoDelay� s$ � ������v�1�1�6�3E�3E�w�Or7 c � � t | j j t j t j � � S r+ )rl r. rm �
SOL_SOCKET�SO_KEEPALIVEr9 s r6 �getTcpKeepAlivezConnection.getTcpKeepAlive� s* � ��D�K�K�*�*�6�+<�+<�f�>Q�>Q�R�S�Sr7 c �v � | j j t j t j |� y r+ )r. rr rw rx rs s r6 �setTcpKeepAlivezConnection.setTcpKeepAlive� s$ � ������v�0�0�&�2E�2E�w�Or7 c �< |