]> Sergey Matveev's repositories - btrtrc.git/blobdiff - webrtc.go
Drop support for go 1.20
[btrtrc.git] / webrtc.go
index c217a510a2372090358dcf361a92d16a4843b119..ca4f80fb9f19d08d7f4e9adf66e9826d75b0d32b 100644 (file)
--- a/webrtc.go
+++ b/webrtc.go
@@ -1,13 +1,17 @@
 package torrent
 
 import (
-       "fmt"
        "net"
+       "strconv"
        "time"
 
-       "github.com/anacrolix/torrent/webtorrent"
        "github.com/pion/datachannel"
        "github.com/pion/webrtc/v3"
+       "go.opentelemetry.io/otel"
+       "go.opentelemetry.io/otel/attribute"
+       "go.opentelemetry.io/otel/trace"
+
+       "github.com/anacrolix/torrent/webtorrent"
 )
 
 const webrtcNetwork = "webrtc"
@@ -30,9 +34,7 @@ func (webrtcNetAddr) Network() string {
 }
 
 func (me webrtcNetAddr) String() string {
-       // Probably makes sense to return the IP:port expected of most net.Addrs. I'm not sure if
-       // Address would be quoted for IPv6 already. If not, net.JoinHostPort might be appropriate.
-       return fmt.Sprintf("%s:%d", me.Address, me.Port)
+       return net.JoinHostPort(me.Address, strconv.FormatUint(uint64(me.Port), 10))
 }
 
 func (me webrtcNetConn) LocalAddr() net.Addr {
@@ -58,13 +60,26 @@ func (me webrtcNetConn) RemoteAddr() net.Addr {
 // PeerConnection or on the channel or some transport?
 
 func (w webrtcNetConn) SetDeadline(t time.Time) error {
+       w.Span.AddEvent("SetDeadline", trace.WithAttributes(attribute.String("time", t.String())))
        return nil
 }
 
 func (w webrtcNetConn) SetReadDeadline(t time.Time) error {
+       w.Span.AddEvent("SetReadDeadline", trace.WithAttributes(attribute.String("time", t.String())))
        return nil
 }
 
 func (w webrtcNetConn) SetWriteDeadline(t time.Time) error {
+       w.Span.AddEvent("SetWriteDeadline", trace.WithAttributes(attribute.String("time", t.String())))
        return nil
 }
+
+func (w webrtcNetConn) Read(b []byte) (n int, err error) {
+       _, span := otel.Tracer(tracerName).Start(w.Context, "Read")
+       defer span.End()
+       span.SetAttributes(attribute.Int("buf_len", len(b)))
+       n, err = w.ReadWriteCloser.Read(b)
+       span.RecordError(err)
+       span.SetAttributes(attribute.Int("bytes_read", n))
+       return
+}