/
basic.go
62 lines (50 loc) · 1.08 KB
/
basic.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
package net
import (
"encoding/json"
"net"
"github.com/stuphlabs/pullcord/config"
)
// BasicListener augments the functionality of a net.Listen call by wrapping it
// in an encoding/json.Unmarshaler (and thereby making it be configurable).
type BasicListener struct {
Listener net.Listener
}
func init() {
e := config.RegisterResourceType(
"basiclistener",
func() json.Unmarshaler {
return new(BasicListener)
},
)
if e != nil {
panic(e)
}
}
// UnmarshalJSON implements encoding/json.Unmarshaler.
func (b *BasicListener) UnmarshalJSON(d []byte) error {
var t struct {
Proto string
Laddr string
}
if e := json.Unmarshal(d, &t); e != nil {
return e
}
l, e := net.Listen(t.Proto, t.Laddr)
if e != nil {
return e
}
b.Listener = l
return nil
}
// Accept implements net.Listener.
func (b *BasicListener) Accept() (net.Conn, error) {
return b.Listener.Accept()
}
// Close implements net.Listener.
func (b *BasicListener) Close() error {
return b.Listener.Close()
}
// Addr implements net.Listener.
func (b *BasicListener) Addr() net.Addr {
return b.Listener.Addr()
}