Commit 97cbe2b8 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 4c098a14
......@@ -1250,13 +1250,13 @@ func (nl *NodeLink) recvPktN() (*pktBuf, error) {
// next packet could be already prefetched in part by previous read
if nl.rxbufN.Len() > 0 {
δn, _ := nl.rxbufN.Read(data[:proto.PktHeaderLen])
δn, _ := nl.rxbufN.Read(data[:proto.PktHeaderLenN])
n += δn
}
// first read to read pkt header and hopefully rest of packet in 1 syscall
if n < proto.PktHeaderLen {
δn, err := io.ReadAtLeast(nl.peerLink, data[n:], proto.PktHeaderLen - n)
if n < proto.PktHeaderLenN {
δn, err := io.ReadAtLeast(nl.peerLink, data[n:], proto.PktHeaderLenN - n)
if err != nil {
return nil, err
}
......@@ -1266,10 +1266,10 @@ func (nl *NodeLink) recvPktN() (*pktBuf, error) {
pkth := pkt.HeaderN()
msgLen := packed.Ntoh32(pkth.MsgLen)
if msgLen > proto.PktMaxSize - proto.PktHeaderLen {
if msgLen > proto.PktMaxSize - proto.PktHeaderLenN {
return nil, ErrPktTooBig
}
pktLen := int(proto.PktHeaderLen + msgLen) // whole packet length
pktLen := int(proto.PktHeaderLenN + msgLen) // whole packet length
// resize data if we don't have enough room in it
data = xbytes.Resize(data, pktLen)
......@@ -1419,7 +1419,7 @@ func pktDecodeHead(e proto.Encoding, pkt *pktBuf) (connID uint32, msgCode uint16
func pktEncodeN(connId uint32, msg proto.Msg) *pktBuf {
l := encN.NEOMsgEncodedLen(msg)
buf := pktAlloc(proto.PktHeaderLen + l)
buf := pktAlloc(proto.PktHeaderLenN + l)
h := buf.HeaderN()
h.ConnId = packed.Hton32(connId)
......@@ -1431,7 +1431,7 @@ func pktEncodeN(connId uint32, msg proto.Msg) *pktBuf {
}
func pktDecodeHeadN(pkt *pktBuf) (connID uint32, msgCode uint16, payload []byte, err error) {
if len(pkt.data) < proto.PktHeaderLen {
if len(pkt.data) < proto.PktHeaderLenN {
return 0, 0, nil, fmt.Errorf("packet too short") // XXX -> ErrTooShort?
}
pkth := pkt.HeaderN()
......
......@@ -136,7 +136,7 @@ func xconnError(err error) error {
func _mkpkt(enc proto.Encoding, connid uint32, msgcode uint16, payload []byte) *pktBuf {
switch enc {
case 'N':
pkt := &pktBuf{make([]byte, proto.PktHeaderLen+len(payload))}
pkt := &pktBuf{make([]byte, proto.PktHeaderLenN+len(payload))}
h := pkt.HeaderN()
h.ConnId = packed.Hton32(connid)
h.MsgCode = packed.Hton16(msgcode)
......
......@@ -39,15 +39,15 @@ type pktBuf struct {
}
// HeaderN returns pointer to packet header in 'N'-encoding.
func (pkt *pktBuf) HeaderN() *proto.PktHeader {
// NOTE no need to check len(.data) < PktHeader:
// .data is always allocated with cap >= PktHeaderLen.
return (*proto.PktHeader)(unsafe.Pointer(&pkt.data[0]))
func (pkt *pktBuf) HeaderN() *proto.PktHeaderN {
// NOTE no need to check len(.data) < PktHeaderN:
// .data is always allocated with cap >= PktHeaderLenN.
return (*proto.PktHeaderN)(unsafe.Pointer(&pkt.data[0]))
}
// PayloadN returns []byte representing packet payload in 'N'-encoding.
func (pkt *pktBuf) PayloadN() []byte {
return pkt.data[proto.PktHeaderLen:]
return pkt.data[proto.PktHeaderLenN:]
}
// ---- pktBuf freelist ----
......@@ -59,11 +59,11 @@ var pktBufPool = sync.Pool{New: func() interface{} {
// pktAlloc allocates pktBuf with len=n.
func pktAlloc(n int) *pktBuf {
// make sure cap >= PktHeaderLen.
// make sure cap >= PktHeaderLenN.
// see HeaderN for why
l := n
if l < proto.PktHeaderLen {
l = proto.PktHeaderLen
if l < proto.PktHeaderLenN {
l = proto.PktHeaderLenN
}
pkt := pktBufPool.Get().(*pktBuf)
pkt.data = xbytes.Realloc(pkt.data, l)[:n]
......@@ -114,7 +114,7 @@ func pktString(e proto.Encoding, pkt *pktBuf) string {
// Dump dumps a packet in raw form.
func (pkt *pktBuf) Dump() string {
// XXX encN-specific
if len(pkt.data) < proto.PktHeaderLen {
if len(pkt.data) < proto.PktHeaderLenN {
return fmt.Sprintf("(! < pktHeaderLen) % x", pkt.data)
}
......
......@@ -24,7 +24,7 @@
// ID of subconnection multiplexed on top of the underlying link, carried
// message code and message data.
//
// PktHeader describes packet header structure.
// PktHeaderN describes packet header structure. XXX + msgpack
//
// Messages are represented by corresponding types that all implement Msg interface.
//
......@@ -83,9 +83,8 @@ const (
// the high order byte 0 is different from TLS Handshake (0x16).
Version = 6
// length of packet header
// XXX encN-specific ?
PktHeaderLen = 10 // = unsafe.Sizeof(PktHeader{}), but latter gives typed constant (uintptr)
// length of packet header in 'N'-encoding
PktHeaderLenN = 10 // = unsafe.Sizeof(PktHeaderN{}), but latter gives typed constant (uintptr)
// packets larger than PktMaxSize are not allowed.
// this helps to avoid out-of-memory error on packets with corrupt message len.
......@@ -100,13 +99,12 @@ const (
INVALID_OID zodb.Oid = 1<<64 - 1
)
// XXX encN-specific ?
// PktHeader represents header of a raw packet.
// PktHeaderN represents header of a raw packet in 'N'-encoding.
//
// A packet contains connection ID and message.
//
//neo:proto typeonly
type PktHeader struct {
type PktHeaderN struct {
ConnId packed.BE32 // NOTE is .msgid in py
MsgCode packed.BE16 // payload message code
MsgLen packed.BE32 // payload message length (excluding packet header)
......
......@@ -68,13 +68,13 @@ func u64(v uint64) string {
return string(b[:])
}
func TestPktHeader(t *testing.T) {
// make sure PktHeader is really packed and its size matches PktHeaderLen
if unsafe.Sizeof(PktHeader{}) != 10 {
t.Fatalf("sizeof(PktHeader) = %v ; want 10", unsafe.Sizeof(PktHeader{}))
func TestPktHeaderN(t *testing.T) {
// make sure PktHeaderN is really packed and its size matches PktHeaderLenN
if unsafe.Sizeof(PktHeaderN{}) != 10 {
t.Fatalf("sizeof(PktHeaderN) = %v ; want 10", unsafe.Sizeof(PktHeaderN{}))
}
if unsafe.Sizeof(PktHeader{}) != PktHeaderLen {
t.Fatalf("sizeof(PktHeader) = %v ; want %v", unsafe.Sizeof(PktHeader{}), PktHeaderLen)
if unsafe.Sizeof(PktHeaderN{}) != PktHeaderLenN {
t.Fatalf("sizeof(PktHeaderN) = %v ; want %v", unsafe.Sizeof(PktHeaderN{}), PktHeaderLenN)
}
}
......
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