tcpless

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2023 License: BSD-2-Clause Imports: 17 Imported by: 0

README

TCPLESS

library for golang rpc tcp serving using gob encoder/decoder

Features

  • TCP RPC server with good performance based on gob package for encode/decode
  • Connection pool
  • Possible to implement your own encrypt/decrypt data protocol
  • Contains client and concurrent client implementation
  • Supports TLS
  • Router and middleware (hooks) OOTB
  • Redial on write connection error
If you find this project useful or want to support the author, you can send tokens to any of these wallets
  • Bitcoin: bc1qgx5c3n7q26qv0tngculjz0g78u6mzavy2vg3tf
  • Ethereum: 0x62812cb089E0df31347ca32A1610019537bbFe0D
  • Dogecoin: DET7fbNzZftp4sGRrBehfVRoi97RiPKajV

Documentation

Index

Constants

View Source
const (
	// DefaultSharedBufferSize - 4 MB
	DefaultSharedBufferSize = 1024 * 1024 * 4 // 4 MB
	// MinimumSharedBufferSize - 2 KB
	MinimumSharedBufferSize = 1024 * 2 // 2 KB
	// ClientModeResponder Client can't ask
	ClientModeResponder = 0
	// ClientModeAsker client can't respond
	ClientModeAsker = 1
)

Variables

This section is empty.

Functions

func ConcurrentClient

func ConcurrentClient(n int, bufferSize int, client ClientConstructor, config *Config, logger gocli.Logger) (*concurrentClient, error)

ConcurrentClient create concurrent GetFreeClient with n, n <= 0 ignores bufferSize - shared buffer size

func CreateBuffer

func CreateBuffer(height, weight int) *buffer

CreateBuffer create buffer for data height - how many connection weight - how many bytes required one connection

func NewConnection

func NewConnection(conn net.Conn, buf *bytes.Buffer, index uint16) *connection

NewConnection prepare streamer

Types

type Client

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

Client structure

func CreateClient

func CreateClient(config *Config, sig ISignature, logger gocli.Logger) Client

CreateClient create base client

func (*Client) Ask

func (c *Client) Ask(route string, v any) porterr.IError

Ask server route

func (*Client) AskBytes

func (c *Client) AskBytes(route string, b []byte) porterr.IError

AskBytes send bytes

func (*Client) Ctx

func (c *Client) Ctx() context.Context

Ctx get context

func (*Client) Dial

func (c *Client) Dial() (net.Conn, porterr.IError)

Dial to server

func (*Client) Logger

func (c *Client) Logger() gocli.Logger

Logger get logger

func (*Client) Parse

func (c *Client) Parse(v any) porterr.IError

Parse data to type

func (*Client) Read

func (c *Client) Read() (ISignature, porterr.IError)

Read get signature from stream

func (*Client) SetStream

func (c *Client) SetStream(stream Streamer)

SetStream set stream io

func (*Client) Signature

func (c *Client) Signature() ISignature

Signature return signature

func (*Client) Stream

func (c *Client) Stream() Streamer

Stream Get stream

func (*Client) WithContext

func (c *Client) WithContext(ctx context.Context)

WithContext with context

type ClientConstructor

type ClientConstructor func(config *Config, logger gocli.Logger) IClient

ClientConstructor func for specific GetFreeClient init

type Config

type Config struct {
	// tcp address
	Address *net.TCPAddr
	// connection limits
	Limits ConnectionLimit
	// TLS configuration
	TLS TLSConfig
	// Mode client mode. 0 - Responder, - 1 Asker
	Mode uint8
}

Config server configuration

type ConnectionLimit

type ConnectionLimit struct {
	// Maximum number of connection
	MaxConnections uint16 `yaml:"maxConnections"`
	// Max idle time before connection will be closed
	MaxIdle time.Duration `yaml:"maxIdle"`
	// Max process body size
	SharedBufferSize int32 `yaml:"sharedBufferSize"`
	// RedialTimeout timeout for redial. Specify in second
	RedialTimeout time.Duration `yaml:"redialTimeout"`
}

ConnectionLimit limits for connection

type DataEncryptor

type DataEncryptor interface {
	// Encode message
	Encode(v any) error
	// Decode message
	Decode(v any) error
	// RegisterType register custom type
	RegisterType(v any)
}

DataEncryptor provide encoding and decoding and register type

func NewEmptyDataEncryptor

func NewEmptyDataEncryptor(buf *bytes.Buffer) DataEncryptor

