ipc

package module
v0.0.0-...-2aaa881 Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2020 License: MIT Imports: 13 Imported by: 0

README

go-ipc

This package provides IPC communication. Special feature of this package is passing file and TCP connection to other process.

See GoDoc for more detail.

go-ipc

!!CAUTION!!

This package was written for just my study. It is not trained in hard real world. Please be carefull to use in your project. If you find any bug or bad code, feel free to report issues.

Documentation

Overview

Package ipc implements cross platform inter process communication.

The main feature of this package is passing a tcp connection to other process. On windows, IPC is implemented with github.com/Microsoft/go-winio.

Example (Client)
conn, err := ipc.Dial("pipename")
if err != nil {
	return
}
defer conn.Close()

for {
	cmd, err := conn.ReceiveCommand()
	if err != nil {
		return
	}

	switch cmd {
	case ipc.DataCommand:
		d, err := conn.ReceiveData()
		if err != nil {
			return
		}
		fmt.Println(d) // 1, 2, 3, 4
	case ipc.FileCommand:
		f, withData, err := conn.ReceiveFile()
		if err != nil {
			return
		}
		fmt.Println(f.Name(), withData) // /tempdir/example~, true
		f.Close()
		if withData {
			msg, err := conn.ReceiveData()
			if err != nil {
				return
			}
			fmt.Println(string(msg)) // "message"
		}
	case ipc.TCPConnCommand:
		tcpc, withData, err := conn.ReceiveTCPConn()
		if err != nil {
			return
		}
		var buf [20]byte
		tcpc.Read(buf[:])
		fmt.Println(buf, withData) // [peeked]+, true
		tcpc.Close()
		if withData {
			msg, err := conn.ReceiveData()
			if err != nil {
				return
			}
			fmt.Println(string(msg)) // "message"
		}
	}
}
Output:

Example (Server)
l, err := ipc.Listen("pipename")
if err != nil {
	return
}
defer l.Close()

conn, err := l.Accept()
if err != nil {
	return
}

// send data
err = conn.SendData([]byte{1, 2, 3})
if err != nil {
	return
}

// send file
f, err := ioutil.TempFile("", "example")
err = conn.SendFile(f, []byte("message"))
if err != nil {
	return
}

// send tcp conn
netl, err := net.Listen("tcp", ":1234")
if err != nil {
	return
}
defer netl.Close()

netc, err := netl.Accept()
if err != nil {
	return
}
defer netc.Close()

var peeked [10]byte
_, err = netc.Read(peeked[:])
if err != nil {
	return
}
err = conn.SendTCPConn(netc.(*net.TCPConn), peeked[:], []byte("message"))
if err != nil {
	return
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrClosedQueue is returned from queue operation on QueueListener
	// that have been closed.
	ErrClosedQueue = errors.New("closed queue")

	// ErrTimeout is returned when read or write operation is not completed
	// before the deadline.
	ErrTimeout = errors.New("timeout")
)

Functions

This section is empty.

Types

type Command

type Command byte

Command represents a IPC command.

const (
	DataCommand Command = iota
	FileCommand
	TCPConnCommand
)

Kinds of Command. See also ReceiveCommand.

type Conn

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

Conn is a IPC connection; it implements net.Conn interface.

func Dial

func Dial(name string) (*Conn, error)

Dial connects to the named pipe.

func (*Conn) Close

func (c *Conn) Close() error

Close implements the Close method in the net.Listener interface; it close the connection.

func (*Conn) Read

func (c *Conn) Read(b []byte) (int, error)

Read implements the Read method in the net.Conn interface.

func (*Conn) ReceiveCommand

func (c *Conn) ReceiveCommand() (Command, error)

ReceiveCommand receives a command from the peer; it bocks until receives any command or an error occurs.

Possible commands are:

DataCommand: The peer called SendData
FileCommand: The peer called SendFile
TCPConnCommand: The peer called SendTCPConn

func (*Conn) ReceiveData

func (c *Conn) ReceiveData() ([]byte, error)

ReceiveData receives byte array from the peer. See also SendData.

func (*Conn) ReceiveDataLen

func (c *Conn) ReceiveDataLen() (int, error)

