new randoffset for each draw

This commit is contained in:
Norwin Roosen 2020-12-29 21:08:31 +01:00
parent 2d536f21f3
commit 1ca79d8e26
No known key found for this signature in database
GPG Key ID: 24BC059DE24C43A3
3 changed files with 24 additions and 19 deletions

View File

@ -6,7 +6,6 @@ import (
"image" "image"
"image/color" "image/color"
"log" "log"
"math/rand"
"net" "net"
"sync" "sync"
@ -37,9 +36,11 @@ func Flut(img *image.NRGBA, position image.Point, shuffle, rgbsplit, randoffset,
} }
var messages [][]byte var messages [][]byte
var maxX, maxY int var maxOffsetX, maxOffsetY int
if randoffset { if randoffset {
maxX, maxY = CanvasSize(address) maxX, maxY := CanvasSize(address)
maxOffsetX = maxX - img.Bounds().Canon().Dx()
maxOffsetY = maxY - img.Bounds().Canon().Dy()
messages = cmds.Chunk(1) // each connection should send the full img messages = cmds.Chunk(1) // each connection should send the full img
} else { } else {
messages = cmds.Chunk(conns) messages = cmds.Chunk(conns)
@ -52,15 +53,7 @@ func Flut(img *image.NRGBA, position image.Point, shuffle, rgbsplit, randoffset,
msg = messages[i] msg = messages[i]
} }
bombWg.Add(1) go bombAddress(msg, address, maxOffsetX, maxOffsetY, stop, &bombWg)
if randoffset {
msg = append(OffsetCmd(
rand.Intn(maxX-img.Bounds().Canon().Dx()),
rand.Intn(maxY-img.Bounds().Canon().Dy()),
), msg...)
}
go bombAddress(msg, address, stop, &bombWg)
} }
bombWg.Wait() bombWg.Wait()
if wg != nil { if wg != nil {
@ -98,7 +91,7 @@ func FetchImage(bounds image.Rectangle, address string, conns int, stop chan boo
} }
go readPixels(img, conn, stop) go readPixels(img, conn, stop)
go bombConn(cmds[i], conn, stop) go bombConn(cmds[i], 0, 0, conn, stop)
} }
return img return img

View File

@ -32,7 +32,7 @@ func (c commands) Shuffle() {
} }
} }
// AddOffset uses the OFFSET command to send the image at a specific place (not supported by all servers. example: https://github.com/TobleMiner/shoreline) // OffsetCmd applies offset to all following requests. Not supported by all servers. example: https://github.com/TobleMiner/shoreline.
func OffsetCmd(x, y int) []byte { func OffsetCmd(x, y int) []byte {
return []byte(fmt.Sprintf("OFFSET %d %d\n", x, y)) return []byte(fmt.Sprintf("OFFSET %d %d\n", x, y))
} }

View File

@ -3,6 +3,7 @@ package pixelflut
import ( import (
"fmt" "fmt"
"log" "log"
"math/rand"
"net" "net"
"sync" "sync"
"time" "time"
@ -74,26 +75,37 @@ func initPerfReporter() *Performance {
// bombAddress writes the given message via plain TCP to the given address, // bombAddress writes the given message via plain TCP to the given address,
// as fast as possible, until stop is closed. // as fast as possible, until stop is closed.
func bombAddress(message []byte, address string, stop chan bool, wg *sync.WaitGroup) { func bombAddress(message []byte, address string, maxOffsetX, maxOffsetY int, stop chan bool, wg *sync.WaitGroup) {
wg.Add(1)
defer wg.Done()
conn, err := net.Dial("tcp", address) conn, err := net.Dial("tcp", address)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
defer conn.Close() defer conn.Close()
bombConn(message, conn, stop) bombConn(message, maxOffsetX, maxOffsetY, conn, stop)
wg.Done()
} }
func bombConn(message []byte, conn net.Conn, stop chan bool) { func bombConn(message []byte, maxOffsetX, maxOffsetY int, conn net.Conn, stop chan bool) {
PerformanceReporter.connsReporter <- 1 PerformanceReporter.connsReporter <- 1
defer func() { PerformanceReporter.connsReporter <- -1 }() defer func() { PerformanceReporter.connsReporter <- -1 }()
var msg = make([]byte, len(message)+16) // leave some space for offset cmd
msg = message
randOffset := maxOffsetX > 1 && maxOffsetY > 0
for { for {
select { select {
case <-stop: case <-stop:
return return
default: default:
b, err := conn.Write(message) if randOffset {
msg = append(
OffsetCmd(rand.Intn(maxOffsetX), rand.Intn(maxOffsetY)),
message...,
)
}
b, err := conn.Write(msg)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }