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 ¶
- Variables
- type Command
- type Conn
- func (c *Conn) Close() error
- func (c *Conn) Read(b []byte) (int, error)
- func (c *Conn) ReceiveCommand() (Command, error)
- func (c *Conn) ReceiveData() ([]byte, error)
- func (c *Conn) ReceiveDataLen() (int, error)
- func (c *Conn) ReceiveFile() (*os.File, bool, error)
- func (c *Conn) ReceiveTCPConn() (TCPConn, bool, error)
- func (c *Conn) SendData(d []byte) error
- func (c *Conn) SendFile(f *os.File, msg []byte) error
- func (c *Conn) SendTCPConn(conn *net.TCPConn, peeked, msg []byte) error
- func (c *Conn) Write(b []byte) (int, error)
- type Listener
- type QueueListener
- type TCPConn
Examples ¶
Constants ¶
This section is empty.
Variables ¶
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 Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
Conn is a IPC connection; it implements net.Conn interface.
func (*Conn) Close ¶
Close implements the Close method in the net.Listener interface; it close the connection.
func (*Conn) ReceiveCommand ¶
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 ¶
ReceiveData receives byte array from the peer. See also SendData.
func (*Conn) ReceiveDataLen ¶
ReceiveDataLen receives data length from the peer.
You generally do not need to use this method; use ReceiveData.
func (*Conn) ReceiveFile ¶
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 ¶
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) SendFile ¶
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 ¶
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.
type Listener ¶
type Listener struct {
// contains filtered or unexported fields
}
Listener is a IPC listener; it implements net.Listener interface.
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.