~fnux/yggdrasil-go-coap

yggdrasil-go-coap/net/connDTLS.go -rw-r--r-- 2.3 KiB
614f652bTimothée Floure Add syntax highlighting to code snippets in README 1 year, 8 months ago
                                                                                
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
package net

import (
	"fmt"
	"net"
	"sync"
	"sync/atomic"
	"time"
)

type connDTLSData struct {
	data []byte
	err  error
}

type ConnDTLS struct {
	conn       net.Conn
	readDataCh chan connDTLSData
	doneCh     chan struct{}
	wg         sync.WaitGroup

	readDeadline atomic.Value
}

func (c *ConnDTLS) readLoop() {
	defer c.wg.Done()
	buf := make([]byte, 8192)
	for {
		n, err := c.conn.Read(buf)
		d := connDTLSData{err: err}
		if err == nil && n > 0 {
			d.data = append(d.data, buf[:n]...)
		}
		select {
		case c.readDataCh <- d:
			if err != nil {
				return
			}
		case <-c.doneCh:
			return
		}
	}
}

func NewConnDTLS(conn net.Conn) *ConnDTLS {
	c := ConnDTLS{
		conn:       conn,
		readDataCh: make(chan connDTLSData),
		doneCh:     make(chan struct{}),
	}
	c.wg.Add(1)
	go c.readLoop()
	return &c
}

type errS struct {
	error
	timeout   bool
	temporary bool
}

func (e errS) Timeout() bool {
	return e.timeout
}

func (e errS) Temporary() bool {
	return e.temporary
}

func (c *ConnDTLS) processData(b []byte, d connDTLSData) (n int, err error) {
	if d.err != nil {
		return 0, errS{
			error: d.err,
		}
	}
	if len(b) < len(d.data) {
		return 0, errS{
			error: fmt.Errorf("buffer is too small"),
		}
	}
	return copy(b, d.data), nil
}

func (c *ConnDTLS) Read(b []byte) (n int, err error) {
	var deadline time.Time
	v := c.readDeadline.Load()
	if v != nil {
		deadline = v.(time.Time)
	}
	if deadline.IsZero() {
		select {
		case d := <-c.readDataCh:
			return c.processData(b, d)
		}
	}

	select {
	case d := <-c.readDataCh:
		return c.processData(b, d)
	case <-time.After(deadline.Sub(time.Now())):
		return 0, errS{
			error:     fmt.Errorf(ioTimeout),
			temporary: true,
			timeout:   true,
		}
	}
}

func (c *ConnDTLS) Write(b []byte) (n int, err error) {
	return c.conn.Write(b)
}

func (c *ConnDTLS) Close() error {
	err := c.conn.Close()
	close(c.doneCh)
	c.wg.Wait()
	return err
}

func (c *ConnDTLS) LocalAddr() net.Addr {
	return c.conn.LocalAddr()
}

func (c *ConnDTLS) RemoteAddr() net.Addr {
	return c.conn.RemoteAddr()
}

func (c *ConnDTLS) SetDeadline(t time.Time) error {
	err := c.SetReadDeadline(t)
	if err != nil {
		return err
	}
	return c.SetWriteDeadline(t)
}

func (c *ConnDTLS) SetReadDeadline(t time.Time) error {
	c.readDeadline.Store(t)
	return nil
}

func (c *ConnDTLS) SetWriteDeadline(t time.Time) error {
	return c.conn.SetWriteDeadline(t)
}