From 67ef07cac5e7aec180597d3c18e5975b3b3578e6 Mon Sep 17 00:00:00 2001 From: Alexandre Iooss Date: Wed, 14 Oct 2020 16:44:13 +0200 Subject: [PATCH] Preallocate text buffer for telnet --- stream/telnet/telnet.go | 45 ++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/stream/telnet/telnet.go b/stream/telnet/telnet.go index 1452e78..3d0cd12 100644 --- a/stream/telnet/telnet.go +++ b/stream/telnet/telnet.go @@ -30,16 +30,16 @@ type Options struct { func Serve(config *Options) { Cfg = config - if !Cfg.Enabled { + if !config.Enabled { return } currentMessage = make(map[string]*string) clientCount = make(map[string]int) - listener, err := net.Listen("tcp", Cfg.ListenAddress) + listener, err := net.Listen("tcp", config.ListenAddress) if err != nil { - log.Printf("Error while listening to the address %s: %s", Cfg.ListenAddress, err) + log.Printf("Error while listening to the address %s: %s", config.ListenAddress, err) return } @@ -111,7 +111,7 @@ func Serve(config *Options) { clientCount[streamID]-- break } - time.Sleep(time.Duration(Cfg.Delay) * time.Millisecond) + time.Sleep(time.Duration(config.Delay) * time.Millisecond) } }(s) } @@ -128,8 +128,8 @@ func GetNumberConnectedSessions(streamID string) int { return clientCount[streamID] } -func asciiChar(pixel byte) string { - asciiChars := []string{"@", "#", "$", "%", "?", "*", "+", ";", ":", ",", ".", " "} +func asciiChar(pixel byte) byte { + asciiChars := []byte{'@', '#', '$', '%', '?', '*', '+', ';', ':', ',', '.', ' '} return asciiChars[(255-pixel)/22] } @@ -141,22 +141,35 @@ func StartASCIIArtStream(streamID string, reader io.ReadCloser) { } currentMessage[streamID] = new(string) - 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" - var pixel byte + pixelBuff := make([]byte, Cfg.Width*Cfg.Height) + textBuff := strings.Builder{} + textBuff.Grow((2*Cfg.Width + 1) * Cfg.Height) for { - n, _ := reader.Read(buff) - if n == 0 { + n, err := reader.Read(pixelBuff) + if err != nil { + log.Printf("An error occured while reading input: %s", err) break } - imageStr := "" + if n == 0 { + // Stream is finished + break + } + + // Header + textBuff.Reset() + for i := 0; i < 42; i++ { + textBuff.WriteByte('\n') + } + + // Convert image to ASCII for j := 0; j < Cfg.Height; j++ { for i := 0; i < Cfg.Width; i++ { - pixel = buff[Cfg.Width*j+i] - imageStr += asciiChar(pixel) + asciiChar(pixel) + textBuff.WriteByte(asciiChar(pixelBuff[Cfg.Width*j+i])) + textBuff.WriteByte(asciiChar(pixelBuff[Cfg.Width*j+i])) } - imageStr += "\n" + textBuff.WriteByte('\n') } - *(currentMessage[streamID]) = header + imageStr + + *(currentMessage[streamID]) = textBuff.String() } }