unet

package
v0.0.0-...-0f8216c Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2024 License: Apache-2.0, MIT Imports: 7 Imported by: 45

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func SocketPair

func SocketPair(packet bool) (*Socket, *Socket, error)

SocketPair creates a pair of connected sockets.

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 Connect

func Connect(addr string, packet bool) (*Socket, error)

Connect connects to a server.

func NewSocket

func NewSocket(fd int) (*Socket, error)

NewSocket returns a socket from an existing FD.

NewSocket takes ownership of fd.

func (*Socket) Close

func (s *Socket) Close() error

Close closes the socket.

func (*Socket) FD

func (s *Socket) FD() int

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

func (s *Socket) GetPeerName() ([]byte, error)

GetPeerName returns the peer name.

func (*Socket) GetSockName

func (s *Socket) GetSockName() ([]byte, error)

GetSockName returns the socket name.

func (*Socket) GetSockOpt

func (s *Socket) GetSockOpt(level int, name int, b []byte) (uint32, error)

GetSockOpt gets the given socket option.

func (*Socket) Read

func (s *Socket) Read(p []byte) (int, error)

Read implements io.Reader.Read.

func (*Socket) Reader

func (s *Socket) Reader(blocking bool) SocketReader

Reader returns a reader for this socket.

func (*Socket) Release

func (s *Socket) Release() (int, error)

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

func (s *Socket) SetSockOpt(level, name int, b []byte) error

SetSockOpt sets the given socket option.

func (*Socket) Shutdown

func (s *Socket) Shutdown() error

Shutdown closes the socket for read and write.

func (*Socket) Write

func (s *Socket) Write(p []byte) (int, error)

Write implements io.Writer.Write.

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.

func (*SocketWriter) WriteVec

func (w *SocketWriter) WriteVec(bufs [][]byte) (int, error)

WriteVec writes the bufs to the socket. Returns bytes written.

This function is not guaranteed to send all data, it returns as soon as a single sendmsg call succeeds.

Jump to

Keyboard shortcuts

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