/
stream.go
67 lines (57 loc) · 1.86 KB
/
stream.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
package stream
import (
"bufio"
"fmt"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multiaddr"
"github.com/ntsd/cross-clipboard/pkg/clipboard"
"github.com/ntsd/cross-clipboard/pkg/config"
"github.com/ntsd/cross-clipboard/pkg/crypto"
"github.com/ntsd/cross-clipboard/pkg/device"
"github.com/ntsd/cross-clipboard/pkg/devicemanager"
)
// StreamHandler struct for stream handler
type StreamHandler struct {
config *config.Config
clipboardManager *clipboard.ClipboardManager
deviceManager *devicemanager.DeviceManager
logChan chan string
errorChan chan error
pgpDecrypter *crypto.PGPDecrypter
}
// NewStreamHandler initial new stream handler
func NewStreamHandler(
cfg *config.Config,
cp *clipboard.ClipboardManager,
deviceManager *devicemanager.DeviceManager,
logChan chan string,
errorChan chan error,
pgpDecrypter *crypto.PGPDecrypter,
) *StreamHandler {
s := &StreamHandler{
config: cfg,
clipboardManager: cp,
deviceManager: deviceManager,
logChan: logChan,
errorChan: errorChan,
pgpDecrypter: pgpDecrypter,
}
go s.CreateWriteData()
return s
}
// HandleStream handler when a peer connect this host
func (s *StreamHandler) HandleStream(stream network.Stream) {
s.logChan <- fmt.Sprintf("peer %s connecting to this host", stream.Conn().RemotePeer())
// Create a new peer
dv := device.NewDevice(peer.AddrInfo{
ID: stream.Conn().RemotePeer(),
Addrs: []multiaddr.Multiaddr{stream.Conn().RemoteMultiaddr()},
}, stream)
dv.Reader = bufio.NewReader(stream)
dv.Writer = bufio.NewWriter(stream)
s.deviceManager.AddDevice(dv)
go s.CreateReadData(dv.Reader, dv)
s.logChan <- fmt.Sprintf("peer %s connected to this host", stream.Conn().RemotePeer())
// 'stream' will stay open until you close it (or the other side closes it).
}