forked from DanielKrawisz/bmd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listener.go
79 lines (66 loc) · 2.02 KB
/
listener.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
// Copyright (c) 2015 Monetas.
// Copyright 2016 Daniel Krawisz.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package peer
import (
"net"
"time"
"github.com/DanielKrawisz/maxrate"
"github.com/DanielKrawisz/bmutil/wire"
)
// Listener represents an open port listening for bitmessage connections.
// It is given as an interface so that mock peer listeners can easily swapped
// for the genuine ones.
type Listener interface {
Accept() (Connection, error)
Close() error
Addr() net.Addr
}
// listener implements the Listener interface. It listens on the given net.Listener
// and creates new bitmessage connections as new peers dial in.
type listener struct {
netListener net.Listener
maxDown int64
maxUp int64
}
// Accept blocks until a new connection dials in. It returns a Connection object,
// which means that only bitmessage messages pass along it.
func (pl *listener) Accept() (Connection, error) {
conn, err := pl.netListener.Accept()
if err != nil {
return nil, err
}
connection := &connection{
conn: conn,
addr: conn.RemoteAddr(),
idleTimeout: time.Minute * pingTimeoutMinutes,
maxDown: maxrate.New(float64(pl.maxDown), 1),
maxUp: maxrate.New(float64(pl.maxUp), 1),
}
connection.idleTimer = time.AfterFunc(connection.idleTimeout, func() {
connection.WriteMessage(&wire.MsgPong{})
})
return connection, nil
}
// Close closes the listener.
func (pl *listener) Close() error {
return pl.netListener.Close()
}
// Addr returns the listener's network address.
func (pl *listener) Addr() net.Addr {
return pl.netListener.Addr()
}
// A value that can be swapped out to create mock listeners.
var listen = net.Listen
// Listen creates a listener object. The value of listen can
// be swapped out with a mock connection dialer for testing purposes.
func Listen(service, addr string) (Listener, error) {
netListener, err := listen(service, addr)
if err != nil {
return nil, err
}
return &listener{
netListener: netListener,
}, nil
}