transport

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2018 License: Apache-2.0 Imports: 12 Imported by: 22

Documentation

Overview

Package transport implements functionality for handling MQTT connections.

Example
// launch server
server, err := Launch("tcp://localhost:1337")
if err != nil {
	panic(err)
}

go func() {
	// accept next incoming connection
	conn, err := server.Accept()
	if err != nil {
		panic(err)
	}

	// receive next packet
	pkt, err := conn.Receive()
	if err != nil {
		panic(err)
	}

	// check packet type
	if _, ok := pkt.(*packet.ConnectPacket); ok {
		// send a connack packet
		err = conn.Send(packet.NewConnackPacket())
		if err != nil {
			panic(err)
		}
	} else {
		panic("unexpected packet")
	}
}()

// dial to server
conn, err := Dial("tcp://localhost:1337")
if err != nil {
	panic(err)
}

// send connect packet
err = conn.Send(packet.NewConnectPacket())
if err != nil {
	panic(err)
}

// receive next packet
pkt, err := conn.Receive()
if err != nil {
	panic(err)
}

// check packet type
if connackPacket, ok := pkt.(*packet.ConnackPacket); ok {
	fmt.Println(connackPacket)

	// close connection
	err = conn.Close()
	if err != nil {
		panic(err)
	}
} else {
	panic("unexpected packet")
}

// close server
err = server.Close()
if err != nil {
	panic(err)
}
Output:

<ConnackPacket SessionPresent=false ReturnCode=0>

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrAcceptAfterClose = errors.New("accept after close")

ErrAcceptAfterClose can be returned by a WebSocketServer during Accept() if the server has been already closed and the internal goroutine is dying.

Note: this error is wrapped in an Error with NetworkError code.

View Source
var ErrNotBinary = errors.New("received web socket message is not binary")

ErrNotBinary may be returned by WebSocket connection when a message is received that is not binary.

View Source
var ErrUnsupportedProtocol = errors.New("unsupported protocol")

ErrUnsupportedProtocol is returned if either the launcher or dialer couldn't infer the protocol from the URL.

Functions

This section is empty.

Types

type BaseConn

type BaseConn struct {
	// contains filtered or unexported fields
}

A BaseConn manages the low-level plumbing between the Carrier and the packet Stream.

func NewBaseConn

func NewBaseConn(c Carrier) *BaseConn

NewBaseConn creates a new BaseConn using the specified Carrier.

func (*BaseConn) BufferedSend

func (c *BaseConn) BufferedSend(pkt packet.GenericPacket) error

BufferedSend will write the packet to an internal buffer. It will flush the internal buffer automatically when it gets stale. Encoding errors are directly returned as in Send, but any network errors caught while flushing the buffer at a later time will be returned on the next call.

Note: Only one goroutine can call BufferedSend at the same time.

func (*BaseConn) Close

func (c *BaseConn) Close() error

Close will close the underlying connection and cleanup resources. It will return an Error if there was an error while closing the underlying connection.

func (*BaseConn) Receive

func (c *BaseConn) Receive() (packet.GenericPacket, error)

Receive will read from the underlying connection and return a fully read packet. It will return an Error if there was an error while decoding or reading from the underlying connection.

Note: Only one goroutine can Receive at the same time.

func (*BaseConn) Send

func (c *BaseConn) Send(pkt packet.GenericPacket) error

Send will write the packet to the underlying connection. It will return an Error if there was an error while encoding or writing to the underlying connection.

Note: Only one goroutine can Send at the same time.

func (*BaseConn) SetReadLimit

func (c *BaseConn) SetReadLimit(limit int64)

SetReadLimit sets the maximum size of a packet that can be received. If the limit is greater than zero, Receive will close the connection and return an Error if receiving the next packet will exceed the limit.

func (*BaseConn) SetReadTimeout

func (c *BaseConn) SetReadTimeout(timeout time.Duration)

SetReadTimeout sets the maximum time that can pass between reads. If no data is received in the set duration the connection will be closed and Read returns an error.

type Carrier

type Carrier interface {
	io.ReadWriteCloser

	SetReadDeadline(time.Time) error
}

A Carrier is a generalized stream that can be used with BaseConn.

type Conn

type Conn interface {
	// Send will write the packet to the underlying connection. It will return
	// an Error if there was an error while encoding or writing to the
	// underlying connection.
	//
	// Note: Only one goroutine can Send at the same time.
	Send(pkt packet.GenericPacket) error

	// BufferedSend will write the packet to an internal buffer. It will flush
	// the internal buffer automatically when it gets stale. Encoding errors are
	// directly returned as in Send, but any network errors caught while flushing
	// the buffer at a later time will be returned on the next call.
	//
	// Note: Only one goroutine can call BufferedSend at the same time.
	BufferedSend(pkt packet.GenericPacket) error

	// Receive will read from the underlying connection and return a fully read
	// packet. It will return an Error if there was an error while decoding or
	// reading from the underlying connection.
	//
	// Note: Only one goroutine can Receive at the same time.
	Receive() (packet.GenericPacket, error)

	// Close will close the underlying connection and cleanup resources. It will
	// return an Error if there was an error while closing the underlying
	// connection.
	Close() error

	// SetReadLimit sets the maximum size of a packet that can be received.
	// If the limit is greater than zero, Receive will close the connection and
	// return an Error if receiving the next packet will exceed the limit.
	SetReadLimit(limit int64)

	// SetReadTimeout sets the maximum time that can pass between reads.
	// If no data is received in the set duration the connection will be closed
	// and Read returns an error.
	SetReadTimeout(timeout time.Duration)

	// LocalAddr will return the underlying connection's local net address.
	LocalAddr() net.Addr

	// RemoteAddr will return the underlying connection's remote net address.
	RemoteAddr() net.Addr
}

