Beispiel #1
0
def _pack_ref(val) -> bytes:
    data = bytes([TAG_NEW_REF_EXT]) \
           + util.to_u16(len(val.id_) // 4) \
           + _pack_atom(val.node_name_) \
           + bytes([val.creation_]) \
           + val.id_
    return data
Beispiel #2
0
    async def _fire_forget_query(ip: str, query: bytes) -> bytes:
        """ Connect to node, fire the query, read and disconnect. """
        try:
            reader, writer = await asyncio.open_connection(
                host=ip,
                port=EPMD_DEFAULT_PORT,
                # timeout=EPMD_REMOTE_DEFAULT_TIMEOUT
            )
        except Exception as err:
            LOG.error(str(err))
            raise err

        query1 = util.to_u16(len(query)) + query
        writer.write(query1)

        # Expect that after everything is received, the peer will close
        # the socket automatically, so we will too
        result = b''
        while True:
            incoming = await reader.read(4096)
            if incoming == b'':
                break

            result += incoming

        writer.close()
        return result
Beispiel #3
0
def _pack_str(val):
    str_bytes = bytes(val, "utf8")
    len_str_bytes = len(str_bytes)
    len_val = len(val)

    if _can_be_a_bytestring(val) and len_str_bytes <= 65535:
        # same length as byte length
        header = bytes([TAG_STRING_EXT]) + util.to_u16(len_str_bytes)
        return header + str_bytes
    else:
        # contains unicode characters! must be encoded as a list of ints
        header = bytes([TAG_LIST_EXT]) + util.to_u32(len_val)
        elements = [_pack_int(ord(ch)) for ch in val]
        return header + b''.join(elements) + bytes([TAG_NIL_EXT])
Beispiel #4
0
    def _fire_forget_query(self, ip: str, query: bytes) -> bytes:
        """ Connect to node, fire the query, read and disconnect. """
        socket = self.engine_.socket_module()
        s = socket.create_connection(address=(ip, EPMD_DEFAULT_PORT),
                                     timeout=EPMD_REMOTE_DEFAULT_TIMEOUT)
        query1 = util.to_u16(len(query)) + query
        s.send(query1)

        # Expect that after everything is received, the peer will close
        # the socket automatically, so we will too
        result = b''
        while True:
            incoming = s.recv(4096)
            if incoming == b'':
                break

            result += incoming

        s.close()
        return result
Beispiel #5
0
def _pack_atom(text: str) -> bytes:
    atom_bytes = bytes(text, "utf8")
    if len(atom_bytes) < 256:
        return bytes([TAG_SMALL_ATOM_UTF8_EXT, len(atom_bytes)]) + atom_bytes
    return bytes([TAG_ATOM_UTF8_EXT]) + util.to_u16(
        len(atom_bytes)) + atom_bytes