pakt

package module
v2.2.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2017 License: GPL-3.0 Imports: 12 Imported by: 4

README

GoDoc Go Report Card

PAKT provides access to exported methods across a network or other I/O connections similar to RPC. It handles any I/O connection which implements the golang net.Conn interface. This library handles synchronization, heartbeat, encoding/decoding using MessagePack (or a custom codec) and ensures thread-safety.

Project Name

"Pakt" is the German word for pact (deal). Server and client make a pact and start communicating.

Documentation

See the documentation at GoDoc.

Protocol Specification

See the protocol specification.

Sample

Check the sample directory for a simple server and client example.

Introduction

Create a PAKT socket:

// Create a socket by passing a net.Conn interface.
s := pakt.NewSocket(conn)

Register a function callable from remote peers:

// Register the function.
s.RegisterFunc("foo", foo)

// ...

func foo(c *pakt.Context) (interface{}, error) {
    data := ... // A struct, string, ...

	// Decode the received data from the peer.
	err := c.Decode(&data)
	if err != nil {
		// The remote peer would get this error.
		return nil, err
	}

	// Just send the data back to the client.
	return data, nil
}

Call the remote function from a remote peer:

c, err := s.Call("foo", data)
if err != nil {
    // Handle error. This includes errors returned from the remote function.
}

// Decode the received data from the peer if it returns a data value.
err = c.Decode(&data)
if err != nil {
    // Handle error.
}

Documentation

Overview

Package pakt provides access to exported methods across a network or other I/O connections similar to RPC. It handles any I/O connection which implements the golang net.Conn interface.

Index

Constants

View Source
const (
	// ProtocolVersion defines the protocol version defined in the specifications.
	ProtocolVersion byte = 0

	// DefaultMaxMessageSize specifies the default maximum message payload size in KiloBytes.
	DefaultMaxMessageSize = 100 * 1024

	// DefaultCallTimeout specifies the default timeout for a call request.
	DefaultCallTimeout = 30 * time.Second
)

Variables

View Source
var (
	// ErrClosed defines the error if the socket connection is closed.
	ErrClosed = errors.New("socket closed")

	// ErrTimeout defines the error if the call timeout is reached.
	ErrTimeout = errors.New("timeout")

	// ErrMaxMsgSizeExceeded if the maximum message size is exceeded for a call request.
	ErrMaxMsgSizeExceeded = errors.New("maximum message size exceeded")
)
View Source
var (
	// ErrNoContextData defines the error if no context data is available.
	ErrNoContextData = errors.New("no context data available to decode")
)
View Source
var (
	// Log is the public logrus value used internally.
	Log *logrus.Logger
)

Functions

This section is empty.

Types

type CallHook

type CallHook func(s *Socket, funcID string, c *Context)

CallHook defines the callback function.

type ClosedChan

type ClosedChan <-chan struct{}

ClosedChan defines a channel which is closed as soon as the socket is closed.

type Context

type Context struct {
	// Data is the raw byte representation of the encoded context data.
	Data []byte
	// contains filtered or unexported fields
}

A Context defines a function context.

func (*Context) Decode

func (c *Context) Decode(v interface{}) error

Decode the context data to a custom value. The value has to be passed as pointer. Returns ErrNoContextData if there is no context data available to decode.

func (*Context) Socket

func (c *Context) Socket() *Socket

Socket returns the socket of the context.

type ErrorHook

type ErrorHook func(s *Socket, funcID string, err error)

ErrorHook defines the error callback function.

type Func

type Func func(c *Context) (data interface{}, err error)

Func defines a callable PAKT function.

type Funcs

type Funcs map[string]Func

Funcs defines a set of functions.

type OnCloseFunc

type OnCloseFunc func(s *Socket)

OnCloseFunc defines the callback function which is triggered as soon as the socket closes.

type Server

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

Server defines the PAKT server implementation.

func NewServer

func NewServer(ln net.Listener) *Server

NewServer creates a new PAKT server.

func (*Server) Close

func (s *Server) Close()

Close the server and disconnect all connected sockets.

func (*Server) ClosedChan

func (s *Server) ClosedChan() ClosedChan

ClosedChan returns a channel which is closed as soon as the network listener is closed.

func (*Server) GetSocket

func (s *Server) GetSocket(id string) (so *Socket)

GetSocket obtains a socket by its ID. Returns nil if not found.

func (*Server) IsClosed

