Exemplo n.º 1
1
    def _is_client_request_ssl(cls, socket_: socket.socket) -> bool:
        while True:
            original_timeout = socket_.gettimeout()
            socket_.setblocking(False)

            try:
                data = socket_.recv(3, socket.MSG_PEEK)
            except OSError as error:
                if error.errno in (errno.EWOULDBLOCK, errno.EAGAIN):
                    yield from asyncio.sleep(0.01)
                else:
                    raise
            else:
                break
            finally:
                socket_.settimeout(original_timeout)

        _logger.debug('peeked data %s', data)
        if all(ord('A') <= char_code <= ord('Z') for char_code in data):
            return False
        else:
            return True
Exemplo n.º 2
0
def handle_get(s: socket, petition: Petition):
    """
    Handles a get petition
    """
    path = petition.arguments[0]

    try:
        route_handler = router[path]
        route_handler(s, petition)
        return
    except KeyError:
        pass

    if path[-1] == '/':
        path += "index.html"

    mime, _ = mimetypes.guess_type(path)

    if mime is None:
        mime = "text/plain"

    print(f"[i] GET for \"{path}\" with {petition.header_map}")

    try:
        with open("./" + path, "rb") as file:
            data = file.read()
            resp = craft_response("200 OK", mime, data)
    except FileNotFoundError:
        resp = craft_response("404 NOT FOUND", mime, ''.encode())

    s.setblocking(True)
    s.sendall(resp)
    s.setblocking(False)
Exemplo n.º 3
0
    def handle_presence(self, client: socket) -> bool:
        print("Handle presence")
        client.setblocking(True)
        data = decode_data(client.recv(BUF_READ_SIZE))

        response = {
            "response": 400,
            "alert": "Smth went wrong"
        }
        print("Presence received")

        if "action" in data and data["action"] == "presence":
            if "login" in data and "password" in data:

                if data["login"] in self.contacts:
                    response["alert"] = "This user is already exists"

                else:
                    self.contacts[data["login"]] = client
                    response["response"] = 200
                    response["token"] = str(uuid.uuid4())
                    print("All clear")

        print(self.contacts)

        self.write_to_socket(client, encode_data(response))

        return response["response"] == 200
 def _recv_result(s: socket.socket):
     s.setblocking(0)  # disable blocking receive
     scope_balance = 0
     received_data = ""
     has_received = False
     received_json = None
     while not has_received:
         try:
             ready = select.select([s], [], [], 5)
             if not ready[0]:
                 print("recv() timeout!")
                 break
             # receive byte when ready
             msg_part = s.recv(1).decode('utf-8')
             received_data += msg_part
             print(msg_part, end='')
             # check if full JSON object received
             for i in range(len(msg_part)):
                 if msg_part[i] == '{':
                     scope_balance += 1
                 elif msg_part[i] == '}':
                     scope_balance -= 1
             # if JSON is invalid
             if scope_balance < 0:
                 print("Received object is not a JSON object: '{}'".format(
                     received_data))
                 break
             elif scope_balance == 0:
                 has_received = True
                 received_json = json.loads(received_data)
         except ConnectionResetError as e:
             print("Connection was reset by peer: {}".format(e), end='')
     s.setblocking(1)  # restore blocking
     return has_received, received_json
Exemplo n.º 5
0
    async def _handle_connection(self, conn: socket.socket,
                                 addr: Tuple) -> None:
        self._log.info(f'handling connection from {addr}')
        conn.setblocking(False)
        recv_data = b''
        try:
            while True:
                chunk = await self._loop.run_in_executor(
                    None, conn.recv, self._params['bufsize'])
                if chunk:
                    recv_data += chunk
                else:
                    break
        except BlockingIOError:
            pass
        try:
            data = json.loads(recv_data)
        except ValueError:
            await self._wrap_error(conn,
                                   f'request "{recv_data}" type is not json')
            return
        self._dialog_logger.log_in(data)
        model_args = []
        for param_name in self._params['model_args_names']:
            param_value = data.get(param_name)
            if param_value is None or (isinstance(param_value, list)
                                       and len(param_value) > 0):
                model_args.append(param_value)
            else:
                await self._wrap_error(
                    conn,
                    f"nonempty array expected but got '{param_name}'={repr(param_value)}"
                )
                return
        lengths = {len(i) for i in model_args if i is not None}

        if not lengths:
            await self._wrap_error(conn, 'got empty request')
            return
        elif len(lengths) > 1:
            await self._wrap_error(
                conn, f'got several different batch sizes: {lengths}')
            return
        batch_size = list(lengths)[0]
        model_args = [arg or [None] * batch_size for arg in model_args]

        # in case when some parameters were not described in model_args
        model_args += [[None] * batch_size
                       for _ in range(len(self._model.in_x) - len(model_args))]

        prediction = await self._loop.run_in_executor(None, self._model,
                                                      *model_args)
        if len(self._model.out_params) == 1:
            prediction = [prediction]
        prediction = list(zip(*prediction))
        result = await self._response('OK', prediction)
        self._dialog_logger.log_out(result)
        await self._loop.sock_sendall(conn, result)
Exemplo n.º 6
0
    def _read(self, sock: socket.socket, bufsize: int = 4096) -> bytes:
        if self.dcc_file_transfer_timeout:
            sock.setblocking(False)
            has_data = select.select([sock], [], [],
                                     self.dcc_file_transfer_timeout)
            if has_data[0]:
                return sock.recv(bufsize)
            raise TimeoutError(f'Time out ({self.dcc_file_transfer_timeout}s)')

        return sock.recv(bufsize)
