// DiarkisUdpTutorialDelegate.cpp
...
void DiarkisUdpTutorialDelegate::OnConnect(DiarkisUdpBase& udp, const ConnectArgs& args)
{
if (!IsInGameThread())
{
UE_LOG(LogTemp, Warning, TEXT("OnConnect called outside of game thread"));
return;
}
if (!GEngine)
{
UE_LOG(LogTemp, Warning, TEXT("GEngine is null"));
return;
}
if (args.connectStatus == DiarkisConnectStatus::DCS_Timeout)
{
GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Red, TEXT("Timeout occurred while connecting to the UDP server"));
return;
}
GEngine->AddOnScreenDebugMessage(-1, 10.0f, FColor::Green, TEXT("Connected to the UDP server"));
UE_LOG(LogTemp, Log, TEXT("Connected to the UDP server"));
}
// TutorialSubsystem.cpp
...
std::shared_ptr<DiarkisInterface> diarkis = NetworkManager->ConnectAsync(endpoint, clientKey, uid, serverType, true);
if (!diarkis)
{
UE_LOG(LogTemp, Error, TEXT("Failed to get endpoint."));
UKismetSystemLibrary::QuitGame(GetWorld(), nullptr, EQuitPreference::Quit, false);
return;
}
// Udp モジュール用のDelegateを設定
DiarkisUdpInstance_ = static_pointer_cast<DiarkisUdp>(diarkis->GetUdpBase());
if (!DiarkisUdpInstance_)
{
UE_LOG(LogTemp, Error, TEXT("Failed to get DiarkisUdp instance."));
UKismetSystemLibrary::QuitGame(GetWorld(), nullptr, EQuitPreference::Quit, false);
return;
}
UdpDelegate_ = std::make_shared<DiarkisUdpTutorialDelegate>();
if (UdpDelegate_)
{
DiarkisUdpInstance_->SetDelegate(UdpDelegate_.get());
}
...
void UTutorialSubsystem::Deinitialize()
{
if (!NetworkManager)
{
UE_LOG(LogTemp, Warning, TEXT("NetworkManager is null during Deinitialize"));
DiarkisInterfaceBase::DiarkisDestroy();
Super::Deinitialize();
return;
}
// UDP モジュールの Delegate をリセット
if (DiarkisUdpInstance_)
{
DiarkisUdpInstance_->SetDelegate(nullptr);
}
if (UdpDelegate_)
{
UdpDelegate_.reset();
}
// UDP サーバから切断
// Disconnect from the UDP server
UE_LOG(LogTemp, Log, TEXT("Disconnecting from the UDP server..."));
NetworkManager->Disconnect();
UE_LOG(LogTemp, Log, TEXT("Disconnected from the UDP server."));
Super::Deinitialize();
}