Asynchronous multi-channel processing.
(id int)
| 312 | |
| 313 | // Asynchronous multi-channel processing. |
| 314 | func (d *Device) packetToMsgRoutine(id int) { |
| 315 | defer d.wg.Done() |
| 316 | defer log.Info("packetToMsgRoutine %d: quit", id) |
| 317 | |
| 318 | log.Info("packetToMsgRoutine %d: start", id) |
| 319 | |
| 320 | for { |
| 321 | select { |
| 322 | case <-d.signals.stop: |
| 323 | return |
| 324 | |
| 325 | case pd, ok := <-d.packetToMsgQueue: |
| 326 | if !ok { |
| 327 | return |
| 328 | } |
| 329 | if pd == nil { |
| 330 | log.Warning("packetToMsgRoutine %d: packetToMsgQueue gets nil data", id) |
| 331 | continue |
| 332 | } |
| 333 | |
| 334 | // packet decryption workflow: connection.RecvQueue -> raw packet -> decryption -> raw message |
| 335 | func() { |
| 336 | msgType := HeaderTypeToString(pd.BasePacket.HeaderType) |
| 337 | log.Debug("packetToMsgRoutine %d: decrypting [%s] raw packet", id, msgType) |
| 338 | log.Evaluate("packetToMsgRoutine %d: decrypting [%s] raw packet", id, msgType) |
| 339 | |
| 340 | var ppd *PacketParserData |
| 341 | var err error |
| 342 | |
| 343 | // error handling |
| 344 | defer func() { |
| 345 | if err != nil { |
| 346 | ppd.Error = err |
| 347 | ppd.Destroy() |
| 348 | |
| 349 | // inform preset channel with error |
| 350 | if ppd.feedbackMsgCh != nil { |
| 351 | ppd.feedbackMsgCh <- ppd |
| 352 | } |
| 353 | if ppd.decryptedMsgCh != nil { |
| 354 | ppd.decryptedMsgCh <- ppd |
| 355 | } |
| 356 | } |
| 357 | }() |
| 358 | |
| 359 | ppd, err = d.createPacketParserData(pd) |
| 360 | if err != nil { |
| 361 | log.Debug("packetToMsgRoutine %d: [%s] packet precheck failed: %v", id, msgType, err) |
| 362 | log.Evaluate("packetToMsgRoutine %d: [%s] packet precheck failed: %v", id, msgType, err) |
| 363 | return |
| 364 | } |
| 365 | |
| 366 | err = ppd.validatePeer() |
| 367 | if err != nil { |
| 368 | log.Debug("packetToMsgRoutine %d: [%s] packet validation failed: %v", id, msgType, err) |
| 369 | log.Evaluate("packetToMsgRoutine %d: [%s] packet validation failed: %v", id, msgType, err) |
| 370 | return |
| 371 | } |
no test coverage detected