コード例 #1
0
    def onReadyRead(self):
        inputStream = QtCore.QDataStream(self.tcpSocket)

        while (1):
            if len(self.rxBuffer) < Messaging.hdrSize:
                if self.tcpSocket.bytesAvailable() < Messaging.hdrSize:
                    return
                self.rxBuffer += inputStream.readRawData(Messaging.hdrSize -
                                                         len(self.rxBuffer))

            if len(self.rxBuffer) >= Messaging.hdrSize:
                hdr = Messaging.hdr(self.rxBuffer)
                bodyLen = hdr.GetDataLength()
                if len(self.rxBuffer) + self.tcpSocket.bytesAvailable(
                ) < Messaging.hdrSize + bodyLen:
                    return

                self.rxBuffer += inputStream.readRawData(Messaging.hdrSize +
                                                         bodyLen -
                                                         len(self.rxBuffer))

                # create a new header object with the appended body
                hdr = Messaging.hdr(self.rxBuffer)

                # if we got this far, we have a whole message! So, emit the signal
                self.messagereceived.emit(hdr)

                # then clear the buffer, so we start over on the next message
                self.rxBuffer = bytearray()
コード例 #2
0
    def onReadyRead(self):
        inputStream = QtCore.QDataStream(self.tcpSocket)

        while(1):
            if len(self.rxBuffer) < Messaging.hdrSize:
                if self.tcpSocket.bytesAvailable() < Messaging.hdrSize:
                    return
                self.rxBuffer += inputStream.readRawData(Messaging.hdrSize - len(self.rxBuffer))

            if len(self.rxBuffer) >= Messaging.hdrSize:
                hdr = Messaging.hdr(self.rxBuffer)
                bodyLen = hdr.GetDataLength()
                if len(self.rxBuffer)+self.tcpSocket.bytesAvailable() < Messaging.hdrSize + bodyLen:
                    return

                self.rxBuffer += inputStream.readRawData(Messaging.hdrSize + bodyLen - len(self.rxBuffer))

                # create a new header object with the appended body
                hdr = Messaging.hdr(self.rxBuffer)

                # if we got this far, we have a whole message! So, emit the signal
                self.messagereceived.emit(hdr)

                # then clear the buffer, so we start over on the next message
                self.rxBuffer = bytearray()
コード例 #3
0
    def sendMsg(self, msgClass):
        msg = msgClass()
        if self.timeInfo:
            hdr = Messaging.hdr(msg.rawBuffer())
            t = self.currentTime
            maxTime = self.timeInfo.maxVal
            # if the timestamp field is small, assume the user wants relative times since midnight.
            if maxTime != "DBL_MAX" and (maxTime == 'FLT_MAX' or float(maxTime) <= 2**32):
                t = (datetime.fromtimestamp(self.currentTime) - datetime.fromtimestamp(self.currentTime).replace(hour=0, minute=0, second=0, microsecond=0)).total_seconds()
            if self.timeInfo.units == "ms":
                t = t * 1000.0
            if self.timeInfo.type == "int":
                t = int(t)
            hdr.SetTime(t)

        for fieldInfo in msgClass.fields:
            if fieldInfo.units == 'ASCII':
                Messaging.set(msg, fieldInfo, 's'+str(self.fieldValue(fieldInfo)))
            else:
                if(fieldInfo.count == 1):
                    Messaging.set(msg, fieldInfo, str(self.fieldValue(fieldInfo)))
                    for bitInfo in fieldInfo.bitfieldInfo:
                        Messaging.set(msg, bitInfo, str(self.fieldValue(bitInfo)))
                else:
                    for i in range(0,fieldInfo.count):
                        Messaging.set(msg, fieldInfo, self.fieldValue(fieldInfo), i)
        self.SendMsg(msg)
コード例 #4
0
ファイル: client.py プロジェクト: BoseCorp/MsgTools
    def recv(self, msgIds=[], timeout=None):
        # if user didn't pass a list, put the single param into a list
        if not isinstance(msgIds, list):
            msgIds = [msgIds]
        # if they passed classes, get the ID of each
        for i in range(0,len(msgIds)):
            if hasattr(msgIds[i], 'ID'):
                msgIds[i] = msgIds[i].ID
        if timeout != None and self._timeout != timeout:
            self._timeout = timeout
            if self._timeout == 0.0:
                self._sock.setblocking(0)
            else:
                self._sock.setblocking(1)
                self._sock.settimeout(self._timeout)
        while True:
            try:
                # see if there's enough for header
                data = self._sock.recv(Messaging.hdr.SIZE, socket.MSG_PEEK)
                if len(data) == Messaging.hdr.SIZE:
                    # create header based on peek'd data
                    hdr = Messaging.hdr(data)

                    # see if there's enough for the body, too
                    data += self._sock.recv(hdr.GetDataLength(), socket.MSG_PEEK)
                    if len(data) != Messaging.hdr.SIZE + hdr.GetDataLength():
                        print("didn't get whole body, error!")
                        continue

                    # read out what we peek'd.
                    data = self._sock.recv(Messaging.hdr.SIZE + hdr.GetDataLength())

                    # reset the header based on appended data
                    hdr = Messaging.hdr(data)
                    id = hdr.GetMessageID()
                    if id in self._extra_msgs_to_record:
                        self.received[id] = msg
                    if len(msgIds) == 0 or id in msgIds:
                        msg = Messaging.MsgFactory(hdr)
                        self.received[id] = msg
                        return msg
            except socket.timeout:
                return None
            except BlockingIOError:
                return None
コード例 #5
0
 def recv(self, msgIds=[], timeout=None):
     # if user didn't pass a list, put the single param into a list
     if not isinstance(msgIds, list):
         msgIds = [msgIds]
     # if they passed classes, get the ID of each
     for i in range(0,len(msgIds)):
         if hasattr(msgIds[i], 'ID'):
             msgIds[i] = msgIds[i].ID
     if timeout != None and self.timeout != timeout:
         self.timeout = timeout
     while True:
         try:
             data = self.synchronous_rx_queue.get(True, self.timeout)
             hdr = Messaging.hdr(data)
             id = hdr.GetMessageID()
             if len(msgIds) == 0 or id in msgIds:
                 msg = Messaging.MsgFactory(hdr)
                 return msg
         except queue.Empty:
             return None
コード例 #6
0
ファイル: noisemaker.py プロジェクト: jkominek/MsgTools
    def sendMsg(self, msgClass):
        msg = msgClass()
        try:
            hdr = Messaging.hdr(msg.rawBuffer())
            hdr.SetTime(self.currentTime)
        except AttributeError:
            pass
        for fieldInfo in msgClass.fields:

            if fieldInfo.units == 'ASCII':
                Messaging.set(msg, fieldInfo,
                              's' + str(self.fieldValue(fieldInfo)))
            else:
                if (fieldInfo.count == 1):
                    Messaging.set(msg, fieldInfo,
                                  str(self.fieldValue(fieldInfo)))
                    for bitInfo in fieldInfo.bitfieldInfo:
                        Messaging.set(msg, bitInfo,
                                      str(self.fieldValue(bitInfo)))
                else:
                    for i in range(0, fieldInfo.count):
                        Messaging.set(msg, fieldInfo,
                                      self.fieldValue(fieldInfo), i)
        self.SendMsg(msg)
コード例 #7
0
ファイル: WebSocketServer.py プロジェクト: BoseCorp/MsgTools
 def processBinaryMessage(self, bytes):
     hdr = Messaging.hdr(bytes.data())
     self.messagereceived.emit(hdr)