Hi, hope all of you are doing very well. I’m very new to multi-threading in c++ and in UE. Would you be so kind to look at my code and tell me what I do wrong or how I misuse API. I have nothing wrong with my code behaviour yet it is an insecurity call to post here.
> bool UEthernetSightswarm::EstablishConnection()
> {
> connectionAttmpts++;
> FIPv4Address address;
> FIPv4Address::Parse(IP, address);
> FIPv4Endpoint Endpoint(address, defaultPort);
> socket = FTcpSocketBuilder(TEXT("TcpSocket")).AsReusable().Build();
> ISocketSubsystem* socketSubsystem = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM);
> bool res = socket->Connect(*socketSubsystem->CreateInternetAddr(Endpoint.Address.Value, Endpoint.Port));
> UE_LOG(LogTemp, Warning, TEXT("Connecting attempt is %s"), res ? *FString("sucssess") : *FString("unsucssess"));
> double start = world->TimeSeconds;
>
> if (res)
> {
> FutureThread = Async(EAsyncExecution::Thread, [this, start]()
> {
> double since = 0.f;
> while (since < timeout && socket->GetConnectionState() != ESocketConnectionState::SCS_Connected && isRunning)
> {
> since = world->TimeSince(start);
> OnTestRecived.Broadcast(since);
> //UE_LOG(LogTemp, Warning, TEXT("%f"), since);
> FPlatformProcess::Sleep(0.5f);
> }
>
> if(isRunning)
> OnConnectionAttempt.Execute(socket->GetConnectionState() == ESocketConnectionState::SCS_Connected);
>
> return;
> });
> }
> else
> {
> FutureThread = Async(EAsyncExecution::Thread, [this]()
> {
> FPlatformProcess::Sleep(0.5f);
> OnConnectionAttempt.Execute(false);
> return;
> });
> }
>
> return res;
> }
>
> void UEthernetSightswarm::ConnectionResponce(bool isConnected)
> {
> if (FutureThread.IsValid() && FutureThread.IsReady())
> FutureThread.Get();
>
> FFunctionGraphTask::CreateAndDispatchWhenReady([this, isConnected]()
> {
> // This code is on game thread
> if (isConnected)
> OnConnectionEstablished.Broadcast();
> else
> {
> if (connectionAttmpts > maxAttempts)
> {
> if (socket)
> {
> ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->DestroySocket(socket);
> socket = nullptr;
> }
> OnConnectionFailed.Broadcast();
> connectionAttmpts = 0;
> }
> else
> {
> EstablishConnection();
> }
> }
> }, TStatId(), nullptr, ENamedThreads::GameThread);
> }
>
> void UEthernetSightswarm::CloseConnection()
> {
> isRunning = false;
> bool res = false;
>
> if (FutureThread.IsValid())
> {
> FutureThread.Wait();
> FutureThread.Get();
> }
>
>
> if (socket)
> {
> if (socket->GetConnectionState() == ESocketConnectionState::SCS_Connected)
> res = socket->Close();
>
> ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->DestroySocket(socket);
> socket = nullptr;
> }
>
> UE_LOG(LogTemp, Warning, TEXT("closing is %s"), res ? *FString("sucssess") : *FString("unsucssess"));
> }
thank to everyone in advance.