Initial commit, wip

This commit is contained in:
Alex D. 2023-12-29 22:21:40 +00:00
commit b66d49aa00
Signed by: caskd
GPG Key ID: F92BA85F61F4C173
3 changed files with 112 additions and 0 deletions

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module git.redxen.eu/caskd/pixelflut
go 1.21.5

0
go.sum Normal file
View File

109
main.go Normal file
View File

@ -0,0 +1,109 @@
package main
import (
//"bufio"
"context"
"flag"
"fmt"
"image"
"image/color"
"log"
"math/rand"
"net"
)
type pos struct {
x, y int
}
func colortoHEXRGB(c color.Color) string {
r, g, b, _ := c.RGBA()
return fmt.Sprintf("%2.2X%2.2X%2.2X", uint8(r), uint8(g), uint8(b))
}
func main() {
var (
ep string
)
flag.StringVar(&ep, "endpoint", "localhost:80", "Endpoint")
flag.Parse()
var (
endpoint *net.TCPAddr
err error
mainlog *log.Logger
)
mainlog = log.Default()
endpoint, err = net.ResolveTCPAddr("tcp", ep)
if err != nil {
mainlog.Fatal("Failed to resolve address, bailing out: ", err)
}
for {
ctx, cancel := context.WithCancel(context.Background())
fb := image.NewRGBA(image.Rect(0, 0, 1920, 1080))
go writePixels(endpoint, fb, cancel)
for {
for x := fb.Rect.Min.X; x < fb.Rect.Max.X; x++ {
for y := fb.Rect.Min.Y; y < fb.Rect.Max.Y; y++ {
select {
case <-ctx.Done():
err = fmt.Errorf("Connection got interrupted")
break
default:
randset := rand.Uint64()
color := color.RGBA{
A: 0xFF,
R: uint8(randset & 0xFF),
G: uint8((randset >> 2) & 0xFF),
B: uint8((randset >> 4) & 0xFF),
}
fb.SetRGBA(x, y, color)
}
}
if err != nil {
break
}
}
if err != nil {
break
}
}
}
}
func writePixels(endpoint *net.TCPAddr, img *image.RGBA, cancel context.CancelFunc) {
defer cancel()
var (
err error
conn *net.TCPConn
)
//conn.New(10)
for {
conn, err = net.DialTCP("tcp", nil, endpoint)
if err != nil {
continue
}
for x := img.Rect.Min.X; x < img.Rect.Max.X; x++ {
for y := img.Rect.Min.Y; y < img.Rect.Max.Y; y++ {
_, err = fmt.Fprintf(conn, "PX %d %d %s\n", x, y, colortoHEXRGB(img.At(x, y)))
if err != nil {
fmt.Println(err)
break
}
}
if err != nil {
break
}
}
if err != nil {
fmt.Println(err)
}
conn.Close()
}
}