Exemplo n.º 7
0
 def _handshake(self, conn_slaver: socket_t) -> bool:
     """handshake"""
     conn_slaver.setblocking(True)  # TODO use nonblocking IO
     conn_slaver.send(self._pkgbuilder.pbuild_hs_m2s())
     buff = conn_slaver.recv(self._pkgbuilder.PACKAGE_SIZE)
     conn_slaver.setblocking(False)
     if buff == b'':  # empty response
         return False
     return self._pkgbuilder.decode_verify(buff,
                                           self._pkgbuilder.PTYPE_HS_S2M)
Exemplo n.º 8
0
	def __init__(self):
		global server_socket, serverpid
		server_socket = Socket(AF_INET, SOCK_STREAM)
		server_socket.setblocking(0)
		try:
			server_socket.setsockopt(SOL_SOCKET, SO_REUSEADDR,
				server_socket.getsockopt(SOL_SOCKET, SO_REUSEADDR) | 1)
		except error:
			pass

		serverpid = server_socket.fileno()
		pollster.register(serverpid, RE)
Exemplo n.º 9
0
def run_client(client: socket):
    client.setblocking(False)
    while True:
        r, w, x = select([client], [], [], 0.1)
        if client not in r:
            continue
        print('received data')
        data = client.recv(0x1000)
        if not data:
            break
        client.send(data)
    client.shutdown(SHUT_WR)
    client.close()
Exemplo n.º 10
0
    def _handshake(self, conn: socket_t) -> bool:
        buff = conn.recv(self._pkgbuilder.PACKAGE_SIZE)
        if (buff == b'' or not self._pkgbuilder.decode_verify(
                buff, self._pkgbuilder.PTYPE_HS_M2S)):
            logger.info('handshake failed')
            conn.close()
            return False

        logger.debug('handshake successful')
        conn.setblocking(True)  # TODO
        conn.send(self._pkgbuilder.pbuild_hs_s2m())
        conn.setblocking(False)
        return True
Exemplo n.º 11
0
    def shakehands(self, client:socket.socket, addr:tuple)->Connection:
        '''完成socks握手过程
        '''
        # 客户端握手
        ver, l = struct.unpack('!BB', client.recv(2))
        assert ver in (self.SOCKS_VERSION_4, self.SOCKS_VERSION_5), f"Unsupported protocol type `{ver}`"
        assert l > 0, "Wrong nmethods"
        methods = struct.unpack("!"+"B"*l, client.recv(l)) # 备用

        # 无验证方式
        client.sendall(struct.pack('!BB', ver, 0))

        # 处理客户端转发请求
        ver, cmd, rsv, atyp = struct.unpack('!BBBB', client.recv(4))
        assert ver in (self.SOCKS_VERSION_4, self.SOCKS_VERSION_5), f"Unsupported protocol type `{ver}`"
        assert rsv == 0, f"Reserved field must be 0, actually `{rsv}`"
        dst_addr = ''
        dst_port = 0
        if atyp == self.ATYP_IPV4:
            dst_addr = socket.inet_ntoa(client.recv(4))
        elif atyp == self.ATYP_IPV6:
            dst_addr = socket.inet_ntop(socket.AF_INET6, client.recv(16))
        elif atyp == self.ATYP_FQDN:
            l = struct.unpack("!B", client.recv(1))[0]
            assert l > 0, f"ATYP is FQDN, but domain length `{l}` is invalid"
            dst_addr = self._handle_domain(client.recv(l).decode())
        else:
            raise AssertionError(f"Unknown atyp `{atyp}`")
        dst_port = struct.unpack('!H', client.recv(2))[0]

        conn = Connection(client, dst_addr, dst_port, addr[0], addr[1], 
            6 if atyp == self.ATYP_IPV6 else 4, self)
        if cmd == self.CMD_CONNECT:
            code = conn.exec_action(Connection.ACTION_CONNECT)
            if code == 0:# 转发成功
                client.sendall(struct.pack("!BBBB", ver, 0, rsv, self.ATYP_IPV4)+
                    socket.inet_pton(socket.AF_INET, self.host)+struct.pack("!H", self.port))
                client.setblocking(False) # 设置非阻塞模式
                return conn
            else:
                client.sendall(struct.pack("!BBBB", ver, code, rsv, self.ATYP_IPV4)+
                    socket.inet_pton(socket.AF_INET, self.host)+struct.pack("!H", self.port))
                raise AssertionError("Connection forward failed!")
        elif cmd == self.CMD_BIND:
            pass
        elif cmd == self.CMD_UDP_ASSOCIATE:
            pass
        else:
            raise AssertionError(f"Unknown cmd `{cmd}`")

        return None
Exemplo n.º 12
0
    def _stream_audio_frames_to_client(self,
                                       client: socket.socket,
                                       frames: List[int],
                                       frames_in_message: int = 80000) -> None:
        """ Streams the audio frames to the provided client in a loop until all frames are sent or client disconnects,
         for the time of streaming the audio frames no other messages should be sent to client,
         sends the :class:`audiostream.MessageType.ENDOFAUDIOFILE` after all frames have been sent.

        :param client: the client for which the frames should be sent

        :param frames: the audio frames which will be sent to the client

        :param frames_in_message: the amount of frames which will be sent in a single message,
         tweaking this parameter can provide better transfer efficiency depending on the situation
        """
        try:
            client.setblocking(True)
            counter = 0
            frames_message = b''
            msg_size = frames_in_message * len(_MSG_PREFIX + b'-2147483647' +
                                               _MSG_SUFIX)
            logging.info(f"Message size {msg_size}")
            client.send(_compose_message(b'%d' % msg_size))
            frames_messages = []
            for frame in frames:
                counter += 1
                sanitized_frame = self._sanitize_message(frame)
                frames_messages.append(_compose_message(sanitized_frame))
                if counter >= frames_in_message:
                    counter = 1
                    fillout = msg_size - len(frames_message)
                    frames_messages.append(b'0' * fillout)
                    frames_message = frames_message.join(frames_messages)
                    client.send(frames_message)
                    frames_message = b''
                    frames_messages = []

            self._send_message_to_client(client, MessageType.ENDOFAUDIOFILE)
            client.setblocking(False)
            print("All audio frames sent")
            logging.info("All audio frames have been sent")
        except ConnectionResetError:
            logging.info(
                f"Connection has been closed by client {client.getsockname()}")

            self._connected_clients_lock.acquire(blocking=True)
            for connected_client in self._connected_clients.copy():
                if client == connected_client[0]:
                    logging.info(f"Removed client from existing connections")
                    self._connected_clients.remove(connected_client)
            self._connected_clients_lock.release()
