
    l8'hx?                    j   d Z ddlmZ ddlZddlZddlZddlZddlZddlZddl	m
Z
mZmZmZmZmZmZmZ ddlmZ ddlmZmZmZ ddlmZ ddlmZmZ dd	lmZmZm Z m!Z! dd
l"m#Z#m$Z$m%Z% ddl&m'Z'm(Z(m)Z) ddl*m+Z+ ddl,m-Z-m.Z.m/Z/m0Z0m1Z1 ddl2m3Z3 e
rFddlm4Z4 ddl5m6Z6 ddl7m8Z8 ddl9m:Z: ddlm;Z;m<Z<m=Z= ddl*m>Z> ddl?m@Z@ ddlAmBZB ddlCmDZDmEZEmFZFmGZG ddlHmIZI dZJ	 	 	 	 	 	 	 	 	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZKef	 	 	 	 	 	 	 d dZLd!dZM	 	 	 	 	 	 	 	 d"dZNy)#z&Internal network layer helper methods.    )annotationsN)TYPE_CHECKINGAnyMappingMutableMappingOptionalSequenceUnioncast)_decode_all_selective)_csothelpers_sharedmessage)MAX_MESSAGE_SIZE)_NO_COMPRESSION
decompress)NotPrimaryErrorOperationFailureProtocolError_OperationCancelled)_COMMAND_LOGGER_CommandStatusMessage
_debug_log)_UNPACK_REPLY_OpMsg_OpReply)_is_speculative_authenticate)_POLL_TIMEOUT_UNPACK_COMPRESSION_HEADER_UNPACK_HEADERBLOCKING_IO_ERRORSasync_sendall)_errno_from_exception)CodecOptions)AsyncClientSession)AsyncMongoClient)AsyncConnection)SnappyContextZlibContextZstdContext)_EventListeners)ReadConcern)_ServerMode)_Address_CollationIn_DocumentOut_DocumentType)WriteConcernFc                  K   t        t        |            }|dz   }d}|}|r|s|J t        j                  ||      }|r=|r|j                  s/|j
                  r|j                  |d<   |r|j                  ||        |||d<   |duxr |j                  }t        j                  j                         }|rt        ||      }|r|j                         t        v rd}|rI|j                  r=|j                  j                  s'|j                  j!                  |||       d{   x}}|r| j#                  ||       t%        j&                  ||       |rl|rt(        j*                  nd}||rt(        j,                  ndz  }t        j.                  ||||||      \  }}}} |r?|=| |kD  r8t        j0                  |||       n t        j2                  d|dd|d||      \  }}}|>||t        j4                  z   kD  r(t        j0                  |||t        j4                  z          |t7        j8                  t:        j<                        rt?        t6        |j@                  jB                  tD        jF                  |t        t        |            |||| jH                  | jJ                  | jL                  d   | jL                  d	   | jN                  
       |r3|J |
J |jQ                  ||||
| jJ                  | jN                         	 tS        | jT                  |       d{    |r	|rd}!dd	i}"n~tW        | |       d{   }!|!jX                  | _,        |!j[                  ||      }#|#d   }"|r|j]                  |"|       d{    |r#t_        j`                  |"| jb                  |	|       t        j                  j                         |z
  }%|t7        j8                  t:        j<                        rt?        t6        |j@                  jB                  tD        jt                  |%|"t        t        |            |||| jH                  | jJ                  | jL                  d   | jL                  d	   | jN                  d|v        |r6|J |
J |jw                  |%|"|||
| jJ                  | jN                  ||	       |rY|j                  rM|!rK|j                  jy                  |!j{                                d{   }'t}        dt        |'||      d         }"|"S 7 7 7 7 # td        $ rS}$t        j                  j                         |z
  }%tg        |$th        tj        f      r|$jl                  }&nt        jn                  |$      }&|t7        j8                  t:        j<                        rt?        t6        |j@                  jB                  tD        jp                  |%|&t        t        |            |||| jH                  | jJ                  | jL                  d   | jL                  d	   | jN                  tg        |$tj                     |r5|J |
