Files
VERSE 1e04949242 refactor: better project structure (#35)
This project is starting to grow, and I've seen flaws in the initial
project structure design
I think I'll restructure the entire project directory for better
scalability and also cleaner
2026-07-11 16:15:05 +07:00

62 lines
976 B
Go

package ipc
import (
"net"
)
type State struct {
Query string
}
type Server struct {
conn *net.UDPConn
StateChan chan State
ListenPort int
}
func NewServer() (*Server, error) {
addr := &net.UDPAddr{
IP: net.ParseIP("127.0.0.1"),
Port: 0, // OS assigns random port
}
conn, err := net.ListenUDP("udp", addr)
if err != nil {
return nil, err
}
localAddr, ok := conn.LocalAddr().(*net.UDPAddr)
if !ok {
return nil, net.InvalidAddrError("not a UDP address")
}
s := &Server{
conn: conn,
StateChan: make(chan State, 100),
ListenPort: localAddr.Port,
}
return s, nil
}
func (s *Server) Start() {
buf := make([]byte, 4096)
for {
n, _, err := s.conn.ReadFromUDP(buf)
if err != nil {
break
}
query := string(buf[:n])
// send non-blocking so we don't choke the UDP read loop
select {
case s.StateChan <- State{Query: query}:
default:
}
}
}
func (s *Server) Close() {
_ = s.conn.Close()
close(s.StateChan)
}