Exemplo n.º 13
0
def ReceiveParams(server: socket.socket, param_queue: Queue):
    server.setblocking(True)
    print("Listening for network updates...")
    while True:
        try:
            msg = communication.Receive(server)
        except Exception as err:
            print(f"Error with server connection, ending connection")
            server.close()
            return

        buffer = io.BytesIO(msg)
        state_dict = torch.load(buffer)

        param_queue.put(state_dict)
Exemplo n.º 14
0
def ReceivePlayouts(worker: socket.socket, worker_id: int, out_queue: Queue):
    worker.setblocking(True)
    while True:
        try:
            msg: bytes = communication.Receive(worker)
        except Exception as err:
            print(f"Error with worker {worker_id}, ending connection")
            worker.close()
            return

        buffer = io.BytesIO(msg)
        tensor = torch.load(buffer)

        print(f"Received message {tensor}")
        out_queue.put(tensor)
Exemplo n.º 15
0
async def connect(sock: socket.socket, address: Tuple) -> None:
    f = Future()
    # 将socket 设置为非阻塞模式
    sock.setblocking(False)
    try:
        sock.connect(address)
    except BlockingIOError:
        pass

    def on_connected():
        f.set_result(None)

    selector.register(sock.fileno(), EVENT_WRITE, on_connected)
    await f
    # 移除监听
    selector.unregister(sock.fileno())
Exemplo n.º 16
0
def run(net_conn: socket.socket, player_colour):
    net_conn.setblocking(False)
    grid_dims = grid_width, grid_height = 19, 19
    game = Game(grid_width, grid_height, player_colour)
    gui = GUI(SCREEN_WIDTH, SCREEN_HEIGHT, grid_dims)
    interface = Interface(gui, net_conn)

    title = f"Go | You play {'white' if player_colour == Colour.WHITE else 'black'}"
    InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, title.encode('ascii'))
    SetTargetFPS(60)

    while not WindowShouldClose():
        interface.handle_inputs(game)
        gui.draw(game)
    net_conn.close()
    CloseWindow()
Exemplo n.º 17
0
 def _slurp(self, sock: socket.socket) -> bytes:
     # Wait for the first few messages
     sock.settimeout(30)
     header = sock.recv(4)
     payload_size: int = struct.unpack('>I', header)[0]
     bytes_left = payload_size
     sock.setblocking(True)
     chunks: List[bytes] = []
     while bytes_left > 0:
         chunk = sock.recv(bytes_left)
         if len(chunk) == 0:
             break
         chunks.append(chunk)
         bytes_left -= len(chunk)
     payload = b''.join(chunks)
     assert len(payload) == payload_size
     return payload
Exemplo n.º 18
0
    def recv(self, sock:socket.socket):
        try:
            data = sock.recv(1024)
        except Exception as e:
            logging.info(e)
            data = b''
        logging.info(data)
        if not data or data == b'quit':
            self.sel.unregister(sock)
            sock.close()
            return
        else:
            if data == b'list':
                msgs = self.dir.iterdir()
                fileinfo=''
                for msg in msgs:
                    fileinfo += msg.name+' '
                sock.send(fileinfo.encode('GBK'))

            elif data.startswith(b'upload'):
                sock.send('start!'.encode('GBK'))
                *_, filepath = data.decode('GBK').rpartition(' ') #写文件路径 客户端直接开始发送数据
                *_ , filename = filepath.rpartition('\\')
                sock.setblocking(True)
                length = int(sock.recv(1024))
                sock.send('ack'.encode('GBK'))

                remote_filedata = b''
                while True:
                    info = sock.recv(1024)
                    remote_filedata += info
                    if len(remote_filedata) >= length:
                        break
                with open(r'recvfile\{}'.format(filename,),'ab') as f:
                    f.write(remote_filedata)
                sock.send('has done'.encode('GBK'))
                sock.setblocking(False)

            elif data.startswith(b'download'):
                *_, filename = data.decode('GBK').partition(' ')
                path = Path('recvfile\{}'.format(filename))
                print(path.absolute())
                with open('{}'.format(path), 'rb') as f:
                    fileinfo = f.read()
                    filelength = len(fileinfo)
                sock.send(str(filelength).encode('GBK'))
                sock.setblocking(True)
                sock.recv(1024)
                logging.info(str(filelength))
                width = 0
                while True:
                    width = sock.send(fileinfo[width:])
                    if not width:
                        break
                sock.setblocking(False)
