Correct HTTPU serving to not require an interface for multicast.
This commit is contained in:
parent
dcc00c8629
commit
5cd41e8371
@ -2,24 +2,19 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"net"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/huin/goupnp/httpu"
|
"github.com/huin/goupnp/httpu"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
eth0, err := net.InterfaceByName("eth0")
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
srv := httpu.Server{
|
srv := httpu.Server{
|
||||||
Addr: "239.255.255.250:1900",
|
Addr: "239.255.255.250:1900",
|
||||||
Interface: eth0,
|
Multicast: true,
|
||||||
Handler: httpu.HandlerFunc(func(r *http.Request) {
|
Handler: httpu.HandlerFunc(func(r *http.Request) {
|
||||||
log.Printf("Got %s %s message from %v: %v", r.Method, r.URL.Path, r.RemoteAddr, r.Header)
|
log.Printf("Got %s %s message from %v: %v", r.Method, r.URL.Path, r.RemoteAddr, r.Header)
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
err = srv.ListenAndServe()
|
err := srv.ListenAndServe()
|
||||||
log.Printf("Serving failed with error: %v", err)
|
log.Printf("Serving failed with error: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -36,13 +36,15 @@ func (f HandlerFunc) ServeMessage(r *http.Request) {
|
|||||||
// A Server defines parameters for running an HTTPU server.
|
// A Server defines parameters for running an HTTPU server.
|
||||||
type Server struct {
|
type Server struct {
|
||||||
Addr string // UDP address to listen on
|
Addr string // UDP address to listen on
|
||||||
Interface *net.Interface // Network interface to listen on
|
Multicast bool // Should listen for multicast?
|
||||||
|
Interface *net.Interface // Network interface to listen on for multicast, nil for default multicast interface
|
||||||
Handler Handler // handler to invoke
|
Handler Handler // handler to invoke
|
||||||
MaxMessageBytes int // maximum number of bytes to read from a packet, DefaultMaxMessageBytes if 0
|
MaxMessageBytes int // maximum number of bytes to read from a packet, DefaultMaxMessageBytes if 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListenAndServe listens on the UDP network address srv.Addr. If srv.Interface
|
// ListenAndServe listens on the UDP network address srv.Addr. If srv.Multicast
|
||||||
// != nil, then a multicast UDP listener will be used on the given interface.
|
// is true, then a multicast UDP listener will be used on srv.Interface (or
|
||||||
|
// default interface if nil).
|
||||||
func (srv *Server) ListenAndServe() error {
|
func (srv *Server) ListenAndServe() error {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
@ -52,7 +54,7 @@ func (srv *Server) ListenAndServe() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var conn net.PacketConn
|
var conn net.PacketConn
|
||||||
if srv.Interface != nil {
|
if srv.Multicast {
|
||||||
if conn, err = net.ListenMulticastUDP("udp", srv.Interface, addr); err != nil {
|
if conn, err = net.ListenMulticastUDP("udp", srv.Interface, addr); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user