mirror of
				https://github.com/aler9/gortsplib
				synced 2025-11-01 02:52:36 +08:00 
			
		
		
		
	multicast: add readOnly flag (#423)
This commit is contained in:
		| @@ -25,6 +25,7 @@ type MultiConn struct { | |||||||
| // NewMultiConn allocates a MultiConn. | // NewMultiConn allocates a MultiConn. | ||||||
| func NewMultiConn( | func NewMultiConn( | ||||||
| 	address string, | 	address string, | ||||||
|  | 	readOnly bool, | ||||||
| 	listenPacket func(network, address string) (net.PacketConn, error), | 	listenPacket func(network, address string) (net.PacketConn, error), | ||||||
| ) (Conn, error) { | ) (Conn, error) { | ||||||
| 	addr, err := net.ResolveUDPAddr("udp4", address) | 	addr, err := net.ResolveUDPAddr("udp4", address) | ||||||
| @@ -67,8 +68,12 @@ func NewMultiConn( | |||||||
| 		return nil, fmt.Errorf("no multicast-capable interfaces found") | 		return nil, fmt.Errorf("no multicast-capable interfaces found") | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	writeConns := make([]*net.UDPConn, len(enabledInterfaces)) | 	var writeConns []*net.UDPConn | ||||||
| 	writeConnIPs := make([]*ipv4.PacketConn, len(enabledInterfaces)) | 	var writeConnIPs []*ipv4.PacketConn | ||||||
|  |  | ||||||
|  | 	if !readOnly { | ||||||
|  | 		writeConns = make([]*net.UDPConn, len(enabledInterfaces)) | ||||||
|  | 		writeConnIPs = make([]*ipv4.PacketConn, len(enabledInterfaces)) | ||||||
|  |  | ||||||
| 		for i, intf := range enabledInterfaces { | 		for i, intf := range enabledInterfaces { | ||||||
| 			tmp, err := listenPacket("udp4", "224.0.0.0:"+strconv.FormatInt(int64(addr.Port), 10)) | 			tmp, err := listenPacket("udp4", "224.0.0.0:"+strconv.FormatInt(int64(addr.Port), 10)) | ||||||
| @@ -104,6 +109,7 @@ func NewMultiConn( | |||||||
| 			writeConns[i] = writeConn | 			writeConns[i] = writeConn | ||||||
| 			writeConnIPs[i] = writeConnIP | 			writeConnIPs[i] = writeConnIP | ||||||
| 		} | 		} | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	return &MultiConn{ | 	return &MultiConn{ | ||||||
| 		addr:         addr, | 		addr:         addr, | ||||||
|   | |||||||
| @@ -24,6 +24,7 @@ type MultiConn struct { | |||||||
| // NewMultiConn allocates a MultiConn. | // NewMultiConn allocates a MultiConn. | ||||||
| func NewMultiConn( | func NewMultiConn( | ||||||
| 	address string, | 	address string, | ||||||
|  | 	readOnly bool, | ||||||
| 	_ func(network, address string) (net.PacketConn, error), | 	_ func(network, address string) (net.PacketConn, error), | ||||||
| ) (Conn, error) { | ) (Conn, error) { | ||||||
| 	addr, err := net.ResolveUDPAddr("udp4", address) | 	addr, err := net.ResolveUDPAddr("udp4", address) | ||||||
| @@ -84,6 +85,10 @@ func NewMultiConn( | |||||||
| 		return nil, fmt.Errorf("no multicast-capable interfaces found") | 		return nil, fmt.Errorf("no multicast-capable interfaces found") | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	var writeFiles []*os.File | ||||||
|  | 	var writeConns []net.PacketConn | ||||||
|  |  | ||||||
|  | 	if !readOnly { | ||||||
| 		writeSocks := make([]int, len(enabledInterfaces)) | 		writeSocks := make([]int, len(enabledInterfaces)) | ||||||
|  |  | ||||||
| 		for i, intf := range enabledInterfaces { | 		for i, intf := range enabledInterfaces { | ||||||
| @@ -145,15 +150,17 @@ func NewMultiConn( | |||||||
| 			writeSocks[i] = writeSock | 			writeSocks[i] = writeSock | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 	readFile := os.NewFile(uintptr(readSock), "") | 		writeFiles = make([]*os.File, len(writeSocks)) | ||||||
| 	readConn, _ := net.FilePacketConn(readFile) | 		writeConns = make([]net.PacketConn, len(writeSocks)) | ||||||
| 	writeFiles := make([]*os.File, len(writeSocks)) |  | ||||||
| 	writeConns := make([]net.PacketConn, len(writeSocks)) |  | ||||||
|  |  | ||||||
| 		for i, writeSock := range writeSocks { | 		for i, writeSock := range writeSocks { | ||||||
| 			writeFiles[i] = os.NewFile(uintptr(writeSock), "") | 			writeFiles[i] = os.NewFile(uintptr(writeSock), "") | ||||||
| 			writeConns[i], _ = net.FilePacketConn(writeFiles[i]) | 			writeConns[i], _ = net.FilePacketConn(writeFiles[i]) | ||||||
| 		} | 		} | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	readFile := os.NewFile(uintptr(readSock), "") | ||||||
|  | 	readConn, _ := net.FilePacketConn(readFile) | ||||||
|  |  | ||||||
| 	return &MultiConn{ | 	return &MultiConn{ | ||||||
| 		addr:       addr, | 		addr:       addr, | ||||||
|   | |||||||
| @@ -76,7 +76,7 @@ func newServerUDPListener( | |||||||
| 	var listenIP net.IP | 	var listenIP net.IP | ||||||
| 	if multicastEnable { | 	if multicastEnable { | ||||||
| 		var err error | 		var err error | ||||||
| 		pc, err = multicast.NewMultiConn(address, listenPacket) | 		pc, err = multicast.NewMultiConn(address, false, listenPacket) | ||||||
| 		if err != nil { | 		if err != nil { | ||||||
| 			return nil, err | 			return nil, err | ||||||
| 		} | 		} | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Alessandro Ros
					Alessandro Ros