github.com/amitbet/vnc2video@v0.0.0-20190616012314-9d50b9dab1d9/example/server/main.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"image"
     7  	"math"
     8  	"net"
     9  	"time"
    10  	vnc "github.com/amitbet/vnc2video"
    11  	"github.com/amitbet/vnc2video/logger"
    12  )
    13  
    14  func main() {
    15  	ln, err := net.Listen("tcp", ":5900")
    16  	if err != nil {
    17  		logger.Fatalf("Error listen. %v", err)
    18  	}
    19  
    20  	chServer := make(chan vnc.ClientMessage)
    21  	chClient := make(chan vnc.ServerMessage)
    22  
    23  	im := image.NewRGBA(image.Rect(0, 0, width, height))
    24  	tick := time.NewTicker(time.Second / 2)
    25  	defer tick.Stop()
    26  
    27  	cfg := &vnc.ServerConfig{
    28  		Width:  800,
    29  		Height: 600,
    30  		//VersionHandler:    vnc.ServerVersionHandler,
    31  		//SecurityHandler:   vnc.ServerSecurityHandler,
    32  		SecurityHandlers: []vnc.SecurityHandler{&vnc.ClientAuthNone{}},
    33  		//ClientInitHandler: vnc.ServerClientInitHandler,
    34  		//ServerInitHandler: vnc.ServerServerInitHandler,
    35  		Encodings:       []vnc.Encoding{&vnc.RawEncoding{}},
    36  		PixelFormat:     vnc.PixelFormat32bit,
    37  		ClientMessageCh: chServer,
    38  		ServerMessageCh: chClient,
    39  		Messages:        vnc.DefaultClientMessages,
    40  	}
    41  	cfg.Handlers = vnc.DefaultServerHandlers
    42  	go vnc.Serve(context.Background(), ln, cfg)
    43  
    44  	// Process messages coming in on the ClientMessage channel.
    45  	for {
    46  		select {
    47  		case <-tick.C:
    48  			drawImage(im, 0)
    49  			fmt.Printf("tick\n")
    50  		case msg := <-chClient:
    51  			switch msg.Type() {
    52  			default:
    53  				logger.Tracef("11 Received message type:%v msg:%v\n", msg.Type(), msg)
    54  			}
    55  		case msg := <-chServer:
    56  			switch msg.Type() {
    57  			default:
    58  				logger.Tracef("22 Received message type:%v msg:%v\n", msg.Type(), msg)
    59  			}
    60  		}
    61  	}
    62  }
    63  
    64  const (
    65  	width  = 800
    66  	height = 600
    67  )
    68  
    69  func drawImage(im *image.RGBA, anim int) {
    70  	pos := 0
    71  	const border = 50
    72  	for y := 0; y < height; y++ {
    73  		for x := 0; x < width; x++ {
    74  			var r, g, b uint8
    75  			switch {
    76  			case x < border*2.5 && x < int((1.1+math.Sin(float64(y+anim*2)/40))*border):
    77  				r = 255
    78  			case x > width-border*2.5 && x > width-int((1.1+math.Sin(math.Pi+float64(y+anim*2)/40))*border):
    79  				g = 255
    80  			case y < border*2.5 && y < int((1.1+math.Sin(float64(x+anim*2)/40))*border):
    81  				r, g = 255, 255
    82  			case y > height-border*2.5 && y > height-int((1.1+math.Sin(math.Pi+float64(x+anim*2)/40))*border):
    83  				b = 255
    84  			default:
    85  				r, g, b = uint8(x+anim), uint8(y+anim), uint8(x+y+anim*3)
    86  			}
    87  			im.Pix[pos] = r
    88  			im.Pix[pos+1] = g
    89  			im.Pix[pos+2] = b
    90  			pos += 4 // skipping alpha
    91  		}
    92  	}
    93  }