ReceiveDataLen receives data length from the peer.

You generally do not need to use this method; use ReceiveData.

func (*Conn) ReceiveFile

func (c *Conn) ReceiveFile() (*os.File, bool, error)

ReceiveFile receives a file handle from the peer.

The second return value indicate trailing data exists; call ReceiveData to receive it if it is true.

See also SendFile.

func (*Conn) ReceiveTCPConn

func (c *Conn) ReceiveTCPConn() (TCPConn, bool, error)

ReceiveTCPConn receives a TCP connection from the peer. The second return value indicate trailing data exists; call ReceiveData to receive it. See also SendTCPConn

func (*Conn) SendData

func (c *Conn) SendData(d []byte) error

SendData sends byte array to the peer. See also ReceiveData.

func (*Conn) SendFile

func (c *Conn) SendFile(f *os.File, msg []byte) error

SendFile passes the file handle to the peer. f is closed when the passing is succeeded but not if an error occurs.

msg is an additional information; it is not mandatory.

See also ReceiveFile.

func (*Conn) SendTCPConn

func (c *Conn) SendTCPConn(conn *net.TCPConn, peeked, msg []byte) error

SendTCPConn passes a TCP connection to the peer. conn is closed when the passing is succeeded but not if an error occurs.

peeked is data peeked from Conn; it will be injected to TCPConn in the peer. This is intended to implements reverse proxy like feature. Specify nil if there is no peeked data.

msg is an additional information. Specify nil if nothing.

See also ReceiveTCPConn.

func (*Conn) Write

func (c *Conn) Write(b []byte) (int, error)

Write implements the Write method in the net.Conn interface.

type Listener

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

Listener is a IPC listener; it implements net.Listener interface.

func Listen

func Listen(name string) (*Listener, error)

Listen announces on the pipe name.

func (*Listener) Accept

func (l *Listener) Accept() (*Conn, error)

Accept implements the Accept method in the net.Listener interface; it waits for the next call and return a Conn.

func (*Listener) Close

func (l *Listener) Close() error

Close implements the Close method in the net.Listener interface; it stop the listening.

type QueueListener

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

QueueListener is a Listener accept from queue; it implements net.Listener interface. TCPConn received from the IPC peer should be pushed to this.

Example
ql := ipc.NewQueueListener(1)
defer ql.Close()

go func() {
	for {
		func() {
			conn, err := ql.Accept()
			if err != nil {
				return
			}
			defer conn.Close()

			// read data from conn
		}()
	}
}()

conn, err := ipc.Dial("pipename")
if err != nil {
	return
}
defer conn.Close()

for {
	cmd, err := conn.ReceiveCommand()
	if err != nil {
		return
	}

	if cmd != ipc.TCPConnCommand {
		return
	}

	func() {
		tcpc, _, err := conn.ReceiveTCPConn()
		if err != nil {
			return
		}
		defer tcpc.Close()

		err = ql.Push(context.Background(), tcpc)
		if err != nil {
			return
		}
	}()
}
Output:

func NewQueueListener

func NewQueueListener(backlog uint) *QueueListener

NewQueueListener creates and initialize a new QueueListener.

The backlog is the capacity of the queue like listen(2). When the queue is filled with connections, Push is blocked until a connection is dequeued with Accept.

func (*QueueListener) Accept

func (l *QueueListener) Accept() (net.Conn, error)

Accept implements the Accept method in the net.Listener interface; it waits for the next push to the queue.

If the listener closed while waiting, Accept returns ErrClosedQueue.

func (*QueueListener) Addr

func (l *QueueListener) Addr() net.Addr

Addr returns the listener's network address.

func (*QueueListener) Close

func (l *QueueListener) Close() error

Close closes the listener. Any blocked Accept operations will be unblocked and return errors.

func (*QueueListener) Push

func (l *QueueListener) Push(ctx context.Context, c net.Conn) error

Push pushes a net.Conn to the queue.

The queue has capacity specified at the creation; Push blocks the call if there is no space.

type TCPConn

type TCPConn interface {
	net.Conn
	CloseRead() error
	CloseWrite() error
}

TCPConn is a TCP connection received from IPC peer.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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