init
This commit is contained in:
@@ -54,8 +54,20 @@ func (s *Server) Run() error {
|
||||
_ = conn.SetWriteBuffer(8 * 1024 * 1024)
|
||||
}
|
||||
|
||||
fmt.Println("[udp] listening on", s.bindAddr, "with", len(s.conns), "worker(s)")
|
||||
fmt.Println("[udp] listening on", s.bindAddr, "with", len(s.conns), "worker socket(s)")
|
||||
|
||||
// Cas Windows : listenUDP a créé une seule socket,
|
||||
// on lance workerCount goroutines sur la même conn.
|
||||
if len(s.conns) == 1 {
|
||||
conn := s.conns[0]
|
||||
for workerID := 0; workerID < workerCount; workerID++ {
|
||||
s.wg.Add(1)
|
||||
go s.workerLoop(workerID, conn)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Cas Unix : une conn par worker (SO_REUSEPORT).
|
||||
for workerID, conn := range s.conns {
|
||||
s.wg.Add(1)
|
||||
go s.workerLoop(workerID, conn)
|
||||
@@ -102,8 +114,7 @@ func (s *Server) workerLoop(workerID int, conn *net.UDPConn) {
|
||||
}
|
||||
}
|
||||
|
||||
// handlePacket reçoit maintenant directement la conn du worker.
|
||||
// Aucun hop supplémentaire : le paquet reste dans la goroutine/conn du worker.
|
||||
// handlePacket reçoit la conn du worker.
|
||||
func (s *Server) handlePacket(conn *net.UDPConn, data []byte, addr *net.UDPAddr) {
|
||||
if len(data) == 0 {
|
||||
return
|
||||
@@ -113,7 +124,6 @@ func (s *Server) handlePacket(conn *net.UDPConn, data []byte, addr *net.UDPAddr)
|
||||
|
||||
switch pt {
|
||||
case PacketTypePing:
|
||||
// exemple simple : echo du ping
|
||||
_, _ = conn.WriteToUDP([]byte{byte(PacketTypePing)}, addr)
|
||||
|
||||
case PacketTypeConnect:
|
||||
|
||||
Reference in New Issue
Block a user