Skip to content

Commit

Permalink
feat(pcap): impl realtime wait() with time offset
Browse files Browse the repository at this point in the history
  • Loading branch information
haruue committed May 8, 2024
1 parent 301f9af commit 87e69c5
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions io/pcap.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ import (
var _ PacketIO = (*pcapPacketIO)(nil)

type pcapPacketIO struct {
pcapFile io.ReadCloser
pcap *pcapgo.Reader
lastTime *time.Time
ioCancel context.CancelFunc
config PcapPacketIOConfig
pcapFile io.ReadCloser
pcap *pcapgo.Reader
timeOffset *time.Duration
ioCancel context.CancelFunc
config PcapPacketIOConfig

dialer *net.Dialer
}
Expand All @@ -43,12 +43,12 @@ func NewPcapPacketIO(config PcapPacketIOConfig) (PacketIO, error) {
}

return &pcapPacketIO{
pcapFile: pcapFile,
pcap: handle,
lastTime: nil,
ioCancel: nil,
config: config,
dialer: &net.Dialer{},
pcapFile: pcapFile,
pcap: handle,
timeOffset: nil,
ioCancel: nil,
config: config,
dialer: &net.Dialer{},
}, nil
}

Expand Down Expand Up @@ -101,20 +101,20 @@ func (p *pcapPacketIO) Close() error {

// Intentionally slow down the replay
// In realtime mode, this is to match the timestamps in the capture
func (p *pcapPacketIO) wait(packet gopacket.Packet) error {
func (p *pcapPacketIO) wait(packet gopacket.Packet) {
if !p.config.Realtime {
return nil
return
}

if p.lastTime == nil {
p.lastTime = &packet.Metadata().Timestamp
if p.timeOffset == nil {
offset := time.Since(packet.Metadata().Timestamp)
p.timeOffset = &offset
} else {
t := packet.Metadata().Timestamp.Sub(*p.lastTime)
t := time.Until(packet.Metadata().Timestamp.Add(*p.timeOffset))
time.Sleep(t)
p.lastTime = &packet.Metadata().Timestamp
}

return nil
return

Check failure on line 117 in io/pcap.go

View workflow job for this annotation

GitHub Actions / Static analysis

redundant return statement (S1023)

Check failure on line 117 in io/pcap.go

View workflow job for this annotation

GitHub Actions / Static analysis

redundant return statement (S1023)
}

var _ Packet = (*pcapPacket)(nil)
Expand Down

0 comments on commit 87e69c5

Please sign in to comment.