J |js                  |%|&|||
| jJ                  | jN                  |        d}$~$ww xY w7 w)a  Execute a command over the socket, or raise socket.error.

    :param conn: a AsyncConnection instance
    :param dbname: name of the database on which to run the command
    :param spec: a command document as an ordered dict type, eg SON.
    :param is_mongos: are we connected to a mongos?
    :param read_preference: a read preference
    :param codec_options: a CodecOptions instance
    :param session: optional AsyncClientSession instance.
    :param client: optional AsyncMongoClient instance for updating $clusterTime.
    :param check: raise OperationFailure if there are errors
    :param allowable_errors: errors to ignore if `check` is True
    :param address: the (host, port) of `conn`
    :param listeners: An instance of :class:`~pymongo.monitoring.EventListeners`
    :param max_bson_size: The maximum encoded bson size for this server
    :param read_concern: The read concern for this command.
    :param parse_write_concern_error: Whether to parse the ``writeConcernError``
        field in the command response.
    :param collation: The collation for this command.
    :param compression_ctx: optional compression Context.
    :param use_op_msg: True if we should use OP_MSG.
    :param unacknowledged: True if this is an unacknowledged command.
    :param user_fields: Response fields that should be decoded
        using the TypeDecoders from codec_options, passed to
        bson._decode_all_selective.
    :param exhaust_allowed: True if we should enable OP_MSG exhaustAllowed.
    z.$cmdFNreadConcern	collationr   )ctx   )clientIdr   commandcommandNamedatabaseName	requestIdoperationIddriverConnectionIdserverConnectionId
