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
22 changes: 19 additions & 3 deletions transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,11 @@ func (a *Agent) Accept(ctx context.Context, remoteUfrag, remotePwd string) (*Con
// Conn represents the ICE connection.
// At the moment the lifetime of the Conn is equal to the Agent.
type Conn struct {
bytesReceived atomic.Uint64
bytesSent atomic.Uint64
agent *Agent
bytesReceived atomic.Uint64
bytesSent atomic.Uint64
packetsReceived atomic.Uint64
packetsSent atomic.Uint64
agent *Agent

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it feels like agent should come first here?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add a lint rule for this soon, also fieldalignment (wouldn't matter here since both orders are the same on 64), but i need to make the lint able to do --fix in the CI first

}

// BytesSent returns the number of bytes sent.
Expand All @@ -108,6 +110,16 @@ func (c *Conn) BytesReceived() uint64 {
return c.bytesReceived.Load()
}

// PacketsSent returns the number of packets sent.
func (c *Conn) PacketsSent() uint64 {
return c.packetsSent.Load()
}

// PacketsReceived returns the number of packets received.
func (c *Conn) PacketsReceived() uint64 {
return c.packetsReceived.Load()
}

func (a *Agent) startConnect(isControlling bool, remoteUfrag, remotePwd string) (*Conn, error) {
err := a.loop.Err()
if err != nil {
Expand Down Expand Up @@ -139,6 +151,9 @@ func (c *Conn) ReadWithAttributes(p []byte, attrs packetio.Attributes) (int, pac

n, attrs, err := c.agent.buf.Read(p, attrs)
c.bytesReceived.Add(uint64(n)) //nolint:gosec // G115
if err == nil {
c.packetsReceived.Add(1)
}

return n, attrs, err
}
Expand All @@ -163,6 +178,7 @@ func (c *Conn) Write(packet []byte) (int, error) {
n, err := pair.Write(packet)
if n > 0 {
c.bytesSent.Add(uint64(n))
c.packetsSent.Add(1)
pair.UpdatePacketSent(n)
}

Expand Down
3 changes: 3 additions & 0 deletions transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,8 @@ func TestConnStats(t *testing.T) {

require.Equal(t, uint64(10), ca.BytesSent())
require.Equal(t, uint64(10), cb.BytesReceived())
require.Equal(t, uint64(1), ca.PacketsSent())
require.Equal(t, uint64(1), cb.PacketsReceived())
}

func TestAgent_connect_ErrEarly(t *testing.T) {
Expand Down Expand Up @@ -461,6 +463,7 @@ func TestStartDialConnWriteBeforeConnectReturnsError(t *testing.T) {
require.Zero(t, n)
require.ErrorIs(t, werr, ErrNoCandidatePairs)
require.Zero(t, conn.BytesSent())
require.Zero(t, conn.PacketsSent())
}

func TestConn_GetCandidatePairsInfo(t *testing.T) {
Expand Down
Loading