Exemplo n.º 19
0
def _get_all_Msgs(sock: socket.socket):
    """ Get all the prepared messages from socket """

    all_msgs = []
    blocking_state = sock.getblocking()
    sock.setblocking(False)

    while True:
        try:
            msg_type, fingerprint, body = _receive_and_decode_Msg(sock)
            all_msgs.append({
                'msg_type': msg_type,
                'fingerprint': fingerprint,
                'body': body
            })
        except BlockingIOError:
            sock.setblocking(blocking_state)
            return all_msgs
Exemplo n.º 20
0
    def _handle_client(self, conn: socket.socket):
        with conn:
            try:
                conn.setblocking(False)
                payload = self._recv_message(conn)
                response = self.handle(payload)
                self._send_message(conn, response)

            except InvalidParameter as e:
                msg = f'Invalid paylad content. "{e}"'
                self.logger.error(msg)
                self._send_message(conn, {"status": "failure", "error": msg})

            except Exception:
                self.logger.exception(
                    f"Unexpected exception, the connection with the unix socket {self.socket_path} has been closed."
                )
            self.logger.debug("Connection with client closed.")
Exemplo n.º 21
0
def ReceivePlayouts(worker: socket.socket, worker_id: int,
                    replay_memory: ReplayMemory, mem_lock: Lock):
    worker.setblocking(True)
    while True:
        try:
            msg = communication.Receive(worker)
        except Exception as err:
            print(f"Error with worker {worker_id}, ending connection")
            worker.close()
            return

        playout: List[MoveDatapoint] = pickle.loads(msg)

        mem_lock.acquire()
        for move_datapoint in playout:
            replay_memory.push(move_datapoint)
        mem_lock.release()
        print(f"{len(playout)} new datapoints added from worker {worker_id}")
Exemplo n.º 22
0
def read_all_data(client_socket: socket.socket) -> bytes:
    chunk_size = 4096  # bytes
    timeout = 0.25  # seconds
    delay = 0.1 # seconds
    data: bytes = b''

    client_socket.setblocking(False)

    until = time.time() + timeout
    while True:
        try:
            chunk: bytes = client_socket.recv(chunk_size)  # try get next chunk of data
            if chunk:
                data += chunk
                until = time.time() + timeout  # reset timer
        except BlockingIOError:  # no data yet
            if time.time() > until:  # timeout reached
                break
            time.sleep(delay)  # add some throttling
    return data
Exemplo n.º 23
0
    def _handle_request(self, conn: socket.socket, address: str) -> None:
        """
        Read data from an accepted connection and return response data.
        """
        # Nonblocking sockets hang and cause connection resets due to failed
        # reads, or something else? But ensuring it is switched to blocking
        # resolves this.
        conn.setblocking(1)

        data = conn.recv(512)
        self.logger.debug(f'{data}')

        response_text = '<html><body>Hello!</body></html>'
        response = ('HTTP/1.1 200 OK\r\n'
                    f'Content-Length: {len(response_text)}\r\n'
                    'Content-Type: text/html\r\n'
                    'Connection: close\r\n'
                    '\r\n'
                    f'{response_text}').encode()
        conn.sendall(response)
        conn.close()
        self.logger.debug(f'{response}')
Exemplo n.º 24
0
class _InProgressSocketManager(object):
    """
    This class does not actually work right now. It's something that I'm
    writing that will some day replace InputThread and OutputThread and allow
    Autobus to function using only a single thread for each Bus instance. So
    it's a work in progress, and it doesn't actually work right now.
    """
    def __init__(self):
        # Set up the interrupt socket
        interrupt_server = Socket()
        interrupt_server.bind(("localhost", 0))
        interrupt_server.listen(1)
        self.interrupt_writer = Socket()
        self.interrupt_writer.setblocking(False)
        self.interrupt_writer.connect("localhost", interrupt_server.getsockname()[1])
        self.interrupt_reader = interrupt_server.accept()
        interrupt_server.shutdown(SHUT_RDWR)
        interrupt_server.close()
        self.interrupt_reader.setblocking(False)
        self.interrupt_writer.setblocking(False)
    
    def loop(self):
        pass
    
    def run_sync(self, function, timeout=None):
        q = Queue()
        @self.run_async
        def new_function():
            q.put(function())
        try:
            return q.get(timeout=timeout)
        except Empty:
            raise exceptions.TimeoutException
    
    def run_async(self, function):
        pass
    
    def interrupt(self):
        self.interrupt_writer.send(0)
Exemplo n.º 25
0
def _receive_and_decode_Msg(sock: socket.socket):
    """ Receive bytes and parse to a message(message type, fingerprint(SHA256 of the task), body) """
    def _get_body_content_one():
        body_bytes = int.from_bytes(_recv_exact_n_bytes(sock, 8), 'big')
        return bytes.decode(_recv_exact_n_bytes(sock, body_bytes), 'utf-8')

    msg_type = _MsgType(int.from_bytes(_recv_exact_n_bytes(sock, 1), 'big'))
    blocking_state = sock.getblocking()
    sock.setblocking(True)

    fingerprint = None

    if msg_type == _MsgType.HeartBeat or msg_type == _MsgType.HeartBeat_Res:
        body = {'slave_id': _get_body_content_one()}
    else:
        fingerprint = hex(int.from_bytes(sock.recv(32), 'big'))[2:]
        body = dict()

        if msg_type == _MsgType.AllTask:
            body['mapFunc'] = _get_body_content_one()
            body['reduceFunc'] = _get_body_content_one()
            body['file'] = json.loads(_get_body_content_one())

        elif msg_type == _MsgType.MapTask:
            body['mapFunc'] = _get_body_content_one()
            body['file'] = json.loads(_get_body_content_one())

        elif msg_type == _MsgType.ReduceTask:
            body['reduceFunc'] = _get_body_content_one()
            body['file'] = json.loads(_get_body_content_one())

        elif msg_type == _MsgType.MapTask_Res or msg_type == _MsgType.ReduceTask_Res or msg_type == _MsgType.AllTask_Res:
            body['result'] = json.loads(_get_body_content_one())

    sock.setblocking(blocking_state)

    return msg_type, fingerprint, body
