def receiveInChunks(cls, socket, chunkSize=None, echo=False): if chunkSize is None: chunkSize = NimbleEnvironment.SOCKET_CHUNK_SIZE chunkSize *= 2 terminator = StringUtils.unicodeToStr( NimbleEnvironment.TERMINATION_IDENTIFIER) message = [] while True: try: result = socket.recv(chunkSize) if result.endswith(terminator): result = result[:-len(terminator)] message.append(result) break message.append(result) except Exception as err: if not message: print(err) raise if message[-1].endswith(terminator): message[-1] = message[-1][:-len(terminator)] break if not message: return None return StringUtils.unicodeToStr('').join(message)
def receiveInChunks(cls, socket, chunkSize =None, echo =False): if chunkSize is None: chunkSize = NimbleEnvironment.SOCKET_CHUNK_SIZE chunkSize *= 2 terminator = StringUtils.unicodeToStr(NimbleEnvironment.TERMINATION_IDENTIFIER) message = [] while True: try: result = socket.recv(chunkSize) if result.endswith(terminator): result = result[:-len(terminator)] message.append(result) break message.append(result) except Exception as err: if not message: print(err) raise if message[-1].endswith(terminator): message[-1] = message[-1][:-len(terminator)] break if not message: return None return StringUtils.unicodeToStr('').join(message)
def unpack(self, dataType, length): data = StringUtils.unicodeToStr(self.read(length)) assert len(data) == length, \ u"[UNPACK ERROR]: Unexpected end of stream [%s | %s]" % ( StringUtils.toUnicode(len(data)), StringUtils.toUnicode(length)) try: return struct.unpack(StringUtils.unicodeToStr(self.endianess + dataType), data)[0] except struct.error: print(len(data)) print(u"Unable to unpack '%r'" % data) raise
def unpack(self, dataType, length): data = StringUtils.unicodeToStr(self.read(length)) assert len(data) == length, \ u"[UNPACK ERROR]: Unexpected end of stream [%s | %s]" % ( StringUtils.toUnicode(len(data)), StringUtils.toUnicode(length)) try: return struct.unpack( StringUtils.unicodeToStr(self.endianess + dataType), data)[0] except struct.error: print(len(data)) print(u"Unable to unpack '%r'" % data) raise
def itemsToString(cls, target, inPlace =False, toUnicode =True): """ Iterates through the elements of the target list and converts each of them to binary strings, including decoding unicode strings to byte strings.""" from pyaid.string.StringUtils import StringUtils output = target if inPlace else (target + []) index = 0 while index < len(target): source = target[index] if StringUtils.isStringType(source): if toUnicode: output[index] = StringUtils.strToUnicode(source) else: output[index] = StringUtils.unicodeToStr(source, force=True) else: output[index] = str(source) index += 1 return output
def itemsToString(cls, target, inPlace=False, toUnicode=True): """ Iterates through the elements of the target list and converts each of them to binary strings, including decoding unicode strings to byte strings.""" from pyaid.string.StringUtils import StringUtils output = target if inPlace else (target + []) index = 0 while index < len(target): source = target[index] if StringUtils.isStringType(source): if toUnicode: output[index] = StringUtils.strToUnicode(source) else: output[index] = StringUtils.unicodeToStr(source, force=True) else: output[index] = str(source) index += 1 return output
def pack(self, dataType, value): """pack doc...""" return struct.pack(StringUtils.unicodeToStr(self.endianess + dataType), value)
def writeInt8(self, value): """ Read a signed 8bit integer.""" self.writeByte(struct.pack(StringUtils.unicodeToStr('b'), int(value)))
def writeString(self, value): self.writeBytes(bytes(StringUtils.unicodeToStr(value)))
def writeNullByte(self): self.writeByte(struct.pack(StringUtils.unicodeToStr('x')))