81 lines
1.9 KiB
Go
81 lines
1.9 KiB
Go
package main
|
|
|
|
import (
|
|
config2 "github.com/usrpserv/config"
|
|
"github.com/usrpserv/server"
|
|
"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()
|
|
serv := server.New()
|
|
if err = serv.Run(config); err != nil {
|
|
log.Println(err)
|
|
}
|
|
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)
|
|
}
|
|
serv.SendData(buffer[32:n], config.Server.Expire)
|
|
if frame.PTT == 0 {
|
|
log.Println("END OF TRANSMISSION")
|
|
}
|
|
}
|
|
}()
|
|
c := func(buffer []byte, size int) {
|
|
serv.SendData(buffer[32:size], config.Server.Expire)
|
|
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)
|
|
}
|
|
}
|