Ejemplo n.º 1
0
    def run(self):
    
        while not self._shutdown:
            time.sleep(0.001)

            try:
                r, w, e = select.select([self._socket], [], [self._socket], 0.5)
                if self._socket in r:
                    client = TCPConnection(self._socket.accept())
                    self.client_connection_opened(client)

                if self._socket in e:
                    self.server_poll_error()
                    self._shutdown = True

                r, w, e = select.select(TCPConnection.get_connections(), [], [], 0.5)

                for connection in r:
                    try:
                        connection.read_next()
                        self.client_connection_read(self, connection)
                    except (socket.error) as e:
                        self.client_connection_lost(connection)
                        TCPConnection.remove_connection(connection.get_address())

                for connection in TCPConnection.get_connections():
                    connection.send_next()
            except (KeyboardInterrupt, SystemExit) as e:
                self._shutdown = True
            except (Exception) as e:
                self.server_uncaptured_exception(e)
        
        self._socket.shutdown(socket.SHUT_RDWR)
        self._socket.close()
Ejemplo n.º 2
0
class DataCenter:

    def __init__(self, url):
        self.dc = DCInfo.new(url)
        self.connection = TCPConnection(str(self.dc.address), self.dc.port)
        self.last_msg_id = 0
        self.auth_key = None
        self.random = random.SystemRandom()

    def init(self, loop):
        pass
        # self.conn = MTProtoConnection.new(self.dc_info.connection_type)
        # self.conn_coro = loop.create_connection(lambda: self.conn, str(self.dc_info.address), self.dc_info.port)

        # f1 = asyncio.async(self.conn_coro)
        # asyncio.async(self.run(loop))
        # f1.add_done_callback(lambda x: self.create_auth_key())
        # loop.run_until_complete(asyncio.wait(tasks))

    def send_rpc_message(self, msg):
        pass
        # self.conn.send_message(msg)
    
    def generate_message_id(self):
        msg_id = int(time() * 2**32)
        if self.last_msg_id > msg_id:
            msg_id = self.last_msg_id + 1
        while msg_id % 4 is not 0:
            msg_id += 1

        return msg_id

    @asyncio.coroutine
    def send_insecure_message(self, request):
        yield from self.connection.send_insecure_message(self.generate_message_id(), request)
        # self.conn.send_insecure_message(self.generate_message_id(), request)

    @asyncio.coroutine
    def create_auth_key(self):
        req_pq = scheme.req_pq(tl.int128_c(self.random.getrandbits(128)))
        yield from self.send_insecure_message(req_pq)
        # res_pq = tl.ResPQ.from_stream(BytesIO(self.recv_plaintext_message()))

        # assert nonce == res_pq.nonce

    @asyncio.coroutine
    def run(self, loop):
        asyncio.async(self.connection.run(loop), loop=loop)
        asyncio.async(self.create_auth_key(), loop=loop)

        while True:
            print('test')
            yield from asyncio.sleep(10)
Ejemplo n.º 3
0
 def __init__(self, url):
     self.dc = DCInfo.new(url)
     self.connection = TCPConnection(str(self.dc.address), self.dc.port)
     self.last_msg_id = 0
     self.auth_key = None
     self.random = random.SystemRandom()