From 5480c4011e89593e1f448ef0b4e354d26fe00292 Mon Sep 17 00:00:00 2001 From: Giovanni Harting <539@idlegandalf.com> Date: Tue, 12 Dec 2017 22:31:26 +0100 Subject: [PATCH] added multi msg reading --- main.go | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/main.go b/main.go index c6adf7f..6902675 100644 --- a/main.go +++ b/main.go @@ -64,28 +64,33 @@ func (daemon *LedDaemon) receive() { break } if length > 0 { - msgLen := binary.BigEndian.Uint32(message[0:4]) + log.Debugf("[%s] Read %d bytes", daemon.socket.RemoteAddr(), length) - log.Debugf("[%s] Read %d bytes, first protobuf is %d long", daemon.name, length, msgLen) + for i := 0; i < length; { + msgLen := int(binary.BigEndian.Uint32(message[i:i+4])) - backendMsg := &ledd.BackendWrapperMessage{} - err = proto.Unmarshal(message[4:msgLen+4], backendMsg) - if err != nil { - log.Warningf("[%s] Couldn't decode protobuf msg!", daemon.name) - continue - } + log.Debugf("[%s] Reading protobuf after %d (len=%d)", daemon.socket.RemoteAddr(), i, msgLen) - switch msg := backendMsg.Msg.(type) { - case *ledd.BackendWrapperMessage_MLedd: - daemon.name = msg.MLedd.Name - log.Infof("Connection with %s etablished; backend registered", msg.MLedd.Name) - case *ledd.BackendWrapperMessage_MSetChannel: - for c, v := range msg.MSetChannel.NewChannelValues { - if pwm, ok := pwmMap[c]; ok { - pwm.SetPercentage(float32(v) / RESOLUTION) - } else { - pwmMap[c] = pca9685.NewPwm(int(c)) - pwmMap[c].SetPercentage(float32(v) / RESOLUTION) + backendMsg := &ledd.BackendWrapperMessage{} + err = proto.Unmarshal(message[i+4:i+msgLen+4], backendMsg) + i += msgLen + 4 + if err != nil { + log.Warningf("[%s] Couldn't decode protobuf msg!", daemon.name) + continue + } + + switch msg := backendMsg.Msg.(type) { + case *ledd.BackendWrapperMessage_MLedd: + daemon.name = msg.MLedd.Name + log.Infof("Connection with %s etablished; backend registered", msg.MLedd.Name) + case *ledd.BackendWrapperMessage_MSetChannel: + for c, v := range msg.MSetChannel.NewChannelValues { + if pwm, ok := pwmMap[c]; ok { + pwm.SetPercentage(float32(v) / RESOLUTION) + } else { + pwmMap[c] = pca9685.NewPwm(int(c)) + pwmMap[c].SetPercentage(float32(v) / RESOLUTION) + } } } }