def request(self,msg,timeout=None): start_time = time.time() req_id = self.send_seq req_msg = BotMessage.BotMessage(msg.type,msg.data,BotMessage.BotMessage.REQ,req_id) with self.init_cond: if(self.initialized == False): self.init_cond.wait() with self.send_lock: txt = BotMessage.toTxt(self.key,self.iv,self.send_seq,req_msg) self.send_seq += 1 self.msg_input_q.put(txt) while(True): with self.msg_arrival_cond: if(self.msg_output_q.empty()): self.msg_arrival_cond.wait(timeout) with self.recv_lock: if(self.msg_output_q.empty() == False): msg = self.msg_output_q.get(False) obj = BotMessage.fromTxt(self.key,self.iv,self.recv_seq,msg) self.recv_seq += 1 if(obj.req_type == BotMessage.BotMessage.RESP and obj.req_id == req_id): return obj else: self.request_buffer.put(obj,True,timeout) #Check the buffer for the desired message result = None tmp = [] while(self.request_buffer.empty() == False): obj = self.request_buffer.get() if(obj.req_type == BotMessage.BotMessage.RESP and obj.req_id == req_id): result = obj break else: tmp.append(obj) for obj in tmp: self.request_buffer.put(obj,True) if(result == None and (time.time() - start_time) < timeout): continue return result """
def receive(self,block=True,timeout=None): with self.init_cond: if(self.initialized == False): self.init_cond.wait() if(self.request_buffer.empty() == False): return self.request_buffer.get(block,timeout) try: msg = self.msg_output_q.get(block,timeout) except Queue.Empty: return None with self.recv_lock: obj = BotMessage.fromTxt(self.key,self.iv,self.recv_seq,msg) self.recv_seq += 1 return obj
def protocolInit(self): with self.init_cond: asyncore.loop(timeout=1,count=2,map=self.map) if(self.msg_output_q.empty() == False): msg = self.msg_output_q.get(self) obj = BotMessage.fromTxt(self.key,self.iv,self.recv_seq,msg) self.recv_seq += 1 if(obj.type == "SET_IV"): self.iv = obj.data hello = ClientHello(self.client_type) txt = BotMessage.toTxt(self.key,self.iv,self.send_seq,hello) self.msg_input_q.put(txt) self.send_seq += 1 self.initialized = True self.init_cond.notifyAll() else: assert("Failed to set IV") else: assert("Server did not send initial message!")
def found_terminator(self): msg = BotMessage.fromTxt(self.key, self.iv, self.recv_seq, self.data) self.recv_seq += 1 self.data = "" self.gamelogic.handleBotMessage(msg, self)
def found_terminator(self): msg = BotMessage.fromTxt(self.key,self.iv,self.recv_seq,self.data) self.recv_seq += 1 self.data = "" self.gamelogic.handleBotMessage(msg,self)