-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconn.go
More file actions
175 lines (155 loc) · 3.89 KB
/
conn.go
File metadata and controls
175 lines (155 loc) · 3.89 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright (c) 2025, Grigory Buteyko aka Hrissan
// Licensed under the MIT License. See LICENSE for details.
package dtls
import (
"io"
"net"
"time"
"github.com/hrissan/dtls/dtlscore"
"github.com/hrissan/dtls/record"
)
// toy implementation - not optimized at all, unlike core
// we store no empty records, because they violate io.Reader contract
const maxRecordsBuffer = 10
type Conn struct {
tc dtlscore.Connection
localAddr net.Addr
remoteAddr net.Addr
closed bool // if true, both channels are closed
closeErr error
condRead chan struct{}
condWrite chan struct{}
condDial chan struct{}
reading [][]byte
writing [][]byte
}
func newConn(localAddr net.Addr, remoteAddr net.Addr) *Conn {
return &Conn{
localAddr: localAddr,
remoteAddr: remoteAddr,
condRead: make(chan struct{}, 1),
condWrite: make(chan struct{}, 1),
condDial: make(chan struct{}, 1),
}
}
var _ net.Conn = &Conn{}
var _ io.ReadWriter = &Conn{}
func signalCond(cond chan struct{}) {
select {
case cond <- struct{}{}:
default:
}
}
func (c *Conn) LocalAddr() net.Addr { return c.localAddr }
func (c *Conn) RemoteAddr() net.Addr { return c.remoteAddr }
func (c *Conn) SetDeadline(t time.Time) error { return nil } // TODO
func (c *Conn) SetReadDeadline(t time.Time) error { return nil } // TODO
func (c *Conn) SetWriteDeadline(t time.Time) error { return nil } // TODO
func (c *Conn) Read(b []byte) (int, error) {
if len(b) == 0 {
return 0, nil
}
c.tc.Lock()
defer c.tc.Unlock()
for {
if c.closed {
return 0, net.ErrClosed
}
if len(c.reading) == 0 {
c.tc.Unlock()
<-c.condRead
c.tc.Lock()
continue
}
if len(c.reading[0]) == 0 {
panic("empty record")
}
copied := copy(b, c.reading[0])
c.reading[0] = c.reading[0][copied:]
if len(c.reading[0]) == 0 {
c.reading = c.reading[1:]
}
return copied, nil
}
}
func (c *Conn) Write(b []byte) (int, error) {
if len(b) == 0 { // we store no empty records, because they violate io.Reader contract
return 0, nil
}
if len(b) > record.MaxPlaintextRecordLength { // limit outgoing buffer
b = b[:record.MaxPlaintextRecordLength]
}
c.tc.Lock()
defer c.tc.Unlock()
for {
if c.closed {
return 0, net.ErrClosed
}
if len(c.writing) >= maxRecordsBuffer { // arbitrary
c.tc.Unlock()
<-c.condWrite
c.tc.Lock()
continue
}
c.writing = append(c.writing, append([]byte{}, b...))
c.tc.SignalWriteable()
return len(b), nil
}
}
func (c *Conn) Close() error {
c.tc.Lock()
defer c.tc.Unlock()
c.closeLocked(nil)
return c.closeErr
}
func (c *Conn) closeLocked(err error) {
if c.closed {
return
}
c.closed = true
c.closeErr = err
close(c.condRead)
close(c.condWrite)
c.tc.SignalWriteable()
}
func (c *Conn) OnHandshakeLocked(info dtlscore.HandshakeInfo) {
signalCond(c.condDial)
}
func (c *Conn) OnConnectLocked() {
}
func (c *Conn) OnDisconnectLocked(err error) {
signalCond(c.condDial)
c.closeLocked(err)
}
func (c *Conn) OnWriteRecordLocked(earlyData bool, recordBody []byte) (recordSize int, send bool, signalWriteable bool, err error) {
if c.closed {
return 0, false, false, net.ErrClosed
}
if len(c.writing) == 0 {
return 0, false, false, nil
}
if len(c.writing[0]) == 0 {
panic("empty record")
}
recordSize = copy(recordBody, c.writing[0])
c.writing[0] = c.writing[0][recordSize:]
if len(c.writing[0]) == 0 {
c.writing = c.writing[1:]
signalCond(c.condWrite)
}
return recordSize, true, false, nil
}
func (conn *Conn) OnReadRecordLocked(earlyData bool, recordBody []byte) error {
if conn.closed {
return io.EOF
}
if len(recordBody) == 0 {
return nil // we do not store empty records, they violate io.Reader contract
}
if len(conn.reading) >= maxRecordsBuffer {
return nil // we are losing records, because no one is reading on our side
}
conn.reading = append(conn.reading, append([]byte{}, recordBody...))
signalCond(conn.condRead)
return nil
}