Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions fakes/tcp_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net"
"sync"
"testing"
"time"
)

type TCPConn struct {
Expand Down Expand Up @@ -51,6 +52,14 @@ func (c *TCPConn) Close() error {
return nil
}

func (c *TCPConn) SetReadDeadline(time.Time) error {
return nil
}

func (c *TCPConn) SetWriteDeadline(time.Time) error {
return nil
}

func (c *TCPConn) TestReadConn(t testing.TB) []byte {
buffer := make([]byte, 65355)
// var buffer [65355]byte
Expand Down
11 changes: 9 additions & 2 deletions sip/transport_tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"log/slog"
"net"
"os"
"sync"
"time"
)
Expand Down Expand Up @@ -160,7 +161,7 @@ func (t *TransportTCP) readConnection(conn *TCPConnection, laddr string, raddr s

for {
num, err := conn.Read(buf)
if err != nil {
if err != nil && !errors.Is(err, os.ErrDeadlineExceeded) {
if errors.Is(err, net.ErrClosed) || errors.Is(err, io.EOF) {
t.log.Debug("connection was closed", "error", err)
return
Expand All @@ -184,7 +185,7 @@ func (t *TransportTCP) readConnection(conn *TCPConnection, laddr string, raddr s
t.log.Debug("Keep alive CRLF received")
if datalen == 4 {
// 2 CRLF is ping
if _, err := conn.Write(data[:2]); err != nil {
if _, err := conn.Write(data[:2]); err != nil && !errors.Is(err, os.ErrDeadlineExceeded) {
t.log.Error("Failed to pong keep alive", "error", err)
return
}
Expand Down Expand Up @@ -261,6 +262,9 @@ func (c *TCPConnection) TryClose() (int, error) {

func (c *TCPConnection) Read(b []byte) (n int, err error) {
// Some debug hook. TODO move to proper way
if err := c.Conn.SetReadDeadline(time.Now().Add(time.Millisecond * 500)); err != nil {
slog.Warn("Failed to set read deadline", "ip", c.LocalAddr().String(), "dst", c.RemoteAddr().String(), "error", err)
}
n, err = c.Conn.Read(b)
if SIPDebug {
logSIPRead("TCP", c.Conn.LocalAddr().String(), c.Conn.RemoteAddr().String(), b[:n])
Expand All @@ -270,6 +274,9 @@ func (c *TCPConnection) Read(b []byte) (n int, err error) {

func (c *TCPConnection) Write(b []byte) (n int, err error) {
// Some debug hook. TODO move to proper way
if err := c.Conn.SetWriteDeadline(time.Now().Add(time.Millisecond * 500)); err != nil {
slog.Warn("Failed to set write deadline", "ip", c.LocalAddr().String(), "dst", c.RemoteAddr().String(), "error", err)
}
n, err = c.Conn.Write(b)
if SIPDebug {
logSIPWrite("TCP", c.Conn.LocalAddr().String(), c.Conn.RemoteAddr().String(), b[:n])
Expand Down
Loading