Skip to content

Instantly share code, notes, and snippets.

@travisperson
Last active November 18, 2015 09:12
Show Gist options
  • Select an option

  • Save travisperson/8683bb586e0d44239b4d to your computer and use it in GitHub Desktop.

Select an option

Save travisperson/8683bb586e0d44239b4d to your computer and use it in GitHub Desktop.
Double length deliminated

In protocol.go the Propose protobuf is writen using a function called writeMsgCtx. This method accepts a w msgio.Writer

func writeMsgCtx(ctx context.Context, w msgio.Writer, msg proto.Message) ([]byte, error)

The msgio.Writer passed into writeMsgCtx is the s.insecureM writer which is set in the newSecureSession function. s.insecureM is created from a insecure io.ReadWriteCloser. This however, is already a msgio ReadWrite.

func (sg *SessionGenerator) NewSession(ctx context.Context, insecure io.ReadWriteCloser) (Session, error)
    ...
    return newSecureSession(ctx, sg.LocalID, sg.PrivateKey, insecure)
    ...
func newSecureConn(ctx context.Context, sk ic.PrivKey, insecure Conn) (Conn, error)
  ...
  secure, err := sessgen.NewSession(ctx, insecure)
  ...
  conn := &secureConn{
	  nsecure: insecure,
	  secure:   secure,
	}
  ...
func (l *listener) Accept() (net.Conn, error)
  ...
  c, err := newSingleConn(ctx, l.local, "", maconn)
  ...
  sc, err := newSecureConn(ctx, l.privk, c)
  ...

The insecure Conn object passed into newSecureConn is created by newSingleConn has a private variables msgrw.

type singleConn struct {
	local  peer.ID
	remote peer.ID
	maconn manet.Conn
	msgrw  msgio.ReadWriteCloser
	event  io.Closer
}

The singleConn type implements a ReaderWriter interface. which uses the msgrw variable for its reading and writing object.

// Read reads data, net.Conn style
func (c *singleConn) Read(buf []byte) (int, error) {
	return c.msgrw.Read(buf)
}

// Write writes data, net.Conn style
func (c *singleConn) Write(buf []byte) (int, error) {
	return c.msgrw.Write(buf)
}

func (c *singleConn) NextMsgLen() (int, error) {
	return c.msgrw.NextMsgLen()
}

// ReadMsg reads data, net.Conn style
func (c *singleConn) ReadMsg() ([]byte, error) {
	return c.msgrw.ReadMsg()
}

// WriteMsg writes data, net.Conn style
func (c *singleConn) WriteMsg(buf []byte) error {
	return c.msgrw.WriteMsg(buf)
}

// ReleaseMsg releases a buffer
func (c *singleConn) ReleaseMsg(m []byte) {
	c.msgrw.ReleaseMsg(m)
}

https://github.com/ipfs/go-libp2p/blob/master/p2p/crypto/secio/protocol.go

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment