Documentation ¶
Index ¶
- Constants
- Variables
- func New(remote, local plan.PeerID, t ConnType, token uint32, useUnixSock bool) *tcpConnection
- func Open(remote, local plan.PeerID, t ConnType, token uint32, useUnixSock bool) (*tcpConnection, error)
- func Stream(conn Connection, accept acceptFunc, handle MsgHandleFunc) (int, error)
- type ByteSlicePool
- type ConnType
- type Connection
- type Handler
- type HandlerFunc
- type Message
- type MessageHeader
- type MsgHandleFunc
Constants ¶
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 )
const NoFlag uint32 = 0
Variables ¶
var ( GetBuf = defaultPool.GetBuf PutBuf = defaultPool.PutBuf )
var (
ErrInvalidConnectionType = errors.New("invalid connection type")
)
Functions ¶
func Stream ¶
func Stream(conn Connection, accept acceptFunc, handle MsgHandleFunc) (int, error)
Types ¶
type ByteSlicePool ¶
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 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 ¶
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) ReadFrom ¶
ReadFrom reads the message from a reader into new buffer. The message length is obtained from the reader and should be trusted.
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
type MsgHandleFunc ¶
type MsgHandleFunc func(name string, msg *Message, conn Connection)