net

package
v0.1.1 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const (
	MaxMessageSize = 1 << 19
)

Variables

View Source
var ErrServerClosed = errors.New("Server closed")

ErrServerClosed occurs when a tcp server is closed.

Functions

func Encode

func Encode(adp ProtoAdapter, msg LineProtocol) (bytes.Buffer, error)

func StreamConn

func StreamConn(
	stream grpc.Stream,
) *common.Conn

Types

type Conn

type Conn struct {
	// Stream is the stream to wrap into a Conn. This is duplex stream.
	Stream *websocket.Conn

	// InMsg is the type to use for reading request data from the streaming
	// endpoint. This must be a non-nil allocated value and must NOT point to
	// the same value as OutMsg since they may be used concurrently.
	//
	// The Reset method will be called on InMsg during Reads so data you
	// set initially will be lost.
	InMsg proto.Message

	// OutMsg is the type to use for sending data to streaming endpoint. This must be
	// a non-nil allocated value and must NOT point to the same value as InMsg
	// since they may be used concurrently.
	//
	// The Reset method is never called on OutMsg so they will be sent for every request
	// unless the Encode field changes it.
	OutMsg proto.Message

	// WriteLock, if non-nil, will be locked while calling SendMsg
	// on the Stream. This can be used to prevent concurrent access to
	// SendMsg which is unsafe.
	WriteLock *sync.Mutex

	// Encode encodes messages into the Request. See Encoder for more information.
	Encode common.Encoder

	// Decode decodes messages from the Response into a byte slice. See
	// Decoder for more information.
	Decode common.Decoder
	// contains filtered or unexported fields
}

Conn implements net.Conn across a Websocket.

Methods such as LocalAddr, RemoteAddr, deadlines, etc. do not work.

func WebSocketConn

func WebSocketConn(
	stream *websocket.Conn,
) *Conn

func (*Conn) Close

func (c *Conn) Close() error

Close will close the client if this is a client. If this is a server stream this does nothing since gRPC expects you to close the stream by returning from the RPC call.

This calls CloseSend underneath for clients, so read the documentation for that to understand the semantics of this call.

func (*Conn) LocalAddr

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

LocalAddr returns nil.

func (*Conn) Read

func (c *Conn) Read(p []byte) (n int, err error)

Read implements io.Reader.

func (*Conn) RemoteAddr

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

RemoteAddr returns nil.

func (*Conn) SetDeadline

func (c *Conn) SetDeadline(time.Time) error

SetDeadline is non-functional due to limitations on how gRPC works. You can mimic deadlines often using call options.

func (*Conn) SetReadDeadline

func (c *Conn) SetReadDeadline(time.Time) error

SetReadDeadline is non-functional, see SetDeadline.

func (*Conn) SetWriteDeadline

func (c *Conn) SetWriteDeadline(time.Time) error

SetWriteDeadline is non-functional, see SetDeadline.

func (*Conn) Write

func (c *Conn) Write(p []byte) (int, error)

Write implements io.Writer.

type Connect

type Connect struct {
	Version             uint8
	InsecureFlag        bool
	ClientID            []byte
	KeepAlive           uint16
	CleanSessFlag       bool
	SessKey             uint32
	Username            string
	Password            []byte
	BatchDuration       int32
	BatchByteThreshold  int32
	BatchCountThreshold int32

	LineProtocol
}

Connect represents a connect Message.

func (*Connect) Info

func (c *Connect) Info() Info

Info returns DeliveryMode and MessageID of this Message.

func (*Connect) Type

func (c *Connect) Type() MessageType

Type returns the Connect Message type.

type ConnectAcknowledge

type ConnectAcknowledge struct {
	ReturnCode uint8
	Epoch      uint32
	ConnID     uint32

	LineProtocol
}

ConnectAcknowledge represents a CONNECT Acknowledge Message. 0x00 connection accepted 0x01 refused: unacceptable proto version 0x02 refused: identifier rejected 0x03 refused: unacceptable identifier, access not allowed 0x04 refused server unavailiable 0x05 not authorized 0x06 bad request

type ControlMessage

type ControlMessage struct {
	MessageID   uint16
	MessageType MessageType
	FlowControl FlowControl
	Message     LineProtocol
}

ControlMessage is to send a Control Message in reponse to another Message Flow.

func (*ControlMessage) Info

func (c *ControlMessage) Info() Info

Info returns DeliveryMode and MessageID of this Message.

func (*ControlMessage) Type

func (c *ControlMessage) Type() MessageType

Type returns the FlowControl Message type.

type DeliveryMode

type DeliveryMode uint8

DeliverMode represents a delivery mode of a Message.

const (
	EXPRESS DeliveryMode = iota
	RELIABLE
	BATCH
)

type Disconnect

type Disconnect struct {
	LineProtocol
}

Disconnect is to signal you want to cease communications with the server

func (*Disconnect) Info

func (d *Disconnect) Info() Info

Info returns DeliveryMode and MessageID of this Message.

func (*Disconnect) Type

func (d *Disconnect) Type() MessageType

Type returns the Disconnect Message type.

type FixedHeader

type FixedHeader struct {
	MessageType    uint8
	FlowControl    uint8
	MesssageLength int
}

