Skip to content

Commit

Permalink
minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
GloryOfNight committed Mar 2, 2024
1 parent 7b079c7 commit cd1e160
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
10 changes: 5 additions & 5 deletions src/relay/relay.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ bool relay::init()
return false;
}

const int32_t wantedBufferSize = 0x20000;
const int32_t wantedBufferSize = 0x20000; // same value as in unreal server
int32_t actualBufferSize{};

if (!m_socket->setSendBufferSize(wantedBufferSize, actualBufferSize))
Expand Down Expand Up @@ -65,15 +65,15 @@ bool relay::run()
continue;

std::shared_ptr<internetaddr> recvAddr = udpsocketFactory::createInternetAddr();
auto bytesRead = m_socket->recvFrom(buffer.data(), buffer.size(), recvAddr.get());
const int32_t bytesRead = m_socket->recvFrom(buffer.data(), buffer.size(), recvAddr.get());
if (bytesRead > 0)
{
const auto findRes = m_addressMappedChannels.find(recvAddr);
// if has a channel mapped to the address as well as two peers, relay
// if recvAddr has a channel mapped to it, as well as two valid peers, relay packet to the other peer
if (findRes != m_addressMappedChannels.end() && findRes->second.m_peerA && findRes->second.m_peerB)
{
const auto sendAddr = *findRes->second.m_peerA != *recvAddr ? findRes->second.m_peerA : findRes->second.m_peerB;
auto bytesSent = m_socket->sendTo(buffer.data(), bytesRead, sendAddr.get());
const auto& sendAddr = *findRes->second.m_peerA != *recvAddr ? findRes->second.m_peerA : findRes->second.m_peerB;
int32_t bytesSent = m_socket->sendTo(buffer.data(), bytesRead, sendAddr.get());

if (bytesSent == -1)
{
Expand Down
16 changes: 12 additions & 4 deletions src/socket/unix/udpsocketUnix.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
#include <arpa/inet.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <poll.h>
#include <sys/socket.h>

udpsocketUnix::udpsocketUnix()
{
Expand Down Expand Up @@ -94,8 +94,12 @@ bool udpsocketUnix::waitForRead(int32_t timeoutms)
fdset.events = POLLIN;
fdset.revents = 0;

int res = ::poll(&fdset, 1, (int)timeoutms);
return res >= 0 ? fdset.revents & fdset.events : false;
const int res = ::poll(&fdset, 1, (int)timeoutms);

if (res < 0) [[unlikely]]
return false;

return fdset.revents & fdset.events;
}

bool udpsocketUnix::waitForWrite(int32_t timeoutms)
Expand All @@ -106,7 +110,11 @@ bool udpsocketUnix::waitForWrite(int32_t timeoutms)
fdset.revents = 0;

int res = ::poll(&fdset, 1, (int)timeoutms);
return res >= 0 ? fdset.revents & fdset.events : false;

if (res < 0) [[unlikely]]
return false;

return fdset.revents & fdset.events;
}

bool udpsocketUnix::isValid()
Expand Down

0 comments on commit cd1e160

Please sign in to comment.