NewEmptyDataEncryptor init empty data encryptor

func NewGobDataEncryptor

func NewGobDataEncryptor(buf *bytes.Buffer) DataEncryptor

NewGobDataEncryptor init gob data encryptor

func NewJSONDataEncryptor

func NewJSONDataEncryptor(buf *bytes.Buffer) DataEncryptor

NewJSONDataEncryptor init json data encryptor

type DataEncryptorConstructor

type DataEncryptorConstructor func(buf *bytes.Buffer) DataEncryptor

DataEncryptorConstructor apply constructor in set stream method

type Decoder

type Decoder interface {
	// Decode message
	Decode(v any) error
}

Decoder interface

func NewEmptyDecoder

func NewEmptyDecoder(r io.Reader) Decoder

NewEmptyDecoder init empty decoder

type EmptyDataEncryptor

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

EmptyDataEncryptor empty data encryptor

func (*EmptyDataEncryptor) Decode

func (d *EmptyDataEncryptor) Decode(v any) error

Decode message

func (*EmptyDataEncryptor) Encode

func (d *EmptyDataEncryptor) Encode(v any) error

Encode message

func (*EmptyDataEncryptor) RegisterType

func (d *EmptyDataEncryptor) RegisterType(v any)

RegisterType register custom type

type EmptyDecoder

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

EmptyDecoder decoder do nothing

func (*EmptyDecoder) Decode

func (e *EmptyDecoder) Decode(v any) error

Decode custom variable

type EmptyEncoder

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

EmptyEncoder encoder do nothing

func (*EmptyEncoder) Encode

func (e *EmptyEncoder) Encode(v any) error

Encode custom variable

type Encoder

type Encoder interface {
	// Encode message
	Encode(v any) error
}

Encoder interface

func NewEmptyEncoder

func NewEmptyEncoder(w io.Writer) Encoder

NewEmptyEncoder init empty encoder

type GobClient

type GobClient struct {
	// Common GetFreeClient
	Client
}

GobClient GetFreeClient for gob serialization

func (*GobClient) Dial

func (g *GobClient) Dial() (net.Conn, porterr.IError)

Dial to server

type GobDataEncryptor

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

GobDataEncryptor encode decode via gob serialisation

func (*GobDataEncryptor) Decode

func (d *GobDataEncryptor) Decode(v any) error

Decode message

func (*GobDataEncryptor) Encode

func (d *GobDataEncryptor) Encode(v any) error

Encode message

func (*GobDataEncryptor) RegisterType

func (d *GobDataEncryptor) RegisterType(v any)

RegisterType register custom type

type Handler

type Handler func(client IClient)

Handler Procedure handler

func (Handler) Reg

func (h Handler) Reg(route string, handler Handler) Handler

Reg register new route

func (Handler) Route

func (h Handler) Route(route string) Route

Route dedicate sub route

func (Handler) UnReg

func (h Handler) UnReg(route string) Handler

UnReg unregister route by name

type IClient

type IClient interface {
	// Ask custom type message
	Ask(route string, v any) porterr.IError
	// AskBytes send bytes
	AskBytes(route string, b []byte) porterr.IError
	// Ctx get context
	Ctx() context.Context
	// Dial to server
	Dial() (net.Conn, porterr.IError)
	// Logger get logger
	Logger() gocli.Logger
	// Parse current message
	Parse(v any) porterr.IError
	// Read get signature from stream
	Read() (ISignature, porterr.IError)
	// SetStream set stream io
	SetStream(stream Streamer)
	// Signature return signature
	Signature() ISignature
	// Stream Get stream
	Stream() Streamer
	// WithContext With context
	WithContext(ctx context.Context)
}

IClient interface for communication

func NewGobClient

func NewGobClient(config *Config, logger gocli.Logger) IClient

NewGobClient gob GetFreeClient constructor

type ISignature

type ISignature interface {
	// Data useful message
	Data() []byte
	// Decode byte message
	Decode(r io.Reader, buf *bytes.Buffer) error
	// Encode byte message
	Encode(buf *bytes.Buffer) []byte
	// Encryptor get data encryptor
	Encryptor() DataEncryptor
	// InitEncryptor return init data encryptor constructor
	InitEncryptor(buf *bytes.Buffer)
	// Len of data
	Len() uint64
	// Read implements reader interface
	Read(p []byte) (n int, err error)
	// Reset signature
	Reset()
	// Route get message route
	Route() string
	// Write implements writer interface
	Write(p []byte) (n int, err error)
}

