1
0
mirror of https://gitlab.crans.org/nounous/ghostream.git synced 2024-12-22 15:02:19 +00:00

Use string pointers for the telnet output to avoid concurrency map read/write

This commit is contained in:
Yohann D'ANELLO 2020-10-13 11:17:35 +02:00
parent 770862cb7d
commit 6d9fe4a028

View File

@ -13,7 +13,7 @@ var (
// Cfg contains the different options of the telnet package, see below // Cfg contains the different options of the telnet package, see below
// TODO Config should not be exported // TODO Config should not be exported
Cfg *Options Cfg *Options
currentMessage map[string]string currentMessage map[string]*string
) )
// Options holds telnet package configuration // Options holds telnet package configuration
@ -33,7 +33,7 @@ func Serve(config *Options) {
return return
} }
currentMessage = make(map[string]string) currentMessage = make(map[string]*string)
listener, err := net.Listen("tcp", Cfg.ListenAddress) listener, err := net.Listen("tcp", Cfg.ListenAddress)
if err != nil { if err != nil {
@ -92,7 +92,7 @@ func Serve(config *Options) {
} }
for { for {
n, err := s.Write([]byte(currentMessage[streamID])) n, err := s.Write([]byte(*currentMessage[streamID]))
if err != nil { if err != nil {
log.Printf("Error while sending TCP data: %s", err) log.Printf("Error while sending TCP data: %s", err)
_ = s.Close() _ = s.Close()
@ -123,6 +123,7 @@ func StartASCIIArtStream(streamID string, reader io.ReadCloser) {
return return
} }
currentMessage[streamID] = new(string)
buff := make([]byte, Cfg.Width*Cfg.Height) buff := make([]byte, Cfg.Width*Cfg.Height)
header := "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" header := "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"
for { for {
@ -138,6 +139,6 @@ func StartASCIIArtStream(streamID string, reader io.ReadCloser) {
} }
imageStr += "\n" imageStr += "\n"
} }
currentMessage[streamID] = header + imageStr *(currentMessage[streamID]) = header + imageStr
} }
} }