streams

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2022 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultTimeout = 15 * time.Second
)

Variables

This section is empty.

Functions

func HandleStream

func HandleStream(stream core.Stream, timeout time.Duration) ([]byte, RespondStream, CloseStream, error)

HandleStream is called at the beginning of stream handlers to create a wrapper stream and read first message

func Request

func Request(peerID peer.ID, protocol protocol.ID, data []byte, cfg StreamConfig) ([]byte, error)

Request sends a message to the given stream and returns the response

Types

type CloseStream

type CloseStream func() error

CloseStream closes the stream

type RespondStream

type RespondStream func([]byte) error

RespondStream abstracts stream with a function that accepts only the data to send

type Stream

type Stream interface {
	// ID returns the id of the stream
	ID() string

	io.Closer
	// CloseWrite closes the stream for writing but leaves it open for
	// reading. It does not free the stream, a call Close or
	// Reset is required.
	CloseWrite() error

	// ReadWithTimeout will read bytes from stream and return the result, will return error if timeout or error.
	// does not close stream when returns
	ReadWithTimeout(timeout time.Duration) ([]byte, error)

	// WriteWithTimeout will write bytes to stream, will return error if timeout or error.
	// does not close stream when returns
	WriteWithTimeout(data []byte, timeout time.Duration) error
}

Stream represents a stream in the system

func NewStream

func NewStream(s core.Stream) Stream

NewStream returns a new instance of stream

type StreamConfig

type StreamConfig struct {
	Ctx     context.Context
	Host    host.Host
	Timeout time.Duration
}

StreamConfig is the config object required to make a request

Jump to

Keyboard shortcuts

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