ISignature common interface

type JsonDataEncryptor

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

JsonDataEncryptor encode decode via json serialisation

func (*JsonDataEncryptor) Decode

func (d *JsonDataEncryptor) Decode(v any) error

Decode message

func (*JsonDataEncryptor) Encode

func (d *JsonDataEncryptor) Encode(v any) error

Encode message

func (*JsonDataEncryptor) RegisterType

func (d *JsonDataEncryptor) RegisterType(v any)

RegisterType register custom type

type PermanentBuffer

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

PermanentBuffer local buffer

func NewPermanentBuffer

func NewPermanentBuffer(data []byte) *PermanentBuffer

NewPermanentBuffer init buffer

func (*PermanentBuffer) Bytes

func (b *PermanentBuffer) Bytes() []byte

Bytes return data

func (*PermanentBuffer) Cap

func (b *PermanentBuffer) Cap() int

Cap return cap for buffer

func (*PermanentBuffer) Next

func (b *PermanentBuffer) Next(n int) []byte

Next get n bytes

func (*PermanentBuffer) Read

func (b *PermanentBuffer) Read(p []byte) (n int, err error)

Read bytes

func (*PermanentBuffer) Reset

func (b *PermanentBuffer) Reset()

Reset cursor

func (*PermanentBuffer) Seek

func (b *PermanentBuffer) Seek(offset int64, whence int) (int64, error)

Seek change offset

func (*PermanentBuffer) Write

func (b *PermanentBuffer) Write(data []byte) (n int, err error)

Bytes return data

type Route

type Route string

Route definition

func (Route) Handle

func (r Route) Handle(route string, handler Handler)

Handle hande route

func (Route) Hook

func (r Route) Hook(handler Handler) Route

Hook register route hook

func (Route) Sub

func (r Route) Sub(route string) Route

Sub create sub route

type Server

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

Server server main struct

func NewServer

func NewServer(config *Config, handler Handler, client ClientConstructor, logger gocli.Logger) *Server

NewServer init new server

func (*Server) Start

func (s *Server) Start() error

Start server tcp connections

func (*Server) Stop

func (s *Server) Stop()

Stop close all tcp connections

type Signature

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

Signature standard handler signature

func CreateSignature

func CreateSignature(route []byte, data []byte, initEncryptor DataEncryptorConstructor) Signature

CreateSignature prepare Signature struct

func (*Signature) Data

func (h *Signature) Data() []byte

Data get useful message

func (*Signature) Decode

func (h *Signature) Decode(r io.Reader, buf *bytes.Buffer) error

Decode message from io r - input with bytes buf - bytes buffer

func (*Signature) Encode

func (h *Signature) Encode(buf *bytes.Buffer) []byte

Encode to byte message

func (*Signature) Encryptor

func (h *Signature) Encryptor() DataEncryptor

Encryptor get data encryptor

func (*Signature) InitEncryptor

func (h *Signature) InitEncryptor(buf *bytes.Buffer)

InitEncryptor init encryptor

func (*Signature) Len

func (h *Signature) Len() uint64

Len Length of current message

func (*Signature) Read

func (h *Signature) Read(p []byte) (n int, err error)

Read all bytes

func (*Signature) Reset

func (h *Signature) Reset()

Reset signature

func (*Signature) Route

func (h *Signature) Route() string

Route get route

func (*Signature) Write

func (h *Signature) Write(p []byte) (n int, err error)

Write rewrite bytes

type Streamer

type Streamer interface {
	// Buffer get buffer
	Buffer() *bytes.Buffer
	// Connection get connection
	Connection() net.Conn
	// Exit chan for exit
	Exit() <-chan struct{}
	// Index get index
	Index() uint16
	// Release connection
	Release() error
}

Streamer interface

type TLSConfig

type TLSConfig struct {
	// Is enabled
	Enabled bool `yaml:"enabled"`
	// Root CA path
	CaPath string `yaml:"caPath"`
	// Path to cert
	CertPath string `yaml:"certPath"`
	// Path to key
	KeyPath string `yaml:"keyPath"`
	// contains filtered or unexported fields
}

TLSConfig configuration

func (*TLSConfig) LoadTLSConfig

func (c *TLSConfig) LoadTLSConfig() (*tls.Config, error)

LoadTLSConfig load and prepare tls.Config

Jump to

Keyboard shortcuts

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