def sendpacket(self): if not self.need_ack and not self.reliable_messages and not self.unreliable_messages: return if not self.sock: return if self.holdoff: return pkt = DataIO() pkt.writeint(self.sendseq) pkt.writeint(self.sendrelseq) pkt.writeint(self.recvrelseq) pkt.writeshort(0) pkt.writeshort(0) numrel = 0 numunrel = 0 trunc = False for msg in self.reliable_messages: if not msg: print "F**K ME! IN THE ASSHOLE!" if msg.message_size() + pkt.tell() >= (MAX_DGRAM-4): trunc = True break numrel += 1 msg.writeto(pkt) while not trunc and self.unreliable_messages: msg = self.unreliable_messages[0]; if msg.message_size() + pkt.tell() >= (MAX_DGRAM-4): trunc = True break msg.writeto(pkt) numunrel += 1 self.queue_size -= msg.message_size() del self.unreliable_messages[0] self.sendseq += 1 self.need_ack = False pkt.seek(12) if numrel == 0 and trunc: pkt.writeshort(65535) else: pkt.writeshort(numrel) pkt.writeshort(numunrel) if trunc: self.holdoff = True print 'sending %r to %r'%(pkt.getvalue(), self.peer) self.sock.sock.sendto(pkt.getvalue(), self.peer);
def __init__(self, type=0, data=None): DataIO.__init__(self, data) self.type = type
def gotpacket(self, data): print 'got packet %r'%data if not self.handler: return False ctime = time.time(); if len(data) < 16: if self.isnew: return False return True self.holdoff = True pkt = DataIO(data) seq = pkt.readint() relseq = pkt.readint() relack = pkt.readint() numrel = pkt.readshort() numunrel = pkt.readshort() if seq < self.recvseq: return True if seq != self.recvseq: num = seq - self.recvseq; self.dropcount += num self.handler.dropped(num) self.recvseq = seq + 1 # numrel == 65535 has a special meaning: it means there are no reliable # messages, but we should send an acknowledgement anyway. if numrel != 0 and numrel != 65535: need_ack = True numdups = self.recvrelseq - relseq self.recvrelseq = relseq + numrel for i in xrange(numrel): msg = readmessage(pkt) msg.time = ctime if i >= numdups: self.putmessage(msg) for i in xrange(numunrel): msg = readmessage(pkt) msg.time = ctime self.putmessage(msg) # okay, process the acks if relack > self.sendrelseq: numrecv = relack - self.sendrelseq; self.sendrelseq = relack; while self.reliable_messages: msg = self.reliable_messages[0] self.queue_size -= msg.message_size() del self.reliable_messages[0] if ((numrel == 65535 or numrel == 0) and numunrel == 0) and self.isnew: return False self.isnew = False self.holdoff = False return True