Documentation
¶
Index ¶
- Constants
- Variables
- func RegisterPacketKindName(kind PacketKind, name string)
- type Conn
- func (c *Conn) Close()
- func (c *Conn) Done() <-chan struct{}
- func (c *Conn) Err() error
- func (c *Conn) LocalAddr() net.Addr
- func (c *Conn) OnReceive(kind PacketKind, handler func(packet Packet))
- func (c *Conn) OnReceiveUnexpected(handler func(packet Packet))
- func (c *Conn) Receive(kind PacketKind, buf []byte) ...
- func (c *Conn) RemoteAddr() net.Addr
- func (c *Conn) Send(packet Packet) <-chan error
- func (c *Conn) SendContext(ctx context.Context, packet Packet) <-chan error
- type OptFunc
- func WithConnectionRefusedCooldown(cooldown time.Duration) OptFunc
- func WithLocalAddr(localAddr string) OptFunc
- func WithPendingAckQueueFullBehavior(behavior string) OptFunc
- func WithPendingAckQueueSize(size int) OptFunc
- func WithReceiveAckTimeout(timeout time.Duration) OptFunc
- func WithReceiveBufferSize(size int) OptFunc
- func WithResendTries(tries int32) OptFunc
- func WithReuseAddress() OptFunc
- func WithSendQueueFullBehavior(behavior string) OptFunc
- func WithSendQueueSize(size int) OptFunc
- type Packet
- type PacketBuilder
- func (p PacketBuilder) Build() Packet
- func (p *PacketBuilder) Reset()
- func (p *PacketBuilder) WriteAngle(v float32)
- func (p *PacketBuilder) WriteBool(v bool)
- func (p *PacketBuilder) WriteBytes(b []byte)
- func (p *PacketBuilder) WriteFloat(v float64)
- func (p *PacketBuilder) WriteFloat32(v float32)
- func (p *PacketBuilder) WriteFloat64(v float64)
- func (p *PacketBuilder) WriteInt(v int)
- func (p *PacketBuilder) WriteInt16(v int16)
- func (p *PacketBuilder) WriteInt32(v int32)
- func (p *PacketBuilder) WriteInt64(v int64)
- func (p *PacketBuilder) WriteInt8(v int8)
- func (p *PacketBuilder) WriteRune(v rune)
- func (p *PacketBuilder) WriteString(s string)
- func (p *PacketBuilder) WriteUint(v uint)
- func (p *PacketBuilder) WriteUint16(v uint16)
- func (p *PacketBuilder) WriteUint32(v uint32)
- func (p *PacketBuilder) WriteUint64(v uint64)
- func (p *PacketBuilder) WriteUint8(v uint8)
- type PacketFlag
- type PacketID
- type PacketKind
- type PacketParser
- func (p *PacketParser) Err() error
- func (p *PacketParser) GetBytes(n int) []byte
- func (p *PacketParser) ParseAngle() float32
- func (p *PacketParser) ParseBool() bool
- func (p *PacketParser) ParseBytes() []byte
- func (p *PacketParser) ParseFloat() float64
- func (p *PacketParser) ParseFloat32() float32
- func (p *PacketParser) ParseFloat64() float64
- func (p *PacketParser) ParseInt() int
- func (p *PacketParser) ParseInt16() int16
- func (p *PacketParser) ParseInt32() int32
- func (p *PacketParser) ParseInt64() int64
- func (p *PacketParser) ParseInt8() int8
- func (p *PacketParser) ParseRune() rune
- func (p *PacketParser) ParseString() string
- func (p *PacketParser) ParseUint() uint
- func (p *PacketParser) ParseUint16() uint16
- func (p *PacketParser) ParseUint32() uint32
- func (p *PacketParser) ParseUint64() uint64
- func (p *PacketParser) ParseUint8() uint8
- func (p *PacketParser) SetErr(err error)
- type PacketPool
- type PendingAck
Constants ¶
const ( Version = 0 MaxPacketSize = 4096 )
const DefaultPort = 42069
const HeaderSize = sVersion + sKind + sFlags + sID
Variables ¶
var DefaultOptions = connOptions{ LocalAddr: fmt.Sprintf("%s:%d", getPrivateIP(), DefaultPort), SendQueueSize: 64, SendQueueFullBehavior: queueBlock, ResendTries: 5, PendingAckQueueSize: 64 * 5, PendingAckQueueFullBehavior: queuePanic, ReceiveAckTimeout: 100 * time.Millisecond, ReceiveBufferSize: 64, ConnectionRefusedCooldown: 1 * time.Second, ReuseAddress: false, }
var ErrPeerClosed = errors.New("connection closed by peer")
var ErrPendingAckQueueFull = errors.New("pending acknowledgment queue full")
var ErrSendQueueFull = errors.New("send queue full")
Functions ¶
func RegisterPacketKindName ¶
func RegisterPacketKindName(kind PacketKind, name string)
RegisterPacketKindName sets the name for a packet kind to be retrieved with PacketKind.String().
This function should only be used for packets without the //p2p:packet directive. Panics if the packet kind was already registered.
Types ¶
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
Conn is a peer to peer network connection. Multiple goroutines may invoke methods on a Conn simultaneously.
func Dial ¶
Dial connects to the given address.
It waits for the connection to be established. The connection is established after sending packets and receiving an acknowledgment from the other peer. If the address does not have a port, DefaultPort will be used.
You can see the default options in DefaultOptions.
func (*Conn) Close ¶
func (c *Conn) Close()
Close signals the connection to end. Before closing the connection it tries to signal the other peer that its closing the connection. There is no guarantee that the other peer will receive the closing signal, especially if it is due to an error.
Panics if called when the connection is not established yet. It is safe to call it from multiple goroutines.
func (*Conn) Done ¶
func (c *Conn) Done() <-chan struct{}
Done returns a channel that is closed when the connection has finished closing and cleaning.
It is safe to call it from multiple goroutines.
func (*Conn) Err ¶
Err returns the reason why the connection ended.
If the other peer sends a closing signal, the error will be ErrPeerClosed.
It is safe to call it from multiple goroutines.
func (*Conn) OnReceive ¶
func (c *Conn) OnReceive(kind PacketKind, handler func(packet Packet))
OnReceive registers a packet handler.
The given handler will be called with the received packet matching the packet kind. The passed packet should only live within the scope of the handler function; if you want to use it outside, you should allocate a copy.
Packets are processed serially, and the receiving process is paused in the meantime, so the handlers should return as soon as possible. It is safe to call it from multiple goroutines.
func (*Conn) OnReceiveUnexpected ¶
OnReceiveUnexpected registers a packet handler for received packets that do not have a registered handler.
The given handler will be called with the received packet that does not match any registered packet kind. The passed packet should only live within the scope of the handler function; if you want to use it outside, you should allocate a copy.
Packets are processed serially, and the receiving process is paused in the meantime, so the handlers should return as soon as possible. It is safe to call it from multiple goroutines.
func (*Conn) Receive ¶
func (c *Conn) Receive(kind PacketKind, buf []byte) <-chan struct { Packet Packet Err error }
Receive waits to receive a packet that matches the given packet kind.
The received packet is written to buf and delivered over the returned channel.
It is safe to call it from multiple goroutines.
func (*Conn) RemoteAddr ¶
RemoteAddr returns the remote network address, if known.
type OptFunc ¶
type OptFunc func(*connOptions)
func WithConnectionRefusedCooldown ¶
Time between failed connection attempts.
func WithLocalAddr ¶
Change local address. If IP is missing, the machine's private IP is used If port is missing, DefaultPort is used.
func WithPendingAckQueueFullBehavior ¶
What should happen when the pending ack queue is full.
Possible values are:
"error": return an error. "panic": well... panic.
func WithPendingAckQueueSize ¶
The number of packets that can be queued waiting for an acknowledgment before it becomes full.
func WithReceiveAckTimeout ¶
Time to wait for an acknowledgment before attempting to resend the packet.
func WithReceiveBufferSize ¶
Buffer size to store packets received to early (to maintain order).
func WithResendTries ¶
Number of attempts to receive acknowledgment before disconnecting. Use -1 to disable retries.
func WithReuseAddress ¶
func WithReuseAddress() OptFunc
Sets syscall.SO_REUSEADDR on the connection socket.
func WithSendQueueFullBehavior ¶
What should happen when the send queue is full.
Possible values are:
"block": block until it is not full. "discard": discard the packet. "error": return an error. "panic": well... panic.
func WithSendQueueSize ¶
The number of packets that can be queued for sending before it becomes full.
type Packet ¶
type Packet []byte
| version | kind | flags | id | payload |
func (Packet) Flags ¶
func (p Packet) Flags() PacketFlag
func (Packet) HasFlags ¶
func (p Packet) HasFlags(flags PacketFlag) bool
func (Packet) Kind ¶
func (p Packet) Kind() PacketKind
type PacketBuilder ¶
type PacketBuilder struct {
// contains filtered or unexported fields
}
A PacketBuilder helps to build a packet payload by keeping track of the write offset.
func NewPacketBuilder ¶
func NewPacketBuilder(buf []byte, kind PacketKind, flags PacketFlag) PacketBuilder
NewPacketBuilder creates a new PacketBuilder and initializes the packet header.
func (PacketBuilder) Build ¶
func (p PacketBuilder) Build() Packet
Build returns the built packet.
Panics if the written payload is larger than MaxPacketSize.
func (*PacketBuilder) Reset ¶
func (p *PacketBuilder) Reset()
Reset deletes the payload. The header is left untouched.
func (*PacketBuilder) WriteAngle ¶
func (p *PacketBuilder) WriteAngle(v float32)
WriteAngle writes an angle value to the payload.
An angle is 2 bytes long and has 3 decimal places of precision. Allowed angle range:
[-PI*2, PI*2]
func (*PacketBuilder) WriteBool ¶
func (p *PacketBuilder) WriteBool(v bool)
WriteBool writes a bool value to the payload.
func (*PacketBuilder) WriteBytes ¶
func (p *PacketBuilder) WriteBytes(b []byte)
WriteBytes writes a []byte value to the payload.
func (*PacketBuilder) WriteFloat ¶
func (p *PacketBuilder) WriteFloat(v float64)
WriteFloat writes a float value to the payload.
func (*PacketBuilder) WriteFloat32 ¶
func (p *PacketBuilder) WriteFloat32(v float32)
WriteFloat32 writes a float32 value to the payload.
func (*PacketBuilder) WriteFloat64 ¶
func (p *PacketBuilder) WriteFloat64(v float64)
WriteFloat64 writes a float64 value to the payload.
func (*PacketBuilder) WriteInt ¶
func (p *PacketBuilder) WriteInt(v int)
WriteInt writes a int value to the payload.
func (*PacketBuilder) WriteInt16 ¶
func (p *PacketBuilder) WriteInt16(v int16)
WriteInt16 writes a int16 value to the payload.
func (*PacketBuilder) WriteInt32 ¶
func (p *PacketBuilder) WriteInt32(v int32)
WriteInt32 writes a int32 value to the payload.
func (*PacketBuilder) WriteInt64 ¶
func (p *PacketBuilder) WriteInt64(v int64)
WriteInt64 writes a int64 value to the payload.
func (*PacketBuilder) WriteInt8 ¶
func (p *PacketBuilder) WriteInt8(v int8)
WriteInt8 writes a int8 value to the payload.
func (*PacketBuilder) WriteRune ¶
func (p *PacketBuilder) WriteRune(v rune)
WriteRune writes a rune value to the payload.
func (*PacketBuilder) WriteString ¶
func (p *PacketBuilder) WriteString(s string)
WriteString writes a string value to the payload.
func (*PacketBuilder) WriteUint ¶
func (p *PacketBuilder) WriteUint(v uint)
WriteUint writes a uint value to the payload.
func (*PacketBuilder) WriteUint16 ¶
func (p *PacketBuilder) WriteUint16(v uint16)
WriteUint16 writes a uint16 value to the payload.
func (*PacketBuilder) WriteUint32 ¶
func (p *PacketBuilder) WriteUint32(v uint32)
WriteUint32 writes a uint32 value to the payload.
func (*PacketBuilder) WriteUint64 ¶
func (p *PacketBuilder) WriteUint64(v uint64)
WriteUint64 writes a uint64 value to the payload.
func (*PacketBuilder) WriteUint8 ¶
func (p *PacketBuilder) WriteUint8(v uint8)
WriteUint8 writes a uint8 value to the payload.
type PacketFlag ¶
type PacketFlag byte
const ( FlagNone PacketFlag = 1 << iota >> 1 // Indicates that the receiver needs to send an acknowledgment. FlagNeedAck )
type PacketKind ¶
type PacketKind uint16
const ( PacketInvalid PacketKind = iota // Represents and acknowledgment. PacketAck // Tells the receiving peer to close the connection. PacketReset PacketPing // Used to keep a "hole" open in the peer's NAT. PacketKeepAlive // Used to register new packet kinds with iota. // It is not a valid packet kind in itself. // //Usage: // // const ( // PacketA = p2p.PacketCustom + iota // PacketB // PacketC // ... // ) PacketCustom )
func (PacketKind) String ¶
func (k PacketKind) String() string
type PacketParser ¶
type PacketParser struct {
// contains filtered or unexported fields
}
A PacketParser helps parse a packet by keeping track of the read offset and delaying any errors until the end.
func NewPacketParser ¶
func NewPacketParser(packet Packet) PacketParser
NewPacketParser creates a new PacketParser and performs some validations.
func (*PacketParser) GetBytes ¶
func (p *PacketParser) GetBytes(n int) []byte
GetBytes returns the next n bytes from payload.
func (*PacketParser) ParseAngle ¶
func (p *PacketParser) ParseAngle() float32
ParseAngle parses an angle value from the payload.
An angle is 2 bytes long and has 3 decimal places of precision. Allowed angle range:
[-PI*2, PI*2]
func (*PacketParser) ParseBool ¶
func (p *PacketParser) ParseBool() bool
ParseBool parses a bool value from the payload.
func (*PacketParser) ParseBytes ¶
func (p *PacketParser) ParseBytes() []byte
ParseBytes parses a []byte value from the payload.
This does not make a copy. Sounds goofy ngl
func (*PacketParser) ParseFloat ¶
func (p *PacketParser) ParseFloat() float64
ParseFloat parses a float value from the payload.
func (*PacketParser) ParseFloat32 ¶
func (p *PacketParser) ParseFloat32() float32
ParseFloat32 parses a float32 value from the payload.
func (*PacketParser) ParseFloat64 ¶
func (p *PacketParser) ParseFloat64() float64
ParseFloat64 parses a float64 value from the payload.
func (*PacketParser) ParseInt ¶
func (p *PacketParser) ParseInt() int
ParseInt parses a int value from the payload.
func (*PacketParser) ParseInt16 ¶
func (p *PacketParser) ParseInt16() int16
ParseInt16 parses a int16 value from the payload.
func (*PacketParser) ParseInt32 ¶
func (p *PacketParser) ParseInt32() int32
ParseInt32 parses a int32 value from the payload.
func (*PacketParser) ParseInt64 ¶
func (p *PacketParser) ParseInt64() int64
ParseInt64 parses a int64 value from the payload.
func (*PacketParser) ParseInt8 ¶
func (p *PacketParser) ParseInt8() int8
ParseInt8 parses a int8 value from the payload.
func (*PacketParser) ParseRune ¶
func (p *PacketParser) ParseRune() rune
ParseRune parses a rune value from the payload.
func (*PacketParser) ParseString ¶
func (p *PacketParser) ParseString() string
ParseString parses a string value from the payload.
func (*PacketParser) ParseUint ¶
func (p *PacketParser) ParseUint() uint
ParseUint parses a uint value from the payload.
func (*PacketParser) ParseUint16 ¶
func (p *PacketParser) ParseUint16() uint16
ParseUint16 parses a uint16 value from the payload.
func (*PacketParser) ParseUint32 ¶
func (p *PacketParser) ParseUint32() uint32
ParseUint32 parses a uint32 value from the payload.
func (*PacketParser) ParseUint64 ¶
func (p *PacketParser) ParseUint64() uint64
ParseUint64 parses a uint64 value from the payload.
func (*PacketParser) ParseUint8 ¶
func (p *PacketParser) ParseUint8() uint8
ParseUint8 parses a uint8 value from the payload.
func (*PacketParser) SetErr ¶
func (p *PacketParser) SetErr(err error)
SetErr sets an error in the parser.
Only the first call will set an error.
type PacketPool ¶
type PacketPool struct {
// contains filtered or unexported fields
}
A PacketPool is a thread-safe pool of Packet.
func (*PacketPool) Clone ¶
func (p *PacketPool) Clone(packet Packet) Packet
Clone returns a packet from the pool with the content of the given packet.
func (*PacketPool) Get ¶
func (p *PacketPool) Get(size int) Packet
Get returns a packet from the pool with at least the given size. If it does not find one, it allocates it.
Panics if size > MaxPacketSize.
func (*PacketPool) Put ¶
func (p *PacketPool) Put(packets ...Packet)
Put returns packets to the pool.
type PendingAck ¶
type PendingAck struct {
// contains filtered or unexported fields
}