FixedHeader

type FlowControl

type FlowControl uint8

FlowControl represents FlowControl Message type

const (
	// Flow Control
	NONE FlowControl = iota
	ACKNOWLEDGE
	NOTIFY
	RECEIVE
	RECEIPT
	COMPLETE
)

func (FlowControl) Value

func (t FlowControl) Value() uint8

type GrpcServer

type GrpcServer server

func NewGrpcServer

func NewGrpcServer(opts ...Options) *GrpcServer

func (*GrpcServer) Serve

func (s *GrpcServer) Serve(list net.Listener) error

func (*GrpcServer) Stream

func (s *GrpcServer) Stream(stream pbx.Unitdb_StreamServer) error

Stream implements duplex unitdb.Stream

type Handler

type Handler func(c net.Conn)

Handler is a callback which get called when a tcp, websocket connection is established or a grpc stream is established

type HttpServer

type HttpServer server

func NewHttpServer

func NewHttpServer(opts ...Options) *HttpServer

func (*HttpServer) HandleFunc

func (s *HttpServer) HandleFunc(w http.ResponseWriter, r *http.Request)

func (*HttpServer) Serve

func (s *HttpServer) Serve(list net.Listener) error

type Info

type Info struct {
	DeliveryMode uint8
	MessageID    uint16
}

Info returns DeliveryMode and MessageID by the Info() function called on the Message

type LineProtocol

type LineProtocol interface {
	Type() MessageType
	Info() Info
}

LineProtocol is the interface all our Messages in the line protocol will be implementing

func Read

func Read(adp ProtoAdapter, r io.Reader) (LineProtocol, error)

type MessageType

type MessageType uint8

MessageType represents a Message type

const (
	// Messages
	CONNECT MessageType = iota + 1
	PUBLISH
	SUBSCRIBE
	UNSUBSCRIBE
	PINGREQ
	DISCONNECT
	FLOWCONTROL
)

func (MessageType) Value

func (t MessageType) Value() uint8

type Options

type Options interface {
	// contains filtered or unexported methods
}

Options it contains configurable options for client

func WithDefaultOptions

func WithDefaultOptions() Options

WithDefaultOptions will create client connection with some default values.

KeepAlive: true
TlsConfig: nil

func WithTLSConfig

func WithTLSConfig(t *tls.Config) Options

WithTLSConfig will set an SSL/TLS configuration to be used when connecting to server.

type Pingreq

type Pingreq struct {
	LineProtocol
}

Pingreq is a keepalive

func (*Pingreq) Info

func (p *Pingreq) Info() Info

Info returns DeliveryMode and MessageID of this Message.

func (*Pingreq) Type

func (p *Pingreq) Type() MessageType

Type returns the Pingreq Message type.

type Proto

type Proto int

Proto represents the type of connection

const (
	UTP    Proto = iota // Unit Transport Protocol
	UNITQL              // Unit Query Language
)

type ProtoAdapter

type ProtoAdapter interface {
	Read(r io.Reader) (LineProtocol, error)
	Encode(msg LineProtocol) (bytes.Buffer, error)
}

type Publish

type Publish struct {
	IsForwarded  bool
	MessageID    uint16
	DeliveryMode uint8
	Messages     []*PublishMessage

	LineProtocol
}

Publish represents a publish Messages.

func (*Publish) Info

func (p *Publish) Info() Info

Info returns DeliveryMode and MessageID of this Message.

func (*Publish) Type

func (p *Publish) Type() MessageType

Type returns the Publish Message type.

type PublishMessage

type PublishMessage struct {
	Topic   []byte
	Payload []byte
	Ttl     string
}

PublishMessage reprensents a publish Message

type Server

type Server interface {
	// Serve serve the requests if type tcp, websocket or grpc stream
	Serve(net.Listener) error
}

type Subscribe

type Subscribe struct {
	IsForwarded   bool
	MessageID     uint16
	Subscriptions []*Subscription

	LineProtocol
}

Subscribe tells the server which topics the client would like to subscribe to

func (*Subscribe) Info

func (s *Subscribe) Info() Info

Info returns DeliveryMode and MessageID of this Message.

func (*Subscribe) Type

func (s *Subscribe) Type() MessageType

Type returns the Subscribe Message type.

type Subscription

type Subscription struct {
	DeliveryMode uint8
	Delay        int32
	Topic        []byte
	Last         string
}

Subscription is a struct for pairing the delivery mode and topic together for the delivery mode's pairs in unsubscribe and subscribe

type TcpServer

type TcpServer server

func NewTcpServer

func NewTcpServer(opts ...Options) *TcpServer

func (*TcpServer) Serve

func (s *TcpServer) Serve(list net.Listener) error

type Unsubscribe

type Unsubscribe struct {
	IsForwarded   bool
	MessageID     uint16
	Subscriptions []*Subscription

	LineProtocol
}

Unsubscribe is the Message to send if you don't want to subscribe to a topic anymore

func (*Unsubscribe) Info

func (u *Unsubscribe) Info() Info

Info returns DeliveryMode and MessageID of this Message.

func (*Unsubscribe) Type

func (u *Unsubscribe) Type() MessageType

Type returns the Unsubscribe Message type.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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