Interactive asciicast editor
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
1.0 KiB

1 year ago
package main
import (
"bufio"
"encoding/json"
"log"
"net"
"time"
1 year ago
)
var (
commandConn net.Conn
commandsIn = make(chan *command)
commandsOut = make(chan *command)
)
type commandType int
const (
commandStatus = 1
commandLoad = 2
commandStop = 3
commandPlay = 4
commandPause = 5
commandResume = 6
)
type command struct {
Type commandType
D time.Duration
S string
}
func sendCommand(c *command) {
if commandConn == nil {
return
}
commandsOut <- c
}
1 year ago
func handleWrite() {
var b []byte
var err error
for c := range commandsOut {
b, err = json.Marshal(c)
if err != nil {
log.Fatalf("failed to marshal %+v: %s", c, err)
}
commandConn.Write(append(b, byte('\n')))
}
}
func handleRead() {
var c *command
1 year ago
scanner := bufio.NewScanner(commandConn)
for scanner.Scan() {
c = &command{}
1 year ago
err := json.Unmarshal(scanner.Bytes(), c)
if err != nil {
log.Fatalf("failed to unmarshal %s: %s", string(scanner.Bytes()), err)
}
commandsIn <- c
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
}