-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
183 lines (151 loc) · 3.67 KB
/
main.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"context"
"fmt"
"io"
"log"
"sync"
"time"
"github.com/atotto/clipboard"
"github.com/awused/awconf"
pb "github.com/Kethsar/clipboardsync/clipboard_proto"
"google.golang.org/grpc"
)
type config struct {
Port string
Server string
Mode int
RetryInterval time.Duration
MaxRetries int
}
const (
clientMode = 1
serverMode = 2
dualMode = 3
)
var (
cboard string
mux sync.Mutex
c *config
stream pb.ClipboardSync_SyncClient
waitc chan struct{}
)
func main() {
err := awconf.LoadConfig("clipboardsync", &c)
if err != nil {
log.Fatalln(err)
}
// Start in the proper mode(s)
if c.Mode == serverMode {
startServer()
}
if c.Mode == dualMode {
go startServer()
}
if c.Mode == clientMode || c.Mode == dualMode {
/*
Start the client and clipboard monitor on different threads
The client thread closes waitc if it exceeds max connection attempts
Could potentially run startClient after monitorClipboard to avoid that
*/
waitc = make(chan struct{})
go startClient()
go monitorClipboard()
<-waitc
}
}
// Attempt to create the client connection to the server, retrying as needed
func startClient() {
attempts := 0
delaySecs := c.RetryInterval
if delaySecs < 5 {
delaySecs = 5
}
delay := time.NewTicker(delaySecs * time.Second)
for {
attempts++
conn, err := grpc.Dial(c.Server, grpc.WithInsecure())
if err != nil {
printToConsole(fmt.Sprintf("Client: Failed to connect: %s", err))
if attempts >= c.MaxRetries && c.MaxRetries != 0 {
break
}
printToConsole(fmt.Sprintf("Will retry in %d seconds", delaySecs))
<-delay.C
continue
}
client := pb.NewClipboardSyncClient(conn)
stream, err = client.Sync(context.Background())
if err != nil {
conn.Close()
printToConsole(fmt.Sprintf("Client: Error creating stream: %s", err))
if attempts >= c.MaxRetries && c.MaxRetries != 0 {
break
}
printToConsole(fmt.Sprintf("Will retry in %d seconds", delaySecs))
<-delay.C
continue
}
attempts = 0
printToConsole("Client: Stream opened")
monitorClientStream()
printToConsole("Client: Stream closed")
// Set stream to nil so we don't try to use it somewhere else
stream = nil
conn.Close()
}
delay.Stop()
close(waitc)
}
// Continously monitor the stream, break when receiving errors
func monitorClientStream() {
for {
in, err := stream.Recv()
if err != nil {
if err == io.EOF {
printToConsole("Client: Reached end of stream")
} else {
printToConsole(fmt.Sprintf("Client: Failed to receive clipboard: %s", err))
}
break
}
if setClipboard(in.GetData()) {
printToConsole("Client: New clipboard received")
err = clipboard.WriteAll(cboard)
if err != nil {
printToConsole(fmt.Sprintf("Client: Failed to set clipboard: %s", err))
}
}
}
}
// Send the clipboard to the server specified in the config if it is different
func syncClipoard(text string) {
if !setClipboard(text) {
return
}
if stream == nil {
printToConsole("Client: No connection to a server is open, unable to send clipboard")
return
}
err := stream.Send(&pb.Clipboard{Data: text})
if err != nil {
printToConsole(fmt.Sprintf("Client: Error sending clipboard: %s", err))
return
}
printToConsole("Client: New clipboard sent")
}
// We have multiple threads accessing cboard, so use a mutex when accessing it
func setClipboard(cb string) bool {
mux.Lock()
defer mux.Unlock()
if cb == cboard {
return false
}
cboard = cb
return true
}
// Eh, I like formatted timestamps
func printToConsole(text string) {
t := time.Now()
fmt.Printf("[%d/%02d/%02d %02d:%02d:%02d] %s\n", t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), text)
}