MCPcopy
hub / github.com/OpenNHP/opennhp / msgToPacketRoutine

Method msgToPacketRoutine

nhp/core/device.go:157–265  ·  view source on GitHub ↗

Asynchronous multi-channel processing.

(id int)

Source from the content-addressed store, hash-verified

155
156// Asynchronous multi-channel processing.
157func (d *Device) msgToPacketRoutine(id int) {
158 defer d.wg.Done()
159 defer log.Info("msgToPacketRoutine %d: quit", id)
160
161 log.Info("msgToPacketRoutine %d: start", id)
162
163 for {
164 select {
165 case <-d.signals.stop:
166 return
167
168 case md, ok := <-d.msgToPacketQueue:
169 if !ok {
170 return
171 }
172 if md == nil {
173 log.Warning("msgToPacketRoutine %d: msgToPacketRoutine gets nil data", id)
174 continue
175 }
176
177 // message encryption workflow: raw message -> encryption -> raw packet -> connection.SendQueue
178 func() {
179 msgType := HeaderTypeToString(md.HeaderType)
180 var msgStr string
181 if md.Message != nil {
182 msgStr = string(md.Message)
183 }
184 log.Debug("msgToPacketRoutine %d: encrypting [%s] raw message: %s", id, msgType, msgStr)
185 log.Evaluate("msgToPacketRoutine %d: encrypting [%s] raw message: %s", id, msgType, msgStr)
186
187 var mad *MsgAssemblerData
188 var err error
189
190 // error handling
191 defer func() {
192 if err != nil {
193 mad.Error = err
194 mad.Destroy()
195
196 // inform preset channel with error
197 if mad.ResponseMsgCh != nil {
198 mad.ResponseMsgCh <- &PacketParserData{
199 Error: err,
200 }
201 }
202 if mad.encryptedPktCh != nil {
203 mad.encryptedPktCh <- mad
204 }
205 }
206 }()
207
208 // process keepalive separately
209 if md.HeaderType == NHP_KPL {
210 mad, _ = d.createKeepalivePacket(md)
211 // send out keepalive packet
212 mad.connData.ForwardOutboundPacket(mad.BasePacket)
213 return
214 }

Callers 1

StartMethod · 0.95

Calls 12

DestroyMethod · 0.95
createKeepalivePacketMethod · 0.95
setPeerPublicKeyMethod · 0.95
encryptBodyMethod · 0.95
IsTransactionRequestMethod · 0.95
AddLocalTransactionMethod · 0.95
HeaderTypeToStringFunction · 0.85
ForwardOutboundPacketMethod · 0.80
ErrorMethod · 0.65
CounterMethod · 0.65

Tested by

no test coverage detected