xtransport

package module
v1.1.7 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2022 License: GPL-3.0 Imports: 9 Imported by: 12

README

⚡ xtransport

xtransport is a easy way to provider tcp/ws transport

Installation

go get -u github.com/hkloudou/xtransport

Quick Start

package main

import (
	"github.com/hkloudou/xtransport"
	"github.com/hkloudou/xtransport/packets/mqtt"
)

func main() {
	tran := xtransport.NewTcpTransport[mqtt.ControlPacket]()
	if err := tran.Listen(); err != nil {
		panic(err)
	}
	tran.Accept(func(sock xtransport.Socket[mqtt.ControlPacket]) {
		defer func() {
			if r := recover(); r != nil {
				println(r)
			}
			sock.Close()
		}()
		for {
			request, err := sock.Recv(mqtt.ReadPacket)
			if err != nil {
				break
			}
			if request.Type() == mqtt.Disconnect {
				break
			}
			if request.Type() <= 0 || request.Type() >= 14 {
				break
			}
			if request.Type() == mqtt.Pingreq {
				sock.Send(mqtt.NewControlPacket(mqtt.Pingresp))
				continue
			}
			if request.Type() == mqtt.Connect {
				sock.Session().Set("clientIdentifier", request.(*mqtt.ConnectPacket).ClientIdentifier)
			}
		}
	})
}

interface


type Transport[T Packet] interface {
	// Init(...Option) error
	Options() Options
	Dial(addr string, opts ...DialOption) (Client[T], error)
	Listen(addr string, opts ...ListenOption) (Listener[T], error)
	String() string
}

type Listener[T Packet] interface {
	Addr() string
	Close() error
	Accept(func(Socket[T])) error
}

type Client[T Packet] interface {
	Socket[T]
}

type Socket[T Packet] interface {
	Recv(func(r io.Reader) (T, error)) (T, error)
	Send(T) error
	io.Closer
	Local() string
	Remote() string
	ConnectionState() *tls.ConnectionState
	Session() *Context
	SetTimeOut(time.Duration)
}

