Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/sammyfreg/netImgui into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
sammyfreg committed Nov 18, 2024
2 parents f507d74 + b669823 commit c89ae6b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 19 deletions.
11 changes: 11 additions & 0 deletions Code/Client/Private/NetImgui_NetworkPosix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@
#include <unistd.h>
#include <fcntl.h>

//NOTE: The socket handling has been modified to improve speed, but the Posix version
// has not been updated. Please review the changes made to 'NetImgui_NetworkWin32.cpp'
// between version 1.11 and 1.12 and bring them over to this file. In particular :
// -Sockets set to non blocking and immediate sending
// -Added 'DataReceivePending' function
// -Reworked 'DataReceive' and 'DataSend' to be non blocking socket operation
// but wait until all data has been processed
// -Connect now handle timeout instead of letting the socket timeout internally
// after a long time
static_assert(0)

namespace NetImgui { namespace Internal { namespace Network
{

Expand Down
38 changes: 21 additions & 17 deletions Code/Client/Private/NetImgui_NetworkUE4.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "NetImgui_Shared.h"

// Tested with Unreal Engine 4.27, 5.0, 5.2
// Tested with Unreal Engine 4.27, 5.3, 5.4, 5.5

#if NETIMGUI_ENABLED && defined(__UNREAL__)

Expand All @@ -14,7 +14,7 @@
#include "IPAddressAsyncResolve.h"
#endif

namespace NetImgui { namespace Internal { namespace Network
namespace NetImgui { namespace Internal { namespace Network
{

//=================================================================================================
Expand Down Expand Up @@ -63,31 +63,31 @@ void Shutdown()
//=================================================================================================
SocketInfo* Connect(const char* ServerHost, uint32_t ServerPort)
{
SocketInfo* pSocketInfo = nullptr;
ISocketSubsystem* SocketSubSystem = ISocketSubsystem::Get();
auto ResolveInfo = SocketSubSystem->GetHostByName(ServerHost);

while( ResolveInfo && !ResolveInfo->IsComplete() ){
FPlatformProcess::YieldThread();
}

if ( ResolveInfo && ResolveInfo->GetErrorCode() == 0)
SocketInfo* pSocketInfo = nullptr;
ISocketSubsystem* SocketSubSystem = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM);
TSharedPtr<FInternetAddr> IpAddressFind = SocketSubSystem->GetAddressFromString((TCHAR*)StringCast<TCHAR>(static_cast<const ANSICHAR*>(ServerHost)).Get());
if(IpAddressFind)
{
TSharedRef<FInternetAddr> IpAddress = ResolveInfo->GetResolvedAddress().Clone();
TSharedRef<FInternetAddr> IpAddress = IpAddressFind->Clone();
IpAddress->SetPort(ServerPort);
if (IpAddress->IsValid())
{
FSocket* pNewSocket = SocketSubSystem->CreateSocket(NAME_Stream, "NetImgui", IpAddress->GetProtocolType());
if (pNewSocket)
{
pNewSocket->SetNonBlocking(true);
if (pNewSocket->Connect(IpAddress.Get()))
if (pNewSocket->Connect(*IpAddress))
{
pSocketInfo = netImguiNew<SocketInfo>(pNewSocket);
return pSocketInfo;
bool bConnectionReady = false;
pNewSocket->WaitForPendingConnection(bConnectionReady, FTimespan::FromSeconds(1.0f));
if( bConnectionReady )
{
pSocketInfo = netImguiNew<SocketInfo>(pNewSocket);
return pSocketInfo;
}
}
}
}
}
}
netImguiDelete(pSocketInfo);
return nullptr;
Expand Down Expand Up @@ -155,14 +155,16 @@ bool DataReceivePending(SocketInfo* pClientSocket)
{
// Note: return true on a connection error, to exit code looping on the data wait. Will handle error after DataReceive()
uint32 PendingDataSize;
return pClientSocket->mpSocket->HasPendingData(PendingDataSize) || (pClientSocket->mpSocket->GetConnectionState() != ESocketConnectionState::SCS_Connected);
return !pClientSocket || pClientSocket->mpSocket->HasPendingData(PendingDataSize) || (pClientSocket->mpSocket->GetConnectionState() != ESocketConnectionState::SCS_Connected);
}

//=================================================================================================
// Block until all requested data has been received from the remote connection
//=================================================================================================
bool DataReceive(SocketInfo* pClientSocket, void* pDataIn, size_t Size)
{
if( !pClientSocket ) return false;

int32 totalRcv(0), sizeRcv(0);
while( totalRcv < static_cast<int>(Size) )
{
Expand All @@ -187,6 +189,8 @@ bool DataReceive(SocketInfo* pClientSocket, void* pDataIn, size_t Size)
//=================================================================================================
bool DataSend(SocketInfo* pClientSocket, void* pDataOut, size_t Size)
{
if( !pClientSocket ) return false;

int32 totalSent(0), sizeSent(0);
while( totalSent < static_cast<int>(Size) )
{
Expand Down
6 changes: 5 additions & 1 deletion Code/Client/Private/NetImgui_NetworkWin32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,16 @@ bool DataReceivePending(SocketInfo* pClientSocket)
char Unused[4];
int resultRcv = recv(pClientSocket->mSocket, Unused, 1, MSG_PEEK);
// Note: return true on a connection error, to exit code looping on the data wait
return resultRcv > 0 || (resultRcv == SOCKET_ERROR && WSAGetLastError() != WSAEWOULDBLOCK);
return !pClientSocket || resultRcv > 0 || (resultRcv == SOCKET_ERROR && WSAGetLastError() != WSAEWOULDBLOCK);
}

//=================================================================================================
// Block until all requested data has been received from the remote connection
//=================================================================================================
bool DataReceive(SocketInfo* pClientSocket, void* pDataIn, size_t Size)
{
if( !pClientSocket ) return false;

int totalRcv(0);
while( totalRcv < static_cast<int>(Size) )
{
Expand All @@ -207,6 +209,8 @@ bool DataReceive(SocketInfo* pClientSocket, void* pDataIn, size_t Size)
//=================================================================================================
bool DataSend(SocketInfo* pClientSocket, void* pDataOut, size_t Size)
{
if( !pClientSocket ) return false;

int totalSent(0);
while( totalSent < static_cast<int>(Size) )
{
Expand Down
2 changes: 1 addition & 1 deletion Code/ServerApp/Source/NetImguiServer_Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ void NetworkConnectRequest_Send()
{
NetImguiServer::Config::Client::SetProperty_ConnectRequest(clientConfig.mRuntimeID, false, false); // Reset the Connection request, we are processing it
NetImguiServer::Config::Client::SetProperty_Status(clientConfig.mRuntimeID, NetImguiServer::Config::Client::eStatus::Disconnected);
clientConfigID = clientConfig.mRuntimeID; // Keep track of ClientConfig we are attempting to connect to
clientConfigID = clientConfig.mRuntimeID; // Keep track of ClientConfig we are attempting to connect to
pClientSocket = NetImgui::Internal::Network::Connect(clientConfig.mHostName, clientConfig.mHostPort);
}
}
Expand Down

0 comments on commit c89ae6b

Please sign in to comment.