Documentation ¶
Overview ¶
Package unet provides a minimal net package based on Unix Domain Sockets.
This does no pooling, and should only be used for a limited number of connections in a Go process. Don't use this package for arbitrary servers.
Index ¶
- func SocketPair(packet bool) (*Socket, *Socket, error)
- type ControlMessage
- type ServerSocket
- type Socket
- func (s *Socket) Close() error
- func (s *Socket) FD() int
- func (s *Socket) GetPeerName() ([]byte, error)
- func (s *Socket) GetSockName() ([]byte, error)
- func (s *Socket) GetSockOpt(level int, name int, b []byte) (uint32, error)
- func (s *Socket) Read(p []byte) (int, error)
- func (s *Socket) Reader(blocking bool) SocketReader
- func (s *Socket) Release() (int, error)
- func (s *Socket) SetSockOpt(level, name int, b []byte) error
- func (s *Socket) Shutdown() error
- func (s *Socket) Write(p []byte) (int, error)
- func (s *Socket) Writer(blocking bool) SocketWriter
- type SocketReader
- type SocketWriter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type ControlMessage ¶
type ControlMessage []byte
ControlMessage wraps around a byte array and provides functions for parsing as a Unix Domain Socket control message.
func (*ControlMessage) CloseFDs ¶
func (c *ControlMessage) CloseFDs()
CloseFDs closes the list of FDs in the control message.
Either this or ExtractFDs should be used after EnableFDs.
func (*ControlMessage) EnableFDs ¶
func (c *ControlMessage) EnableFDs(count int)
EnableFDs enables receiving FDs via control message.
This guarantees only a MINIMUM number of FDs received. You may receive MORE than this due to the way FDs are packed. To be specific, the number of receivable buffers will be rounded up to the nearest even number.
This must be called prior to ReadVec if you want to receive FDs.
func (*ControlMessage) ExtractFDs ¶
func (c *ControlMessage) ExtractFDs() ([]int, error)
ExtractFDs returns the list of FDs in the control message.
Either this or CloseFDs should be used after EnableFDs.
func (*ControlMessage) PackFDs ¶
func (c *ControlMessage) PackFDs(fds ...int)
PackFDs packs the given list of FDs in the control message.
This must be used prior to WriteVec.
func (*ControlMessage) UnpackFDs ¶
func (c *ControlMessage) UnpackFDs()
UnpackFDs clears the control message.
type ServerSocket ¶
type ServerSocket struct {
// contains filtered or unexported fields
}
ServerSocket is a bound unix domain socket.
func Bind ¶
func Bind(addr string, packet bool) (*ServerSocket, error)
Bind creates and binds a new socket.
func BindAndListen ¶
func BindAndListen(addr string, packet bool) (*ServerSocket, error)
BindAndListen creates, binds and listens on a new socket.
func NewServerSocket ¶
func NewServerSocket(fd int) (*ServerSocket, error)
NewServerSocket returns a socket from an existing FD.
func (*ServerSocket) Accept ¶
func (s *ServerSocket) Accept() (*Socket, error)
Accept accepts a new connection.
This is always blocking.
Preconditions:
- ServerSocket is listening (Listen called).
func (*ServerSocket) Close ¶
func (s *ServerSocket) Close() error
Close closes the server socket.
This must only be called once.
func (*ServerSocket) FD ¶
func (s *ServerSocket) FD() int
FD returns the socket's file descriptor.
See Socket.FD.
func (*ServerSocket) Listen ¶
func (s *ServerSocket) Listen() error
Listen starts listening on the socket.
func (*ServerSocket) Release ¶
func (s *ServerSocket) Release() (int, error)
Release releases ownership of the socket's file descriptor.
See Socket.Release.
type Socket ¶
type Socket struct {
// contains filtered or unexported fields
}
Socket is a connected unix domain socket.
func (*Socket) FD ¶
FD returns the FD for this Socket.
The FD is non-blocking and must not be made blocking.
N.B. os.File.Fd makes the FD blocking. Use of Release instead of FD is strongly preferred.
The returned FD cannot be used safely if there may be concurrent callers to Close or Release.
Use Release to take ownership of the FD.
func (*Socket) GetPeerName ¶
GetPeerName returns the peer name.
func (*Socket) GetSockName ¶
GetSockName returns the socket name.
func (*Socket) GetSockOpt ¶
GetSockOpt gets the given socket option.
func (*Socket) Reader ¶
func (s *Socket) Reader(blocking bool) SocketReader
Reader returns a reader for this socket.
func (*Socket) Release ¶
Release releases ownership of the socket FD.
The returned FD is non-blocking.
Any concurrent or future callers of Socket methods will receive EBADF.
func (*Socket) SetSockOpt ¶
SetSockOpt sets the given socket option.
func (*Socket) Writer ¶
func (s *Socket) Writer(blocking bool) SocketWriter
Writer returns a writer for this socket.
type SocketReader ¶
type SocketReader struct { ControlMessage // contains filtered or unexported fields }
SocketReader wraps an individual receive operation.
This may be used for doing vectorized reads and/or sending additional control messages (e.g. FDs). The normal entrypoint is ReadVec.
One of ExtractFDs or DisposeFDs must be called if EnableFDs is used.
func (*SocketReader) ReadVec ¶
func (r *SocketReader) ReadVec(bufs [][]byte) (int, error)
ReadVec reads into the pre-allocated bufs. Returns bytes read.
The pre-allocatted space used by ReadVec is based upon slice lengths.
This function is not guaranteed to read all available data, it returns as soon as a single recvmsg call succeeds.
type SocketWriter ¶
type SocketWriter struct { ControlMessage // contains filtered or unexported fields }
SocketWriter wraps an individual send operation.
The normal entrypoint is WriteVec.