def data_received(self, data): data = self._rest + data (msgs, rest) = tincanchat.parse_recvd_data(data) self._rest = rest for msg in msgs: msg = msg.decode('utf-8') msg = '{}: {}'.format(self.addr, msg) print(msg) msg = tincanchat.prep_msg(msg) for client in clients: client.transport.write(msg)
def data_received(self, data): """ handle data as its received, broadcast complete message to all other clients """ data = self._rest + data (msgs, rest) = tincanchat.parse_recvd_data(data) self._rest = rest for msg in msgs: msg = msg.decode('utf-8') msg = '{}: {}'.format(self.addr, msg) print(msg) msg = tincanchat.prep_msg(msg) for client in clients: client.transport.write(msg)
def data_received(self, data): """ Handle data as it's received. Broadcast complete messages to all other clients """ data = self._rest + data (msgs, rest) = tincanchat.parse_recvd_data(data) self._rest = rest for msg in msgs: msg = '{}: {}'.format(self.addr, msg) print(msg) msg = tincanchat.prep_msg(msg) for client in clients: client.transport.write(msg) # <-- non-blocking
def data_received(self, data): """ Handle data as it's received. Broadcast complete messages to all other clients """ data = self._rest + data (msgs, rest) = tincanchat.parse_recvd_data(data) self._rest = rest for msg in msgs: msg = '{}: {}'.format(self.addr, msg) print(msg) msg = tincanchat.prep_msg(msg) for client in clients: client.transport.write(msg) # <-- non-blocking
# Handle recieved data on socket elif event & select.POLLIN: client = clients[fd] addr = client.sock.getpeername() recvd = client.sock.recv(4096) if not recvd: # THe client state will get cleaned up in # the next iteration of the event loop, as close() # sets the sock to POLLNVAL client.sock.close() print("Client {} disconnected".format(addr)) continue data = client.rest + recvd (msgs, client.rest) = \ tincanchat.parse_recvd_data(data) # If we have any messages, broadcast them to all clients for msg in msgs: msg = '{}: {}'.format(addr, msg) print(msg) broadcast_msg(msg) # Send message to ready client elif event & select.POLLOUT: client = clients[fd] data = client.send_queue.popleft() sent = client.sock.send(data) if sent < len(data): client.sends.appendleft(data[sent:]) if not client.send_queue: poll.modify(client.sock, select.POLLIN)
# Handle received data on socket elif event & select.POLLIN: client = clients[fd] addr = client.sock.getpeername() recvd = client.sock.recv(4096) if not recvd: # The client state will get cleaned up in the # next iteration of the event loop, as close() # sets the socket to POLLNVAL client.sock.close() print('Client {} disconnected'.format(addr)) continue data = client.rest + recvd (msgs, client.rest) = \ tincanchat.parse_recvd_data(data) # If we have any messages, broadcast them to all # clients for msg in msgs: msg = '{}: {}'.format(addr, msg) print(msg) broadcast_msg(msg) # Send message to ready client elif event & select.POLLOUT: client = clients[fd] data = client.send_queue.popleft() sent = client.sock.send(data) if sent < len(data): client.sends.appendleft(data[sent:]) if not client.send_queue:
elif fd == listen_sock.fileno(): client_sock, addr = listen_sock.accept() client_sock.setblocking(False) fd = client_sock.fileno() clients[fd] = create_client(client_sock) poll.register(fd, select.POLLIN) print('Connection from {}'.format(addr)) elif event & select.POLLIN: client = clients[fd] addr = client.sock.getpeername() recvd = client.sock.recv(4096) if not recvd: client.sock.close() print('Client {} disconnected'.format(addr)) continue data = client.rest + recvd (msgs, client.rest) = tincanchat.parse_recvd_data(data) for msg in msgs: msg = '{}: {}'.format(addr, msg) print(msg) broadcast_msg(msg) elif event & select.POLLOUT: client = clients[fd] data = client.send_queue.popleft() sent = client.sock.send(data) if sent < len(data): client.sends.appendleft(data[sent:]) if not client.send_queue: poll.modify(client.sock, select.POLLIN)