mirror of
				https://github.com/yggdrasil-network/yggdrasil-go.git
				synced 2025-11-04 03:05:07 +03:00 
			
		
		
		
	Try to more gracefully handle shutdowns on Windows
This commit is contained in:
		
							parent
							
								
									b2607a7205
								
							
						
					
					
						commit
						12486b0557
					
				
					 4 changed files with 32 additions and 7 deletions
				
			
		| 
						 | 
				
			
			@ -26,6 +26,7 @@ type Multicast struct {
 | 
			
		|||
	groupAddr  string
 | 
			
		||||
	listeners  map[string]*yggdrasil.TcpListener
 | 
			
		||||
	listenPort uint16
 | 
			
		||||
	isOpen     bool
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Init prepares the multicast interface for use.
 | 
			
		||||
| 
						 | 
				
			
			@ -61,6 +62,7 @@ func (m *Multicast) Start() error {
 | 
			
		|||
		// Windows can't set this flag, so we need to handle it in other ways
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	m.isOpen = true
 | 
			
		||||
	go m.multicastStarted()
 | 
			
		||||
	go m.listen()
 | 
			
		||||
	go m.announce()
 | 
			
		||||
| 
						 | 
				
			
			@ -70,6 +72,8 @@ func (m *Multicast) Start() error {
 | 
			
		|||
 | 
			
		||||
// Stop is not implemented for multicast yet.
 | 
			
		||||
func (m *Multicast) Stop() error {
 | 
			
		||||
	m.isOpen = false
 | 
			
		||||
	m.sock.Close()
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -246,6 +250,9 @@ func (m *Multicast) listen() {
 | 
			
		|||
	for {
 | 
			
		||||
		nBytes, rcm, fromAddr, err := m.sock.ReadFrom(bs)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			if !m.isOpen {
 | 
			
		||||
				return
 | 
			
		||||
			}
 | 
			
		||||
			panic(err)
 | 
			
		||||
		}
 | 
			
		||||
		if rcm != nil {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -98,6 +98,9 @@ func (tun *TunAdapter) writer() error {
 | 
			
		|||
			util.PutBytes(b)
 | 
			
		||||
		}
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			if !tun.isOpen {
 | 
			
		||||
				return err
 | 
			
		||||
			}
 | 
			
		||||
			tun.log.Errorln("TUN/TAP iface write error:", err)
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
| 
						 | 
				
			
			@ -114,6 +117,9 @@ func (tun *TunAdapter) reader() error {
 | 
			
		|||
		// Wait for a packet to be delivered to us through the TUN/TAP adapter
 | 
			
		||||
		n, err := tun.iface.Read(bs)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			if !tun.isOpen {
 | 
			
		||||
				return err
 | 
			
		||||
			}
 | 
			
		||||
			panic(err)
 | 
			
		||||
		}
 | 
			
		||||
		if n == 0 {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -181,6 +181,16 @@ func (tun *TunAdapter) Start() error {
 | 
			
		|||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Start the setup process for the TUN/TAP adapter. If successful, starts the
 | 
			
		||||
// read/write goroutines to handle packets on that interface.
 | 
			
		||||
func (tun *TunAdapter) Stop() error {
 | 
			
		||||
	tun.isOpen = false
 | 
			
		||||
	// TODO: we have nothing that cleanly stops all the various goroutines opened
 | 
			
		||||
	// by TUN/TAP, e.g. readers/writers, sessions
 | 
			
		||||
	tun.iface.Close()
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// UpdateConfig updates the TUN/TAP module with the provided config.NodeConfig
 | 
			
		||||
// and then signals the various module goroutines to reconfigure themselves if
 | 
			
		||||
// needed.
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue