DEV9: Format comments in TCP session

This commit is contained in:
TheLastRar 2024-04-21 15:58:44 +01:00 committed by lightningterror
parent 56b06a0495
commit 894f2f145c
4 changed files with 93 additions and 94 deletions

View File

@ -110,7 +110,7 @@ namespace Sessions
TCP_Packet* ret = new TCP_Packet(data);
//and now to setup THE ENTIRE THING
// Setup common packet infomation
ret->sourcePort = destPort;
ret->destinationPort = srcPort;
@ -149,7 +149,6 @@ namespace Sessions
void TCP_Session::Reset()
{
//CloseSocket();
RaiseEventConnectionClosed();
}
@ -157,7 +156,7 @@ namespace Sessions
{
CloseSocket();
//Clear out _recvBuff
// Clear out _recvBuff
while (!_recvBuff.IsQueueEmpty())
{
TCP_Packet* retPay;

View File

@ -32,7 +32,7 @@ namespace Sessions
Closing_ClosedByPS2ThenRemote_WaitingForAck,
Closing_ClosedByRemote,
Closing_ClosedByRemoteThenPS2_WaitingForAck,
CloseCompletedFlushBuffer, //Packets in recvBuff to send
CloseCompletedFlushBuffer, // Send any remaining packets in recvBuff
CloseCompleted,
};
enum struct NumCheckResult
@ -55,17 +55,17 @@ namespace Sessions
u16 srcPort = 0;
u16 destPort = 0;
u16 maxSegmentSize = 1460; //Accesed By Both In and Out Threads, but set only on Connect Thread
u16 maxSegmentSize = 1460; // Accesed by both in and out threads, but set only on connect thread
int windowScale = 0;
std::atomic<int> windowSize{1460}; //Make atomic instead
std::atomic<int> windowSize{1460};
u32 lastRecivedTimeStamp; //Accesed By Both In and Out Threads
std::chrono::steady_clock::time_point timeStampStart; //Set By In on connect, read by In and Out Threads, unsure as to correct C++ type
bool sendTimeStamps = false; //Accesed By Out Thread Only
u32 lastRecivedTimeStamp; // Accesed by both in and out threads
std::chrono::steady_clock::time_point timeStampStart; // Set by in thread on connect, read by in and out threads
bool sendTimeStamps = false; // Accesed by out thread only
const int receivedPS2SeqNumberCount = 5;
u32 expectedSeqNumber; //Accesed By Out Thread Only
std::vector<u32> receivedPS2SeqNumbers; //Accesed By Out Thread Only
u32 expectedSeqNumber; // Accesed by out thread only
std::vector<u32> receivedPS2SeqNumbers; // Accesed by out thread only
std::mutex myNumberSentry;
const int oldMyNumCount = 64;
@ -84,8 +84,7 @@ namespace Sessions
virtual ~TCP_Session();
private:
//Async stuff
// Async functions
void PushRecvBuff(PacketReader::IP::TCP::TCP_Packet* tcp);
PacketReader::IP::TCP::TCP_Packet* PopRecvBuff();
@ -99,11 +98,12 @@ namespace Sessions
NumCheckResult CheckRepeatSYNNumbers(PacketReader::IP::TCP::TCP_Packet* tcp);
NumCheckResult CheckNumbers(PacketReader::IP::TCP::TCP_Packet* tcp, bool rejectOldSeq = false);
s32 GetDelta(u32 a, u32 b); //Returns a - b
//Returns true if errored
// Returns a - b, accounting for overflow
s32 GetDelta(u32 a, u32 b);
// Returns true if errored
bool ValidateEmptyPacket(PacketReader::IP::TCP::TCP_Packet* tcp, bool ignoreOld = true);
//PS2 sent SYN
// PS2 sent SYN
PacketReader::IP::TCP::TCP_Packet* ConnectTCPComplete(bool success);
bool SendConnect(PacketReader::IP::TCP::TCP_Packet* tcp);
bool SendConnected(PacketReader::IP::TCP::TCP_Packet* tcp);
@ -111,33 +111,35 @@ namespace Sessions
bool SendData(PacketReader::IP::TCP::TCP_Packet* tcp);
bool SendNoData(PacketReader::IP::TCP::TCP_Packet* tcp);
//On Close by PS2
//S1: PS2 Sends FIN+ACK
//S2: CloseByPS2Stage1_2 sends ACK, state set to Closing_ClosedByPS2
//S3: When server closes socket, we send FIN in CloseByPS2Stage3
//and set state to Closing_ClosedByPS2ThenRemote_WaitingForAck
//S4: PS2 then Sends ACK
/*
* On close by PS2
* S1: PS2 Sends FIN+ACK
* S2: CloseByPS2Stage1_2 sends ACK, state set to Closing_ClosedByPS2
* S3: When server closes socket, we send FIN in CloseByPS2Stage3
* and set state to Closing_ClosedByPS2ThenRemote_WaitingForAck
* S4: PS2 then Sends ACK
*/
bool CloseByPS2Stage1_2(PacketReader::IP::TCP::TCP_Packet* tcp);
PacketReader::IP::TCP::TCP_Packet* CloseByPS2Stage3();
bool CloseByPS2Stage4(PacketReader::IP::TCP::TCP_Packet* tcp);
//On Close By Server
//S1: CloseByRemoteStage1 sends FIN+ACK, state set to Closing_ClosedByRemote
//S2: PS2 Will then sends ACK, this is only checked after stage4
//S3: PS2 Will send FIN, possible in the previous ACK packet
//S4: CloseByRemoteStage3_4 sends ACK, state set to
//Closing_ClosedByRemoteThenPS2_WaitingForAck
//We Then Check if S3 has been compleated
/*
* On close By Server
* S1: CloseByRemoteStage1 sends FIN+ACK, state set to Closing_ClosedByRemote
* S2: PS2 Will then sends ACK, this is only checked after stage4
* S3: PS2 Will send FIN, possible in the previous ACK packet
* S4: CloseByRemoteStage3_4 sends ACK, state set to
* Closing_ClosedByRemoteThenPS2_WaitingForAck
* we then check if S3 has been completed
*/
PacketReader::IP::TCP::TCP_Packet* CloseByRemoteStage1();
bool CloseByRemoteStage2_ButAfter4(PacketReader::IP::TCP::TCP_Packet* tcp);
bool CloseByRemoteStage3_4(PacketReader::IP::TCP::TCP_Packet* tcp);
//Error on sending data
// Error on sending data
void CloseByRemoteRST();
//Returned TCP_Packet Takes ownership of data
// Returned TCP_Packet takes ownership of data
PacketReader::IP::TCP::TCP_Packet* CreateBasePacket(PacketReader::PayloadData* data = nullptr);
void CloseSocket();

View File

@ -50,18 +50,20 @@ namespace Sessions
return nullptr;
}
case TCP_State::SentSYN_ACK:
//Don't read data untill PS2 ACKs connection
// Don't read data untill PS2 ACKs connection
return nullptr;
case TCP_State::CloseCompletedFlushBuffer:
//When TCP connection is closed by the server
//the server is the last to send a packet
//so the event must be raised here
/*
* When TCP connection is closed by the server
* the server is the last to send a packet
* so the event must be raised here
*/
state = TCP_State::CloseCompleted;
RaiseEventConnectionClosed();
return nullptr;
case TCP_State::Connected:
case TCP_State::Closing_ClosedByPS2:
//Only accept data in above two states
// Only accept data in above two states
break;
default:
return nullptr;
@ -70,8 +72,8 @@ namespace Sessions
if (ShouldWaitForAck())
return nullptr;
//Note, windowSize will be updated before _ReceivedAckNumber, potential race condition
//in practice, we just get a smaller or -ve maxSize
// Note, windowSize will be updated before _ReceivedAckNumber, potential race condition
// in practice, we just get a smaller or -ve maxSize
const u32 outstanding = GetOutstandingSequenceLength();
int maxSize = 0;
@ -86,8 +88,8 @@ namespace Sessions
int err = 0;
int recived;
//FIONREAD uses unsigned long on windows and int on linux
//Zero init so we don't have bad data on any unused bytes
// FIONREAD uses unsigned long on windows and int on linux
// Zero init so we don't have bad data on any unused bytes
unsigned long available = 0;
#ifdef _WIN32
err = ioctlsocket(client, FIONREAD, &available);
@ -113,9 +115,8 @@ namespace Sessions
#ifdef _WIN32
case WSAEINVAL:
case WSAESHUTDOWN:
//In theory, this should only occur when the PS2 has RST the connection
//and the call to TCPSession.Recv() occurs at just the right time.
// In theory, this should only occur when the PS2 has RST the connection
// and the call to TCPSession.Recv() occurs at just the right time.
//Console.WriteLn("DEV9: TCP: Recv() on shutdown socket");
return nullptr;
case WSAEWOULDBLOCK:
@ -123,7 +124,7 @@ namespace Sessions
#elif defined(__POSIX__)
case EINVAL:
case ESHUTDOWN:
//See WSAESHUTDOWN
// See WSAESHUTDOWN
//Console.WriteLn("DEV9: TCP: Recv() on shutdown socket");
return nullptr;
case EWOULDBLOCK:
@ -137,7 +138,7 @@ namespace Sessions
return nullptr;
}
//Server Closed Socket
// Server closed the Socket
if (recived == 0)
{
int result = shutdown(client, SD_RECEIVE);
@ -189,7 +190,7 @@ namespace Sessions
state = TCP_State::SentSYN_ACK;
TCP_Packet* ret = new TCP_Packet(new PayloadData(0));
//Return the fact we connected
// Send packet to say we connected
ret->sourcePort = destPort;
ret->destinationPort = srcPort;

View File

@ -38,18 +38,16 @@ namespace Sessions
}
}
//Maybe untested
// Maybe untested
if (tcp.GetRST() == true)
{
//DevCon.Writeln("DEV9: TCP: PS2 has reset connection");
//PS2 has reset connection;
// PS2 has reset connection;
if (client != INVALID_SOCKET)
CloseSocket();
else
Console.Error("DEV9: TCP: Reset closed connection");
//PS2 sent RST
//clearly not expecting
//more data
// PS2 sent RST, clearly not expecting more data
state = TCP_State::CloseCompleted;
RaiseEventConnectionClosed();
return true;
@ -65,11 +63,11 @@ namespace Sessions
Console.Error("DEV9: TCP: Invalid repeated SYN (SendingSYN_ACK)");
return false;
}
return true; //Ignore reconnect attempts while we are still attempting connection
return true; // Ignore reconnect attempts while we are still attempting connection
case TCP_State::SentSYN_ACK:
return SendConnected(&tcp);
case TCP_State::Connected:
if (tcp.GetFIN() == true) //Connection Close Part 1, received FIN from PS2
if (tcp.GetFIN() == true) // Connection close part 1, received FIN from PS2
return CloseByPS2Stage1_2(&tcp);
else
return SendData(&tcp);
@ -80,7 +78,7 @@ namespace Sessions
return CloseByPS2Stage4(&tcp);
case TCP_State::Closing_ClosedByRemote:
if (tcp.GetFIN() == true) //Connection Close Part 3, received FIN from PS2
if (tcp.GetFIN() == true) // Connection close part 3, received FIN from PS2
return CloseByRemoteStage3_4(&tcp);
return SendData(&tcp);
@ -96,10 +94,10 @@ namespace Sessions
}
}
//PS2 sent SYN
// PS2 sent SYN
bool TCP_Session::SendConnect(TCP_Packet* tcp)
{
//Expect SYN Packet
// Expects SYN Packet
destPort = tcp->destinationPort;
srcPort = tcp->sourcePort;
@ -110,7 +108,7 @@ namespace Sessions
return true;
}
expectedSeqNumber = tcp->sequenceNumber + 1;
//Fill out last received numbers
// Reset last received numbers
receivedPS2SeqNumbers.clear();
for (int i = 0; i < receivedPS2SeqNumberCount; i++)
receivedPS2SeqNumbers.push_back(tcp->sequenceNumber);
@ -121,18 +119,18 @@ namespace Sessions
{
switch (tcp->options[i]->GetCode())
{
case 0: //End
case 1: //Nop
case 0: // End
case 1: // Nop
continue;
case 2: //MSS
case 2: // MSS
maxSegmentSize = static_cast<TCPopMSS*>(tcp->options[i])->maxSegmentSize;
break;
case 3: //WindowScale
case 3: // WindowScale
windowScale = static_cast<TCPopWS*>(tcp->options[i])->windowScale;
if (windowScale > 0)
Console.Error("DEV9: TCP: Non-zero window scale option");
break;
case 8: //TimeStamp
case 8: // TimeStamp
lastRecivedTimeStamp = static_cast<TCPopTS*>(tcp->options[i])->senderTimeStamp;
sendTimeStamps = true;
timeStampStart = std::chrono::steady_clock::now();
@ -147,7 +145,6 @@ namespace Sessions
CloseSocket();
//client = new Socket(SocketType.Stream, ProtocolType.Tcp);
client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (client == INVALID_SOCKET)
{
@ -196,7 +193,7 @@ namespace Sessions
#endif
const int noDelay = true; //BOOL
const int noDelay = true; // BOOL on Windows
ret = setsockopt(client, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast<const char*>(&noDelay), sizeof(noDelay));
if (ret != 0)
@ -228,14 +225,14 @@ namespace Sessions
RaiseEventConnectionClosed();
return false;
}
//Compleation of socket connection checked in recv
// Compleation of socket connection checked in recv
}
state = TCP_State::SendingSYN_ACK;
return true;
}
//PS2 responding to our SYN-ACK (by sending ACK)
// PS2 responding to our SYN-ACK (by sending ACK)
bool TCP_Session::SendConnected(TCP_Packet* tcp)
{
if (tcp->GetSYN() == true)
@ -246,7 +243,7 @@ namespace Sessions
Console.Error("DEV9: TCP: Invalid repeated SYN (SentSYN_ACK)");
return true;
}
return true; //Ignore reconnect attempts while we are still attempting connection
return true; // Ignore reconnect attempts while we are still attempting connection
}
const NumCheckResult Result = CheckNumbers(tcp);
if (Result == NumCheckResult::Bad)
@ -260,10 +257,10 @@ namespace Sessions
{
switch (tcp->options[i]->GetCode())
{
case 0: //End
case 1: //Nop
case 0: // End
case 1: // Nop
continue;
case 8: //Timestamp
case 8: // Timestamp
lastRecivedTimeStamp = static_cast<TCPopTS*>(tcp->options[i])->senderTimeStamp;
break;
default:
@ -271,7 +268,7 @@ namespace Sessions
break;
}
}
//Next packet will be data
// Next packet will be data
state = TCP_State::Connected;
return true;
}
@ -294,8 +291,8 @@ namespace Sessions
{
switch (tcp->options[i]->GetCode())
{
case 0: //End
case 1: //Nop
case 0: // End
case 1: // Nop
continue;
case 8:
lastRecivedTimeStamp = static_cast<TCPopTS*>(tcp->options[i])->senderTimeStamp;
@ -318,7 +315,7 @@ namespace Sessions
}
if (tcp->GetPayload()->GetLength() != 0)
{
//Check if we already have sent some of this data
// Check if we already have sent some of this data
const int delta = GetDelta(expectedSeqNumber, tcp->sequenceNumber);
pxAssert(delta >= 0);
//if (Result == NumCheckResult::OldSeq)
@ -333,7 +330,7 @@ namespace Sessions
receivedPS2SeqNumbers.erase(receivedPS2SeqNumbers.begin());
receivedPS2SeqNumbers.push_back(expectedSeqNumber);
//Send the Data
// Send the Data
int sent = 0;
PayloadPtr* payload = static_cast<PayloadPtr*>(tcp->GetPayload());
while (sent != payload->GetLength())
@ -362,9 +359,9 @@ namespace Sessions
}
expectedSeqNumber += tcp->GetPayload()->GetLength() - delta;
//Done send
// Done send
}
//ACK data
// ACK data
//DevCon.WriteLn("[SRV] ACK data: %u", expectedSeqNumber);
TCP_Packet* ret = CreateBasePacket();
ret->SetACK(true);
@ -386,8 +383,8 @@ namespace Sessions
{
switch (tcp->options[i]->GetCode())
{
case 0: //End
case 1: //Nop
case 0: // End
case 1: // Nop
continue;
case 8:
lastRecivedTimeStamp = static_cast<TCPopTS*>(tcp->options[i])->senderTimeStamp;
@ -431,7 +428,7 @@ namespace Sessions
{
//DevCon.WriteLn("DEV9: TCP: [PS2] Sent outdated acknowledgement number, got %u expected %u", tcp->acknowledgementNumber, seqNum);
//Check if oldSeqNums contains tcp->acknowledgementNumber
// Check if oldSeqNums contains tcp->acknowledgementNumber
if (std::find(oldSeqNums.begin(), oldSeqNums.end(), tcp->acknowledgementNumber) == oldSeqNums.end())
{
Console.Error("DEV9: TCP: [PS2] Sent unexpected acknowledgement number, did not match old numbers, got %u expected %u", tcp->acknowledgementNumber, seqNum);
@ -460,7 +457,7 @@ namespace Sessions
}
else
{
//Check if receivedPS2SeqNumbers contains tcp->sequenceNumber
// Check if receivedPS2SeqNumbers contains tcp->sequenceNumber
if (std::find(receivedPS2SeqNumbers.begin(), receivedPS2SeqNumbers.end(), tcp->sequenceNumber) == receivedPS2SeqNumbers.end())
{
Console.Error("DEV9: TCP: [PS2] Sent outdated sequence number in an data packet, got %u expected %u", tcp->sequenceNumber, expectedSeqNumber);
@ -489,7 +486,7 @@ namespace Sessions
{
const int delta = GetDelta(expectedSeqNumber, tcp->sequenceNumber);
pxAssert(delta >= 0);
//Check if packet contains only old data
// Check if packet contains only old data
if (delta >= tcp->GetPayload()->GetLength())
return false;
@ -500,12 +497,12 @@ namespace Sessions
return false;
}
//Connection Closing Finished in CloseByPS2Stage4
// Connection Closing Finished in CloseByPS2Stage4
bool TCP_Session::CloseByPS2Stage1_2(TCP_Packet* tcp)
{
//Console.WriteLn("DEV9: TCP: PS2 has closed connection");
if (ValidateEmptyPacket(tcp, false)) //Sending FIN with data
if (ValidateEmptyPacket(tcp, false)) // Check if valid packet for FIN
return true;
receivedPS2SeqNumbers.erase(receivedPS2SeqNumbers.begin());
@ -523,7 +520,7 @@ namespace Sessions
errno);
#endif
//Connection Close Part 2, Send ACK to PS2
// Connection close part 2, send ACK to PS2
TCP_Packet* ret = CreateBasePacket();
ret->SetACK(true);
@ -532,10 +529,10 @@ namespace Sessions
return true;
}
//PS2 responding to server response to PS2 Closing connection
// PS2 responding to server response to PS2 closing connection
bool TCP_Session::CloseByPS2Stage4(TCP_Packet* tcp)
{
//Close Part 4, Receive ACK from PS2
// Close part 4, receive ACK from PS2
//Console.WriteLn("DEV9: TCP: Completed close by PS2");
if (ValidateEmptyPacket(tcp))
@ -545,8 +542,8 @@ namespace Sessions
{
//Console.WriteLn("DEV9: TCP: ACK was for FIN");
CloseSocket();
// recv buffer should be empty
state = TCP_State::CloseCompleted;
//recv buffer should be empty
RaiseEventConnectionClosed();
}
@ -564,8 +561,8 @@ namespace Sessions
{
//Console.WriteLn("DEV9: TCP: ACK was for FIN");
CloseSocket();
// Receive buffer may not be empty
state = TCP_State::CloseCompletedFlushBuffer;
//Recive buffer may not be empty
}
return true;
}
@ -574,7 +571,7 @@ namespace Sessions
{
//Console.WriteLn("DEV9: TCP: PS2 has closed connection after remote");
if (ValidateEmptyPacket(tcp, false)) //Sending FIN with data
if (ValidateEmptyPacket(tcp, false)) // Check if valid packet for FIN
return true;
receivedPS2SeqNumbers.erase(receivedPS2SeqNumbers.begin());
@ -601,7 +598,7 @@ namespace Sessions
//Console.WriteLn("DEV9: TCP: ACK was for FIN");
CloseSocket();
state = TCP_State::CloseCompletedFlushBuffer;
//Recive buffer may not be empty
// Receive buffer may not be empty
}
else
state = TCP_State::Closing_ClosedByRemoteThenPS2_WaitingForAck;
@ -609,7 +606,7 @@ namespace Sessions
return true;
}
//Error on sending data
// Error on sending data
void TCP_Session::CloseByRemoteRST()
{
TCP_Packet* reterr = CreateBasePacket();