File tree Expand file tree Collapse file tree 1 file changed +8
-0
lines changed Expand file tree Collapse file tree 1 file changed +8
-0
lines changed Original file line number Diff line number Diff line change @@ -7,12 +7,14 @@ import (
7
7
"net"
8
8
"strconv"
9
9
"strings"
10
+ "sync"
10
11
)
11
12
12
13
type SyncServer struct {
13
14
listener net.Listener
14
15
port int
15
16
clients map [net.Conn ]bool
17
+ clientsMu sync.Mutex
16
18
running bool
17
19
currentSlide int
18
20
}
@@ -59,10 +61,12 @@ func (s *SyncServer) Stop() {
59
61
s .listener .Close ()
60
62
}
61
63
64
+ s .clientsMu .Lock ()
62
65
for client := range s .clients {
63
66
client .Close ()
64
67
}
65
68
s .clients = make (map [net.Conn ]bool )
69
+ s .clientsMu .Unlock ()
66
70
67
71
slog .Info ("Sync server stopped" )
68
72
}
@@ -72,13 +76,15 @@ func (s *SyncServer) BroadcastSlideChange(slideNumber int) {
72
76
73
77
message := fmt .Sprintf ("SLIDE:%d\n " , slideNumber )
74
78
79
+ s .clientsMu .Lock ()
75
80
for client := range s .clients {
76
81
_ , err := client .Write ([]byte (message ))
77
82
if err != nil {
78
83
delete (s .clients , client )
79
84
client .Close ()
80
85
}
81
86
}
87
+ s .clientsMu .Unlock ()
82
88
}
83
89
84
90
func (s * SyncServer ) acceptConnections () error {
@@ -94,7 +100,9 @@ func (s *SyncServer) acceptConnections() error {
94
100
return fmt .Errorf ("failed to accept connection: %w" , err )
95
101
}
96
102
103
+ s .clientsMu .Lock ()
97
104
s .clients [conn ] = true
105
+ s .clientsMu .Unlock ()
98
106
99
107
currentSlide := s .currentSlide
100
108
You can’t perform that action at this time.
0 commit comments