usrpserv/main.go

81 lines
1.9 KiB
Go
Raw Normal View History

2024-05-24 23:07:40 +01:00
package main
import (
config2 "github.com/usrpserv/config"
2024-05-25 12:05:15 +01:00
"github.com/usrpserv/server"
2024-05-24 23:07:40 +01:00
"github.com/usrpserv/usrp"
"log"
"net"
"time"
)
func main() {
config, err := config2.ParseConfig("config.json")
if err != nil {
log.Fatal(err)
}
client := usrp.NewClient(net.UDPAddr{Port: config.RemoteAddr.Port, IP: net.ParseIP(config.RemoteAddr.IP)},
net.UDPAddr{Port: config.LocalAddr.Port, IP: net.ParseIP(config.LocalAddr.IP)})
defer client.Sock.Close()
2024-05-25 12:05:15 +01:00
serv := server.New()
if err = serv.Run(config); err != nil {
log.Println(err)
}
2024-05-24 23:07:40 +01:00
connectedIP := make(map[string]net.UDPAddr, 1)
listenAddr := net.UDPAddr{
Port: 4810,
IP: net.ParseIP("0.0.0.0"),
}
netListener, err := net.ListenUDP("udp", &listenAddr)
if err != nil {
log.Fatal(err)
}
buffer := make([]byte, 500)
clientBuffer := make([]byte, 500)
frame := usrp.NewFrame()
go func() {
for {
n, clientAddr, err := netListener.ReadFromUDP(buffer)
if err != nil {
log.Println(err)
continue
}
_, ok := connectedIP[clientAddr.String()]
if !ok {
log.Println("Adding new client:", clientAddr.String())
connectedIP[clientAddr.String()] = *clientAddr
}
if frame.Metadata == 0 {
if err = frame.Init(buffer, n); err != nil {
log.Println(err)
}
continue
}
if err = frame.Parse(buffer, n); err != nil {
log.Println(err)
}
if err = client.WriteFrame(buffer, n); err != nil {
log.Println(err)
}
2024-05-25 12:05:15 +01:00
serv.SendData(buffer[32:n], config.Server.Expire)
2024-05-24 23:07:40 +01:00
if frame.PTT == 0 {
log.Println("END OF TRANSMISSION")
}
}
}()
c := func(buffer []byte, size int) {
2024-05-25 12:05:15 +01:00
serv.SendData(buffer[32:size], config.Server.Expire)
2024-05-24 23:07:40 +01:00
for _, c := range connectedIP {
_, err := netListener.WriteToUDP(buffer[:size], &c)
if err != nil {
log.Println(err)
continue
}
}
}
client.Poll(clientBuffer, &c)
for {
time.Sleep(time.Millisecond * 250)
}
}