Skip to content

Commit

Permalink
Com: Change to UE networking code to new protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
sammyfreg committed Dec 6, 2024
1 parent 237dc78 commit 84eac25
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 41 deletions.
9 changes: 8 additions & 1 deletion Code/Client/Private/NetImgui_Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,14 @@ bool Communications_Initialize(ClientInfo& client)
client.mPendingRcv = PendingCom();
client.mPendingSend = PendingCom();
}
return client.mpSocketPending.load() == nullptr;

// Disconnect pending socket if init failed
Network::SocketInfo* SocketPending = client.mpSocketPending.exchange(nullptr);
if( SocketPending ){
NetImgui::Internal::Network::Disconnect(SocketPending);
return false;
}
return true;
}

//=================================================================================================
Expand Down
86 changes: 50 additions & 36 deletions Code/Client/Private/NetImgui_NetworkUE4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ struct SocketInfo
{
mpSocket->SetNonBlocking(true);
mpSocket->SetNoDelay(true);

int32 NewSize(0);
while( !mpSocket->SetSendBufferSize(2*mSendSize, NewSize) ){
mSendSize /= 2;
}
mSendSize = NewSize/2;
}
}

Expand All @@ -46,7 +52,8 @@ struct SocketInfo
mpSocket = nullptr;
}
}
FSocket* mpSocket = nullptr;
FSocket* mpSocket = nullptr;
int32 mSendSize = 1024*1024; // Limit tx data to avoid socket error on large amount (texture)
};

bool Startup()
Expand Down Expand Up @@ -149,7 +156,7 @@ void Disconnect(SocketInfo* pClientSocket)
}

//=================================================================================================
// Return true if data has been received (or there's a connection error)
// Return true if data has been received, or there's a connection error
//=================================================================================================
bool DataReceivePending(SocketInfo* pClientSocket)
{
Expand All @@ -159,55 +166,62 @@ bool DataReceivePending(SocketInfo* pClientSocket)
}

//=================================================================================================
// Block until all requested data has been received from the remote connection
// Receive as much as possible a command and keep track of transfer status
//=================================================================================================
bool DataReceive(SocketInfo* pClientSocket, void* pDataIn, size_t Size)
void DataReceive(SocketInfo* pClientSocket, NetImgui::Internal::PendingCom& PendingComRcv)
{
if( !pClientSocket ) return false;
// Invalid command
if( !pClientSocket || !PendingComRcv.pCommand ){
PendingComRcv.bError = true;
}

int32 totalRcv(0), sizeRcv(0);
while( totalRcv < static_cast<int>(Size) )
int32 sizeRcv(0);
if( pClientSocket->mpSocket->Recv( &reinterpret_cast<uint8*>(PendingComRcv.pCommand)[PendingComRcv.SizeCurrent],
static_cast<int>(PendingComRcv.pCommand->mSize-PendingComRcv.SizeCurrent),
sizeRcv,
ESocketReceiveFlags::None) )
{
if( pClientSocket->mpSocket->Recv(&reinterpret_cast<uint8*>(pDataIn)[totalRcv], static_cast<int>(Size)-totalRcv, sizeRcv, ESocketReceiveFlags::None) )
{
totalRcv += sizeRcv;
}
else
{
if( pClientSocket->mpSocket->GetConnectionState() != ESocketConnectionState::SCS_Connected )
{
return false; // Connection error, abort transmission
}
std::this_thread::yield();
PendingComRcv.SizeCurrent += static_cast<size_t>(sizeRcv);
}
// Connection error, abort transmission
else
{
const ESocketErrors SocketError = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->GetLastErrorCode();
if( SocketError != ESocketErrors::SE_EWOULDBLOCK ){
PendingComRcv.bError = true;
}
}
return totalRcv == static_cast<int32>(Size);
}

//=================================================================================================
// Block until all requested data has been sent to remote connection
// Receive as much as possible a command and keep track of transfer status
//=================================================================================================
bool DataSend(SocketInfo* pClientSocket, void* pDataOut, size_t Size)
void DataSend(SocketInfo* pClientSocket, NetImgui::Internal::PendingCom& PendingComSend)
{
if( !pClientSocket ) return false;
// Invalid command
if( !pClientSocket || !PendingComSend.pCommand ){
PendingComSend.bError = true;
}

int32 sizeSent = 0;
int32 sizeToSend = PendingComSend.pCommand->mSize-PendingComSend.SizeCurrent;
sizeToSend = sizeToSend > pClientSocket->mSendSize ? pClientSocket->mSendSize : sizeToSend;

int32 totalSent(0), sizeSent(0);
while( totalSent < static_cast<int>(Size) )
if( pClientSocket->mpSocket->Send( &reinterpret_cast<uint8*>(PendingComSend.pCommand)[PendingComSend.SizeCurrent],
static_cast<int>(sizeToSend),
sizeSent) )
{
if( pClientSocket->mpSocket->Send(&reinterpret_cast<uint8*>(pDataOut)[totalSent], Size-totalSent, sizeSent) )
{
totalSent += sizeSent;
}
else
PendingComSend.SizeCurrent += static_cast<size_t>(sizeSent);
}
// Connection error, abort transmission
else
{
const ESocketErrors SocketError = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->GetLastErrorCode();
if( SocketError != ESocketErrors::SE_EWOULDBLOCK )
{
if( pClientSocket->mpSocket->GetConnectionState() != ESocketConnectionState::SCS_Connected )
{
return false; // Connection error, abort transmission
}
std::this_thread::yield();
}
PendingComSend.bError = true;
}
}
return totalSent == static_cast<int32>(Size);
}

}}} // namespace NetImgui::Internal::Network
Expand Down
6 changes: 3 additions & 3 deletions Code/Client/Private/NetImgui_NetworkWin32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ void Disconnect(SocketInfo* pClientSocket)
}

//=================================================================================================
// Return trie if data has been received, or there's a connection error
// Return true if data has been received, or there's a connection error
//=================================================================================================
bool DataReceivePending(SocketInfo* pClientSocket)
{
Expand All @@ -173,7 +173,7 @@ bool DataReceivePending(SocketInfo* pClientSocket)
}

//=================================================================================================
// Try receiving data from remote connection
// Receive as much as possible a command and keep track of transfer status
//=================================================================================================
void DataReceive(SocketInfo* pClientSocket, NetImgui::Internal::PendingCom& PendingComRcv)
{
Expand All @@ -198,7 +198,7 @@ void DataReceive(SocketInfo* pClientSocket, NetImgui::Internal::PendingCom& Pend
}

//=================================================================================================
// Block until all requested data has been sent to remote connection
// Receive as much as possible a command and keep track of transfer status
//=================================================================================================
void DataSend(SocketInfo* pClientSocket, NetImgui::Internal::PendingCom& PendingComSend)
{
Expand Down
1 change: 0 additions & 1 deletion Code/ServerApp/Source/NetImguiServer_Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ void Communications_Incoming_CmdClipboard(RemoteClient::Client& Client)

//=================================================================================================
// Receive every commands sent by remote client and process them
// We keep receiving until we detect a ping command (signal end of commands)
//=================================================================================================
void Communications_Incoming(RemoteClient::Client& Client)
{
Expand Down

0 comments on commit 84eac25

Please sign in to comment.