
    if(                     l    d dl Z d dlZddlmZ ddlmZ ddlmZ ddlmZ  G d dej                  ZdS )	    N   )base_socket)
exceptions)packet)payloadc                   V    e Zd ZdZd Zd Zd Zd Zd Zd Z	ddZ
d Zd Zd Zd Zd
S )SocketzAn Engine.IO socket.c                    | j                                         }	 | j                            | j         j        | j         j        z             g}| j                                         n# |$ r t          j                    w xY w|dgk    rg S 	 	 | j                            d          }| j                                         || j        	                    d           n"|
                    |           n# |$ r Y nw xY wt|S )z'Wait for packets to send to the client.)timeoutNTF)block)serverget_queue_empty_exceptionqueuegetping_intervalping_timeout	task_doner   
QueueEmptyputappend)selfqueue_emptypacketspkts       :/usr/local/lib/python3.11/dist-packages/engineio/socket.pypollzSocket.poll   s1   k;;==	*z~~1DK4LL & N N OGJ  """" 	* 	* 	*')))	*tfI		jnn5n11
$$&&&;JNN4(((s####   		 s%   AA( (BAC6  C6 6C>=C>c           	      "   |j         t          t          j                  k     rt          j        |j                  nd}| j        j                            d| j        |t          |j	        t                    s|j	        nd           |j         t          j        k    r|                                  d	S |j         t          j        k    r4| j                            d| j        |j	        | j        j                   d	S |j         t          j        k    r3|                     t          j        t          j                             d	S |j         t          j        k    r)|                     dd| j        j        j                   d	S t1          j                    )
zReceive packet from the client.UNKNOWNz%s: Received packet %s data %s<binary>message	run_asyncFTwaitabortreasonN)packet_typelenr   packet_namesr   loggerinfosid
isinstancedatabytesPONGschedule_pingMESSAGE_trigger_eventasync_handlersUPGRADEsendPacketNOOPCLOSEcloser&   CLIENT_DISCONNECTr   UnknownPacketError)r   r   packet_names      r   receivezSocket.receive#   s~    V%8!9!999 )#/::?H 	 @ $+0:38U0K0K !0%/	1 	1 	1 ?fk))     _..K&&y$(CH151K ' M M M M M_..IIfmFK0011111_,,JJE"k0B  D D D D D /111    c                 6   | j         rt          j                    | j        rwt	          j                    | j        z
  | j        j        k    rN| j        j                            d| j	                   | 
                    dd| j        j        j                   dS dS )z2Make sure the client is still responding to pings.z"%s: Client is gone, closing socketFr#   T)closedr   SocketIsClosedError	last_pingtimer   r   r*   r+   r,   r:   r&   PING_TIMEOUTr   s    r   check_ping_timeoutzSocket.check_ping_timeout8   s    ; 	30222> 			dn,t{/GGGK##$H$(H. . .
 JJE"k0=  ? ? ?5tr?   c           	          |                                  sdS | j                            |           | j        j                            d| j        t          j        |j	                 t          |j        t                    s|j        nd           dS )zSend a packet to the client.Nz%s: Sending packet %s data %sr   )rG   r   r   r   r*   r+   r,   r   r)   r'   r-   r.   r/   )r   r   s     r   r6   zSocket.sendH   s    &&(( 	 FJNN3 ? $&*=co*N0:38U0K0K !0%/	1 	1 	1 	1 	1r?   c                    d |                     dd                                                              d          D             }|                     dd                                          }d|v rL|| j        v rC| j        j                            d| j        |            t          | d|z             ||          S | j	        s| j
        rt          j        t          j                  gS 	 |                                 }nm# t          j        $ r[ t#          j                    }|                     d	| j        j        j        
           |d                             |d                   w xY w|S )z2Handle a long-polling GET request from the client.c                 6    g | ]}|                                 S  )strip).0ss     r   
<listcomp>z-Socket.handle_get_request.<locals>.<listcomp>U   s6     L L L GGIIL L Lr?   HTTP_CONNECTION ,HTTP_UPGRADEupgradez%%s: Received request to upgrade to %s	_upgrade_F)r$   r&   r      )r   lowersplitupgrade_protocolsr   r*   r+   r,   getattr	upgradingupgradedr   r7   r8   r   r   r   sysexc_infor:   r&   TRANSPORT_ERRORwith_traceback)r   environstart_responseconnections	transportr   excs          r   handle_get_requestzSocket.handle_get_requestS   s   L L[[!2B77==??EEcJJL L L KK3399;;	##	T5K(K(KK##$K$(Hi9 9 9974y!899':HJ J J> 	0T] 	0 M&+..//	0iikkGG$ 	0 	0 	0,..CJJE$+*<*LJMMMa&''A///	0 s   ,D A*E+c                 V   t          |                    dd                    }|| j        j        k    rt	          j                    |d                             |                              d          }t          j	        |          }|j
        D ]}|                     |           dS )z3Handle a long-polling POST request from the client.CONTENT_LENGTH0z
wsgi.inputzutf-8)encoded_payloadN)intr   r   max_http_buffer_sizer   ContentTooLongErrorreaddecoder   Payloadr   r>   )r   ra   lengthbodypr   s         r   handle_post_requestzSocket.handle_post_requestj   s    W[[!137788DK4440222<(--f55<<WEED555Ay " "S!!!!" "r?   TFNc                    | j         s| j        sd| _        | j                            d| j        |p| j        j        j        d           |s1|                     t          j	        t          j
                             d| _         | j                            d           |r| j                                         dS dS dS dS )zClose the socket connection.T
disconnectFr!   N)rA   closingr   r3   r,   r&   SERVER_DISCONNECTr6   r   r7   r9   r   r   join)r   r$   r%   r&   s       r   r:   zSocket.closeu   s    { 	"4< 	"DLK&&dh>$+,> ' ! ! !  7		&-55666DKJNN4    "
!!!!!	" 	" 	" 	"" "r?   c                 D    | j                             | j                   d S N)r   start_background_task
_send_pingrF   s    r   r1   zSocket.schedule_ping   s     ))$/:::::r?   c                    d | _         | j                            | j        j                   | j        sR| j        sMt          j                    | _         |                     t          j	        t          j
                             d S d S d S r{   )rC   r   sleepr   rw   rA   rD   r6   r   r7   PINGrF   s    r   r}   zSocket._send_ping   s|    $+3444| 	2DK 	2!Y[[DNIIfmFK0011111	2 	2 	2 	2r?   c                     | j         rt          d          | j        j        d         | j                                        S  | j        j        d         | j        | j                  } |||          S )z1Upgrade the connection from polling to websocket.z Socket has been upgraded already	websocket)r\   OSErrorr   _async_bad_request_websocket_handler)r   ra   rb   wss       r   _upgrade_websocketzSocket._upgrade_websocket   sv    = 	><===;k*2;++---,T[,#T[2 2r'>***r?   c                 
     fd}dD ]j}t          |          rXt          t          |          d          r:t          |                               j        j         j        j        z              k j        rhd _         |            }t          j	        |          }|j
        t          j        k    s|j        dk    r. j        j                            d j                   d _        g S                     t          j	        t          j        d	                                                      j                            t          j	        t          j                              |            }t          j	        |          }|j
        t          j        k    r6d _         j        j                            d
 j        |           d _        g S d _        d _        nd _        d _         fd} j                            |          } j        j                            d j                   	 d}	  |            }nQ# t2          $ rD}	 j        s3 j        j                            d j        t7          |	                     Y d}	~	nd}	~	ww xY w|nt          j	        |          }	                      |           ne# t:          j        $ r Y nTt:          j        $ r"  j        j                            d           Y n&  j        j                             d           Y nxY w j                            d           |!                                  "                    dd j        j#        j$                   g S )z*Engine.IO handler for websocket transport.c                                                       } | r,t          |           j        j        k    rt	          d          | S )Nzpacket is too large)r$   r(   r   rl   
ValueError)r.   r   r   s    r   websocket_waitz1Socket._websocket_handler.<locals>.websocket_wait   sA    7799D 8D		DK$DDD !6777Kr?   )_socksocket
settimeoutT)encoded_packetprobez,%s: Failed websocket upgrade, no PING packetF)r.   zK%s: Failed websocket upgrade, expected UPGRADE packet, received %s instead.c                      	 d } 	                                  } n# t          j        $ r Y n=w xY w| sn6	 | D ])}                    |                                           *n#  Y nxY wf                                 d S r{   )r   r   r   r6   encoder:   )r   r   r   r   s     r   writerz)Socket._websocket_handler.<locals>.writer   s    "iikkGG!,   E & . .

----.E HHJJJJJs    --,A! !A%z#%s: Upgrade to websocket successfulNz-%s: Unexpected error "%s", closing connectionz!Receive error -- socket is closedzUnknown receive errorr#   )%hasattrrZ   r   r   r   r   	connectedr[   r   r7   r'   r   r.   r*   r+   r,   r6   r0   r   r   r   r8   r5   r\   r|   	ExceptionrA   strr>   r   r<   rB   	exceptionry   r:   r&   TRANSPORT_CLOSE)
r   r   r   attrr   decoded_pktr   writer_taskrs   es
   ``        r   r   zSocket._websocket_handler   s   	 	 	 	 	 	 ( 	J 	JDr4   JWWR->->%M%M JD!!,,K-0HHJ J J > 	!!DN .""C -s;;;K&&+55$//"''BDHN N N!&	GGFM&+G<<<CCEEFFFJNN6=55666 .""C -s;;;K&&.88 %"'',Hc# # # "'	 DM"DNN!DN DM	 	 	 	 	 	" k77??148	= 	= 	=	A	"N$$    { *K&++G#a&&* * *  y-q111CS!!!!0   1   "''(KLLL ",,-DEEE7	: 	
t

T+,< 	 	> 	> 	> 	s0   =
I 
J:JJ2K L*-L* L*)TFN)__name__
__module____qualname____doc__r   r>   rG   r6   rf   rt   r:   r1   r}   r   r   rK   r?   r   r	   r	   
   s          .2 2 2*   	1 	1 	1  .	" 	" 	"" " " "; ; ;2 2 2	+ 	+ 	+g g g g gr?   r	   )	r]   rD   rQ   r   r   r   r   
BaseSocketr	   rK   r?   r   <module>r      s    



                         v v v v v[# v v v v vr?   