Announcement

Collapse
No announcement yet.

HttpServer Module doesn't seem to work with multiple instances

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    [PROGRAMMING] HttpServer Module doesn't seem to work with multiple instances

    ->UE4.26 (Engine Source Release)
    ->Win64 (Windows10)

    Given this code using HttpServer module:

    Code:
    bool FPGHTTPServer::Start(int Port)
    {
    bool succeed = false;
    ListenPort = Port;
    auto HttpServerModule = &FHttpServerModule::Get();
    TSharedPtr<IHttpRouter> HttpRouter;
    do
    {
    HttpRouter = HttpServerModule->GetHttpRouter(ListenPort);
    }
    while (!HttpRouter.IsValid() && ListenPort++ < 60000);
    
    if(HttpRouter.IsValid())
    {
    FString Route = "/health";
    // One point to note here is that the bottom layer does not support different request methods with the same http path configuration
    auto Handler = HttpRouter->BindRoute(FHttpPath(Route), EHttpServerRequestVerbs::VERB_GET, CreateHandler(&HealthCheck));
    if(Handler)
    {
    HttpServerModule->StartAllListeners();
    succeed = true;
    }
    else
    {
    UE_LOG(LogTemp, Warning, TEXT("Http Listener couldn't be created!"));
    }
    }
    else
    {
    UE_LOG(LogTemp, Warning, TEXT("HttpRouter couldn't be created!"));
    }
    return succeed;
    }

    and TWO server instances in TWO separate processes I have the next log report running the second instance:

    LogHttpServerModule: Starting all listeners...
    LogInit: WinSock: version 1.1 (2.2), MaxSocks=32767, MaxUdp=65467
    LogHttpListener: Error: HttpListener unable to bind to 0.0.0.0:30010
    LogHttpServerModule: All listeners started

    At this code point:

    Code:
    HttpServerModule->StartAllListeners();

    But programmer won't be able to be notified about this issue at runtime cause HttpServerModule.cpp -> StartAllListeners has this interface:

    Code:
    void FHttpServerModule::StartAllListeners()

    The simplest solution I though is making this simple change in this method in HttpServerModule.cpp:

    Code:
    TSharedPtr<IHttpRouter> FHttpServerModule::GetHttpRouter(uint32 Port)
    {
    check(Singleton == this);
    
    // We may already be listening on this port
    TUniquePtr<FHttpListener>* ExistingListener = Listeners.Find(Port);
    if (ExistingListener)
    {
    return ExistingListener->Get()->GetRouter();
    }
    
    // Otherwise create a new one
    TUniquePtr<FHttpListener> NewListener = MakeUnique<FHttpListener>(Port);
    
    // Try to start this listener now
    if (bHttpListenersEnabled)
    {
    NewListener->StartListening();
    }
    const auto& NewListenerRef = Listeners.Add(Port, MoveTemp(NewListener));
    return NewListenerRef->GetRouter();
    }
    for:

    Code:
    if (!NewListener->StartListening())
    return nullptr;

    Consequently, I can know If something went wrong and I can try it again using other port.

    It might not be the best solution but works great for me. Now I could have as many UE4 Dedicated Servers with their HttpServer listening and it's the feature that I need.

    I hope this issue is fixed soon it doesn't matter the way,
    Jaime.
    Attached Files
Working...
X