Commit 1f89aa94 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 5f517fdc
...@@ -22,6 +22,7 @@ package neo ...@@ -22,6 +22,7 @@ package neo
import ( import (
"fmt" "fmt"
"reflect"
"unsafe" "unsafe"
) )
...@@ -54,8 +55,7 @@ func (pkt *PktBuf) Payload() []byte { ...@@ -54,8 +55,7 @@ func (pkt *PktBuf) Payload() []byte {
} }
// Strings dumps a packet // Strings dumps a packet in human-readable form
// XXX -> use .Dump() for full dump?
func (pkt *PktBuf) String() string { func (pkt *PktBuf) String() string {
if len(pkt.Data) < PktHeadLen { if len(pkt.Data) < PktHeadLen {
return fmt.Sprintf("(! < PktHeadLen) % x", pkt.Data) return fmt.Sprintf("(! < PktHeadLen) % x", pkt.Data)
...@@ -65,15 +65,39 @@ func (pkt *PktBuf) String() string { ...@@ -65,15 +65,39 @@ func (pkt *PktBuf) String() string {
s := fmt.Sprintf(".%d", ntoh32(h.ConnId)) s := fmt.Sprintf(".%d", ntoh32(h.ConnId))
msgCode := ntoh16(h.MsgCode) msgCode := ntoh16(h.MsgCode)
msgLen := ntoh32(h.MsgLen)
data := pkt.Payload()
msgType := msgTypeRegistry[msgCode] msgType := msgTypeRegistry[msgCode]
if msgType == nil { if msgType == nil {
s += fmt.Sprintf(" ? (%d)", msgCode) s += fmt.Sprintf(" ? (%d) #%d [%d]: % x", msgCode, msgLen, len(data), data)
} else { return s
s += fmt.Sprintf(" %s", msgType) }
// XXX dup wrt Conn.Recv
msg := reflect.New(msgType).Interface().(Msg)
n, err := msg.neoMsgDecode(data)
if err != nil {
s += fmt.Sprintf(" (%s) %v; #%d [%d]: % x", msgType, err, msgLen, len(data), data)
} }
s += fmt.Sprintf(" #%d | ", ntoh32(h.MsgLen)) s += fmt.Sprintf(" %s %+v", msgType.Name(), msg)
if n < len(data) {
tail := data[n:]
s += fmt.Sprintf(" ; [%d]tail: % x", len(tail), tail)
}
s += fmt.Sprintf("% x", pkt.Payload()) // XXX better decode
return s return s
} }
// Dump dumps a packet in raw form
func (pkt *PktBuf) Dump() string {
if len(pkt.Data) < PktHeadLen {
return fmt.Sprintf("(! < PktHeadLen) % x", pkt.Data)
}
h := pkt.Header()
data := pkt.Payload()
return fmt.Sprintf(".%d (%d) #%d [%d]: % x",
ntoh32(h.ConnId), ntoh16(h.MsgCode), ntoh32(h.MsgLen), len(data), data)
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment