musique/server/src/musique-bridge.go

69 lines
1.2 KiB
Go
Raw Normal View History

2022-12-05 22:34:40 +01:00
package main
import (
"C"
2022-12-09 16:05:25 +01:00
"net"
"bufio"
2022-12-05 22:34:40 +01:00
"fmt"
"time"
2022-12-09 16:05:25 +01:00
"strings"
2022-12-05 22:34:40 +01:00
)
2022-12-09 16:05:25 +01:00
var clients []client
var pinger chan struct{}
2022-12-05 22:34:40 +01:00
//export ServerInit
func ServerInit() {
2022-12-09 16:05:25 +01:00
// scanResult = scan()
scanResult := []string{
"10.100.5.112:8081",
"10.100.5.44:8081",
}
clients = timesync(scanResult)
pinger = make(chan struct{}, 100)
go func() {
l, err := net.Listen("tcp", ":8081")
if err != nil {
return
}
defer l.Close()
for {
conn, err := l.Accept()
if err != nil {
continue
}
go func(c net.Conn) {
defer c.Close()
s := bufio.NewScanner(c)
for s.Scan() {
response := s.Text()
if response == "time" {
fmt.Fprintln(conn, time.Now().UnixMilli())
continue
}
if strings.HasPrefix(response, "start") {
startTimeString := strings.TrimSpace(response[len("start"):])
startTime := int64(0)
fmt.Sscanf(startTimeString, "%d", &startTime)
time.Sleep(time.Duration(startTime) * time.Millisecond)
pinger <- struct{}{}
continue
}
}
}(conn)
}
}()
2022-12-05 22:34:40 +01:00
}
//export ServerBeginProtocol
func ServerBeginProtocol() {
2022-12-09 16:05:25 +01:00
self := notifyAll(clients)
select {
case <- self:
case <- pinger:
2022-12-05 22:34:40 +01:00
}
}