Exemplo n.º 26
0
def anetNonBlock(fd: socket.socket) -> None:
    try:
        fd.setblocking(False)
    except OSError:
        fd.close()
        raise
Exemplo n.º 27
0
 def __init__(self, sock: socket, loop: EventLoop):
     sock.setblocking(False)
     self.sock: socket = sock
     self.loop = loop
Exemplo n.º 28
0
    def proc_msg(self, sock: socket.socket) -> None:  # noqa: C901
        """
        This is the thread message loop to process messages.
        We will send any message that is queued via the self.msg variable
        when our socket is in a writable state.
        And we will read any messages when it's in a readable state and then
        call self.on_msg_recv with the json object message.
        """
        sock.setblocking(False)
        inputs = [sock]
        outputs = [sock]
        localbuffer = ""

        while self.do_process_msgs:
            # without this sleep, I was getting very consistent socket errors
            # on Windows. Perhaps we don't need this sleep on other platforms.
            time.sleep(self.poll_socket_sleep_sec)
            try:
                # test our socket for readable, writable states.
                readable, writable, exceptional = select.select(
                    inputs, outputs, inputs)

                for s in readable:
                    try:
                        data = s.recv(1024 * 256)
                    except ConnectionAbortedError:
                        logger.warn("socket connection aborted")
                        print("socket connection aborted")
                        self.do_process_msgs = False
                        break

                    # we don't technically need to convert from bytes to string
                    # for json.loads, but we do need a string in order to do
                    # the split by \n newline char. This seperates each json msg.
                    data = data.decode("utf-8")

                    localbuffer += data

                    n0 = localbuffer.find("{")
                    n1 = localbuffer.rfind("}\n")
                    if n1 >= 0 and 0 <= n0 < n1:  # there is at least one message :
                        msgs = localbuffer[n0:n1 + 1].split("\n")
                        localbuffer = localbuffer[n1:]

                        for m in msgs:
                            if len(m) <= 2:
                                continue
                            # Replace comma with dots for floats
                            # useful when using unity in a language different from English
                            m = replace_float_notation(m)
                            try:
                                j = json.loads(m)
                            except Exception as e:
                                logger.error("Exception:" + str(e))
                                logger.error("json: " + m)
                                continue

                            if "msg_type" not in j:
                                logger.error("Warning expected msg_type field")
                                logger.error("json: " + m)
                                continue
                            else:
                                self.on_msg_recv(j)

                for s in writable:
                    if self.msg is not None:
                        logger.debug("sending " + self.msg)
                        s.sendall(self.msg.encode("utf-8"))
                        self.msg = None

                if len(exceptional) > 0:
                    logger.error("problems w sockets!")

            except Exception as e:
                print("Exception:", e)
                self.aborted = True
                self.on_msg_recv({"msg_type": "aborted"})
                break
Exemplo n.º 29
0
    def _handle_connection(
        self,
        receive_frame_generator: Iterator[Optional[bytes]],
        tcp_socket: socket.socket
    ) -> None:
        """ Inner loop that handles data exchange over socket. """
        # Set socket back blocking mode.
        tcp_socket.setblocking(True)
        for raw_message_received in receive_frame_generator:
            try:
                middleman_message = AbstractFrame.deserialize(
                    raw_message_received,
                    public_key=self.concent_public_key,
                )
                # Heartbeat is received: connection is still active and Signing Service doesn't have to respond.
                if middleman_message.payload_type == PayloadType.HEARTBEAT:
                    continue

                if (
                    not middleman_message.payload_type == PayloadType.GOLEM_MESSAGE or
                    not isinstance(middleman_message.payload, TransactionSigningRequest)
                ):
                    raise SigningServiceUnexpectedMessageError

            # Is the frame correct according to the protocol? If not, error code is InvalidFrame.
            except (
                FrameInvalidMiddlemanProtocolError,
                PayloadTypeInvalidMiddlemanProtocolError,
                RequestIdInvalidTypeMiddlemanProtocolError,
            ) as exception:
                middleman_message_response = self._prepare_error_response(ErrorCode.InvalidFrame, exception)
            # Is frame signature correct? If not, error code is InvalidFrameSignature.
            except SignatureInvalidMiddlemanProtocolError as exception:
                middleman_message_response = self._prepare_error_response(ErrorCode.InvalidFrameSignature, exception)
            # Is the content of the message valid? Do types match the schema and all values are within allowed ranges?
            # If not, error code is InvalidPayload.
            # Can the payload be decoded as a Golem message? If not, error code is InvalidPayload.
            # Is payload message signature correct? If not, error code is InvalidPayload.
            except (MessageError, PayloadInvalidMiddlemanProtocolError) as exception:
                middleman_message_response = self._prepare_error_response(ErrorCode.InvalidPayload, exception)
            # Is frame type GOLEM_MESSAGE? If not, error code is UnexpectedMessage.
            # Is Golem message type TransactionSigningRequest? If not, error code is UnexpectedMessage.
            except SigningServiceUnexpectedMessageError as exception:
                middleman_message_response = self._prepare_error_response(ErrorCode.UnexpectedMessage, exception)
            # If received frame is correct, validate transaction.
            else:
                self._update_daily_transactions_limit_file_name()
                golem_message_response = self._get_signed_transaction(middleman_message.payload)
                if isinstance(golem_message_response, SignedTransaction):
                    transaction_sum_combined = self.signing_service_daily_transaction_sum_so_far + middleman_message.payload.value
                    if transaction_sum_combined > MAXIMUM_DAILY_THRESHOLD:
                        logger.warning(
                            f'Signing Service is unable to transact more then {MAXIMUM_DAILY_THRESHOLD} GNTB today.'
                            f'Transaction from {middleman_message.payload.from_address} rejected.'
                        )
                        self.notifier.send(
                            f'Signing Service is unable to transact more then {MAXIMUM_DAILY_THRESHOLD} GNTB today.'
                        )
                        golem_message_response = TransactionRejected(
                            reason=TransactionRejected.REASON.DailyLimitExceeded,
                            nonce=middleman_message.payload.nonce,
                        )
                    elif transaction_sum_combined > WARNING_DAILY_THRESHOLD:
                        logger.warning(f'Signing Service has signed transactions worth {transaction_sum_combined} GNTB today.')
                        self.notifier.send(
                            f'Signing Service has signed transactions worth {transaction_sum_combined} GNTB today.'
                        )
                        self._add_payload_value_to_daily_transactions_sum(transaction_sum_combined)
                    else:
                        self._add_payload_value_to_daily_transactions_sum(transaction_sum_combined)
                golem_message_response.sign_message(private_key=self.signing_service_private_key)
                middleman_message_response = GolemMessageFrame(
                    payload=golem_message_response,
                    request_id=middleman_message.request_id,
                )

            logger.info(
                f'Sending Middleman protocol message with request_id: {middleman_message_response.request_id}.'
            )
            send_over_stream(
                connection=tcp_socket,
                raw_message=middleman_message_response,
                private_key=self.signing_service_private_key,
            )
