ipc

package module
v0.0.0-...-7e765e1 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2024 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.

Intergration

As well as using this library just for go processes it was also designed to work with other languages, with the go process as the server and the other languages processing being the client.

NodeJs

I currently use this library to comunicate between a ElectronJS GUI and a go program.

Below is a link to the nodeJS client library:

https://github.com/james-barrow/node-ipc-client

Python

To do

Usage

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


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

Create a client and connect to the server:


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

Read messages

Read each message sent:


    for {

        // message, err := s.Read() server
        message, err := c.Read() // client

        if err == nil {
            // handle error
        }

        // do something with the received messages
    }

All received messages are formated into the type Message


type Message struct {
	Err     error  // details of any error
	MsgType int    // 0 = reserved , -1 is an internal message (disconnection or error etc), all messages recieved will be > 0
	Data    []byte // message data received
	Status  string // the status of the connection
}

Write a message

	//err := s.Write(1, []byte("<Message for client"))
    err := c.Write(1, []byte("<Message for server"))

    if err == nil {
        // handle error
    }

Advanced Configuaration

Server options:


    config := &ipc.ServerConfig{
		Encryption: (bool),        // allows encryption to be switched off (bool - default is true)
        MaxMsgSize: (int) ,        // the maximum size in bytes of each message ( default is 3145728 / 3Mb)
	    UnmaskPermissions: (bool), // make the socket writeable for other users (default is false)
    }


Client options:


	config := ClientConfig  {
		Encryption (bool),          // allows encryption to be switched off (bool - default is true)
		Timeout    (float64),       // number of seconds to wait before timing out trying to connect/reconnect (default is 0 no timeout)
		RetryTimer (time.Duration), // number of seconds to wait before connection retry (default is 20)

	}

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 function:

	Encryption: false
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 a custom configuration to the server start function. This will make the socket writable for any user.

	UnmaskPermissions: true

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

View Source
var (
	DefaultServerConfig = ServerConfig{
		SocketBasePath:    defaultSocketBasePath,
		Timeout:           0,
		MaxMsgSize:        defaultMaxMsgSize,
		Encryption:        true,
		UnmaskPermissions: false,
	}

	DefaultClientConfig = ClientConfig{
		SocketBasePath: defaultSocketBasePath,
		Timeout:        0,
		RetryTimer:     defaultRetryTimer,
		MaxMsgSize:     defaultMaxMsgSize,
		Encryption:     true,
	}
)

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.

func (*Client) Close

func (c *Client) Close()

Close - closes the connection

func (*Client) Read

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

Read - blocking function that receices messages if MsgType is a negative number its an internal message

func (*Client) Status

func (c *Client) Status() Status

StatusCode - returns the current connection status

func (*Client) Write

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

Write - writes a 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 {
	SocketBasePath string
	Timeout        time.Duration
	RetryTimer     time.Duration
	MaxMsgSize     int
	Encryption     bool
}

ClientConfig - used to pass configuration overrides to ClientStart()

type Message

type Message struct {
	Err     error  // details of any error
	MsgType int    // 0 = reserved , -1 is an internal message (disconnection or error etc), all messages recieved will be > 0
	Data    []byte // message data received
	Status  string // the status of the connection
}

Message - contains the received message

type Server

type Server struct {
	Name string
	// 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, the client needs to use the same name

func (*Server) Close

func (s *Server) Close()

Close - closes the connection

func (*Server) Read

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

Read - blocking function, reads each message recieved if MsgType is a negative number its an internal message

func (*Server) Status

func (s *Server) Status() Status

Status - returns the current connection status

func (*Server) Write

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

Write - writes a 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 {
	SocketBasePath    string
	Timeout           time.Duration
	MaxMsgSize        int
	Encryption        bool
	UnmaskPermissions bool
}

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
	// Connecting - 2
	Connecting
	// Connected - 3
	Connected
	// ReConnecting - 4
	ReConnecting
	// Closed - 5
	Closed
	// Closing - 6
	Closing
	// Error - 7
	Error
	// Timeout - 8
	Timeout
	// Disconnected - 9
	Disconnected
)

func (*Status) String

func (status *Status) String() string

returns the status of the connection as a string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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