def _do_a_test(self, c): c.sendall(net.PackageHeader(net.PackageType.QUERY_MAX_ID, 10).dump()) c.sendall(b'\x02\x00\x00\x00\x00\x00\x00\x0013') (h, _) = net.PackageHeader.load(c.recv(net.PackageHeader.SIZE)) self.assertEqual(h.typee, net.PackageType.REPLY_MAX_ID) self.assertEqual(h.size, 4) (a, _) = net.IdentityStruct.unpack(c.recv(4)) self.assertEqual(a, 13)
def runTest(self): header = net.PackageHeader(7, 13) buf = b'\x07\x00\x00\x00' + b'\x0d\x00\x00\x00\x00\x00\x00\x00' self.assertEqual(header.SIZE, len(buf)) self.assertEqual(header.dump(), buf) header2, offs = net.PackageHeader.load(buf) self.assertEqual(offs, len(buf)) self.assertEqual(header2.typee, header.typee) self.assertEqual(header2.size, header.size)
def runTest(self): header = net.PackageHeader(7, 13) buf = b'\x07\x00\x00\x00' + b'\x0d\x00\x00\x00\x00\x00\x00\x00' self.assertEqual(header.SIZE, len(buf)) self.assertEqual(net.CustomStruct.pack(header), buf) h2, offs = net.CustomStruct(net.PackageHeader).unpack(buf) self.assertEqual(offs, len(buf)) self.assertEqual(h2.typee, header.typee) self.assertEqual(h2.size, header.size)
def _send_package(self, typee, buf): '''Sents a typed package content. Args: typee: Type of the package. buf: Byte array of the content. ''' buf = net.PackageHeader(typee, len(buf)).dump() + buf total_sent = 0 while buf and not self._stop_flag: curr_sent = self._connection.send(buf) if not curr_sent: raise _ProtocolError('cannot send') buf = buf[curr_sent:] total_sent += curr_sent if self._stop_flag: raise self._StopException()
def send_pkg(self, typee, buf): '''Send a buf with specifying package type. Args: typee: The package type. buf: The package content. ''' for i in range(10): try: self._sock.sendall( net.PackageHeader(typee, len(buf)).dump() + buf) return except socket.error as e: self._logger.warning('It seems that the server is dead.') try: self._sock.close() except Exception as _: pass time.sleep(10) self._init_sock() raise ProxyClientError('Cannot send message.')
def _send_type_pkg(self, typee, buf): ph = net.PackageHeader(typee, len(buf)) self._conn.sendall(ph.dump() + buf)
def _do_a_bad_test(self, c): c.sendall( net.PackageHeader(29958, 1).dump() + b'\x02\x00\x00\x00\x00\x00\x00\x0013')
def do_a_test_small(self, sz): buf = net.PackageHeader(net.PackageType.QUERY_MAX_ID, 10).dump() + \ b'\x02\x00\x00\x00\x00\x00\x00\x0013' self.conn.sendall(buf[:sz])
def do_a_bad_test(self): self.conn.sendall( net.PackageHeader(29958, 1).dump() + b'\x02\x00\x00\x00\x00\x00\x00\x0013')