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

.

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