-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconn.go
113 lines (94 loc) · 2.01 KB
/
conn.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package rtcnet
import (
"errors"
"net"
"sync"
"sync/atomic"
"time"
"github.com/pion/datachannel"
"github.com/pion/webrtc/v3"
)
type Conn struct {
peerConn *webrtc.PeerConnection
dataChannel *webrtc.DataChannel
raw datachannel.ReadWriteCloser
// readChan chan []byte
errorChan chan error
closeOnce sync.Once
closed atomic.Bool
}
func newConn(peer *webrtc.PeerConnection) *Conn {
c := &Conn{
peerConn: peer,
errorChan: make(chan error, 16), //TODO! - Sizing
}
return c
}
// For pushing error data out of the webrtc connection into the error buffer
func (c *Conn) pushErrorData(err error) {
if c.closed.Load() { return } // Skip if we are already closed
c.errorChan <- err
}
func (c *Conn) Read(b []byte) (int, error) {
select {
case err := <-c.errorChan:
return 0, err // There was some error
default:
// Just exit
}
return c.raw.Read(b)
}
func (c *Conn) Write(b []byte) (int, error) {
select {
case err := <-c.errorChan:
return 0, err // There was some error
default:
// Just exit
}
return c.raw.Write(b)
}
func (c *Conn) Close() error {
var closeErr error
c.closeOnce.Do(func() {
trace("conn: closing: ")
c.closed.Store(true)
var err1, err2, err3 error
if c.dataChannel != nil {
err1 = c.dataChannel.Close()
}
if c.peerConn != nil {
err2 = c.peerConn.Close()
}
if c.raw != nil {
err3 = c.raw.Close()
}
close(c.errorChan)
if err1 != nil || err2 != nil || err3 != nil {
closeErr = errors.Join(errors.New("failed to close: (datachannel, peerconn, raw)"), err1, err2, err3)
logger.Error().
Err(closeErr).
Msg("Closing rtc connection")
}
})
return closeErr
}
func (c *Conn) LocalAddr() net.Addr {
//TODO: implement
return nil
}
func (c *Conn) RemoteAddr() net.Addr {
//TODO: implement
return nil
}
func (c *Conn) SetDeadline(t time.Time) error {
//TODO: implement
return nil
}
func (c *Conn) SetReadDeadline(t time.Time) error {
//TODO: implement
return nil
}
func (c *Conn) SetWriteDeadline(t time.Time) error {
//TODO: implement
return nil
}