diff --git a/client_test.go b/client_test.go index e848e28..f442344 100644 --- a/client_test.go +++ b/client_test.go @@ -267,13 +267,15 @@ func TestUDPEmulator(t *testing.T) { addrEmulator := "127.0.0.1:24001" addrClient := "127.0.0.1:24002" - connEmulator, err := xsensemulator.NewUDPSerialPort(addrEmulator, addrClient) + timeout := 100 * time.Millisecond + + connEmulator, err := xsensemulator.NewUDPSerialPort(addrEmulator, addrClient, timeout) assert.NilError(t, err) defer func() { assert.NilError(t, connEmulator.Close()) }() - connClient, err := xsensemulator.NewUDPSerialPort(addrClient, addrEmulator) + connClient, err := xsensemulator.NewUDPSerialPort(addrClient, addrEmulator, timeout) assert.NilError(t, err) emu := xsensemulator.NewEmulator(connEmulator) @@ -294,9 +296,6 @@ func TestUDPEmulator(t *testing.T) { var g errgroup.Group g.Go(func() error { - assert.NilError(t, connEmulator.SetReadDeadline(deadline)) - assert.NilError(t, connEmulator.SetWriteDeadline(deadline)) - err := emu.Receive(ctx) if !strings.Contains(err.Error(), "timeout") { return err @@ -305,9 +304,6 @@ func TestUDPEmulator(t *testing.T) { }) g.Go(func() error { - assert.NilError(t, connClient.SetReadDeadline(deadline)) - assert.NilError(t, connClient.SetWriteDeadline(deadline)) - client := xsens.NewClient(connClient) assert.NilError(t, client.SetOutputConfiguration(ctx, outputConf)) diff --git a/xsensemulator/emulator.go b/xsensemulator/emulator.go index c0ff951..632e779 100644 --- a/xsensemulator/emulator.go +++ b/xsensemulator/emulator.go @@ -20,11 +20,13 @@ var ( type UDPSerialPort struct { io.ReadWriteCloser + // Timeout for setting read/write deadlines + timeout time.Duration OriginConn *net.UDPConn DestinationAddr *net.UDPAddr } -func NewUDPSerialPort(origin, destination string) (*UDPSerialPort, error) { +func NewUDPSerialPort(origin, destination string, timeout time.Duration) (*UDPSerialPort, error) { udpOriginAddr, err := net.ResolveUDPAddr("udp", origin) if err != nil { return nil, fmt.Errorf("new udp serial port: %w", err) @@ -40,15 +42,24 @@ func NewUDPSerialPort(origin, destination string) (*UDPSerialPort, error) { return &UDPSerialPort{ OriginConn: originConn, DestinationAddr: udpDestinationAddr, + timeout: timeout, }, nil } -func (t UDPSerialPort) Read(p []byte) (int, error) { - n, _, err := t.OriginConn.ReadFromUDP(p) +func (t UDPSerialPort) Read(p []byte) (n int, err error) { + err = t.OriginConn.SetReadDeadline(time.Now().Add(t.timeout)) + if err != nil { + return 0, fmt.Errorf("udp serial port read: %w", err) + } + n, _, err = t.OriginConn.ReadFromUDP(p) return n, err } func (t UDPSerialPort) Write(p []byte) (n int, err error) { + err = t.OriginConn.SetWriteDeadline(time.Now().Add(t.timeout)) + if err != nil { + return 0, fmt.Errorf("udp serial port write: %w", err) + } return t.OriginConn.WriteToUDP(p, t.DestinationAddr) } @@ -56,14 +67,6 @@ func (t UDPSerialPort) Close() error { return t.OriginConn.Close() } -func (t UDPSerialPort) SetReadDeadline(t2 time.Time) error { - return t.OriginConn.SetReadDeadline(t2) -} - -func (t UDPSerialPort) SetWriteDeadline(t2 time.Time) error { - return t.OriginConn.SetWriteDeadline(t2) -} - type Emulator struct { port io.ReadWriteCloser w *bufio.Writer