-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for batch reads and kernel buffer sizing
Enables a single receiver to receive data from multiple pucks on the same port. Sizing the kernel buffer should hopefully mitigate packet loss.
- Loading branch information
Showing
13 changed files
with
193 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package vlp16 | ||
|
||
import ( | ||
"context" | ||
"net" | ||
|
||
"golang.org/x/net/ipv4" | ||
"golang.org/x/xerrors" | ||
) | ||
|
||
// ListenUDP listens for VLP-16 UDP packets on the specified address. | ||
func ListenUDP(ctx context.Context, addr string, receiverOpts ...ReceiverOption) (*Receiver, error) { | ||
opts := defaultReceiverOptions() | ||
for _, receiverOpt := range receiverOpts { | ||
receiverOpt(opts) | ||
} | ||
var listenConfig net.ListenConfig | ||
packetConn, err := listenConfig.ListenPacket(ctx, "udp4", addr) | ||
if err != nil { | ||
return nil, xerrors.Errorf("VLP-16: listen UDP: %w", err) | ||
} | ||
udpConn := packetConn.(*net.UDPConn) | ||
if err := udpConn.SetReadBuffer(opts.bufferSizeBytes); err != nil { | ||
return nil, xerrors.Errorf("VLP-16: listen UDP: %w", err) | ||
} | ||
conn := ipv4.NewPacketConn(udpConn) | ||
c := &Receiver{conn: conn} | ||
// allocate memory for batch reads | ||
c.messages = make([]ipv4.Message, 0, opts.batchSize) | ||
for i := 0; i < opts.batchSize; i++ { | ||
c.packetBuf = append(c.packetBuf, &[lengthOfPacket]byte{}) | ||
c.messages = append(c.messages, ipv4.Message{ | ||
Buffers: [][]byte{c.packetBuf[i][:]}, | ||
}) | ||
} | ||
return c, nil | ||
} | ||
|
||
// Receiver receives VLP-16 packets. | ||
type Receiver struct { | ||
conn *ipv4.PacketConn | ||
messages []ipv4.Message | ||
packetBuf []*[lengthOfPacket]byte | ||
messageBufSize int | ||
currMessageIndex int | ||
currPacket Packet | ||
currPointCloud PointCloud | ||
} | ||
|
||
// Receive a VLP-16 packet. | ||
func (c *Receiver) Receive(ctx context.Context) error { | ||
c.currMessageIndex++ | ||
if c.currMessageIndex >= c.messageBufSize { | ||
c.currMessageIndex = 0 | ||
deadline, _ := ctx.Deadline() | ||
if err := c.conn.SetReadDeadline(deadline); err != nil { | ||
return xerrors.Errorf("VLP-16 receiver: %w", err) | ||
} | ||
n, err := c.conn.ReadBatch(c.messages, 0) | ||
if err != nil { | ||
return xerrors.Errorf("VLP-16 receiver: %w", err) | ||
} | ||
c.messageBufSize = n | ||
} | ||
c.currPacket.unmarshal(c.packetBuf[c.currMessageIndex]) | ||
c.currPointCloud.UnmarshalPacket(&c.currPacket) | ||
return nil | ||
} | ||
|
||
// SourceIP returns the source IP of the last received VLP-16 packet. | ||
func (c *Receiver) SourceIP() net.IP { | ||
return c.messages[c.currMessageIndex].Addr.(*net.UDPAddr).IP | ||
} | ||
|
||
// RawPacket returns the raw bytes of the last received VLP-16 packet. | ||
func (c *Receiver) RawPacket() []byte { | ||
return c.packetBuf[c.currMessageIndex][:c.messages[c.currMessageIndex].N] | ||
} | ||
|
||
// Packet returns the last received VLP-16 packet. | ||
func (c *Receiver) Packet() *Packet { | ||
return &c.currPacket | ||
} | ||
|
||
// PointCloud returns the point cloud representation of the last received packet. | ||
func (c *Receiver) PointCloud() *PointCloud { | ||
return &c.currPointCloud | ||
} | ||
|
||
// Close the client's underlying UDP connection. | ||
func (c *Receiver) Close() error { | ||
if err := c.conn.Close(); err != nil { | ||
return xerrors.Errorf("VLP-16 receiver: close: %w", err) | ||
} | ||
return nil | ||
} |
Oops, something went wrong.