func (s *Server) IsClosed() bool

IsClosed returns a boolean indicating if the network listener is closed.

func (*Server) Listen

func (s *Server) Listen()

Listen for new socket connections. This method is blocking.

func (*Server) NewSocketChan

func (s *Server) NewSocketChan() <-chan *Socket

NewSocketChan returns the channel for new incoming sockets. Either use NewSocketChan or OnNewSocket.

func (*Server) OnClose

func (s *Server) OnClose(f func())

OnClose triggers the function as soon as the network listener closes. This method can be called multiple times to bind multiple functions.

func (*Server) OnNewSocket

func (s *Server) OnNewSocket(f func(*Socket))

OnNewSocket sets the function which is triggered if a new socket connection was made. Only set this during initialization and only set this once! The callback function is called in a new goroutine. Either use NewSocketChan or OnNewSocket.

func (*Server) Sockets

func (s *Server) Sockets() []*Socket

Sockets returns a list of all current connected sockets.

type Socket

type Socket struct {
	// Value is a custom value which can be set.
	Value interface{}

	// Codec holds the encoding and decoding interface.
	Codec codec.Codec
	// contains filtered or unexported fields
}

Socket defines the PAKT socket implementation.

func NewSocket

func NewSocket(conn net.Conn, vars ...string) *Socket

NewSocket creates a new PAKT socket using the passed connection. One variadic argument specifies the socket ID. Ready() must be called to start the socket read routine.

func (*Socket) Call

func (s *Socket) Call(id string, args ...interface{}) (*Context, error)

Call a remote function and wait for its result. This method blocks until the remote socket function returns. The first variadic argument specifies an optional data value [interface{}]. The second variadic argument specifies an optional call timeout time.Duration. Returns ErrTimeout on a timeout. Returns ErrClosed if the connection is closed. This method is thread-safe.

func (*Socket) Close

func (s *Socket) Close() error

Close the socket connection. This method is thread-safe.

func (*Socket) ClosedChan

func (s *Socket) ClosedChan() ClosedChan

ClosedChan returns a channel which is closed as soon as the socket is closed. This method is thread-safe.

func (*Socket) ID

func (s *Socket) ID() string

ID returns the socket ID.

func (*Socket) IsClosed

func (s *Socket) IsClosed() bool

IsClosed returns a boolean indicating if the socket connection is closed. This method is thread-safe.

func (*Socket) LocalAddr

func (s *Socket) LocalAddr() net.Addr

LocalAddr returns the local network address.

func (*Socket) OnClose

func (s *Socket) OnClose(f OnCloseFunc)

OnClose triggers the function as soon as the connection closes. This method can be called multiple times to bind multiple functions. This method is thread-safe.

func (*Socket) Ready

func (s *Socket) Ready()

Ready signalizes the Socket that the initialization is done. The socket starts reading from the underlying connection. This should be only called once per socket.

func (*Socket) RegisterFunc

func (s *Socket) RegisterFunc(id string, f Func)

RegisterFunc registers a remote function. This method is thread-safe.

func (*Socket) RegisterFuncs

func (s *Socket) RegisterFuncs(funcs Funcs)

RegisterFuncs registers a map of remote functions. This method is thread-safe.

func (*Socket) RemoteAddr

func (s *Socket) RemoteAddr() net.Addr

RemoteAddr returns the remote network address.

func (*Socket) SetCallHook

func (s *Socket) SetCallHook(h CallHook)

SetCallHook sets the call hook function which is triggered, if a local remote callable function will be called. This hook can be used for logging purpose. Only set this hook during initialization.

func (*Socket) SetCallTimeout

func (s *Socket) SetCallTimeout(t time.Duration)

SetCallTimeout sets the timeout for call requests. Only set this during initialization.

func (*Socket) SetErrorHook

func (s *Socket) SetErrorHook(h ErrorHook)

SetErrorHook sets the error hook function which is triggered, if a local remote callable function returns an error. This hook can be used for logging purpose. Only set this hook during initialization.

func (*Socket) SetMaxMessageSize

func (s *Socket) SetMaxMessageSize(size int)

SetMaxMessageSize sets the maximum message size in bytes. Only set this during initialization.

Directories

Path Synopsis
Package codec contains sub-packages with different codecs that can be used to encode and decode the PAKT messages.
Package codec contains sub-packages with different codecs that can be used to encode and decode the PAKT messages.
sample

Jump to

Keyboard shortcuts

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