Package npipe provides a pure Go wrapper around Windows named pipes.
Windows named pipe documentation: http://msdn.microsoft.com/en-us/library/windows/desktop/aa365780
npipe provides an interface based on stdlib's net package, with Dial, Listen, and Accept functions, as well as associated implementations of net.Conn and net.Listener. It supports rpc over the connection.
-
Deadlines for reading/writing to the connection are only functional in Windows Vista/Server 2008 and above, due to limitations with the Windows API.
-
The pipes support byte mode only (no support for message mode)
The Dial function connects a client to a named pipe:
conn, err := npipe.Dial(`\\.\pipe\mypipename`)
if err != nil {
<handle error>
}
fmt.Fprintf(conn, "Hi server!\n")
msg, err := bufio.NewReader(conn).ReadString('\n')
...
The Listen function creates servers:
maxInstances := 1 // maximum number of concurrently existing pipes with the same name
ln, err := npipe.Listen(`\\.\pipe\mypipename`, maxInstances)
if err != nil {
// handle error
}
for {
conn, err := ln.Accept()
if err != nil {
// handle error
continue
}
go handleConnection(conn)
}
var ErrClosed = PipeError{"Pipe has been closed.", false}
ErrClosed is the error returned by PipeListener.Accept when Close is called on the PipeListener.
type PipeAddr string
PipeAddr represents the address of a named pipe.
func (a PipeAddr) Network() string
Network returns the address's network name, "pipe".
func (a PipeAddr) String() string
String returns the address of the pipe
type PipeConn struct {
// contains filtered or unexported fields
}
PipeConn is the implementation of the net.Conn interface for named pipe connections.
func Dial(address string) (*PipeConn, error)
Dial connects to a named pipe with the given address. If the specified pipe is not available, it will wait indefinitely for the pipe to become available.
The address must be of the form \.\pipe<name> for local pipes and \\pipe<name> for remote pipes.
Dial will return a PipeError if you pass in a badly formatted pipe name.
Examples:
// local pipe
conn, err := Dial(`\\.\pipe\mypipename`)
// remote pipe
conn, err := Dial(`\\othercomp\pipe\mypipename`)
func DialTimeout(address string, timeout time.Duration) (*PipeConn, error)
DialTimeout acts like Dial, but will time out after the duration of timeout
func (c *PipeConn) Close() error
Close closes the connection.
func (c *PipeConn) LocalAddr() net.Addr
LocalAddr returns the local network address.
func (c *PipeConn) Read(b []byte) (int, error)
Read implements the net.Conn Read method.
func (c *PipeConn) RemoteAddr() net.Addr
RemoteAddr returns the remote network address.
func (c *PipeConn) SetDeadline(t time.Time) error
SetDeadline implements the net.Conn SetDeadline method. Note that timeouts are only supported on Windows Vista/Server 2008 and above
func (c *PipeConn) SetReadDeadline(t time.Time) error
SetReadDeadline implements the net.Conn SetReadDeadline method. Note that timeouts are only supported on Windows Vista/Server 2008 and above
func (c *PipeConn) SetWriteDeadline(t time.Time) error
SetWriteDeadline implements the net.Conn SetWriteDeadline method. Note that timeouts are only supported on Windows Vista/Server 2008 and above
func (c *PipeConn) Write(b []byte) (int, error)
Write implements the net.Conn Write method.
type PipeError struct {
// contains filtered or unexported fields
}
PipeError is an error related to a call to a pipe
func (e PipeError) Error() string
Error implements the error interface
func (e PipeError) Temporary() bool
Temporary implements net.AddrError.Temporary()
func (e PipeError) Timeout() bool
Timeout implements net.AddrError.Timeout()
type PipeListener struct {
// contains filtered or unexported fields
}
PipeListener is a named pipe listener. Clients should typically use variables of type net.Listener instead of assuming named pipe.
func Listen(address string, maxInstances uint32) (*PipeListener, error)
Listen returns a new PipeListener that will listen on a pipe with the given address. The address must be of the form \.\pipe<name>
Listen will return a PipeError for an incorrectly formatted pipe name.
func (l *PipeListener) Accept() (net.Conn, error)
Accept implements the Accept method in the net.Listener interface; it waits for the next call and returns a generic net.Conn.
func (l *PipeListener) AcceptPipe() (*PipeConn, error)
AcceptPipe accepts the next incoming call and returns the new connection.
func (l *PipeListener) Addr() net.Addr
Addr returns the listener's network address, a PipeAddr.
func (l *PipeListener) Close() error
Close stops listening on the address. Already Accepted connections are not closed.