Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
N
neo
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Labels
Merge Requests
2
Merge Requests
2
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Jobs
Commits
Open sidebar
Kirill Smelkov
neo
Commits
97cbe2b8
Commit
97cbe2b8
authored
Jan 12, 2021
by
Kirill Smelkov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
.
parent
4c098a14
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
28 additions
and
30 deletions
+28
-30
go/neo/neonet/connection.go
go/neo/neonet/connection.go
+7
-7
go/neo/neonet/connection_test.go
go/neo/neonet/connection_test.go
+1
-1
go/neo/neonet/pkt.go
go/neo/neonet/pkt.go
+9
-9
go/neo/proto/proto.go
go/neo/proto/proto.go
+5
-7
go/neo/proto/proto_test.go
go/neo/proto/proto_test.go
+6
-6
No files found.
go/neo/neonet/connection.go
View file @
97cbe2b8
...
...
@@ -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
.
PktHeaderLen
N
])
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
.
PktHeaderLen
N
{
δn
,
err
:=
io
.
ReadAtLeast
(
nl
.
peerLink
,
data
[
n
:
],
proto
.
PktHeaderLen
N
-
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
.
PktHeaderLen
N
{
return
nil
,
ErrPktTooBig
}
pktLen
:=
int
(
proto
.
PktHeaderLen
+
msgLen
)
// whole packet length
pktLen
:=
int
(
proto
.
PktHeaderLen
N
+
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
.
PktHeaderLen
N
+
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
.
PktHeaderLen
N
{
return
0
,
0
,
nil
,
fmt
.
Errorf
(
"packet too short"
)
// XXX -> ErrTooShort?
}
pkth
:=
pkt
.
HeaderN
()
...
...
go/neo/neonet/connection_test.go
View file @
97cbe2b8
...
...
@@ -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
.
PktHeaderLen
N
+
len
(
payload
))}
h
:=
pkt
.
HeaderN
()
h
.
ConnId
=
packed
.
Hton32
(
connid
)
h
.
MsgCode
=
packed
.
Hton16
(
msgcode
)
...
...
go/neo/neonet/pkt.go
View file @
97cbe2b8
...
...
@@ -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
.
PktHeader
N
{
// NOTE no need to check len(.data) < PktHeader
N
:
// .data is always allocated with cap >= PktHeaderLen
N
.
return
(
*
proto
.
PktHeader
N
)(
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
.
PktHeaderLen
N
:
]
}
// ---- 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 >= PktHeaderLen
N
.
// see HeaderN for why
l
:=
n
if
l
<
proto
.
PktHeaderLen
{
l
=
proto
.
PktHeaderLen
if
l
<
proto
.
PktHeaderLen
N
{
l
=
proto
.
PktHeaderLen
N
}
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.PktHeaderLen
N
{
return fmt.Sprintf("(! < pktHeaderLen) % x", pkt.data)
}
...
...
go/neo/proto/proto.go
View file @
97cbe2b8
...
...
@@ -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.
// PktHeader
N 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
PktHeader
N
struct
{
ConnId
packed
.
BE32
// NOTE is .msgid in py
MsgCode
packed
.
BE16
// payload message code
MsgLen
packed
.
BE32
// payload message length (excluding packet header)
...
...
go/neo/proto/proto_test.go
View file @
97cbe2b8
...
...
@@ -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
TestPktHeader
N
(
t
*
testing
.
T
)
{
// make sure PktHeader
N is really packed and its size matches PktHeaderLenN
if
unsafe
.
Sizeof
(
PktHeader
N
{})
!=
10
{
t
.
Fatalf
(
"sizeof(PktHeader
N) = %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
(
PktHeader
N
{})
!=
PktHeaderLenN
{
t
.
Fatalf
(
"sizeof(PktHeader
N) = %v ; want %v"
,
unsafe
.
Sizeof
(
PktHeaderN
{}),
PktHeaderLenN
)
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment