mirror of
https://gitlab.crans.org/nounous/ghostream.git
synced 2024-12-22 19:42:20 +00:00
Preallocate text buffer for telnet
This commit is contained in:
parent
868ee12726
commit
67ef07cac5
@ -30,16 +30,16 @@ type Options struct {
|
|||||||
func Serve(config *Options) {
|
func Serve(config *Options) {
|
||||||
Cfg = config
|
Cfg = config
|
||||||
|
|
||||||
if !Cfg.Enabled {
|
if !config.Enabled {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
currentMessage = make(map[string]*string)
|
currentMessage = make(map[string]*string)
|
||||||
clientCount = make(map[string]int)
|
clientCount = make(map[string]int)
|
||||||
|
|
||||||
listener, err := net.Listen("tcp", Cfg.ListenAddress)
|
listener, err := net.Listen("tcp", config.ListenAddress)
|
||||||
if err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -111,7 +111,7 @@ func Serve(config *Options) {
|
|||||||
clientCount[streamID]--
|
clientCount[streamID]--
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
time.Sleep(time.Duration(Cfg.Delay) * time.Millisecond)
|
time.Sleep(time.Duration(config.Delay) * time.Millisecond)
|
||||||
}
|
}
|
||||||
}(s)
|
}(s)
|
||||||
}
|
}
|
||||||
@ -128,8 +128,8 @@ func GetNumberConnectedSessions(streamID string) int {
|
|||||||
return clientCount[streamID]
|
return clientCount[streamID]
|
||||||
}
|
}
|
||||||
|
|
||||||
func asciiChar(pixel byte) string {
|
func asciiChar(pixel byte) byte {
|
||||||
asciiChars := []string{"@", "#", "$", "%", "?", "*", "+", ";", ":", ",", ".", " "}
|
asciiChars := []byte{'@', '#', '$', '%', '?', '*', '+', ';', ':', ',', '.', ' '}
|
||||||
return asciiChars[(255-pixel)/22]
|
return asciiChars[(255-pixel)/22]
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -141,22 +141,35 @@ func StartASCIIArtStream(streamID string, reader io.ReadCloser) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
currentMessage[streamID] = new(string)
|
currentMessage[streamID] = new(string)
|
||||||
buff := make([]byte, Cfg.Width*Cfg.Height)
|
pixelBuff := 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"
|
textBuff := strings.Builder{}
|
||||||
var pixel byte
|
textBuff.Grow((2*Cfg.Width + 1) * Cfg.Height)
|
||||||
for {
|
for {
|
||||||
n, _ := reader.Read(buff)
|
n, err := reader.Read(pixelBuff)
|
||||||
if n == 0 {
|
if err != nil {
|
||||||
|
log.Printf("An error occured while reading input: %s", err)
|
||||||
break
|
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 j := 0; j < Cfg.Height; j++ {
|
||||||
for i := 0; i < Cfg.Width; i++ {
|
for i := 0; i < Cfg.Width; i++ {
|
||||||
pixel = buff[Cfg.Width*j+i]
|
textBuff.WriteByte(asciiChar(pixelBuff[Cfg.Width*j+i]))
|
||||||
imageStr += asciiChar(pixel) + asciiChar(pixel)
|
textBuff.WriteByte(asciiChar(pixelBuff[Cfg.Width*j+i]))
|
||||||
}
|
}
|
||||||
imageStr += "\n"
|
textBuff.WriteByte('\n')
|
||||||
}
|
}
|
||||||
*(currentMessage[streamID]) = header + imageStr
|
|
||||||
|
*(currentMessage[streamID]) = textBuff.String()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user