A Conn is a connection between a client and a broker. It abstracts an existing underlying stream connection.

func Dial

func Dial(urlString string) (Conn, error)

Dial is a shorthand function.

type Dialer

type Dialer struct {
	TLSConfig     *tls.Config
	RequestHeader http.Header

	DefaultTCPPort string
	DefaultTLSPort string
	DefaultWSPort  string
	DefaultWSSPort string
	// contains filtered or unexported fields
}

The Dialer handles connecting to a server and creating a connection.

func NewDialer

func NewDialer() *Dialer

NewDialer returns a new Dialer.

func (*Dialer) Dial

func (d *Dialer) Dial(urlString string) (Conn, error)

Dial initiates a connection based in information extracted from an URL.

type Launcher

type Launcher struct {
	TLSConfig *tls.Config
}

The Launcher helps with launching a server and accepting connections.

func NewLauncher

func NewLauncher() *Launcher

NewLauncher returns a new Launcher.

func (*Launcher) Launch

func (l *Launcher) Launch(urlString string) (Server, error)

Launch will launch a server based on information extracted from an URL.

type NetConn

type NetConn struct {
	BaseConn
	// contains filtered or unexported fields
}

A NetConn is a wrapper around a basic TCP connection.

func NewNetConn

func NewNetConn(conn net.Conn) *NetConn

NewNetConn returns a new NetConn.

func (*NetConn) LocalAddr

func (c *NetConn) LocalAddr() net.Addr

LocalAddr returns the local network address.

func (*NetConn) RemoteAddr

func (c *NetConn) RemoteAddr() net.Addr

RemoteAddr returns the remote network address.

func (*NetConn) UnderlyingConn

func (c *NetConn) UnderlyingConn() net.Conn

UnderlyingConn returns the underlying net.Conn.

type NetServer

type NetServer struct {
	// contains filtered or unexported fields
}

A NetServer accepts net.Conn based connections.

func NewNetServer

func NewNetServer(address string) (*NetServer, error)

NewNetServer creates a new TCP server that listens on the provided address.

func NewSecureNetServer

func NewSecureNetServer(address string, config *tls.Config) (*NetServer, error)

NewSecureNetServer creates a new TLS server that listens on the provided address.

func (*NetServer) Accept

func (s *NetServer) Accept() (Conn, error)

Accept will return the next available connection or block until a connection becomes available, otherwise returns an Error.

func (*NetServer) Addr

func (s *NetServer) Addr() net.Addr

Addr returns the server's network address.

func (*NetServer) Close

func (s *NetServer) Close() error

Close will close the underlying listener and cleanup resources. It will return an Error if the underlying listener didn't close cleanly.

type Server

type Server interface {
	// Accept will return the next available connection or block until a
	// connection becomes available, otherwise returns an Error.
	Accept() (Conn, error)

	// Close will close the underlying listener and cleanup resources. It will
	// return an Error if the underlying listener didn't close cleanly.
	Close() error

	// Addr returns the server's network address.
	Addr() net.Addr
}

A Server is a local port on which incoming connections can be accepted.

func Launch

func Launch(urlString string) (Server, error)

Launch is a shorthand function.

type WebSocketConn

type WebSocketConn struct {
	BaseConn
	// contains filtered or unexported fields
}

The WebSocketConn wraps a websocket.Conn. The implementation supports packets that are chunked over several WebSocket messages and packets that are coalesced to one WebSocket message.

func NewWebSocketConn

func NewWebSocketConn(conn *websocket.Conn) *WebSocketConn

NewWebSocketConn returns a new WebSocketConn.

func (*WebSocketConn) LocalAddr

func (c *WebSocketConn) LocalAddr() net.Addr

LocalAddr returns the local network address.

func (*WebSocketConn) RemoteAddr

func (c *WebSocketConn) RemoteAddr() net.Addr

RemoteAddr returns the remote network address.

func (*WebSocketConn) UnderlyingConn

func (c *WebSocketConn) UnderlyingConn() *websocket.Conn

UnderlyingConn returns the underlying websocket.Conn.

type WebSocketServer

type WebSocketServer struct {
	// contains filtered or unexported fields
}

The WebSocketServer accepts websocket.Conn based connections.

func NewSecureWebSocketServer

func NewSecureWebSocketServer(address string, config *tls.Config) (*WebSocketServer, error)

NewSecureWebSocketServer creates a new WSS server that listens on the provided address.

func NewWebSocketServer

func NewWebSocketServer(address string) (*WebSocketServer, error)

NewWebSocketServer creates a new WS server that listens on the provided address.

func (*WebSocketServer) Accept

func (s *WebSocketServer) Accept() (Conn, error)

Accept will return the next available connection or block until a connection becomes available, otherwise returns an Error.

func (*WebSocketServer) Addr

func (s *WebSocketServer) Addr() net.Addr

Addr returns the server's network address.

func (*WebSocketServer) Close

func (s *WebSocketServer) Close() error

Close will close the underlying listener and cleanup resources. It will return an Error if the underlying listener didn't close cleanly.

func (*WebSocketServer) SetFallback

func (s *WebSocketServer) SetFallback(handler http.Handler)

SetFallback will register a http.Handler that gets called if a request is not a WebSocket upgrade request.

func (*WebSocketServer) SetOriginChecker

func (s *WebSocketServer) SetOriginChecker(fn func(r *http.Request) bool)

SetOriginChecker sets an optional function that allows check the request origin before accepting the connection.

Directories

Path Synopsis
Package flow can be used to test MQTT packet flows.
Package flow can be used to test MQTT packet flows.

Jump to

Keyboard shortcuts

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