ipc

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2024 License: GPL-3.0 Imports: 12 Imported by: 1

README

gomon-ipc

Overview

This is a simple cross-platform IPC (interprocess communication protocol) library. It was written to support gomon but is general enough be used on any project that requires simple interprocess comms. Other libraries do exist but are either too complex or not properly idiomatic Go.

A UDP based network protocol was chosen. This is simple and cross-platform. UDP does not have a reliability layer however, on a single local machine this should not present any problems.

How it works

The library offers up a Connection object which can be configured as a client or server. Both open a UDP socket on the local loopback interface on a well-known port (this can be configured if required). The connection has blocking Read and Write operations and also a non-blocking read handler. Only one of blocking or non-blocking can be used. Messages are transferred using UDP packets.

All operations make use of Context objects and respect context timeout/cancellation.

Example

See /example/main.go for a working example.

Documentation

Index

Constants

View Source
const (
	ClientConnection ConnectionRole = "client"
	ServerConnection ConnectionRole = "server"

	DefaultServerHost        = "127.0.0.1"
	DefaultServerPort        = 33333
	DefaultConnectionTimeout = time.Second * 60
	DefaultReadTimeout       = time.Millisecond * 500
	DefaultWriteTimeout      = time.Millisecond * 500
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AtomicConnectionState

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

func NewAtomicConnectionState

func NewAtomicConnectionState(initial ConnectionState) AtomicConnectionState

func (*AtomicConnectionState) Get

func (*AtomicConnectionState) Set

type Connection

type Connection interface {
	ListenAndServe(ctx context.Context, callbackFn StateHandler) error
	Read(ctx context.Context) ([]byte, error)
	Write(ctx context.Context, data []byte) error
	Close() error
	IsConnected() bool
}

func NewConnection

func NewConnection(role ConnectionRole, opts ...OptionFunction) (Connection, error)

type ConnectionRole

type ConnectionRole string

type ConnectionState

type ConnectionState int32
const (
	NotConnected ConnectionState = iota
	ConnectionPending
	Connected
	Disconnecting
	Disconnected
)

type Message

type Message struct {
	ID     string
	Type   MessageType
	Data   []byte
	Source string
}

type MessageHandler

type MessageHandler func(data []byte) error

type MessageType

type MessageType int
const (
	Ack MessageType = iota
	Connect
	Disconnect
	Data
)

type OptionFunction

type OptionFunction func(*connection)

func WithConnectionTimeout

func WithConnectionTimeout(timeout time.Duration) OptionFunction

func WithReadHandler

func WithReadHandler(handler MessageHandler) OptionFunction

func WithReadTimeout

func WithReadTimeout(timeout time.Duration) OptionFunction

func WithServerHost

func WithServerHost(host string) OptionFunction

func WithServerPort

func WithServerPort(port int) OptionFunction

func WithWriteTimeout

func WithWriteTimeout(timeout time.Duration) OptionFunction

type StateHandler

type StateHandler func(state ConnectionState) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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