Support OS assining random port for given Musique instance

This commit is contained in:
Robert Bendun 2023-01-02 07:23:30 +01:00
parent 6588363fa3
commit 3183786271
5 changed files with 15 additions and 8 deletions

View File

@ -500,7 +500,7 @@ static std::optional<Error> Main(std::span<char const*> args)
// FIXME Handle port number parsing errors // FIXME Handle port number parsing errors
std::from_chars(port_str.data(), port_str.data() + port_str.size(), port); std::from_chars(port_str.data(), port_str.data() + port_str.size(), port);
} else { } else {
port = 8081; port = 0;
config["net"]["port"] = std::to_string(port); config["net"]["port"] = std::to_string(port);
save_new_config = true; save_new_config = true;
} }

View File

@ -20,9 +20,9 @@ bin/$(os)/$(Target): $(Release_Obj) bin/$(os)/main.o bin/$(os)/rtmidi.o $(Bestli
Debug_Obj=$(addprefix bin/$(os)/debug/,$(Obj)) Debug_Obj=$(addprefix bin/$(os)/debug/,$(Obj))
bin/$(os)/debug/$(Target): $(Debug_Obj) bin/$(os)/debug/main.o bin/$(os)/rtmidi.o $(Bestline) bin/$(os)/debug/$(Target): $(Debug_Obj) bin/$(os)/debug/main.o bin/$(os)/rtmidi.o $(Bestline) $(Server)
@echo "CXX $@" @echo "CXX $@"
@$(CXX) $(CXXFLAGS) $(DEBUG_FLAGS) $(CPPFLAGS) -o $@ $(Debug_Obj) bin/$(os)/rtmidi.o $(Bestline) $(LDFLAGS) $(LDLIBS) @$(CXX) $(CXXFLAGS) $(DEBUG_FLAGS) $(CPPFLAGS) -o $@ $(Debug_Obj) bin/$(os)/rtmidi.o $(Bestline) $(LDFLAGS) $(LDLIBS) bin/$(os)/server/server.o
bin/$(os)/debug/%.o: musique/%.cc bin/$(os)/debug/%.o: musique/%.cc
@echo "CXX $@" @echo "CXX $@"

View File

@ -305,7 +305,7 @@ func main() {
r := router.Router{} r := router.Router{}
registerRoutes(&r) registerRoutes(&r)
exit, err := r.Run(baseIP, uint16(port)) exit, err := r.Run(baseIP, &port)
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }

View File

@ -25,10 +25,12 @@ func ServerInit(inputNick string, inputPort int) {
nick = inputNick nick = inputNick
port = inputPort port = inputPort
r := router.Router{} r := router.Router{}
registerRoutes(&r) registerRoutes(&r)
_, err = r.Run(baseIP, uint16(port)) _, err = r.Run(baseIP, &port)
if err != nil { if err != nil {
fmt.Println("Address already in use. You have probably another instance of Musique running")
log.Fatalln(err) log.Fatalln(err)
} }

View File

@ -12,7 +12,7 @@ type Route func(incoming net.Conn, request proto.Request) interface{}
type Router struct { type Router struct {
routes map[string]Route routes map[string]Route
port uint16 port int
baseIP string baseIP string
} }
@ -23,14 +23,19 @@ func (router *Router) Add(name string, route Route) {
router.routes[name] = route router.routes[name] = route
} }
func (router *Router) Run(ip string, port uint16) (<-chan struct{}, error) { func (router *Router) Run(ip string, port *int) (<-chan struct{}, error) {
router.port = port router.port = *port
router.baseIP = ip router.baseIP = ip
listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", router.baseIP, router.port)) listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", router.baseIP, router.port))
if err != nil { if err != nil {
return nil, err return nil, err
} }
_, actualPort, err := net.SplitHostPort(listener.Addr().String())
if err != nil {
return nil, err
}
fmt.Sscan(actualPort, port)
exit := make(chan struct{}) exit := make(chan struct{})