Exemple #1
0
    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)
Exemple #2
0
    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)
Exemple #3
0
    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
Exemple #4
0
    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
Exemple #5
0
    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
Exemple #6
0
    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
Exemple #7
0
 def pack(self, dataType, value):
     """pack doc..."""
     return struct.pack(StringUtils.unicodeToStr(self.endianess + dataType),
                        value)
Exemple #8
0
 def writeInt8(self, value):
     """ Read a signed 8bit integer."""
     self.writeByte(struct.pack(StringUtils.unicodeToStr('b'), int(value)))
Exemple #9
0
 def writeString(self, value):
     self.writeBytes(bytes(StringUtils.unicodeToStr(value)))
Exemple #10
0
 def writeNullByte(self):
     self.writeByte(struct.pack(StringUtils.unicodeToStr('x')))
Exemple #11
0
 def pack(self, dataType, value):
     """pack doc..."""
     return struct.pack(StringUtils.unicodeToStr(self.endianess + dataType), value)
Exemple #12
0
 def writeInt8(self, value):
     """ Read a signed 8bit integer."""
     self.writeByte(struct.pack(StringUtils.unicodeToStr('b'), int(value)))
Exemple #13
0
 def writeString(self, value):
     self.writeBytes(bytes(StringUtils.unicodeToStr(value)))
Exemple #14
0
 def writeNullByte(self):
     self.writeByte(struct.pack(StringUtils.unicodeToStr('x')))