def _receive(self, packet_buffer={}): packet = PacketBuffer(self._connection.recv(PACKET_SIZE)) packet_type = packet.get_long() if packet_type == PACKET_HEAD: return packet elif packet_type == PACKET_SPLIT: request_id = packet.get_long() # TODO: compressed? if request_id not in packet_buffer: packet_buffer.setdefault(request_id, []) total_packets = packet.get_byte() current_packet_number = packet.get_byte() paket_size = packet.get_short() packet_buffer[request_id].insert(current_packet_number, packet.read()) if current_packet_number == total_packets - 1: full_packet = PacketBuffer(b''.join(packet_buffer[request_id])) if full_packet.get_long() == PACKET_HEAD: return full_packet else: return self._receive(packet_buffer)
def __init__(self, peer, N): """GBN receiving-side :param peer: peer (hostname, port) :param N: receive window size """ GBN.__init__(self, peer) self.N = N # buffer size self.rcvbuf = PacketBuffer(self.N) self.state = State.Wait self.base = Seq(0) self.FIN_delivered = False
def __init__(self, peer, N): """GBN sending-side :param peer: peer (hostname, port) :param N: send window size """ GBN.__init__(self, peer) self.N = N # buffer size self.sndbuf = PacketBuffer(self.N) self.state = State.Wait self.base = Seq(0) self.next_seq = Seq(0)
def main(): """Main""" next_no = 0 exit_flag = False sin_port, sout_port, csin_port, file = checkCommandArgs() #sin socket sin = Server(IP, sin_port, BUFFER_SIZE) sin.accept() #sout socket sout = Client(IP, sout_port) sout.connect(IP, csin_port) open_file = File(file) buffer = PacketBuffer() sent = 0 while (not exit_flag): data, data_len = open_file.readBytes() pack = Packet(DATA_PACKET, next_no, data_len, data) if data_len == 0: exit_flag = True buffer.onqueue(pack) while True: sout.send(pickle.dumps(buffer.peek())) sent += 1 if not sin.receiveSecond(next_no): print("no responce or incorrect responce") continue print("response and checked") next_no += 1 buffer.dequeue() break open_file.close() sout.shutdown() sin.close() sout.close() print("sent: " + str(sent))
def create(packet_type): packet = PacketBuffer() packet.put_long(PACKET_HEAD) if packet_type == PacketType.Info: packet.put_string(RequestType.Info) elif packet_type == PacketType.Challenge: packet.put_byte(RequestType.Players) packet.put_long(RequestType.Challenge) elif packet_type == PacketType.Players: packet.put_byte(RequestType.Players) elif packet_type == PacketType.Rules: packet.put_byte(RequestType.Rules) else: return None return packet