RichΛrd 40359f9c1b
go-waku integration (#2247)
* Adding wakunode module
* Adding wakuv2 fleet files
* Add waku fleets to update-fleet-config script
* Adding config items for waku v2
* Conditionally start waku v2 node depending on config
* Adapting common code to use go-waku
* Setting log level to info
* update dependencies
* update fleet config to use WakuNodes instead of BootNodes
* send and receive messages
* use hash returned when publishing a message
* add waku store protocol
* trigger signal after receiving store messages
* exclude linting rule SA1019 to check deprecated packages
2021-06-16 16:19:45 -04:00

52 lines
1.3 KiB
Go

package multistream
import (
"io"
"sync"
)
// lazyServerConn is an io.ReadWriteCloser adapter used for negotiating inbound
// streams (see NegotiateLazy).
//
// This is "lazy" because it doesn't wait for the write half to succeed before
// allowing us to read from the stream.
type lazyServerConn struct {
waitForHandshake sync.Once
werr error
con io.ReadWriteCloser
}
func (l *lazyServerConn) Write(b []byte) (int, error) {
l.waitForHandshake.Do(func() { panic("didn't initiate handshake") })
if l.werr != nil {
return 0, l.werr
}
return l.con.Write(b)
}
func (l *lazyServerConn) Read(b []byte) (int, error) {
if len(b) == 0 {
return 0, nil
}
return l.con.Read(b)
}
func (l *lazyServerConn) Close() error {
// As the server, we MUST flush the handshake on close. Otherwise, if
// the other side is actually waiting for our close (i.e., reading until
// EOF), they may get an error even though we received the request.
//
// However, we MUST NOT return any errors from Flush. The initiator may
// have already closed their side for reading. Basically, _we_ don't
// care about the outcome of this flush, only the other side does.
_ = l.Flush()
return l.con.Close()
}
// Flush sends the handshake.
func (l *lazyServerConn) Flush() error {
l.waitForHandshake.Do(func() { panic("didn't initiate handshake") })
return l.werr
}