split src/devices/input into src/devices/input/{raw,wrap}
This commit is contained in:
39
src/device/input/raw/udp.go
Normal file
39
src/device/input/raw/udp.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package raw
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type UDP struct {
|
||||
conn net.PacketConn
|
||||
}
|
||||
|
||||
func NewUDP(port int) UDP {
|
||||
conn, err := net.ListenPacket("udp", ":"+strconv.Itoa(port))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return UDP{
|
||||
conn: conn,
|
||||
}
|
||||
}
|
||||
|
||||
func (udp UDP) Read() []byte {
|
||||
panic("NEEDS TO BE IN BG THREAD SO WE CAN SHUT DOWN WITHOUT BLOCKING ON READ OR AT LEAST BE RESPONSIVE")
|
||||
buff := make([]byte, 1024)
|
||||
n, _, err := udp.conn.ReadFrom(buff)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if os.Getenv("DEBUG") == "true" {
|
||||
log.Printf("raw.UDP.Read() => %s", buff[:n])
|
||||
}
|
||||
return buff[:n]
|
||||
}
|
||||
|
||||
func (udp UDP) Close() {
|
||||
udp.conn.Close()
|
||||
}
|
||||
Reference in New Issue
Block a user