Exemplo n.º 30
0
class GoClient:

    def __init__(self, IP='127.0.0.1', port=5005):
        self.root = tk.Tk()
        self.IP = IP
        self.port = port
        self.players = []

    def start_game(self, player, size):
        self.players.append(player)
        self.board = Board(size)
        print 'beginning game as player %d' % player
        self.make_display()
        self.gui.start_game()

    def made_move(self, i, j):
        self.board.place_piece(i, j)
        self.gui.made_move()

    def passed_turn(self):
        self.board.pass_turn()
        self.gui.passed_turn()

    def game_over(self, score1, score2):
        self.gui.set_message('Game Over, Black: %d White: %d' % (score1, score2))
        self.board.gameover = True

    def on_click(self, i, j):
        if self.board.turn in self.players:
            self.send('MAKEMOVE %d %d' % (i, j))
            # self.receive('MADEMOVE %d %d' % (i, j))

    def on_quit(self):
        send('QUIT')
        self.gui.parent.destroy()

    def on_pass(self):
        if self.board.turn in self.players:
            self.send('PASSTURN')

    def run(self):
        # self.start_game(1)
        # self.start_game(2)
        self.connect_to_server()
        self.root.title('Python Online Five-In-A-Row')
        # root.resizable(0,0)
        self.root.mainloop()
        # print 'received data:', data

    def make_display(self):
        self.gui = BoardGui(parent=self.root, board=self.board, players=self.players)
        self.gui.on_click.append(self.on_click)
        self.gui.on_pass.append(self.on_pass)
        self.gui.pack(side='top', fill='both', expand='true', padx=4, pady=4)
       
    def receive(self, data):
        print 'receiving [%s]' % data
        data = data.split()
        if not data:
            return
        message = data[0]
        if message == 'BEGINGAME':
            self.start_game(int(data[1]), int(data[2]))
        elif message == 'MADEMOVE':
            i, j = map(int, data[1:3])
            self.made_move(i, j)
        elif message == 'PASSEDTURN':
            self.passed_turn()
        elif message == 'GAMEOVER':
            a, b = map(int, data[1:3])
            self.game_over(a, b)

    def send(self, data):
        print 'sending %s' % data
        self.skt.send(data)

    def connect_to_server(self):
        BUFFER_SIZE = 1024

        self.skt = Socket(AF_INET, SOCK_STREAM)
        self.skt.connect((self.IP, self.port))
        self.skt.setblocking(0)
        def listen():
            try:
                data = self.skt.recv(BUFFER_SIZE)
            except SocketError:
                pass
            else:
                if not data:
                    return
                for line in data.split('\n'):
                    self.receive(line)
            self.root.after(500, listen)

        listen()
Exemplo n.º 31
0
class GoBotClient:

    def __init__(self, IP='127.0.0.1', port=5005):
        self.IP = IP
        self.port = port
        self.player = None
        self.turn = 2

    def start_game(self, player, size):
        self.player = player
        print 'beginning game as player %d' % player
        self.GoBot = GoBot(0 if player == 2 else 1)
        
        if player == 2:
            self.make_move('pass')

    def made_move(self, i, j):
        self.turn = 1 if self.turn == 2 else 2

    def passed_turn(self):
        self.turn = 1 if self.turn == 2 else 2

    def make_move(self, move):
        if self.turn == self.player:
            if move != 'pass':
                gobot_move = self.GoBot.make_move((move[1], move[0]))
            else:
                gobot_move = self.GoBot.make_move('pass')
            self.send('MAKEMOVE %d %d' % (gobot_move[1], gobot_move[0]))

    def run(self):
        self.connect_to_server()
        while True:
            if self.listen():
                print "Disconnected from server\n"
                return
       
    def receive(self, data):
        print 'receiving [%s]' % data
        data = data.split()
        if not data:
            return
        message = data[0]
        if message == 'BEGINGAME':
            self.start_game(int(data[1]), int(data[2]))
        elif message == 'MADEMOVE':
            i, j = map(int, data[1:3])
            self.made_move(i, j)
            if self.turn == self.player:
                self.make_move((i, j))
        elif message == 'PASSEDTURN':
            self.passed_turn()
            if self.turn == self.player:
                self.make_move('pass')
        elif message == 'GAMEOVER':
            a, b = map(int, data[1:3])
            self.game_over(a, b)
        elif message == 'FINISH':
            return True

    def send(self, data):
        print 'sending %s' % data
        self.skt.send(data)

    def connect_to_server(self):
        BUFFER_SIZE = 1024

        self.skt = Socket(AF_INET, SOCK_STREAM)
        self.skt.connect((self.IP, self.port))
        self.skt.setblocking(0)
    
    def listen(self):
        BUFFER_SIZE = 1024

        try:
            data = self.skt.recv(BUFFER_SIZE)
        except SocketError:
            pass
        else:
            if not data:
                return
            for line in data.split('\n'):
                if self.receive(line):
                    return True
