Commit 57a86cd5 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 547a6fa9
...@@ -53,11 +53,11 @@ import ( ...@@ -53,11 +53,11 @@ import (
// //
// It is safe to use NodeLink from multiple goroutines simultaneously. // It is safe to use NodeLink from multiple goroutines simultaneously.
type NodeLink struct { type NodeLink struct {
peerLink net.Conn // raw conn to peer peerLink net.Conn // raw conn to peer
connMu sync.Mutex connMu sync.Mutex
connTab map[uint32]*Conn // connId -> Conn associated with connId connTab map[uint32]*Conn // connId -> Conn associated with connId
nextConnId uint32 // next connId to use for Conn initiated by us nextConnId uint32 // next connId to use for Conn initiated by us
serveWg sync.WaitGroup // for serve{Send,Recv} serveWg sync.WaitGroup // for serve{Send,Recv}
acceptq chan *Conn // queue of incoming connections for Accept acceptq chan *Conn // queue of incoming connections for Accept
...@@ -65,10 +65,10 @@ type NodeLink struct { ...@@ -65,10 +65,10 @@ type NodeLink struct {
txq chan txReq // tx requests from Conns go via here txq chan txReq // tx requests from Conns go via here
// (rx packets are routed to Conn.rxq) // (rx packets are routed to Conn.rxq)
down chan struct{} // ready when NodeLink is marked as no longer operational down chan struct{} // ready when NodeLink is marked as no longer operational
downOnce sync.Once // shutdown may be due to both Close and IO error downOnce sync.Once // shutdown may be due to both Close and IO error
downWg sync.WaitGroup // for activities at shutdown downWg sync.WaitGroup // for activities at shutdown
errClose error // error got from peerLink.Close errClose error // error got from peerLink.Close
errMu sync.Mutex errMu sync.Mutex
errRecv error // error got from recvPkt on shutdown errRecv error // error got from recvPkt on shutdown
...@@ -118,11 +118,11 @@ type ConnError struct { ...@@ -118,11 +118,11 @@ type ConnError struct {
// LinkRole is a role an end of NodeLink is intended to play // LinkRole is a role an end of NodeLink is intended to play
type LinkRole int type LinkRole int
const ( const (
LinkServer LinkRole = iota // link created as server LinkServer LinkRole = iota // link created as server
LinkClient // link created as client LinkClient // link created as client
// for testing: // for testing:
linkNoRecvSend LinkRole = 1<<16 // do not spawn serveRecv & serveSend linkNoRecvSend LinkRole = 1 << 16 // do not spawn serveRecv & serveSend
linkFlagsMask LinkRole = (1<<32 - 1) << 16 linkFlagsMask LinkRole = (1<<32 - 1) << 16
) )
...@@ -148,13 +148,13 @@ const ( ...@@ -148,13 +148,13 @@ const (
func newNodeLink(conn net.Conn, role LinkRole) *NodeLink { func newNodeLink(conn net.Conn, role LinkRole) *NodeLink {
var nextConnId uint32 var nextConnId uint32
var acceptq chan *Conn var acceptq chan *Conn
switch role&^linkFlagsMask { switch role &^ linkFlagsMask {
case LinkServer: case LinkServer:
nextConnId = 0 // all initiated by us connId will be even nextConnId = 0 // all initiated by us connId will be even
acceptq = make(chan *Conn) // accept queue; TODO use backlog acceptq = make(chan *Conn) // accept queue; TODO use backlog
case LinkClient: case LinkClient:
nextConnId = 1 // ----//---- odd nextConnId = 1 // ----//---- odd
acceptq = nil // not accepting incoming connections acceptq = nil // not accepting incoming connections
default: default:
panic("invalid conn role") panic("invalid conn role")
} }
...@@ -180,9 +180,9 @@ func newNodeLink(conn net.Conn, role LinkRole) *NodeLink { ...@@ -180,9 +180,9 @@ func newNodeLink(conn net.Conn, role LinkRole) *NodeLink {
func (nl *NodeLink) newConn(connId uint32) *Conn { func (nl *NodeLink) newConn(connId uint32) *Conn {
c := &Conn{nodeLink: nl, c := &Conn{nodeLink: nl,
connId: connId, connId: connId,
rxq: make(chan *PktBuf, 1), // NOTE non-blocking - see serveRecv rxq: make(chan *PktBuf, 1), // NOTE non-blocking - see serveRecv
txerr: make(chan error, 1), // NOTE non-blocking - see Conn.Send txerr: make(chan error, 1), // NOTE non-blocking - see Conn.Send
down: make(chan struct{}), down: make(chan struct{}),
} }
nl.connTab[connId] = c nl.connTab[connId] = c
return c return c
...@@ -229,7 +229,7 @@ func (nl *NodeLink) shutdown() { ...@@ -229,7 +229,7 @@ func (nl *NodeLink) shutdown() {
// connMu - else it will deadlock. // connMu - else it will deadlock.
conn.shutdown() conn.shutdown()
} }
nl.connTab = nil // clear + mark down nl.connTab = nil // clear + mark down
nl.connMu.Unlock() nl.connMu.Unlock()
}() }()
}) })
...@@ -535,7 +535,7 @@ func (nl *NodeLink) sendPkt(pkt *PktBuf) error { ...@@ -535,7 +535,7 @@ func (nl *NodeLink) sendPkt(pkt *PktBuf) error {
return err return err
} }
var ErrPktTooBig = errors.New("packet too big") var ErrPktTooBig = errors.New("packet too big")
// recvPkt receives raw packet from peer // recvPkt receives raw packet from peer
// rx error, if any, is returned as is and is analyzed in serveRecv // rx error, if any, is returned as is and is analyzed in serveRecv
...@@ -604,9 +604,9 @@ func Handshake(ctx context.Context, conn net.Conn, role LinkRole) (nl *NodeLink, ...@@ -604,9 +604,9 @@ func Handshake(ctx context.Context, conn net.Conn, role LinkRole) (nl *NodeLink,
// HandshakeError is returned when there is an error while performing handshake // HandshakeError is returned when there is an error while performing handshake
type HandshakeError struct { type HandshakeError struct {
// XXX just keep .Conn? (but .Conn can be closed) // XXX just keep .Conn? (but .Conn can be closed)
LocalAddr net.Addr LocalAddr net.Addr
RemoteAddr net.Addr RemoteAddr net.Addr
Err error Err error
} }
func (e *HandshakeError) Error() string { func (e *HandshakeError) Error() string {
...@@ -666,7 +666,7 @@ func handshake(ctx context.Context, conn net.Conn, version uint32) (err error) { ...@@ -666,7 +666,7 @@ func handshake(ctx context.Context, conn net.Conn, version uint32) (err error) {
for i := 0; i < 2; i++ { for i := 0; i < 2; i++ {
select { select {
case <-ctx.Done(): case <-ctx.Done():
conn.Close() // interrupt IO conn.Close() // interrupt IO
connClosed = true connClosed = true
return ctx.Err() return ctx.Err()
...@@ -776,12 +776,12 @@ func (c *Conn) Send(msg Msg) error { ...@@ -776,12 +776,12 @@ 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())
......
//go:generate stringer -output proto-str2.go -type ErrorCode,NodeType proto.go //go:generate stringer -output zproto-str.go -type ErrorCode,NodeType proto.go
package neo package neo
......
//go:generate sh -c "go run protogen.go >proto-marshal.go" //go:generate sh -c "go run protogen.go >zproto-marshal.go"
package neo package neo
// protocol definition // protocol definition
......
...@@ -180,6 +180,8 @@ func main() { ...@@ -180,6 +180,8 @@ func main() {
buf.emit(`// Code generated by protogen.go; DO NOT EDIT. buf.emit(`// Code generated by protogen.go; DO NOT EDIT.
package neo package neo
// protocol messages to/from wire marshalling.
import ( import (
"encoding/binary" "encoding/binary"
"reflect" "reflect"
......
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
package neo package neo
// protocol messages to/from wire marshalling.
import ( import (
"encoding/binary" "encoding/binary"
"reflect" "reflect"
......
// Code generated by "stringer -output proto-str2.go -type ErrorCode,NodeType proto.go"; DO NOT EDIT. // Code generated by "stringer -output zproto-str.go -type ErrorCode,NodeType proto.go"; DO NOT EDIT.
package neo package neo
......
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