This commit is contained in:
2025-11-15 00:35:41 +01:00
parent 8acfbf1215
commit 7ec38a443b
4 changed files with 139 additions and 93 deletions

View File

@@ -22,13 +22,22 @@ func NewServer(bindAddr string) *Server {
ctx, cancel := context.WithCancel(context.Background())
return &Server{
bindAddr: bindAddr,
ctx: ctx,
cancel: cancel,
bindAddr: bindAddr,
routingTable: NewRoutingTable(),
ctx: ctx,
cancel: cancel,
}
}
func (s *Server) Router() *RoutingTable {
return s.routingTable
}
func (s *Server) Run() error {
if s.bindAddr == "" {
return fmt.Errorf("bind address is empty")
}
workerCount := runtime.NumCPU()
conns, err := listenUDP(s.bindAddr, workerCount)
@@ -41,78 +50,102 @@ func (s *Server) Run() error {
s.conns = conns
for _, conn := range s.conns {
conn.SetReadBuffer(8 * 1024 * 1024)
conn.SetWriteBuffer(8 * 1024 * 1024)
_ = conn.SetReadBuffer(8 * 1024 * 1024)
_ = conn.SetWriteBuffer(8 * 1024 * 1024)
}
fmt.Println("Listening on", s.bindAddr)
fmt.Println("[udp] listening on", s.bindAddr, "with", len(s.conns), "worker(s)")
for i, conn := range s.conns {
for workerID, conn := range s.conns {
s.wg.Add(1)
go s.workerLoop(i, conn)
go s.workerLoop(workerID, conn)
}
return nil
}
func (s *Server) sendTo(data []byte, addr *net.UDPAddr) error {
if len(s.conns) == 0 || s.conns[0] == nil {
return fmt.Errorf("server not started")
func (s *Server) Stop() {
s.cancel()
for _, c := range s.conns {
_ = c.Close()
}
// On utilise la première conn pour lenvoi (cest suffisant pour UDP).
_, err := s.conns[0].WriteToUDP(data, addr)
return err
s.wg.Wait()
}
func (s *Server) workerLoop(id int, conn *net.UDPConn) {
func (s *Server) workerLoop(workerID int, conn *net.UDPConn) {
defer s.wg.Done()
buffer := make([]byte, 1500)
fmt.Println("Worker", id, "started")
buf := make([]byte, 1500)
fmt.Println("[udp] worker", workerID, "started")
for {
select {
case <-s.ctx.Done():
fmt.Println("Worker", id, "stopped")
fmt.Println("[udp] worker", workerID, "stopped")
return
default:
size, addr, err := conn.ReadFromUDP(buffer)
n, addr, err := conn.ReadFromUDP(buf)
if err != nil {
if s.ctx.Err() != nil {
return
}
if opErr, ok := err.(*net.OpError); ok && opErr.Temporary() {
if ne, ok := err.(net.Error); ok && ne.Timeout() {
continue
}
fmt.Printf("Error reading from UDP (worker %d): %v\n", id, err)
fmt.Printf("[udp] worker %d: read error: %v\n", workerID, err)
continue
}
s.handlePacket(buffer[:size], addr)
// Le worker qui lit est aussi celui qui traite et qui écrit.
s.handlePacket(conn, buf[:n], addr)
}
}
}
func (s *Server) handlePacket(data []byte, addr *net.UDPAddr) {
// handlePacket reçoit maintenant directement la conn du worker.
// Aucun hop supplémentaire : le paquet reste dans la goroutine/conn du worker.
func (s *Server) handlePacket(conn *net.UDPConn, data []byte, addr *net.UDPAddr) {
if len(data) == 0 {
return
}
pt := PacketType(data[0])
switch pt {
case PacketTypePing:
_ = s.sendTo([]byte{byte(PacketTypePing)}, addr)
return
// exemple simple : echo du ping
_, _ = conn.WriteToUDP([]byte{byte(PacketTypePing)}, addr)
case PacketTypeConnect:
return
if len(data) < 2 {
return
}
channelID := string(data[1:])
s.routingTable.Add(channelID, addr)
case PacketTypeDisconnect:
return
if len(data) < 2 {
return
}
channelID := string(data[1:])
s.routingTable.Remove(channelID, addr)
case PacketTypeVoiceData:
// todo : déterminer le format du packet
// channelID := string(data[1:5])
return
if len(data) < 2 {
return
}
channelID := string(data[1:]) // à adapter selon ton vrai format
recipients := s.routingTable.GetAddrs(channelID)
for _, dst := range recipients {
// optionnel: ne pas renvoyer à la source
if dst.IP.Equal(addr.IP) && dst.Port == addr.Port {
continue
}
_, _ = conn.WriteToUDP(data, dst)
}
default:
return
// type inconnu -> ignore
}
}