U mÃfÅã@sÀddlZddlZddlZddlZddlZddlZddlZddlZddl Zddl Z ddl Z ddl mZmZmZmZmZeedƒr†ejZnejZGdd„deƒZGdd„deƒZGdd „d eƒZdS) éN)ÚQUIC_MAX_DATAGRAMÚBaseQuicConnectionÚBaseQuicManagerÚBaseQuicStreamÚ UnexpectedEOFÚ PollSelectorcsXeZdZ‡fdd„Zdd„Zddd„Zdd d „Zd d „Zd d„Zdd„Z dd„Z ‡Z S)ÚSyncQuicStreamcs&tƒ ||¡t ¡|_t ¡|_dS©N)ÚsuperÚ__init__Ú threadingÚ ConditionÚ_wake_upÚLockÚ_lock)ÚselfÚ connectionÚ stream_id©Ú __class__©ús zSyncQuicStream._add_inputc Cs|j| ¡W5QRXdSr )rÚ_close©rrrrÚcloseCszSyncQuicStream.closecCs|Sr rr0rrrÚ __enter__GszSyncQuicStream.__enter__c Cs(| ¡|j|j ¡W5QRXdS©NF)r1rr-)rÚexc_typeÚexc_valÚexc_tbrrrÚ__exit__JszSyncQuicStream.__exit__)N)F) Ú__name__Ú __module__Ú __qualname__r r r&r,r.r1r2r7Ú __classcell__rrrrrs  rcsleZdZ‡fdd„Zdd„Zdd„Zdd„Zd d „Zdd d „Zdd„Z ddd„Z ‡fdd„Z dd„Z ‡Z S)ÚSyncQuicConnectioncsÈtƒ ||||||¡t |jtjd¡|_|jdk rtz|j tj   |j|j¡¡Wn t k rr|j  ¡‚YnX|j  |j¡t ¡\|_|_|j d¡|j d¡t ¡|_d|_t ¡|_dS)NrF)r r ÚsocketZ_afÚ SOCK_DGRAMÚ_socketÚ_sourceÚbindrZinetZlow_level_address_tupleÚ Exceptionr1ÚconnectÚ_peerÚ socketpairÚ _send_wakeupÚ_receive_wakeupÚ setblockingr ÚEventÚ_handshake_completeÚ_worker_threadrr)rrÚaddressÚportÚsourceÚ source_portÚmanagerrrrr Rs" ÿ    zSyncQuicConnection.__init__c Csjd}|dkrf|d7}z|j t¡}Wntk r:YdSX|j|j ||jt ¡¡W5QRXqdS)Nré é) r?ÚrecvrÚBlockingIOErrorrr'Zreceive_datagramrDÚtime)rÚcountr)rrrÚ_readeszSyncQuicConnection._readcCs.z|j d¡Wqtk r&YdSXqdS)Né )rGrSrTr0rrrÚ _drain_wakeuppsz SyncQuicConnection._drain_wakeupc Cs zâtƒ}| |jtj|j ¡| |j tj|j ¡|jsà|  d¡\}}|  |¡}|D]\}}| ¡qR|j| |¡W5QRX| ¡|j|j t ¡¡}W5QRX|D].\}}z|j |¡Wq®tk rÚYq®Xq®q0W5|j d|_W5QRX|j ¡XdS)NTF)rÚ_donerJÚsetÚ_selector_classÚregisterr?Ú selectorsÚ EVENT_READrWrGrYZ_get_timer_valuesÚselectr+Z _handle_timerÚ_handle_eventsr'Zdatagrams_to_sendrUr,rT) rÚselrÚintervalÚitemsÚkeyÚ_Z datagramsr)rrrÚ_workerws,    zSyncQuicConnection._workerc Csú|j|j ¡}W5QRX|dkr(dSt|tjjjƒrn|j|j  |j ¡}W5QRX|rô|  |j |j ¡qt|tjjjƒrŠ|j ¡qt|tjjjƒr´|j d|_W5QRXqt|tjjjƒr|j|j  |j ¡}W5QRX|r|  dd¡qdS)NTó)rr'Z next_eventÚ isinstanceÚaioquicZquicÚeventsZStreamDataReceivedÚ_streamsr$rr.r+Z end_streamZHandshakeCompletedrJr[ZConnectionTerminatedrZZ StreamReset)rÚeventÚstreamrrrra’s& z!SyncQuicConnection._handle_eventsFc Cs2|j|j |||¡W5QRX|j d¡dS)Nó)rr'Zsend_stream_datarFr,)rrnr+r*rrrr(¨szSyncQuicConnection.writecCs(|jr dStj|jd|_|j ¡dS)N)Útarget)Ú_closedr ÚThreadrgrKÚstartr0rrrÚrun­szSyncQuicConnection.runNc CsT|j |¡stjj‚|j0|jr&t‚|j  d¡}t ||ƒ}||j |<W5QRX|Sr3) rJrrrrrrZrr'Zget_next_available_stream_idrrl)rrrrnrrrÚ make_stream³s   zSyncQuicConnection.make_streamc s"|jtƒ |¡W5QRXdSr )rr Ú close_stream)rrrrrrv¾szSyncQuicConnection.close_streamc Csj|jP|jrW5QR£dS|j |jd|jd¡d|_|j ¡|j d¡W5QRX|j   ¡dS)NrrRTro) rrqÚ_managerÚclosedrDr'r1rFr,rKÚjoinr0rrrr1Âs zSyncQuicConnection.close)F)N)r8r9r:r rWrYrgrar(rtrurvr1r;rrrrr<Qs    r<csXeZdZdejdf‡fdd„ Zddd„Z‡fd d „Z‡fd d „Zd d„Z dd„Z ‡Z S)ÚSyncQuicManagerNcs tƒ ||t|¡t ¡|_dSr )r r r<r rr)rZconfÚ verify_modeZ server_namerrrr ÎszSyncQuicManager.__init__éUrTc CsD|j4| |||||¡\}}|r*| ¡|W5QR£SQRXdSr )rÚ_connectrt)rrLrMrNrOZwant_session_ticketrrsrrrrCÒsÿzSyncQuicManager.connectc s$|jtƒ ||¡W5QRXdSr )rr rx)rrLrMrrrrxÝszSyncQuicManager.closedc s&|jtƒ |||¡W5QRXdSr )rr Úsave_session_ticket)rrLrMÚticketrrrr~ász#SyncQuicManager.save_session_ticketcCs|Sr rr0rrrr2åszSyncQuicManager.__enter__cCs$t|j ¡ƒ}|D] }| ¡qdSr3)ÚlistZ _connectionsÚvaluesr1)rr4r5r6Ú connectionsrrrrr7ès zSyncQuicManager.__exit__)r|NrT) r8r9r:ÚsslÚ CERT_REQUIREDr rCrxr~r2r7r;rrrrrzÍsÿ  rz)r^r=rƒr"r rUZaioquic.quic.configurationrjZaioquic.quic.connectionZaioquic.quic.eventsZ dns.exceptionrZdns.inetZdns.quic._commonrrrrrÚhasattrrr\ÚSelectSelectorrr<rzrrrrÚs" 2|