serverHost
serverPort	serviceId)
service_idok)codec_optionsuser_fields)parse_write_concern_error)r9   r   
durationMSfailurer;   r<   r=   r>   r?   r@   rA   rB   rC   isServerSideError)rD   database_namespeculativeAuthenticate)r9   r   rI   replyr;   r<   r=   r>   r?   r@   rA   rB   rC   speculative_authenticate)rD   speculative_hellorL   r0   )@nextiterr   _maybe_add_read_preferencein_transactionleveldocument_update_read_concernenabled_for_commandsdatetimenowr   lowerr   
_encrypter_bypass_auto_encryptionencryptapply_timeoutr   apply_write_concernr   MORE_TO_COMEEXHAUST_ALLOWED_op_msg_raise_document_too_large_query_COMMAND_OVERHEADr   isEnabledForloggingDEBUGr   _topology_settings_topology_idr   STARTEDidserver_connection_idaddressrD   publish_command_startr"   connreceive_messagemore_to_comeunpack_response_process_responser   _check_command_responsemax_wire_version	Exception
isinstancer   r   details_convert_exceptionFAILEDpublish_command_failure	SUCCEEDEDpublish_command_successdecryptraw_command_responser   r   )(rq   dbnamespec	is_mongosread_preferencerF   sessionclientcheckallowable_errorsro   	listenersmax_bson_sizeread_concernrH   r5   compression_ctx
use_op_msgunacknowledgedrG   exhaust_allowedwrite_concernnamensrP   origpublishstartflags
request_idmsgsizemax_doc_sizerN   response_docunpacked_docsexcdurationrJ   	decrypteds(                                           X/var/www/html/trade_iq/venv/lib/python3.12/site-packages/pymongo/asynchronous/network.pyr:   r:   H   s9    f T
D	'	B D***11$HW)?)?"."7"7D((t4%[t#F	(F(FG!!#E8tD4::<?:&##F,=,=,U,U"--55fdMRRRt 64(	dM2'5##1?''A.5oo4-_/
+
C|
 m7L=<X--dD-H 'r1b$m_!

C  TMG<U<U,U%U))$mgF_F_6_`''622??-55 d,#$&#'77#'#<#<<<?<<?// $$$"""''%% 	( 	
=DIIs+++.E*.L)$
;;E % 2 2D!11+ 2 M )+L..|WEEE66 ))$.G	Z   $$&.H''622??-77#" d,#$&#'77#'#<#<<<?<<?//)Bd)J" $$$"""))%%/  	* 
	
 &## ++33E4N4N4PQQ	1)]KXYZ[
 y Sl 	, < F  &$$((*U2cO-=>?$'KKG005G++GMM:##66CC188'# $T$Z 0!'( *'+ww'+'@'@#||A#||A"oo&06F&G" (((&&&--))??$ . 	 	M&V Rs|   DY? T!G Y?T TT :T;AT ?T )T )EY?/Y<0!Y?T T T 
Y9&EY44Y99Y?c                  K   t        j                         rt        j                         }n6| j                  j	                         }|rt        j                         |z   }nd}t        t        | d|       d{         \  }}}}|||k7  rt        d|d|      |dk  rt        d|d      ||kD  rt        d|d|d      |d	k(  rEt        t        | d
|       d{         \  }}}	t        t        | |dz
  |       d{   |	      }
nt        | |dz
  |       d{   }
	 t        |   } ||
      S 7 7 [7 87 # t        $ r% t        d|dt        j                               dw xY ww)z1Receive a raw BSON message or raise socket.error.N   zGot response id z but expected zMessage length (z3) not longer than standard message header size (16)z*) is larger than server max message size ()i  	      zGot opcode )r   get_timeoutget_deadlinerq   
gettimeouttime	monotonicr    _receive_data_on_socketr   r   r   r   KeyErrorkeys)rq   r   max_message_sizedeadlinetimeoutlength_response_toop_codecompressor_iddataunpack_replys               r   rr   rr   4  s     %%'))&&(~~''1HH&4%dB99'#FA{G $"2;/PZ~ ^__|vj([\
 	
   vj )-03
 	
 $$>)$8<<%
!M  7frk8 TTVcd,T6B;II$W-
 ; 	:" =TI  'N=3E3E3G2JK
	sa   A5E57D<8A*E5"D>#$E5E E5%E&E5+	E 4	E5>E5 E5E5.E22E5c                  K   | j                   }d}|j                         dk(  ry	 t        |d      r|j                         dkD  rd}n_|r9|t	        j
                         z
  }|dk  rd}t        t        |t              d      }nt        }| j                  j                  |d|      }| j                  j                  rt        d      |ry|rt        j                  d	      t!        j"                  d       d{    7 w)
zABlock until at least one byte is read, or a timeout, or a cancel.Fr7   NTpendingr   )readr   zoperation cancelled	timed out)rq   filenohasattrr   r   r   maxminr   socket_checkerselectcancel_context	cancelledr   socketr   asynciosleep)rq   r   sock	timed_outreadable	remainingr   s          r   wait_for_readr   b  s     99DI{{}
4#(:H $t~~'77	 > $Ic)];Q?'**11$T71SH((%&;<<..--mmA1 0 	s   C8D:D ;Dc                  K   t        |      }t        |      }d}||k  r	 t        | |       d {    t        j                         r2|0| j                  t        |t        j                         z
  d             | j                  j                  ||d        }|dk(  rt        d      ||z  }||k  r|S 7 # t        $ r t        j                  d      d t        $ r'}t        |      t         j"                  k(  rY d }~ d }~ww xY ww)Nr   r   zconnection closed)	bytearray
memoryviewr   r   r   set_conn_timeoutr   r   r   rq   	recv_intor!   r   r   OSErrorr#   errnoEINTR)rq   r   r   bufmv
bytes_readchunk_lengthr   s           r   r   r     s     F
C	CBJ
v
	h///   "x';%%c(T^^5E*Eq&IJ99..r*+?L 1-..l"
% v
( I% 0 " 	8..-47 	$S)U[[8	sK   D	B8 B6A(B8 D	4D	6B8 8'DD;D	 DDD	)TNNNNNFNNFFNFN).rq   r'   r   strr   zMutableMapping[str, Any]r   boolr   zOptional[_ServerMode]rF   zCodecOptions[_DocumentType]r   zOptional[AsyncClientSession]r   zOptional[AsyncMongoClient]r   r   r   z#Optional[Sequence[Union[str, int]]]ro   zOptional[_Address]r   zOptional[_EventListeners]r   Optional[int]r   zOptional[ReadConcern]rH   r   r5   zOptional[_CollationIn]r   z4Union[SnappyContext, ZlibContext, ZstdContext, None]r   r   r   r   rG   zOptional[Mapping[str, Any]]r   r   r   zOptional[WriteConcern]returnr1   )rq   r'   r   r   r   intr   zUnion[_OpReply, _OpMsg])rq   r'   r   Optional[float]r   None)rq   r'   r   r   r   r   r   r   )O__doc__
__future__r   r   rY   r   rh   r   r   typingr   r   r   r   r   r	   r
   r   bsonr   pymongor   r   r   pymongo.commonr   pymongo.compression_supportr   r   pymongo.errorsr   r   r   r   pymongo.loggerr   r   r   pymongo.messager   r   r   pymongo.monitoringr   pymongo.network_layerr   r   r    r!   r"   pymongo.socket_checkerr#   r$   #pymongo.asynchronous.client_sessionr%   !pymongo.asynchronous.mongo_clientr&   pymongo.asynchronous.poolr'   r(   r)   r*   r+   pymongo.read_concernr,   pymongo.read_preferencesr-   pymongo.typingsr.   r/   r0   r1   pymongo.write_concernr2   _IS_SYNCr:   rr   r   r        r   <module>r      s   - "      	 	 	 ' 2 2 + C  N M ; ; ;  9!FB9SS204SS2 <@"&+/#'*.&+(,LP /3!,0-i
ii #i 	i
 +i /i *i 'i i :i  i )i !i (i  $i  &!i" J#i$ %i& 'i( -)i* +i, *-i. /iZ O_+
+'4+HK++\D
#&2Ar   