type Writer interface {
	Write(io.Writer) error
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Write added in v1.1.6

func Write(w io.Writer, v interface{}) (n int, err error)

func WriteBytes added in v1.1.6

func WriteBytes(w io.Writer, data []byte) (n int, err error)

func WriteString added in v1.1.6

func WriteString(w io.Writer, data string) (n int, err error)

Types

type Client added in v1.1.5

type Client interface {
	Socket
}

type Context added in v1.1.1

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

func NewSession added in v1.0.9

func NewSession() *Context

func (*Context) Get added in v1.1.1

func (c *Context) Get(key string) (value interface{}, exists bool)

Get returns the value for the given key, ie: (value, true). If the value does not exists it returns (nil, false)

func (*Context) GetBool added in v1.1.1

func (c *Context) GetBool(key string) (b bool)

GetBool returns the value associated with the key as a boolean.

func (*Context) GetDuration added in v1.1.1

func (c *Context) GetDuration(key string) (d time.Duration)

GetDuration returns the value associated with the key as a duration.

func (*Context) GetFloat64 added in v1.1.1

func (c *Context) GetFloat64(key string) (f64 float64)

GetFloat64 returns the value associated with the key as a float64.

func (*Context) GetInt added in v1.1.1

func (c *Context) GetInt(key string) (i int)

GetInt returns the value associated with the key as an integer.

func (*Context) GetInt64 added in v1.1.1

func (c *Context) GetInt64(key string) (i64 int64)

GetInt64 returns the value associated with the key as an integer.

func (*Context) GetString added in v1.1.1

func (c *Context) GetString(key string) (s string)

GetString returns the value associated with the key as a string.

func (*Context) GetStringMap added in v1.1.1

func (c *Context) GetStringMap(key string) (sm map[string]interface{})

GetStringMap returns the value associated with the key as a map of interfaces.

func (*Context) GetStringMapString added in v1.1.1

func (c *Context) GetStringMapString(key string) (sms map[string]string)

GetStringMapString returns the value associated with the key as a map of strings.

func (*Context) GetStringMapStringSlice added in v1.1.1

func (c *Context) GetStringMapStringSlice(key string) (smss map[string][]string)

GetStringMapStringSlice returns the value associated with the key as a map to a slice of strings.

func (*Context) GetStringSlice added in v1.1.1

func (c *Context) GetStringSlice(key string) (ss []string)

GetStringSlice returns the value associated with the key as a slice of strings.

func (*Context) GetTime added in v1.1.1

func (c *Context) GetTime(key string) (t time.Time)

GetTime returns the value associated with the key as time.

func (*Context) GetUint added in v1.1.1

func (c *Context) GetUint(key string) (ui uint)

GetUint returns the value associated with the key as an unsigned integer.

func (*Context) GetUint64 added in v1.1.1

func (c *Context) GetUint64(key string) (ui64 uint64)

GetUint64 returns the value associated with the key as an unsigned integer.

func (*Context) MustGet added in v1.1.1

func (c *Context) MustGet(key string) interface{}

MustGet returns the value for the given key if it exists, otherwise it panics.

func (*Context) Session added in v1.1.1

func (c *Context) Session() *Context

func (*Context) Set added in v1.1.1

func (c *Context) Set(key string, value interface{})

Set is used to store a new key/value pair exclusively for this context. It also lazy initializes c.Keys if it was not used previously.

type DialOption added in v1.1.4

type DialOption func(*DialOptions)

func WithStream added in v1.1.4

func WithStream() DialOption

Indicates whether this is a streaming connection

func WithTimeout added in v1.1.4

func WithTimeout(d time.Duration) DialOption

Timeout used when dialling the remote side

type DialOptions added in v1.1.4

type DialOptions struct {
	// Tells the transport this is a streaming connection with
	// multiple calls to send/recv and that send may not even be called
	Stream bool
	// Timeout for dialing
	Timeout time.Duration

	// Other options for implementations of the interface
	// can be stored in a context
	Context context.Context
}

type ListenOption added in v1.1.4

type ListenOption func(*ListenOptions)

type ListenOptions added in v1.1.4

type ListenOptions struct {

	// Other options for implementations of the interface
	// can be stored in a context
	Context context.Context
}

type Listener added in v1.1.4

type Listener interface {
	Addr() string
	Close() error
	Accept(func(Socket)) error
}

type Option

type Option func(*Options)

func Addrs added in v1.1.4

func Addrs(addrs ...string) Option

Addrs to use for transport

func Secure

func Secure(b bool) Option

Use secure communication. If TLSConfig is not specified we use InsecureSkipVerify and generate a self signed cert

func TLSConfig added in v1.1.4

func TLSConfig(t *tls.Config) Option

TLSConfig to be used for the transport.

func Timeout

func Timeout(t time.Duration) Option

Timeout sets the timeout for Send/Recv execution

type Options

type Options struct {
	// Addrs is the list of intermediary addresses to connect to
	Addrs []string

	// Codec is the codec interface to use where headers are not supported
	// by the transport and the entire payload must be encoded
	// Codec codec.Marshaler
	// Secure tells the transport to secure the connection.
	// In the case TLSConfig is not specified best effort self-signed
	// certs should be used
	Secure bool
	// TLSConfig to secure the connection. The assumption is that this
	// is mTLS keypair
	TLSConfig *tls.Config
	// Timeout sets the timeout for Send/Recv
	Timeout time.Duration
	// Other options for implementations of the interface
	// can be stored in a context
	Context context.Context
}

type Socket

type Socket interface {
	Recv(func(r io.Reader) (interface{}, error)) (interface{}, error)
	Send(interface{}) error
	io.Closer
	Local() string
	Remote() string
	ConnectionState() *tls.ConnectionState
	Session() *Context
	SetTimeOut(time.Duration)
}

type Transport

type Transport interface {
	// Init(...Option) error
	Options() Options
	Dial(addr string, opts ...DialOption) (Client, error)
	Listen(addr string, opts ...ListenOption) (Listener, error)
	String() string
}

Transport is an interface which is used for communication between services. It uses connection based socket send/recv semantics and has various implementations; http, grpc, quic.

Directories

Path Synopsis
packets
mqtt Module
plugins
ws Module
transports
quic Module
tcp Module
ws Module

Jump to

Keyboard shortcuts

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