Repository navigation
Expand file tree
/
Copy pathnetwork.go
More file actions
62 lines (47 loc) · 1.5 KB
/
Copy pathnetwork.go
File metadata and controls
62 lines (47 loc) · 1.5 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
// SPDX-FileCopyrightText: 2021 The Go-SSB Authors
//
// SPDX-License-Identifier: MIT
package ssb
import (
"context"
"io"
"net"
"net/http"
"time"
"github.com/ssbc/go-muxrpc/v2"
refs "github.com/ssbc/go-ssb-refs"
)
// EndpointStat gives some information about a connected peer
type EndpointStat struct {
ID refs.FeedRef
Addr net.Addr
Since time.Duration
Endpoint muxrpc.Endpoint
}
type Network interface {
Connect(ctx context.Context, addr net.Addr) error
Serve(context.Context, ...muxrpc.HandlerWrapper) error
GetListenAddr() net.Addr
GetAllEndpoints() []EndpointStat
GetEndpointFor(refs.FeedRef) (muxrpc.Endpoint, bool)
GetConnTracker() ConnTracker
DialViaRoom(portal, target refs.FeedRef) error
// websock hack
HandleHTTP(handler http.Handler)
io.Closer
}
// ConnTracker decides if connections should be established and keeps track of them
type ConnTracker interface {
// Active returns true and since when a peer connection is active
Active(net.Addr) (bool, time.Duration)
// OnAccept receives a new connection as an argument.
// If it decides to accept it, it returns true and a context that will be canceled once it should shut down
// If it decides to deny it, it returns false (and a nil context)
OnAccept(context.Context, net.Conn) (bool, context.Context)
// OnClose notifies the tracker that a connection was closed
OnClose(conn net.Conn) time.Duration
// Count returns the number of open connections
Count() uint
// CloseAll closes all tracked connections
CloseAll()
}