Commit 69d5916b authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent daff41d2
......@@ -784,7 +784,7 @@ func (c *Conn) Recv() (Msg, error) {
// TODO use free-list for decoded messages + when possible decode in-place
msg := reflect.New(msgType).Interface().(Msg)
_, err = msg.NEOMsgDecode(pkt.Payload())
_, err = msg.neoMsgDecode(pkt.Payload())
if err != nil {
return nil, &ConnError{Conn: c, Op: "decode", Err: err}
}
......@@ -798,15 +798,15 @@ func (c *Conn) Recv() (Msg, error) {
func (c *Conn) Send(msg Msg) error {
traceConnSendPre(c, msg)
l := msg.NEOMsgEncodedLen()
l := msg.neoMsgEncodedLen()
buf := PktBuf{make([]byte, PktHeadLen+l)} // TODO -> freelist
h := buf.Header()
// h.ConnId will be set by conn.Send
h.MsgCode = hton16(msg.NEOMsgCode())
h.MsgCode = hton16(msg.neoMsgCode())
h.MsgLen = hton32(uint32(l)) // XXX casting: think again
msg.NEOMsgEncode(buf.Payload())
msg.neoMsgEncode(buf.Payload())
// XXX why pointer?
// XXX more context in err? (msg type)
......@@ -832,8 +832,8 @@ func (c *Conn) Expect(msgv ...Msg) (which int, err error) {
msgCode := ntoh16(pkth.MsgCode)
for i, msg := range msgv {
if msg.NEOMsgCode() == msgCode {
_, err = msg.NEOMsgDecode(pkt.Payload())
if msg.neoMsgCode() == msgCode {
_, err = msg.neoMsgDecode(pkt.Payload())
if err != nil {
return -1, &ConnError{Conn: c, Op: "decode", Err: err}
}
......
......@@ -138,24 +138,24 @@ type NodeUUID int32
// TODO NodeType -> base NodeUUID
// ErrDecodeOverflow is the error returned by NEOMsgDecode when decoding hit buffer overflow
// ErrDecodeOverflow is the error returned by neoMsgDecode when decoding hit buffer overflow
var ErrDecodeOverflow = errors.New("decode: bufer overflow")
// Msg is the interface implemented by NEO messages to marshal/unmarshal them into/from wire format
type Msg interface {
// NEOMsgCode returns message code needed to be used for particular message type
// neoMsgCode returns message code needed to be used for particular message type
// on the wire
NEOMsgCode() uint16
neoMsgCode() uint16
// NEOMsgEncodedLen returns how much space is needed to encode current message payload
NEOMsgEncodedLen() int
// neoMsgEncodedLen returns how much space is needed to encode current message payload
neoMsgEncodedLen() int
// NEOMsgEncode encodes current message state into buf.
// len(buf) must be >= NEOMsgEncodedLen()
NEOMsgEncode(buf []byte)
// neoMsgEncode encodes current message state into buf.
// len(buf) must be >= neoMsgEncodedLen()
neoMsgEncode(buf []byte)
// NEOMsgDecode decodes data into message in-place.
NEOMsgDecode(data []byte) (nread int, err error)
// neoMsgDecode decodes data into message in-place.
neoMsgDecode(data []byte) (nread int, err error)
}
......@@ -165,7 +165,7 @@ type Address struct {
}
// NOTE if Host == "" -> Port not added to wire (see py.PAddress):
// func (a *Address) NEOMsgEncode(b []byte) int {
// func (a *Address) neoMsgEncode(b []byte) int {
// n := string_NEOEncode(a.Host, b[0:])
// if a.Host != "" {
// BigEndian.PutUint16(b[n:], a.Port)
......
......@@ -82,8 +82,8 @@ func testMsgMarshal(t *testing.T, msg Msg, encoded string) {
}()
// msg.encode() == expected
msgCode := msg.NEOMsgCode()
n := msg.NEOMsgEncodedLen()
msgCode := msg.neoMsgCode()
n := msg.neoMsgEncodedLen()
msgType := msgTypeRegistry[msgCode]
if msgType != typ {
t.Errorf("%v: msgCode = %v which corresponds to %v", typ, msgCode, msgType)
......@@ -93,7 +93,7 @@ func testMsgMarshal(t *testing.T, msg Msg, encoded string) {
}
buf := make([]byte, n)
msg.NEOMsgEncode(buf)
msg.neoMsgEncode(buf)
if string(buf) != encoded {
t.Errorf("%v: encode result unexpected:", typ)
t.Errorf("\thave: %s", hexpkg.EncodeToString(buf))
......@@ -123,13 +123,13 @@ func testMsgMarshal(t *testing.T, msg Msg, encoded string) {
}
}()
msg.NEOMsgEncode(buf[:l])
msg.neoMsgEncode(buf[:l])
}()
}
// msg.decode() == expected
data := []byte(encoded + "noise")
n, err := msg2.NEOMsgDecode(data)
n, err := msg2.neoMsgDecode(data)
if err != nil {
t.Errorf("%v: decode error %v", typ, err)
}
......@@ -143,7 +143,7 @@ func testMsgMarshal(t *testing.T, msg Msg, encoded string) {
// decode must detect buffer overflow
for l := len(encoded)-1; l >= 0; l-- {
n, err = msg2.NEOMsgDecode(data[:l])
n, err = msg2.neoMsgDecode(data[:l])
if !(n==0 && err==ErrDecodeOverflow) {
t.Errorf("%v: decode overflow not detected on [:%v]", typ, l)
}
......@@ -271,11 +271,11 @@ func TestMsgMarshalAllOverflowLightly(t *testing.T) {
for _, typ := range msgTypeRegistry {
// zero-value for a type
msg := reflect.New(typ).Interface().(Msg)
l := msg.NEOMsgEncodedLen()
l := msg.neoMsgEncodedLen()
zerol := make([]byte, l)
// decoding will turn nil slice & map into empty allocated ones.
// we need it so that reflect.DeepEqual works for msg encode/decode comparison
n, err := msg.NEOMsgDecode(zerol)
n, err := msg.neoMsgDecode(zerol)
if !(n == l && err == nil) {
t.Errorf("%v: zero-decode unexpected: %v, %v ; want %v, nil", typ, n, err, l)
}
......
......@@ -25,10 +25,10 @@ NEO. Protocol module. Code generator
This program generates marshalling code for message types defined in proto.go .
For every type 4 methods are generated in accordance with neo.Msg interface:
NEOMsgCode() uint16
NEOMsgEncodedLen() int
NEOMsgEncode(buf []byte)
NEOMsgDecode(data []byte) (nread int, err error)
neoMsgCode() uint16
neoMsgEncodedLen() int
neoMsgEncode(buf []byte)
neoMsgDecode(data []byte) (nread int, err error)
List of message types is obtained via searching through proto.go AST - looking
for appropriate struct declarations there.
......@@ -218,7 +218,7 @@ import (
case *ast.StructType:
fmt.Fprintf(&buf, "// %d. %s\n\n", msgCode, typename)
buf.emit("func (*%s) NEOMsgCode() uint16 {", typename)
buf.emit("func (*%s) neoMsgCode() uint16 {", typename)
buf.emit("return %d", msgCode)
buf.emit("}\n")
......@@ -491,7 +491,7 @@ type sizer struct {
//
// when type is recursively walked, for every case code to update `data[n:]` is generated.
// no overflow checks are generated as by neo.Msg interface provided data
// buffer should have at least payloadLen length returned by NEOMsgEncodedInfo()
// buffer should have at least payloadLen length returned by neoMsgEncodedInfo()
// (the size computed by sizer).
//
// the code emitted looks like:
......@@ -500,7 +500,7 @@ type sizer struct {
// encode<typ2>(data[n2:], path2)
// ...
//
// TODO encode have to care in NEOMsgEncode to emit preambule such that bound
// TODO encode have to care in neoMsgEncode to emit preambule such that bound
// checking is performed only once (currenty compiler emits many of them)
type encoder struct {
commonCodeGen
......@@ -548,7 +548,7 @@ var _ CodeGenerator = (*decoder)(nil)
func (s *sizer) generatedCode() string {
code := Buffer{}
// prologue
code.emit("func (%s *%s) NEOMsgEncodedLen() int {", s.recvName, s.typeName)
code.emit("func (%s *%s) neoMsgEncodedLen() int {", s.recvName, s.typeName)
if s.varUsed["size"] {
code.emit("var %s int", s.var_("size"))
}
......@@ -569,7 +569,7 @@ func (s *sizer) generatedCode() string {
func (e *encoder) generatedCode() string {
code := Buffer{}
// prologue
code.emit("func (%s *%s) NEOMsgEncode(data []byte) {", e.recvName, e.typeName)
code.emit("func (%s *%s) neoMsgEncode(data []byte) {", e.recvName, e.typeName)
code.Write(e.buf.Bytes())
......@@ -676,7 +676,7 @@ func (d *decoder) generatedCode() string {
code := Buffer{}
// prologue
code.emit("func (%s *%s) NEOMsgDecode(data []byte) (int, error) {", d.recvName, d.typeName)
code.emit("func (%s *%s) neoMsgDecode(data []byte) (int, error) {", d.recvName, d.typeName)
if d.varUsed["nread"] {
code.emit("var %v uint32", d.var_("nread"))
}
......
This diff is collapsed.
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