Skip to content

Commit

Permalink
DEV9: Use ReceivedPayload more in TCP_Session
Browse files Browse the repository at this point in the history
Instead of shuffling a unique_ptr around
Fixes handling of connection failure in ConnectTCPComplete()
  • Loading branch information
TheLastRar authored and F0bes committed Oct 15, 2024
1 parent e3a666b commit 0806ca4
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 30 deletions.
10 changes: 5 additions & 5 deletions pcsx2/DEV9/Sessions/TCP_Session/TCP_Session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ using namespace PacketReader::IP::TCP;

namespace Sessions
{
void TCP_Session::PushRecvBuff(std::unique_ptr<TCP_Packet> tcp)
void TCP_Session::PushRecvBuff(ReceivedPayload tcp)
{
_recvBuff.Enqueue(std::move(tcp));
}
std::unique_ptr<TCP_Packet> TCP_Session::PopRecvBuff()
std::optional<ReceivedPayload> TCP_Session::PopRecvBuff()
{
std::unique_ptr<TCP_Packet> ret;
ReceivedPayload ret;
if (_recvBuff.Dequeue(&ret))
return ret;
else
return nullptr;
return std::nullopt;
}

void TCP_Session::IncrementMyNumber(u32 amount)
Expand Down Expand Up @@ -159,7 +159,7 @@ namespace Sessions
// Clear out _recvBuff
while (!_recvBuff.IsQueueEmpty())
{
std::unique_ptr<TCP_Packet> retPay;
ReceivedPayload retPay;
if (!_recvBuff.Dequeue(&retPay))
{
using namespace std::chrono_literals;
Expand Down
12 changes: 6 additions & 6 deletions pcsx2/DEV9/Sessions/TCP_Session/TCP_Session.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ namespace Sessions
Bad
};

SimpleQueue<std::unique_ptr<PacketReader::IP::TCP::TCP_Packet>> _recvBuff;
SimpleQueue<ReceivedPayload> _recvBuff;

#ifdef _WIN32
SOCKET client = INVALID_SOCKET;
Expand Down Expand Up @@ -85,8 +85,8 @@ namespace Sessions

private:
// Async functions
void PushRecvBuff(std::unique_ptr<PacketReader::IP::TCP::TCP_Packet> tcp);
std::unique_ptr<PacketReader::IP::TCP::TCP_Packet> PopRecvBuff();
void PushRecvBuff(ReceivedPayload tcp);
std::optional<ReceivedPayload> PopRecvBuff();

void IncrementMyNumber(u32 amount);
void UpdateReceivedAckNumber(u32 ack);
Expand All @@ -104,7 +104,7 @@ namespace Sessions
bool ValidateEmptyPacket(PacketReader::IP::TCP::TCP_Packet* tcp, bool ignoreOld = true);

// PS2 sent SYN
std::unique_ptr<PacketReader::IP::TCP::TCP_Packet> ConnectTCPComplete(bool success);
std::optional<ReceivedPayload> ConnectTCPComplete(bool success);
bool SendConnect(PacketReader::IP::TCP::TCP_Packet* tcp);
bool SendConnected(PacketReader::IP::TCP::TCP_Packet* tcp);

Expand All @@ -120,7 +120,7 @@ namespace Sessions
* S4: PS2 then Sends ACK
*/
bool CloseByPS2Stage1_2(PacketReader::IP::TCP::TCP_Packet* tcp);
std::unique_ptr<PacketReader::IP::TCP::TCP_Packet> CloseByPS2Stage3();
ReceivedPayload CloseByPS2Stage3();
bool CloseByPS2Stage4(PacketReader::IP::TCP::TCP_Packet* tcp);

/*
Expand All @@ -132,7 +132,7 @@ namespace Sessions
* Closing_ClosedByRemoteThenPS2_WaitingForAck
* we then check if S3 has been completed
*/
std::unique_ptr<PacketReader::IP::TCP::TCP_Packet> CloseByRemoteStage1();
ReceivedPayload CloseByRemoteStage1();
bool CloseByRemoteStage2_ButAfter4(PacketReader::IP::TCP::TCP_Packet* tcp);
bool CloseByRemoteStage3_4(PacketReader::IP::TCP::TCP_Packet* tcp);

Expand Down
30 changes: 15 additions & 15 deletions pcsx2/DEV9/Sessions/TCP_Session/TCP_Session_In.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ namespace Sessions
{
std::optional<ReceivedPayload> TCP_Session::Recv()
{
std::unique_ptr<TCP_Packet> ret = PopRecvBuff();
if (ret != nullptr)
return ReceivedPayload{destIP, std::move(ret)};
std::optional<ReceivedPayload> ret = PopRecvBuff();
if (ret.has_value())
return ret;

switch (state)
{
Expand All @@ -43,9 +43,9 @@ namespace Sessions
select(client + 1, nullptr, &writeSet, &exceptSet, &nowait);

if (FD_ISSET(client, &writeSet))
return ReceivedPayload{destIP, ConnectTCPComplete(true)};
return ConnectTCPComplete(true);
if (FD_ISSET(client, &exceptSet))
return ReceivedPayload{destIP, ConnectTCPComplete(false)};
return ConnectTCPComplete(false);

return std::nullopt;
}
Expand Down Expand Up @@ -153,9 +153,9 @@ namespace Sessions
switch (state)
{
case TCP_State::Connected:
return ReceivedPayload{destIP, CloseByRemoteStage1()};
return CloseByRemoteStage1();
case TCP_State::Closing_ClosedByPS2:
return ReceivedPayload{destIP, CloseByPS2Stage3()};
return CloseByPS2Stage3();
default:
CloseByRemoteRST();
Console.Error("DEV9: TCP: Remote close occured with invalid TCP state");
Expand Down Expand Up @@ -183,7 +183,7 @@ namespace Sessions
return std::nullopt;
}

std::unique_ptr<TCP_Packet> TCP_Session::ConnectTCPComplete(bool success)
std::optional<ReceivedPayload> TCP_Session::ConnectTCPComplete(bool success)
{
if (success)
{
Expand Down Expand Up @@ -217,7 +217,7 @@ namespace Sessions

ret->options.push_back(new TCPopTS(timestampSeconds, lastRecivedTimeStamp));
}
return ret;
return ReceivedPayload{destIP, std::move(ret)};
}
else
{
Expand All @@ -232,15 +232,15 @@ namespace Sessions
Console.Error("DEV9: TCP: Unkown TCP connection error (getsockopt error: %d)", errno);
#endif
else
Console.Error("DEV9: TCP: Send error: %d", error);
Console.Error("DEV9: TCP: Connect error: %d", error);

state = TCP_State::CloseCompleted;
RaiseEventConnectionClosed();
return nullptr;
return std::nullopt;
}
}

std::unique_ptr<TCP_Packet> TCP_Session::CloseByPS2Stage3()
ReceivedPayload TCP_Session::CloseByPS2Stage3()
{
//Console.WriteLn("DEV9: TCP: Remote has closed connection after PS2");

Expand All @@ -254,10 +254,10 @@ namespace Sessions
//DevCon.WriteLn("myNumberACKed reset");

state = TCP_State::Closing_ClosedByPS2ThenRemote_WaitingForAck;
return ret;
return ReceivedPayload{destIP, std::move(ret)};
}

std::unique_ptr<TCP_Packet> TCP_Session::CloseByRemoteStage1()
ReceivedPayload TCP_Session::CloseByRemoteStage1()
{
//Console.WriteLn("DEV9: TCP: Remote has closed connection");

Expand All @@ -271,6 +271,6 @@ namespace Sessions
//DevCon.WriteLn("myNumberACKed reset");

state = TCP_State::Closing_ClosedByRemote;
return ret;
return ReceivedPayload{destIP, std::move(ret)};
}
} // namespace Sessions
8 changes: 4 additions & 4 deletions pcsx2/DEV9/Sessions/TCP_Session/TCP_Session_Out.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ namespace Sessions
std::unique_ptr<TCP_Packet> ret = CreateBasePacket();
ret->SetACK(true);

PushRecvBuff(std::move(ret));
PushRecvBuff(ReceivedPayload{destIP, std::move(ret)});
}
return true;
}
Expand Down Expand Up @@ -524,7 +524,7 @@ namespace Sessions
std::unique_ptr<TCP_Packet> ret = CreateBasePacket();

ret->SetACK(true);
PushRecvBuff(std::move(ret));
PushRecvBuff(ReceivedPayload{destIP, std::move(ret)});

return true;
}
Expand Down Expand Up @@ -591,7 +591,7 @@ namespace Sessions

ret->SetACK(true);

PushRecvBuff(std::move(ret));
PushRecvBuff(ReceivedPayload{destIP, std::move(ret)});

if (myNumberACKed.load())
{
Expand All @@ -611,7 +611,7 @@ namespace Sessions
{
std::unique_ptr<TCP_Packet> reterr = CreateBasePacket();
reterr->SetRST(true);
PushRecvBuff(std::move(reterr));
PushRecvBuff(ReceivedPayload{destIP, std::move(reterr)});

CloseSocket();
state = TCP_State::CloseCompletedFlushBuffer;
Expand Down

0 comments on commit 0806ca4

Please sign in to comment.