MCPcopy
hub / github.com/tinode/chat / dispatchRaw

Method dispatchRaw

server/session.go:431–463  ·  view source on GitHub ↗

Message received, convert bytes to ClientComMessage and dispatch

(raw []byte)

Source from the content-addressed store, hash-verified

429
430// Message received, convert bytes to ClientComMessage and dispatch
431func (s *Session) dispatchRaw(raw []byte) {
432 now := types.TimeNow()
433 var msg ClientComMessage
434
435 if atomic.LoadInt32(&s.terminating) > 0 {
436 logs.Warn.Println("s.dispatch: message received on a terminating session", s.sid)
437 s.queueOut(ErrLocked("", "", now))
438 return
439 }
440
441 if len(raw) == 1 && raw[0] == 0x31 {
442 // 0x31 == '1'. This is a network probe message. Respond with a '0':
443 s.queueOutBytes([]byte{0x30})
444 return
445 }
446
447 toLog := raw
448 truncated := ""
449 if len(raw) > 512 {
450 toLog = raw[:512]
451 truncated = "<...>"
452 }
453 logs.Info.Printf("in: '%s%s' sid='%s' uid='%s'", toLog, truncated, s.sid, s.uid)
454
455 if err := json.Unmarshal(raw, &msg); err != nil {
456 // Malformed message
457 logs.Warn.Println("s.dispatch", err, s.sid)
458 s.queueOut(ErrMalformed("", "", now))
459 return
460 }
461
462 s.dispatch(&msg)
463}
464
465func (s *Session) dispatch(msg *ClientComMessage) {
466 now := types.TimeNow()

Callers 2

readLoopMethod · 0.95
readOnceMethod · 0.95

Calls 7

queueOutMethod · 0.95
queueOutBytesMethod · 0.95
dispatchMethod · 0.95
TimeNowFunction · 0.92
ErrLockedFunction · 0.85
ErrMalformedFunction · 0.85
PrintlnMethod · 0.80

Tested by

no test coverage detected