Documentation
¶
Overview ¶
Package npipe provides a pure Go wrapper around Windows named pipes.
See 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
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:
ln, err := npipe.Listen(`\\.\pipe\mypipename`)
if err != nil {
// handle error
}
for {
conn, err := ln.Accept()
if err != nil {
// handle error
continue
}
go handleConnection(conn)
}
Index ¶
- type PipeAddr
- type PipeConn
- func (c *PipeConn) Close() error
- func (c *PipeConn) LocalAddr() net.Addr
- func (c *PipeConn) Read(b []byte) (int, error)
- func (c *PipeConn) RemoteAddr() net.Addr
- func (c *PipeConn) SetDeadline(t time.Time) error
- func (c *PipeConn) SetReadDeadline(t time.Time) error
- func (c *PipeConn) SetWriteDeadline(t time.Time) error
- func (c *PipeConn) Write(b []byte) (int, error)
- type PipeError
- type PipeListener
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type PipeAddr ¶
type PipeAddr string
PipeAddr represents the address of a named pipe.
type PipeConn ¶
type PipeConn struct {
// contains filtered or unexported fields
}
PipeConn is the implementation of the net.Conn interface for named pipe connections.
func Dial ¶
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 \\<computer>\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`)
Example ¶
Use Dial to connect to a server and read messages from it.
package main
import (
"bufio"
"fmt"
"github.com/natefinch/npipe"
)
func main() {
conn, err := npipe.Dial(`\\.\pipe\mypipe`)
if err != nil {
// handle error
}
if _, err := fmt.Fprintln(conn, "Hi server!"); err != nil {
// handle error
}
r := bufio.NewReader(conn)
msg, err := r.ReadString('\n')
if err != nil {
// handle eror
}
fmt.Println(msg)
}
Output:
func DialTimeout ¶
DialTimeout acts like Dial, but will time out after the duration of timeout
func (*PipeConn) RemoteAddr ¶
RemoteAddr returns the remote network address.
func (*PipeConn) SetDeadline ¶
SetDeadline implements the net.Conn SetDeadline method. Note that timeouts are only supported on Windows Vista/Server 2008 and above
func (*PipeConn) SetReadDeadline ¶
SetReadDeadline implements the net.Conn SetReadDeadline method. Note that timeouts are only supported on Windows Vista/Server 2008 and above
func (*PipeConn) SetWriteDeadline ¶
SetWriteDeadline implements the net.Conn SetWriteDeadline method. Note that timeouts are only supported on Windows Vista/Server 2008 and above
type PipeError ¶
type PipeError struct {
// contains filtered or unexported fields
}
PipeError is an error related to a call to a pipe
type PipeListener ¶
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 ¶
func Listen(address string) (*PipeListener, error)
New 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.
Example ¶
Use Listen to start a server, and accept connections with Accept().
package main
import (
"bufio"
"fmt"
"github.com/natefinch/npipe"
"net"
)
func main() {
ln, err := npipe.Listen(`\\.\pipe\mypipe`)
if err != nil {
// handle error
}
for {
conn, err := ln.Accept()
if err != nil {
// handle error
continue
}
// handle connection like any other net.Conn
go func(conn net.Conn) {
r := bufio.NewReader(conn)
msg, err := r.ReadString('\n')
if err != nil {
// handle error
return
}
fmt.Println(msg)
}(conn)
}
}
Output:
func (*PipeListener) Accept ¶
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 (*PipeListener) AcceptPipe ¶
func (l *PipeListener) AcceptPipe() (*PipeConn, error)
AcceptPipe accepts the next incoming call and returns the new connection.
func (*PipeListener) Addr ¶
func (l *PipeListener) Addr() net.Addr
Addr returns the listener's network address, a PipeAddr.
func (*PipeListener) Close ¶
func (l *PipeListener) Close() error
Close stops listening on the address. Already Accepted connections are not closed.