connection

package
Version: v0.2.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 3, 2021 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// TODO: meaning of flags should be based on conn Type
	WaitRecvBuf   uint32 = 1 << iota // The recevier should wait receive buffer
	IsResponse    uint32 = 1 << iota // This is a response message for ConnPeerToPeer
	RequestFailed uint32 = 1 << iota // This is a response meesage for failed request
)
View Source
const NoFlag uint32 = 0

Variables

View Source
var (
	GetBuf = defaultPool.GetBuf
	PutBuf = defaultPool.PutBuf
)
View Source
var (
	ErrInvalidConnectionType = errors.New("invalid connection type")
)

Functions

func New

func New(remote, local plan.PeerID, t ConnType, token uint32, useUnixSock bool) *tcpConnection

func Open

func Open(remote, local plan.PeerID, t ConnType, token uint32, useUnixSock bool) (*tcpConnection, error)

func Stream

func Stream(conn Connection, accept acceptFunc, handle MsgHandleFunc) (int, error)

Types

type ByteSlicePool

type ByteSlicePool struct {
	sync.Mutex
	// contains filtered or unexported fields
}

ByteSlicePool reuse byte slices: chunk size -> pool.

func (*ByteSlicePool) GetBuf

func (p *ByteSlicePool) GetBuf(size uint32) []byte

GetBuf gets a chunk from reuse pool or creates a new one if reuse failed.

func (*ByteSlicePool) PutBuf

func (p *ByteSlicePool) PutBuf(buf []byte)

PutBuf puts a chunk to reuse pool if it can be reused.

type ConnType

type ConnType uint16
const (
	ConnPing       ConnType = iota // 0
	ConnControl    ConnType = iota
	ConnCollective ConnType = iota
	ConnPeerToPeer ConnType = iota
)

func (ConnType) String

func (t ConnType) String() string

type Connection

type Connection interface {
	io.Closer

	Conn() net.Conn // FIXME: don't allow access net.Conn
	Type() ConnType
	Src() plan.PeerID
	Dest() plan.PeerID
	Send(name string, m Message, flags uint32) error
	Read(name string, m Message) error
}

Connection is a simplex logical connection from one peer to another

func UpgradeFrom

func UpgradeFrom(conn net.Conn, self plan.PeerID, token uint32) (Connection, error)

UpgradeFrom performs the server side operations to upgrade a TCP connection to a Connection

type Handler

type Handler interface {
	Handle(conn Connection) (int, error)
}

type HandlerFunc

type HandlerFunc func(Connection) (int, error)

func (HandlerFunc) Handle

func (f HandlerFunc) Handle(c Connection) (int, error)

type Message

type Message struct {
	Length uint32
	Data   []byte
	Flags  uint32 // copied from Header, shouldn't be used during Read or Write
}

Message is the data transferred via channel

func Accept

func Accept(conn Connection) (string, *Message, error)

Accept accepts one message from connection

func (*Message) HasFlag

func (m *Message) HasFlag(flag uint32) bool

func (*Message) ReadFrom

func (m *Message) ReadFrom(r io.Reader) error

ReadFrom reads the message from a reader into new buffer. The message length is obtained from the reader and should be trusted.

func (*Message) ReadInto

func (m *Message) ReadInto(r io.Reader) error

ReadInto reads the message from a reader into existing buffer. The message length obtained from the reader should be checked.

func (*Message) Same

func (m *Message) Same(pm *Message) bool

func (Message) String

func (m Message) String() string

func (Message) WriteTo

func (m Message) WriteTo(w io.Writer) error

type MessageHeader

type MessageHeader struct {
	NameLength uint32
	Name       []byte
	Flags      uint32 // TODO: meaning of flags should be based on conn Type
}

func (*MessageHeader) Expect

func (h *MessageHeader) Expect(r io.Reader, name string) error

Expect reads the messageHeader from a reader into new buffer. The result Name should be checked against name.

func (*MessageHeader) HasFlag

func (h *MessageHeader) HasFlag(flag uint32) bool

func (*MessageHeader) ReadFrom

func (h *MessageHeader) ReadFrom(r io.Reader) error

ReadFrom reads the messageHeader from a reader into new buffer. The name length is obtained from the reader and should be trusted.

func (MessageHeader) String

func (h MessageHeader) String() string

func (*MessageHeader) WriteTo

func (h *MessageHeader) WriteTo(w io.Writer) error

type MsgHandleFunc

type MsgHandleFunc func(name string, msg *Message, conn Connection)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL