I have noticed that ue4 with pixel streaming uses webrtc .
i was wondering how can i use ue4 webrtc implementation for contacting a Stun/turn server and get my info so i can establish a peer to peer connection?
i tried to implement a stun client.
void UMyGameInstance::StunClientConnection()
{
ISocketSubsystem* SocketSubsystem = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM);
//Set Send Buffer Size
int32 BufferSize = 2 * 1024 * 1024;
//////////prepare the listener
///
FIPv4Address Addr;
FIPv4Address::Parse(TEXT("0.0.0.0"), Addr);
//Create Socket
FIPv4Endpoint Endpoint(Addr, 3002);
ListenSocket = FUdpSocketBuilder(TEXT("Udpsocket"))
.AsReusable()
.AsNonBlocking()
.BoundToEndpoint(Endpoint)
.WithBroadcast()
.WithReceiveBufferSize(BufferSize)
;
if (ListenSocket == nullptr)
{
UE_LOG(LogTemp, Error, TEXT("ListenSocket==nullptr "));
return;
}
FTimespan ThreadWaitTime = FTimespan::FromMilliseconds(100);
UDPReceiver = new FUdpSocketReceiver(ListenSocket, ThreadWaitTime, TEXT("thread_udp_receiver"));
UDPReceiver->OnDataReceived().BindUObject(this, &UMyGameInstance::OnDataReceivedDelegate);
UDPReceiver->Start();
////prepare the package to send
FSocket* socketd = FUdpSocketBuilder(TEXT("Udpsocket")).AsReusable().WithBroadcast();
TSharedRef<FInternetAddr> RemoteAddr = SocketSubsystem->CreateInternetAddr();
bool bTestLocal = false;
if(bTestLocal==false)
{
FAddressInfoResult GAIResults = SocketSubsystem->GetAddressInfo(ANSI_TO_TCHAR("stun.l.google.com"), nullptr, EAddressInfoFlags::Default, NAME_None);
ESocketErrors ErrorCode = GAIResults.ReturnCode;
if (ErrorCode != SE_NO_ERROR)
{
switch (ErrorCode)
{
case ESocketErrors::SE_HOST_NOT_FOUND:UE_LOG(LogTemp, Error, TEXT("ErrorCode --> SE_HOST_NOT_FOUND ")); break;
case ESocketErrors::SE_NO_DATA:UE_LOG(LogTemp, Error, TEXT("ErrorCode --> SE_NO_DATA ")); break;
case ESocketErrors::SE_ETIMEDOUT:UE_LOG(LogTemp, Error, TEXT("ErrorCode --> SE_ETIMEDOUT ")); break;
}
UE_LOG(LogTemp, Error, TEXT("RemoteAddr --> "), *RemoteAddr->ToString(true));
return;
}
if ((GAIResults.Results.Num() == 0))
{
UE_LOG(LogTemp, Error, TEXT("GAIResults.Results.Num()==0"));
return;
}
RemoteAddr = (GAIResults.Results[0].Address);
RemoteAddr->SetPort(19302);
}else
{
bool bValidAddress;
RemoteAddr->SetIp(TEXT("192.168.0.13"), bValidAddress);
RemoteAddr->SetPort(3000);
UE_LOG(LogTemp, Warning, TEXT("bValidAddress %s"), (bValidAddress ? TEXT("True") : TEXT("False")));
}
socketd->SetSendBufferSize(BufferSize, BufferSize);
socketd->SetReceiveBufferSize(BufferSize, BufferSize);
unsigned char bindingReq[20];
//## first bind
*(short*)(&bindingReq[0]) = int16(0x0001); // stun_method
*(short*)(&bindingReq[2]) = int16(0x0000); // msg_length
*(int*)(&bindingReq[4]) = int32(0x2112A442);// magic cookie
*(int*)(&bindingReq[8]) = int32(0x63c7117e); // transacation ID
*(int*)(&bindingReq[12]) = int32(0x0714278f);
*(int*)(&bindingReq[16]) = int32(0x5ded3221);
int32 BytesSent = 0;
socketd->SendTo(bindingReq,sizeof(bindingReq) , BytesSent ,*RemoteAddr);
if (BytesSent <= 0)
{
const FString Str = "Socket is valid but the receiver received 0 bytes, make sure it is listening properly!";
UE_LOG(LogTemp, Error, TEXT("%s"), *Str);
return ;
}else
UE_LOG(LogTemp, Warning, TEXT("Package Sent to %s with %d BytesSent"), *RemoteAddr->ToString(true),BytesSent);
}
void UMyGameInstance::OnDataReceivedDelegate(const FArrayReaderPtr& DataPtr, const FIPv4Endpoint& Endpoint)
{
UE_LOG(LogTemp, Error, TEXT("OnDataReceivedDelegate..."));
TArray<uint8> Data;
Data.AddUninitialized(DataPtr->TotalSize());
DataPtr->Serialize(Data.GetData(), DataPtr->TotalSize());
UE_LOG(LogTemp, Error, TEXT("OnDataReceivedDelegate..."));
}
when i call it the ue4 log shows
LogTemp: Warning: USMainMenuWidget::OnHostMatchPressed()
LogTemp: Warning: Package Sent to 64.233.186.127:19302 with 20 BytesSent
using wireshark i can see that the packet is send and recived, but the stun server doesnt respond.
if i do the same using nodejs i get the response from the Stun server so i wonder, maybe there is a problem with the packet sent?
Did you figure it out?
I finally abandon the project