ipc

package module
v1.0.10 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2022 License: MIT Imports: 18 Imported by: 0

README

golang-ipc

Golang Inter-process communication library for Window, Mac and Linux.

Overview

A simple to use package that uses unix sockets on Macos/Linux and named pipes on Windows to create a communication channel between two go processes.

Usage

Create a server with the default configuation and start listening for the client:


	sc, err := ipc.StartServer("<name of socket or pipe>", nil)
	if err != nil {
		log.Println(err)
		return
	}

Create a client and connect to the server:


	cc, err := ipc.StartClient("<name of socket or pipe>", nil)
	if err != nil {
		log.Println(err)
		return
	}

Read and write data to the connection:

        // write data
        _ = sc.Write(1, []byte("Message from server"))
        
        _ = cc.Write(5, []byte("Message from client"))


        // Read data
        for {
            
            dataType, data, err := sc.Read()

            if err == nil {
                log.Println("Server received: "+string(data)+" - Message type: ", dataType)
            } else {
                log.Println(err)
                break
            }
	    }


        for {
            
            dataType, data, err := cc.Read()

            if err == nil {
                log.Println("Client received: "+string(data)+" - Message type: ", dataType)     
            } else {
                log.Println(err)
                break
            }
	    }

Encryption

By default the connection established will be encypted, ECDH384 is used for the key exchange and AES 256 GCM is used for the cipher.

Encryption can be swithed off by passing in a custom configuation to the server & client start functions.

    
    config := &ipc.ServerConfig{Encryption: false}
	sc, err := ipc.StartServer("<name of socket or pipe>", config)

Unix Socket Permissions

Under most configurations, a socket created by a user will by default not be writable by another user, making it impossible for the client and server to communicate if being run by separate users.

The permission mask can be dropped during socket creation by passing custom configuration to the server start function. This will make the socket writable by any user.


	config := &ipc.ServerConfig{UnmaskPermissions: true}
	sc, err := ipc.StartServer("<name of socket or pipe>", config)

Note: Tested on Linux, not tested on Mac, not implemented on Windows.

Testing

The package has been tested on Mac, Windows and Linux and has extensive test coverage.

Licence

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client - holds the details of the client connection and config.

func StartClient

func StartClient(ipcName string, config ClientConfig) (*Client, error)

StartClient - start the ipc client.

ipcName = is the name of the unix socket or named pipe that the client will try and connect to. timeout = number of seconds before the socket/pipe times out trying to connect/re-cconnect - if -1 or 0 it never times out. retryTimer = number of seconds before the client tries to connect again.

func (*Client) Close

func (cc *Client) Close()

Close - closes the connection

func (*Client) Read

func (cc *Client) Read() (*Message, error)

Read - blocking function that waits until an non multipart message is received returns the message type, data and any error.

func (*Client) Status

func (cc *Client) Status() string

Status - returns the current connection status as a string

func (*Client) StatusCode

func (cc *Client) StatusCode() Status

StatusCode - returns the current connection status

func (*Client) Write

func (cc *Client) Write(msgType int, message []byte) error

Write - writes a non multipart message to the ipc connection. msgType - denotes the type of data being sent. 0 is a reserved type for internal messages and errors.

type ClientConfig

type ClientConfig struct {
	Timeout            time.Duration
	RetryTimer         time.Duration
	EncryptionRequired bool
	SocketBasePath     string
}

ClientConfig - used to pass configuration overrides to ClientStart()

type Message

type Message struct {
	MsgType int    // type of message sent - 0 is reserved
	Data    []byte // message data received
	Status  string
	// contains filtered or unexported fields
}

Message - contains the received message

type Server

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

Server - holds the details of the server connection & config.

func StartServer

func StartServer(ipcName string, config ServerConfig) (*Server, error)

StartServer - starts the ipc server.

ipcName = is the name of the unix socket or named pipe that will be created. timeout = number of seconds before the socket/pipe times out waiting for a connection/re-cconnection - if -1 or 0 it never times out.

func (*Server) Close

func (sc *Server) Close()

Close - closes the connection

func (*Server) Read

func (sc *Server) Read() (*Message, error)

func (*Server) Status

func (sc *Server) Status() string

Status - returns the current connection status as a string

func (*Server) StatusCode

func (sc *Server) StatusCode() Status

StatusCode - returns the current connection status

func (*Server) Write

func (sc *Server) Write(msgType int, message []byte) error

Write - writes a non multipart message to the ipc connection. msgType - denotes the type of data being sent. 0 is a reserved type for internal messages and errors.

type ServerConfig

type ServerConfig struct {
	Timeout           time.Duration
	MaxMsgSize        int
	Encryption        bool
	UnmaskPermissions bool
	SocketBasePath    string
}

ServerConfig - used to pass configuration overrides to ServerStart()

type Status

type Status int

Status - Status of the connection

const (

	// NotConnected - 0
	NotConnected Status = iota
	// Listening - 1
	Listening Status = iota
	// Connecting - 2
	Connecting Status = iota
	// Connected - 3
	Connected Status = iota
	// ReConnecting - 4
	ReConnecting Status = iota
	// Closed - 5
	Closed Status = iota
	// Closing - 6
	Closing Status = iota
	// Error - 7
	Error Status = iota
	// Timeout - 8
	Timeout Status = iota
)

func (*Status) String

func (status *Status) String() string

returns the status of the connection as a string

Jump to

Keyboard shortcuts

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