func (c *MemoryConnection) SendMessage(chID ChannelID, msg []byte) error {
	select {
	case <-c.closer.Done():
		return io.EOF
	default:
	}
	select {
	case c.sendCh <- memoryMessage{channelID: chID, message: msg}:
		c.logger.Debug("sent message", "chID", chID, "msg", msg)
		return nil
	case <-c.closer.Done():
		return io.EOF
	}
}
SendMessage implements Connection.
package p2p
import (
	"context"
	"errors"
	"fmt"
	"io"
	"net"
	"sync"
	"github.com/tendermint/tendermint/crypto"
	tmsync "github.com/tendermint/tendermint/internal/libs/sync"
	"github.com/tendermint/tendermint/libs/log"
	"github.com/tendermint/tendermint/types"
)
const (
	MemoryProtocol Protocol = "memory"
)
type MemoryNetwork struct {
	logger log.Logger
	mtx        sync.RWMutex
	transports map[types.NodeID]*MemoryTransport
	bufferSize int
}
func NewMemoryNetwork(logger log.Logger, bufferSize int) *MemoryNetwork {
	return &MemoryNetwork{
		bufferSize: bufferSize,
		logger:     logger,
		transports: map[types.NodeID]*MemoryTransport{},
	}
}
func (n *MemoryNetwork) CreateTransport(nodeID types.NodeID) *MemoryTransport {
	t := newMemoryTransport(n, nodeID)
	n.mtx.Lock()
	defer n.mtx.Unlock()
	if _, ok := n.transports[nodeID]; ok {
		panic(fmt.Sprintf("memory transport with node ID %q already exists", nodeID))
	}
	n.transports[nodeID] = t
	return t
}
func (n *MemoryNetwork) GetTransport(id types.NodeID) *MemoryTransport {
	n.mtx.RLock()
	defer n.mtx.RUnlock()
	return n.transports[id]
}
func (n *MemoryNetwork) RemoveTransport(id types.NodeID) {
	n.mtx.Lock()
	t, ok := n.transports[id]
	delete(n.transports, id)
	n.mtx.Unlock()
	if ok {
		if err := t.Close(); err != nil {
			n.logger.Error("failed to close memory transport", "id", id, "err", err)
		}
	}
}
func (n *MemoryNetwork) Size() int {
	return len(n.transports)
}
type MemoryTransport struct {
	logger     log.Logger
	network    *MemoryNetwork
	nodeID     types.NodeID
	bufferSize int
	acceptCh  chan *MemoryConnection
	closeCh   chan struct{}
	closeOnce sync.Once
}
func newMemoryTransport(network *MemoryNetwork, nodeID types.NodeID) *MemoryTransport {
	return &MemoryTransport{
		logger:     network.logger.With("local", nodeID),
		network:    network,
		nodeID:     nodeID,
		bufferSize: network.bufferSize,
		acceptCh:   make(chan *MemoryConnection),
		closeCh:    make(chan struct{}),
	}
}
func (t *MemoryTransport) String() string {
	return string(MemoryProtocol)
}
func (t *MemoryTransport) AddChannelDescriptors([]*ChannelDescriptor) {}
func (t *MemoryTransport) Protocols() []Protocol {
	return []Protocol{MemoryProtocol}
}
func (t *MemoryTransport) Endpoints() []Endpoint {
	select {
	case <-t.closeCh:
		return []Endpoint{}
	default:
		return []Endpoint{{
			Protocol: MemoryProtocol,
			Path:     string(t.nodeID),
			IP:   net.IPv4zero,
			Port: 0,
		}}
	}
}
func (t *MemoryTransport) Accept() (Connection, error) {
	select {
	case conn := <-t.acceptCh:
		t.logger.Info("accepted connection", "remote", conn.RemoteEndpoint().Path)
		return conn, nil
	case <-t.closeCh:
		return nil, io.EOF
	}
}
func (t *MemoryTransport) Dial(ctx context.Context, endpoint Endpoint) (Connection, error) {
	if endpoint.Protocol != MemoryProtocol {
		return nil, fmt.Errorf("invalid protocol %q", endpoint.Protocol)
	}
	if endpoint.Path == "" {
		return nil, errors.New("no path")
	}
	if err := endpoint.Validate(); err != nil {
		return nil, err
	}
	nodeID, err := types.NewNodeID(endpoint.Path)
	if err != nil {
		return nil, err
	}
	t.logger.Info("dialing peer", "remote", nodeID)
	peer := t.network.GetTransport(nodeID)
	if peer == nil {
		return nil, fmt.Errorf("unknown peer %q", nodeID)
	}
	inCh := make(chan memoryMessage, t.bufferSize)
	outCh := make(chan memoryMessage, t.bufferSize)
	closer := tmsync.NewCloser()
	outConn := newMemoryConnection(t.logger, t.nodeID, peer.nodeID, inCh, outCh, closer)
	inConn := newMemoryConnection(peer.logger, peer.nodeID, t.nodeID, outCh, inCh, closer)
	select {
	case peer.acceptCh <- inConn:
		return outConn, nil
	case <-peer.closeCh:
		return nil, io.EOF
	case <-ctx.Done():
		return nil, ctx.Err()
	}
}
func (t *MemoryTransport) Close() error {
	t.network.RemoveTransport(t.nodeID)
	t.closeOnce.Do(func() {
		close(t.closeCh)
		t.logger.Info("closed transport")
	})
	return nil
}
type MemoryConnection struct {
	logger   log.Logger
	localID  types.NodeID
	remoteID types.NodeID
	receiveCh <-chan memoryMessage
	sendCh    chan<- memoryMessage
	closer    *tmsync.Closer
}
type memoryMessage struct {
	channelID ChannelID
	message   []byte
	nodeInfo *types.NodeInfo
	pubKey   crypto.PubKey
}
func newMemoryConnection(
	logger log.Logger,
	localID types.NodeID,
	remoteID types.NodeID,
	receiveCh <-chan memoryMessage,
	sendCh chan<- memoryMessage,
	closer *tmsync.Closer,
) *MemoryConnection {
	return &MemoryConnection{
		logger:    logger.With("remote", remoteID),
		localID:   localID,
		remoteID:  remoteID,
		receiveCh: receiveCh,
		sendCh:    sendCh,
		closer:    closer,
	}
}
func (c *MemoryConnection) String() string {
	return c.RemoteEndpoint().String()
}
func (c *MemoryConnection) LocalEndpoint() Endpoint {
	return Endpoint{
		Protocol: MemoryProtocol,
		Path:     string(c.localID),
	}
}
func (c *MemoryConnection) RemoteEndpoint() Endpoint {
	return Endpoint{
		Protocol: MemoryProtocol,
		Path:     string(c.remoteID),
	}
}
func (c *MemoryConnection) Handshake(
	ctx context.Context,
	nodeInfo types.NodeInfo,
	privKey crypto.PrivKey,
) (types.NodeInfo, crypto.PubKey, error) {
	select {
	case c.sendCh <- memoryMessage{nodeInfo: &nodeInfo, pubKey: privKey.PubKey()}:
		c.logger.Debug("sent handshake", "nodeInfo", nodeInfo)
	case <-c.closer.Done():
		return types.NodeInfo{}, nil, io.EOF
	case <-ctx.Done():
		return types.NodeInfo{}, nil, ctx.Err()
	}
	select {
	case msg := <-c.receiveCh:
		if msg.nodeInfo == nil {
			return types.NodeInfo{}, nil, errors.New("no NodeInfo in handshake")
		}
		c.logger.Debug("received handshake", "peerInfo", msg.nodeInfo)
		return *msg.nodeInfo, msg.pubKey, nil
	case <-c.closer.Done():
		return types.NodeInfo{}, nil, io.EOF
	case <-ctx.Done():
		return types.NodeInfo{}, nil, ctx.Err()
	}
}
func (c *MemoryConnection) ReceiveMessage() (ChannelID, []byte, error) {
	select {
	case <-c.closer.Done():
		return 0, nil, io.EOF
	default:
	}
	select {
	case msg := <-c.receiveCh:
		c.logger.Debug("received message", "chID", msg.channelID, "msg", msg.message)
		return msg.channelID, msg.message, nil
	case <-c.closer.Done():
		return 0, nil, io.EOF
	}
}