testing/reludp/socket.go

62 lines
1.9 KiB
Go

package main
import "net"
// ID is our general type for IDs.
type ID = uint16
// ReliableConn represents a UDP connection to an associated UDPAddr.
type ReliableConn struct {
addr *net.UDPAddr // Associated UDP addr
lastID ID // Last ID sent
awaitingQueue []ReliablePacket // Queue of sent packets awaiting a response.
outboundQueue []ReliablePacket // Queue of packets awaiting to be sent.
inboundQueue []ReliablePacket // Queue of packets received.
inbound []byte // Inbound byte buffer.
}
// ReliablePacketType is the 8-bit value representing the packet's type.
type ReliablePacketType uint8
const (
// TypeEmpty represents a packet with no payload data. Generally useful for keepalives and NAT bypass.
TypeEmpty ReliablePacketType = iota
// TypeMessage represents a packet that contains an entire message.
TypeMessage
// TypeMessageBegin represents a packet that contains the beginning of a message.
TypeMessageBegin
// TypeMessagePart represents a packet that contains the part of a message.
TypeMessagePart
// TypeMessageEnd represents a packet that contains the end of a message.
TypeMessageEnd
)
// ReliablePacket represents our multiple ReliablePacket types.
type ReliablePacket interface {
Type() ReliablePacketType
Size() uint16 // Size is the total size of the packet in bytes.
ID() ID // The ID associated with this packet.
//PayloadSize() // PayloadSize is the total payload size.
//Payload() []byte // Payload returns the byte list of the payload.
}
// EmptyPacket represents an empty reliable packet.
type EmptyPacket struct {
id ID
}
// Type returns TypeEmpty
func (p *EmptyPacket) Type() ReliablePacketType {
return TypeEmpty
}
// Size returns the total size of the packet in bytes.
func (p *EmptyPacket) Size() uint16 {
return 1 + 2 // Type + ID
}
// ID returns the ID of the packet.
func (p *EmptyPacket) ID() ID {
return p.id
}