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

.

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