Exemplo n.º 32
0
def accept(conn: socket.socket, msk: int):
    print(msk)
    conn, r_address = conn.accept()
    conn.setblocking(False)
    keys = s.register(conn, selectors.EVENT_READ, rec)
    print(keys)
Exemplo n.º 33
0
class MSO3000andDPO3000Series():
    #pylint: disable=too-many-instance-attributes
    """PLACE device class for the MSO3000 and DPO3000 series oscilloscopes."""
    _bytes_per_sample = 2
    _data_type = np.dtype(
        '<i' + str(_bytes_per_sample))  # (<)little-endian, (i)signed integer

    def __init__(self, config):
        self._config = config
        self.priority = 100
        self._updates = None
        self._ip_address = None
        self._scope = None
        self._channels = None
        self._samples = None
        self._record_length = None
        self._x_zero = None
        self._x_increment = None

    def config(self, metadata, total_updates):
        """Configure the oscilloscope.

        :param metadata: metadata for the experiment
        :type metadata: dict

        :param total_updates: the number of update steps that will be in this experiment
        :type total_updates: int

        :raises OSError: if unable to connect to oscilloscope
        """
        name = self.__class__.__name__
        self._updates = total_updates
        self._ip_address = OSCILLOSCOPE_IP_ADDRESS
        self._scope = Socket(AF_INET, SOCK_STREAM)
        self._scope.settimeout(5.0)
        try:
            self._scope.connect((self._ip_address, 4000))
        except OSError:
            self._scope.close()
            del self._scope
            raise
        self._channels = [
            self._is_active(x + 1)
            for x in range(self._get_num_analog_channels())
        ]
        self._record_length = self._get_record_length()
        metadata[name + '-record_length'] = self._record_length
        self._x_zero = [None for _ in self._channels]
        self._x_increment = [None for _ in self._channels]
        metadata[name + '-active_channels'] = self._channels
        self._samples = self._get_sample_rate()
        metadata[name + '-sample_rate'] = self._samples
        for channel, active in enumerate(self._channels):
            if not active:
                continue
            chan = channel + 1
            self._send_config_msg(chan)
            self._x_zero[channel] = self._get_x_zero(chan)
            self._x_increment[channel] = self._get_x_increment(chan)
            metadata[name +
                     '-ch{:d}_x_zero'.format(chan)] = self._x_zero[channel]
            metadata[name + '-ch{:d}_x_increment'.format(
                chan)] = self._x_increment[channel]
            metadata[name +
                     '-ch{:d}_y_zero'.format(chan)] = self._get_y_zero(chan)
            metadata[name +
                     '-ch{:d}_y_offset'.format(chan)] = self._get_y_offset(
                         chan)
            metadata[name + '-ch{:d}_y_multiplier'.format(
                chan)] = self._get_y_multiplier(chan)
        self._scope.close()

    def update(self):
        """Get data from the oscilloscope.

        :returns: the trace data
        :rtype: numpy.array dtype='(*number_channels*,*number_samples*)int16'
        """
        self._scope = Socket(AF_INET, SOCK_STREAM)
        self._scope.settimeout(5.0)
        self._scope.connect((self._ip_address, 4000))
        #self._activate_acquisition()
        field = '{}-trace'.format(self.__class__.__name__)
        type_ = '({:d},{:d})int16'.format(len(self._channels),
                                          self._record_length)
        data = np.zeros((1, ), dtype=[(field, type_)])
        print('transfering waveform... ', end='')
        sys.stdout.flush()
        for channel, active in enumerate(self._channels):
            if not active:
                continue
            self._request_curve(channel + 1)
            trace = self._receive_curve()
            data[field][0][channel] = trace
        self._scope.close()
        print('done')
        return data.copy()

    def cleanup(self, abort=False):
        """End the experiment.

        :param abort: indicates the experiment is being aborted rather than
                      having finished normally
        :type abort: bool
        """
        pass

    def _clear_errors(self):
        self._scope.sendall(bytes(':*ESR?;:ALLEv?\n', encoding='ascii'))
        dat = ''
        while '\n' not in dat:
            dat += self._scope.recv(4096).decode('ascii')

    def _is_active(self, channel):
        self._scope.settimeout(5.0)
        self._clear_errors()
        self._scope.sendall(
            bytes(':DATA:SOURCE CH{:d};:WFMOUTPRE?\n'.format(channel),
                  encoding='ascii'))
        dat = ''
        while '\n' not in dat:
            dat += self._scope.recv(4096).decode('ascii')
        self._scope.sendall(b'*ESR?\n')
        dat = ''
        while '\n' not in dat:
            dat += self._scope.recv(4096).decode('ascii')
        self._clear_errors()
        return int(dat) == 0

    def _get_num_analog_channels(self):
        self._scope.settimeout(5.0)
        self._scope.sendall(b':CONFIGURATION:ANALOG:NUMCHANNELS?\n')
        dat = ''
        while '\n' not in dat:
            dat += self._scope.recv(4096).decode('ascii')
        return int(dat)

    def _get_x_zero(self, channel):
        self._scope.settimeout(5.0)
        self._scope.sendall(
            bytes(':HEADER OFF;:DATA:SOURCE CH{:d};:WFMOUTPRE:XZERO?\n'.format(
                channel),
                  encoding='ascii'))
        dat = ''
        while '\n' not in dat:
            dat += self._scope.recv(4096).decode('ascii')
        return float(dat)

    def _get_y_zero(self, channel):
        self._scope.settimeout(5.0)
        self._scope.sendall(
            bytes(':HEADER OFF;:DATA:SOURCE CH{:d};:WFMOUTPRE:YZERO?\n'.format(
                channel),
                  encoding='ascii'))
        dat = ''
        while '\n' not in dat:
            dat += self._scope.recv(4096).decode('ascii')
        return float(dat)

    def _get_x_increment(self, channel):
        self._scope.settimeout(5.0)
        self._scope.sendall(
            bytes(':HEADER OFF;:DATA:SOURCE CH{:d};:WFMOUTPRE:XINCR?\n'.format(
                channel),
                  encoding='ascii'))
        dat = ''
        while '\n' not in dat:
            dat += self._scope.recv(4096).decode('ascii')
        return float(dat)

    def _get_y_offset(self, channel):
        self._scope.settimeout(5.0)
        self._scope.sendall(
            bytes(':HEADER OFF;:DATA:SOURCE CH{:d};:WFMOUTPRE:YOFF?\n'.format(
                channel),
                  encoding='ascii'))
        dat = ''
        while '\n' not in dat:
            dat += self._scope.recv(4096).decode('ascii')
        return float(dat)

    def _get_y_multiplier(self, channel):
        self._scope.settimeout(5.0)
        self._scope.sendall(
            bytes(':HEADER OFF;:DATA:SOURCE CH{:d};:WFMOUTPRE:YMULT?\n'.format(
                channel),
                  encoding='ascii'))
        dat = ''
        while '\n' not in dat:
            dat += self._scope.recv(4096).decode('ascii')
        return float(dat)

    def _get_sample_rate(self):
        self._scope.settimeout(5.0)
        self._scope.sendall(b':HEADER OFF;:HORIZONTAL:SAMPLERATE?\n')
        dat = ''
        while '\n' not in dat:
            dat += self._scope.recv(4096).decode('ascii')
        return float(dat)

    def _get_record_length(self):
        self._scope.settimeout(5.0)
        self._scope.sendall(b':HEADER OFF;:HORIZONTAL:RECORDLENGTH?\n')
        dat = ''
        while '\n' not in dat:
            dat += self._scope.recv(4096).decode('ascii')
        return int(dat)

    def _send_config_msg(self, channel):
        config_msg = bytes(
            ':DATA:' + ('SOURCE CH{:d};'.format(channel) + 'START 1;' +
                        'STOP {};'.format(self._record_length)) +
            ':WFMOUTPRE:' + ('BYT_NR 2;' + 'BIT_NR 16;' + 'ENCDG BINARY;' +
                             'BN_FMT RI;' + 'BYT_OR LSB;') + ':HEADER 0\n',
            encoding='ascii')
        self._scope.sendall(config_msg)

    def _activate_acquisition(self):
        self._scope.sendall(b':ACQUIRE:STATE ON\n')
        sleep(0.1)
        if self._config['force_trigger']:
            self._force_trigger()
        else:
            self._wait_for_trigger()

    def _force_trigger(self):
        for _ in range(120):
            self._scope.settimeout(60)
            self._scope.sendall(b':TRIGGER FORCE\n')
            sleep(0.1)
            self._scope.settimeout(0.25)
            try:
                self._scope.recv(4096)
            except OSError:
                pass
            self._scope.settimeout(60)
            self._scope.sendall(b':ACQUIRE:STATE?\n')
            sleep(0.1)
            byte = b''
            for _ in range(600):
                byte = self._scope.recv(1)
                if byte == b'0' or byte == b'1':
                    self._scope.settimeout(0.25)
                    try:
                        self._scope.recv(4096)
                    except OSError:
                        pass
                    break
            if byte == b'0':
                break

    def _wait_for_trigger(self):
        print('waiting for trigger(s)... ', end='')
        sys.stdout.flush()
        self._scope.setblocking(False)
        for _ in range(120):
            self._scope.sendall(b':ACQUIRE:STATE?\n')
            byte = b''
            for _ in range(600):
                try:
                    byte = self._scope.recv(1)
                except BlockingIOError:
                    sleep(0.1)
                    continue
                if byte == b'0' or byte == b'1':
                    break

            if byte == b'0':
                break
            sleep(0.5)
        print('done')
        sys.stdout.flush()

    def _request_curve(self, channel):
        self._scope.settimeout(60.0)
        self._scope.sendall(
            bytes(':DATA:SOURCE CH{:d};:CURVE?\n'.format(channel),
                  encoding='ascii'))

    def _receive_curve(self):
        hash_message = b''
        while hash_message != b'#':
            hash_message = self._scope.recv(1)

        length_length = int(self._scope.recv(1).decode(), base=16)
        length = int(self._scope.recv(length_length).decode(), base=10)
        data = b''
        while len(data) < length:
            data += self._scope.recv(4096)
        data = data[:length]
        return np.frombuffer(data, dtype='int16')