added multi msg reading

This commit is contained in:
2017-12-12 22:31:26 +01:00
parent 14566eb2a6
commit 5480c4011e

43
main.go
View File

@@ -64,28 +64,33 @@ func (daemon *LedDaemon) receive() {
break break
} }
if length > 0 { 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{} log.Debugf("[%s] Reading protobuf after %d (len=%d)", daemon.socket.RemoteAddr(), i, msgLen)
err = proto.Unmarshal(message[4:msgLen+4], backendMsg)
if err != nil {
log.Warningf("[%s] Couldn't decode protobuf msg!", daemon.name)
continue
}
switch msg := backendMsg.Msg.(type) { backendMsg := &ledd.BackendWrapperMessage{}
case *ledd.BackendWrapperMessage_MLedd: err = proto.Unmarshal(message[i+4:i+msgLen+4], backendMsg)
daemon.name = msg.MLedd.Name i += msgLen + 4
log.Infof("Connection with %s etablished; backend registered", msg.MLedd.Name) if err != nil {
case *ledd.BackendWrapperMessage_MSetChannel: log.Warningf("[%s] Couldn't decode protobuf msg!", daemon.name)
for c, v := range msg.MSetChannel.NewChannelValues { continue
if pwm, ok := pwmMap[c]; ok { }
pwm.SetPercentage(float32(v) / RESOLUTION)
} else { switch msg := backendMsg.Msg.(type) {
pwmMap[c] = pca9685.NewPwm(int(c)) case *ledd.BackendWrapperMessage_MLedd:
pwmMap[c].SetPercentage(float32(v) / RESOLUTION) 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)
}
} }
} }
} }