From f7e9482038230e74741a71842cce6fe930cfe941 Mon Sep 17 00:00:00 2001 From: TheLastRar Date: Fri, 12 Apr 2024 17:14:13 +0100 Subject: [PATCH 1/6] DEV9: Eliminate some c-style casts in TCP_Session --- .../Sessions/TCP_Session/TCP_Session_In.cpp | 6 ++-- .../Sessions/TCP_Session/TCP_Session_Out.cpp | 29 ++++++++++--------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/pcsx2/DEV9/Sessions/TCP_Session/TCP_Session_In.cpp b/pcsx2/DEV9/Sessions/TCP_Session/TCP_Session_In.cpp index 8a8eeb50c5af7..eab10a3cc77cd 100644 --- a/pcsx2/DEV9/Sessions/TCP_Session/TCP_Session_In.cpp +++ b/pcsx2/DEV9/Sessions/TCP_Session/TCP_Session_In.cpp @@ -100,7 +100,7 @@ namespace Sessions Console.WriteLn("DEV9: TCP: Got a lot of data: %lu Using: %d", available, maxSize); buffer = std::make_unique(maxSize); - recived = recv(client, (char*)buffer.get(), maxSize, 0); + recived = recv(client, reinterpret_cast(buffer.get()), maxSize, 0); if (recived == -1) #ifdef _WIN32 err = WSAGetLastError(); @@ -223,11 +223,11 @@ namespace Sessions int error = 0; #ifdef _WIN32 int len = sizeof(error); - if (getsockopt(client, SOL_SOCKET, SO_ERROR, (char*)&error, &len) < 0) + if (getsockopt(client, SOL_SOCKET, SO_ERROR, reinterpret_cast(&error), &len) < 0) Console.Error("DEV9: TCP: Unkown TCP Connection Error (getsockopt Error: %d)", WSAGetLastError()); #elif defined(__POSIX__) socklen_t len = sizeof(error); - if (getsockopt(client, SOL_SOCKET, SO_ERROR, (char*)&error, &len) < 0) + if (getsockopt(client, SOL_SOCKET, SO_ERROR, reinterpret_cast(&error), &len) < 0) Console.Error("DEV9: TCP: Unkown TCP Connection Error (getsockopt Error: %d)", errno); #endif else diff --git a/pcsx2/DEV9/Sessions/TCP_Session/TCP_Session_Out.cpp b/pcsx2/DEV9/Sessions/TCP_Session/TCP_Session_Out.cpp index 9c039bbe25059..a57634eb14661 100644 --- a/pcsx2/DEV9/Sessions/TCP_Session/TCP_Session_Out.cpp +++ b/pcsx2/DEV9/Sessions/TCP_Session/TCP_Session_Out.cpp @@ -2,6 +2,7 @@ // SPDX-License-Identifier: LGPL-3.0+ #include +#include #include #ifdef __POSIX__ @@ -124,15 +125,15 @@ namespace Sessions case 1: //Nop continue; case 2: //MSS - maxSegmentSize = ((TCPopMSS*)(tcp->options[i]))->maxSegmentSize; + maxSegmentSize = static_cast(tcp->options[i])->maxSegmentSize; break; case 3: //WindowScale - windowScale = ((TCPopWS*)(tcp->options[i]))->windowScale; + windowScale = static_cast(tcp->options[i])->windowScale; if (windowScale > 0) Console.Error("DEV9: TCP: Non-Zero WindowScale Option"); break; case 8: //TimeStamp - lastRecivedTimeStamp = ((TCPopTS*)(tcp->options[i]))->senderTimeStamp; + lastRecivedTimeStamp = static_cast(tcp->options[i])->senderTimeStamp; sendTimeStamps = true; timeStampStart = std::chrono::steady_clock::now(); break; @@ -163,9 +164,9 @@ namespace Sessions int ret; if (adapterIP.integer != 0) { - sockaddr_in endpoint{0}; + sockaddr_in endpoint{}; endpoint.sin_family = AF_INET; - *(IP_Address*)&endpoint.sin_addr = adapterIP; + endpoint.sin_addr = std::bit_cast(adapterIP); ret = bind(client, (const sockaddr*)&endpoint, sizeof(endpoint)); @@ -196,7 +197,7 @@ namespace Sessions const int noDelay = true; //BOOL - ret = setsockopt(client, IPPROTO_TCP, TCP_NODELAY, (const char*)&noDelay, sizeof(noDelay)); + ret = setsockopt(client, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast(&noDelay), sizeof(noDelay)); if (ret != 0) Console.Error("DEV9: TCP: Failed to set TCP_NODELAY. Error: %d", @@ -206,9 +207,9 @@ namespace Sessions errno); #endif - sockaddr_in endpoint{0}; + sockaddr_in endpoint{}; endpoint.sin_family = AF_INET; - *(IP_Address*)&endpoint.sin_addr = destIP; + endpoint.sin_addr = std::bit_cast(destIP); endpoint.sin_port = htons(destPort); ret = connect(client, (const sockaddr*)&endpoint, sizeof(endpoint)); @@ -263,7 +264,7 @@ namespace Sessions case 1: //Nop continue; case 8: //Timestamp - lastRecivedTimeStamp = ((TCPopTS*)(tcp->options[i]))->senderTimeStamp; + lastRecivedTimeStamp = static_cast(tcp->options[i])->senderTimeStamp; break; default: Console.Error("DEV9: TCP: Got Unknown Option %d", tcp->options[i]->GetCode()); @@ -297,7 +298,7 @@ namespace Sessions case 1: //Nop continue; case 8: - lastRecivedTimeStamp = ((TCPopTS*)(tcp->options[i]))->senderTimeStamp; + lastRecivedTimeStamp = static_cast(tcp->options[i])->senderTimeStamp; break; default: Console.Error("DEV9: TCP: Got Unknown Option %d", tcp->options[i]->GetCode()); @@ -323,7 +324,7 @@ namespace Sessions //if (Result == NumCheckResult::OldSeq) //{ // DevCon.WriteLn("[PS2] New Data Offset: %d bytes", delta); - // DevCon.WriteLn("[PS2] New Data Length: %d bytes", ((uint)tcp->GetPayload()->GetLength() - delta)); + // DevCon.WriteLn("[PS2] New Data Length: %d bytes", tcp->GetPayload()->GetLength() - delta); //} if (tcp->GetPayload()->GetLength() - delta > 0) { @@ -337,7 +338,7 @@ namespace Sessions PayloadPtr* payload = static_cast(tcp->GetPayload()); while (sent != payload->GetLength()) { - int ret = send(client, (const char*)&payload->data[sent], payload->GetLength() - sent, 0); + int ret = send(client, reinterpret_cast(&payload->data[sent]), payload->GetLength() - sent, 0); if (sent == SOCKET_ERROR) { @@ -360,7 +361,7 @@ namespace Sessions sent += ret; } - expectedSeqNumber += ((uint)tcp->GetPayload()->GetLength() - delta); + expectedSeqNumber += tcp->GetPayload()->GetLength() - delta; //Done send } //ACK data @@ -389,7 +390,7 @@ namespace Sessions case 1: //Nop continue; case 8: - lastRecivedTimeStamp = ((TCPopTS*)(tcp->options[i]))->senderTimeStamp; + lastRecivedTimeStamp = static_cast(tcp->options[i])->senderTimeStamp; break; default: Console.Error("DEV9: TCP: Got Unknown Option %d", tcp->options[i]->GetCode()); From 950f4e1c3feae890f4354577d9f0c6b034c5810a Mon Sep 17 00:00:00 2001 From: TheLastRar Date: Mon, 15 Apr 2024 12:08:48 +0100 Subject: [PATCH 2/6] DEV9: Use reinterpret_cast for sockaddr This is UB, but is required by the api --- pcsx2/DEV9/Sessions/TCP_Session/TCP_Session_Out.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pcsx2/DEV9/Sessions/TCP_Session/TCP_Session_Out.cpp b/pcsx2/DEV9/Sessions/TCP_Session/TCP_Session_Out.cpp index a57634eb14661..a03549a080a10 100644 --- a/pcsx2/DEV9/Sessions/TCP_Session/TCP_Session_Out.cpp +++ b/pcsx2/DEV9/Sessions/TCP_Session/TCP_Session_Out.cpp @@ -168,7 +168,7 @@ namespace Sessions endpoint.sin_family = AF_INET; endpoint.sin_addr = std::bit_cast(adapterIP); - ret = bind(client, (const sockaddr*)&endpoint, sizeof(endpoint)); + ret = bind(client, reinterpret_cast(&endpoint), sizeof(endpoint)); if (ret != 0) Console.Error("DEV9: UDP: Failed to bind socket. Error: %d", @@ -212,7 +212,7 @@ namespace Sessions endpoint.sin_addr = std::bit_cast(destIP); endpoint.sin_port = htons(destPort); - ret = connect(client, (const sockaddr*)&endpoint, sizeof(endpoint)); + ret = connect(client, reinterpret_cast(&endpoint), sizeof(endpoint)); if (ret != 0) { From d8e8aa0069e9bd14f1fb6929bab88f73df1b085d Mon Sep 17 00:00:00 2001 From: TheLastRar Date: Sun, 21 Apr 2024 15:32:37 +0100 Subject: [PATCH 3/6] DEV9: Correct capitalisation on log messages Also reword a few messages --- .../DEV9/Sessions/TCP_Session/TCP_Session.cpp | 10 +-- .../Sessions/TCP_Session/TCP_Session_In.cpp | 20 ++--- .../Sessions/TCP_Session/TCP_Session_Out.cpp | 74 +++++++++---------- 3 files changed, 52 insertions(+), 52 deletions(-) diff --git a/pcsx2/DEV9/Sessions/TCP_Session/TCP_Session.cpp b/pcsx2/DEV9/Sessions/TCP_Session/TCP_Session.cpp index 2a08eae80d11a..937c1933df936 100644 --- a/pcsx2/DEV9/Sessions/TCP_Session/TCP_Session.cpp +++ b/pcsx2/DEV9/Sessions/TCP_Session/TCP_Session.cpp @@ -90,21 +90,21 @@ namespace Sessions if (delta > 0.5 * UINT_MAX) { delta = -static_cast(UINT_MAX) + a - b - 1; - Console.Error("DEV9: TCP: [PS2] SequenceNumber Overflow Detected"); - Console.Error("DEV9: TCP: [PS2] New Data Offset: %d bytes", delta); + Console.Error("DEV9: TCP: [PS2] Sequence number overflow detected"); + Console.Error("DEV9: TCP: [PS2] New data offset: %d bytes", delta); } if (delta < -0.5 * UINT_MAX) { delta = UINT_MAX - b + a + 1; - Console.Error("DEV9: TCP: [PS2] SequenceNumber Overflow Detected"); - Console.Error("DEV9: TCP: [PS2] New Data Offset: %d bytes", delta); + Console.Error("DEV9: TCP: [PS2] Sequence number overflow detected"); + Console.Error("DEV9: TCP: [PS2] New data offset: %d bytes", delta); } return delta; } TCP_Packet* TCP_Session::CreateBasePacket(PayloadData* data) { - //DevCon.WriteLn("Creating Base Packet"); + //DevCon.WriteLn("Creating base packet"); if (data == nullptr) data = new PayloadData(0); diff --git a/pcsx2/DEV9/Sessions/TCP_Session/TCP_Session_In.cpp b/pcsx2/DEV9/Sessions/TCP_Session/TCP_Session_In.cpp index eab10a3cc77cd..5199eb3cbaa9b 100644 --- a/pcsx2/DEV9/Sessions/TCP_Session/TCP_Session_In.cpp +++ b/pcsx2/DEV9/Sessions/TCP_Session/TCP_Session_In.cpp @@ -97,7 +97,7 @@ namespace Sessions if (err != SOCKET_ERROR) { if (available > static_cast(maxSize)) - Console.WriteLn("DEV9: TCP: Got a lot of data: %lu Using: %d", available, maxSize); + Console.WriteLn("DEV9: TCP: Got a lot of data: %lu using: %d", available, maxSize); buffer = std::make_unique(maxSize); recived = recv(client, reinterpret_cast(buffer.get()), maxSize, 0); @@ -133,7 +133,7 @@ namespace Sessions break; default: CloseByRemoteRST(); - Console.Error("DEV9: TCP: Recv Error: %d", err); + Console.Error("DEV9: TCP: Recv error: %d", err); return nullptr; } @@ -142,7 +142,7 @@ namespace Sessions { int result = shutdown(client, SD_RECEIVE); if (result == SOCKET_ERROR) - Console.Error("DEV9: TCP: Shutdown SD_RECEIVE Error: %d", + Console.Error("DEV9: TCP: Shutdown SD_RECEIVE error: %d", #ifdef _WIN32 WSAGetLastError()); #elif defined(__POSIX__) @@ -157,7 +157,7 @@ namespace Sessions return CloseByPS2Stage3(); default: CloseByRemoteRST(); - Console.Error("DEV9: TCP: Remote Close In Invalid State"); + Console.Error("DEV9: TCP: Remote close occured with invalid TCP state"); break; } return nullptr; @@ -174,7 +174,7 @@ namespace Sessions iRet->SetPSH(true); myNumberACKed.store(false); - //DevCon.WriteLn("DEV9: TCP: myNumberACKed Reset"); + //DevCon.WriteLn("DEV9: TCP: myNumberACKed reset"); return iRet; } } @@ -224,14 +224,14 @@ namespace Sessions #ifdef _WIN32 int len = sizeof(error); if (getsockopt(client, SOL_SOCKET, SO_ERROR, reinterpret_cast(&error), &len) < 0) - Console.Error("DEV9: TCP: Unkown TCP Connection Error (getsockopt Error: %d)", WSAGetLastError()); + Console.Error("DEV9: TCP: Unkown TCP connection error (getsockopt error: %d)", WSAGetLastError()); #elif defined(__POSIX__) socklen_t len = sizeof(error); if (getsockopt(client, SOL_SOCKET, SO_ERROR, reinterpret_cast(&error), &len) < 0) - Console.Error("DEV9: TCP: Unkown TCP Connection Error (getsockopt Error: %d)", errno); + 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: Send error: %d", error); state = TCP_State::CloseCompleted; RaiseEventConnectionClosed(); @@ -250,7 +250,7 @@ namespace Sessions ret->SetFIN(true); myNumberACKed.store(false); - //DevCon.WriteLn("myNumberACKed Reset"); + //DevCon.WriteLn("myNumberACKed reset"); state = TCP_State::Closing_ClosedByPS2ThenRemote_WaitingForAck; return ret; @@ -267,7 +267,7 @@ namespace Sessions ret->SetFIN(true); myNumberACKed.store(false); - //DevCon.WriteLn("myNumberACKed Reset"); + //DevCon.WriteLn("myNumberACKed reset"); state = TCP_State::Closing_ClosedByRemote; return ret; diff --git a/pcsx2/DEV9/Sessions/TCP_Session/TCP_Session_Out.cpp b/pcsx2/DEV9/Sessions/TCP_Session/TCP_Session_Out.cpp index a03549a080a10..b73c4dfce816e 100644 --- a/pcsx2/DEV9/Sessions/TCP_Session/TCP_Session_Out.cpp +++ b/pcsx2/DEV9/Sessions/TCP_Session/TCP_Session_Out.cpp @@ -46,7 +46,7 @@ namespace Sessions if (client != INVALID_SOCKET) CloseSocket(); else - Console.Error("DEV9: TCP: RESET CLOSED CONNECTION"); + Console.Error("DEV9: TCP: Reset closed connection"); //PS2 sent RST //clearly not expecting //more data @@ -62,7 +62,7 @@ namespace Sessions case TCP_State::SendingSYN_ACK: if (CheckRepeatSYNNumbers(&tcp) == NumCheckResult::Bad) { - Console.Error("DEV9: TCP: Invalid Repeated SYN (SendingSYN_ACK)"); + Console.Error("DEV9: TCP: Invalid repeated SYN (SendingSYN_ACK)"); return false; } return true; //Ignore reconnect attempts while we are still attempting connection @@ -91,7 +91,7 @@ namespace Sessions return false; default: CloseByRemoteRST(); - Console.Error("DEV9: TCP: Invalid TCP State"); + Console.Error("DEV9: TCP: Invalid TCP state"); return true; } } @@ -106,7 +106,7 @@ namespace Sessions if (tcp->GetSYN() == false) { CloseByRemoteRST(); - Console.Error("DEV9: TCP: Attempt To Send Data On Non Connected Connection"); + Console.Error("DEV9: TCP: Attempt to send data to a non connected connection"); return true; } expectedSeqNumber = tcp->sequenceNumber + 1; @@ -130,7 +130,7 @@ namespace Sessions case 3: //WindowScale windowScale = static_cast(tcp->options[i])->windowScale; if (windowScale > 0) - Console.Error("DEV9: TCP: Non-Zero WindowScale Option"); + Console.Error("DEV9: TCP: Non-zero window scale option"); break; case 8: //TimeStamp lastRecivedTimeStamp = static_cast(tcp->options[i])->senderTimeStamp; @@ -138,7 +138,7 @@ namespace Sessions timeStampStart = std::chrono::steady_clock::now(); break; default: - Console.Error("DEV9: TCP: Got Unknown Option %d", tcp->options[i]->GetCode()); + Console.Error("DEV9: TCP: Got unknown option %d", tcp->options[i]->GetCode()); break; } } @@ -188,7 +188,7 @@ namespace Sessions #endif if (ret != 0) - Console.Error("DEV9: TCP: Failed to set non blocking. Error: %d", + Console.Error("DEV9: TCP: Failed to set non-blocking. Error: %d", #ifdef _WIN32 WSAGetLastError()); #elif defined(__POSIX__) @@ -243,7 +243,7 @@ namespace Sessions if (CheckRepeatSYNNumbers(tcp) == NumCheckResult::Bad) { CloseByRemoteRST(); - Console.Error("DEV9: TCP: Invalid Repeated SYN (SentSYN_ACK)"); + Console.Error("DEV9: TCP: Invalid repeated SYN (SentSYN_ACK)"); return true; } return true; //Ignore reconnect attempts while we are still attempting connection @@ -252,7 +252,7 @@ namespace Sessions if (Result == NumCheckResult::Bad) { CloseByRemoteRST(); - Console.Error("DEV9: TCP: Bad TCP Numbers Received"); + Console.Error("DEV9: TCP: Bad TCP numbers received"); return true; } @@ -267,7 +267,7 @@ namespace Sessions lastRecivedTimeStamp = static_cast(tcp->options[i])->senderTimeStamp; break; default: - Console.Error("DEV9: TCP: Got Unknown Option %d", tcp->options[i]->GetCode()); + Console.Error("DEV9: TCP: Got unknown option %d", tcp->options[i]->GetCode()); break; } } @@ -281,13 +281,13 @@ namespace Sessions if (tcp->GetSYN()) { CloseByRemoteRST(); - Console.Error("DEV9: TCP: Attempt to Connect to an open Port"); + Console.Error("DEV9: TCP: Attempt to connect to an existing connection"); return true; } if (tcp->GetURG()) { CloseByRemoteRST(); - Console.Error("DEV9: TCP: Urgent Data Not Supported"); + Console.Error("DEV9: TCP: Urgent data not supported"); return true; } for (size_t i = 0; i < tcp->options.size(); i++) @@ -301,7 +301,7 @@ namespace Sessions lastRecivedTimeStamp = static_cast(tcp->options[i])->senderTimeStamp; break; default: - Console.Error("DEV9: TCP: Got Unknown Option %d", tcp->options[i]->GetCode()); + Console.Error("DEV9: TCP: Got unknown option %d", tcp->options[i]->GetCode()); break; } } @@ -313,7 +313,7 @@ namespace Sessions if (Result == NumCheckResult::Bad) { CloseByRemoteRST(); - Console.Error("DEV9: TCP: Bad TCP Numbers Received"); + Console.Error("DEV9: TCP: Bad TCP numbers received"); return true; } if (tcp->GetPayload()->GetLength() != 0) @@ -323,8 +323,8 @@ namespace Sessions pxAssert(delta >= 0); //if (Result == NumCheckResult::OldSeq) //{ - // DevCon.WriteLn("[PS2] New Data Offset: %d bytes", delta); - // DevCon.WriteLn("[PS2] New Data Length: %d bytes", tcp->GetPayload()->GetLength() - delta); + // DevCon.WriteLn("[PS2] New data offset: %d bytes", delta); + // DevCon.WriteLn("[PS2] New data length: %d bytes", tcp->GetPayload()->GetLength() - delta); //} if (tcp->GetPayload()->GetLength() - delta > 0) { @@ -353,7 +353,7 @@ namespace Sessions else { CloseByRemoteRST(); - Console.Error("DEV9: TCP: Send Error: %d", err); + Console.Error("DEV9: TCP: Send error: %d", err); return true; } } @@ -365,7 +365,7 @@ namespace Sessions //Done send } //ACK data - //DevCon.WriteLn("[SRV] ACK Data: %u", expectedSeqNumber); + //DevCon.WriteLn("[SRV] ACK data: %u", expectedSeqNumber); TCP_Packet* ret = CreateBasePacket(); ret->SetACK(true); @@ -379,7 +379,7 @@ namespace Sessions if (tcp->GetSYN() == true) { CloseByRemoteRST(); - Console.Error("DEV9: TCP: Attempt to Connect to an open Port"); + Console.Error("DEV9: TCP: Attempt to connect to an existing connection"); return true; } for (size_t i = 0; i < tcp->options.size(); i++) @@ -406,11 +406,11 @@ namespace Sessions TCP_Session::NumCheckResult TCP_Session::CheckRepeatSYNNumbers(TCP_Packet* tcp) { //DevCon.WriteLn("DEV9: TCP: CHECK_REPEAT_SYN_NUMBERS"); - //DevCon.WriteLn("DEV9: TCP: [SRV] CurrAckNumber = %u [PS2] Seq Number = %u", expectedSeqNumber, tcp->sequenceNumber); + //DevCon.WriteLn("DEV9: TCP: [SRV] CurrAckNumber = %u [PS2] Seq number = %u", expectedSeqNumber, tcp->sequenceNumber); if (tcp->sequenceNumber != expectedSeqNumber - 1) { - Console.Error("DEV9: TCP: [PS2] Sent Unexpected Sequence Number From Repeated SYN Packet, Got %u Expected %u", tcp->sequenceNumber, (expectedSeqNumber - 1)); + Console.Error("DEV9: TCP: [PS2] Sent unexpected sequence number from repeated SYN packet, got %u expected %u", tcp->sequenceNumber, (expectedSeqNumber - 1)); return NumCheckResult::Bad; } return NumCheckResult::OK; @@ -423,24 +423,24 @@ namespace Sessions std::tie(seqNum, oldSeqNums) = GetAllMyNumbers(); //DevCon.WriteLn("DEV9: TCP: CHECK_NUMBERS"); - //DevCon.WriteLn("DEV9: TCP: [SRV] CurrSeqNumber = %u [PS2] Ack Number = %u", seqNum, tcp->acknowledgementNumber); - //DevCon.WriteLn("DEV9: TCP: [SRV] CurrAckNumber = %u [PS2] Seq Number = %u", expectedSeqNumber, tcp->sequenceNumber); - //DevCon.WriteLn("DEV9: TCP: [PS2] Data Length = %u", tcp->GetPayload()->GetLength()); + //DevCon.WriteLn("DEV9: TCP: [SRV] CurrSeqNumber = %u [PS2] Ack number = %u", seqNum, tcp->acknowledgementNumber); + //DevCon.WriteLn("DEV9: TCP: [SRV] CurrAckNumber = %u [PS2] Seq number = %u", expectedSeqNumber, tcp->sequenceNumber); + //DevCon.WriteLn("DEV9: TCP: [PS2] Data length = %u", tcp->GetPayload()->GetLength()); if (tcp->acknowledgementNumber != seqNum) { - //DevCon.WriteLn("DEV9: TCP: [PS2] Sent Outdated Acknowledgement Number, Got %u Expected %u", tcp->acknowledgementNumber, seqNum); + //DevCon.WriteLn("DEV9: TCP: [PS2] Sent outdated acknowledgement number, got %u expected %u", tcp->acknowledgementNumber, seqNum); //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); + Console.Error("DEV9: TCP: [PS2] Sent unexpected acknowledgement number, did not match old numbers, got %u expected %u", tcp->acknowledgementNumber, seqNum); return NumCheckResult::Bad; } } else { - //DevCon.WriteLn("[PS2] CurrSeqNumber Acknowleged By PS2"); + //DevCon.WriteLn("[PS2] CurrSeqNumber acknowledged by PS2"); myNumberACKed.store(true); } @@ -450,12 +450,12 @@ namespace Sessions { if (rejectOldSeq) { - Console.Error("DEV9: TCP: [PS2] Sent Unexpected Sequence Number, Got %u Expected %u", tcp->sequenceNumber, expectedSeqNumber); + Console.Error("DEV9: TCP: [PS2] Sent unexpected sequence number, got %u expected %u", tcp->sequenceNumber, expectedSeqNumber); return NumCheckResult::Bad; } else if (tcp->GetPayload()->GetLength() == 0) { - Console.Error("DEV9: TCP: [PS2] Sent Unexpected Sequence Number From ACK Packet, Got %u Expected %u", tcp->sequenceNumber, expectedSeqNumber); + Console.Error("DEV9: TCP: [PS2] Sent unexpected sequence number in a empty packet, got %u expected %u", tcp->sequenceNumber, expectedSeqNumber); return NumCheckResult::OldSeq; } else @@ -463,12 +463,12 @@ namespace Sessions //Check if receivedPS2SeqNumbers contains tcp->sequenceNumber if (std::find(receivedPS2SeqNumbers.begin(), receivedPS2SeqNumbers.end(), tcp->sequenceNumber) == receivedPS2SeqNumbers.end()) { - Console.Error("DEV9: TCP: [PS2] Sent an Old Seq Number on an Data packet, Got %u Expected %u", tcp->sequenceNumber, expectedSeqNumber); + Console.Error("DEV9: TCP: [PS2] Sent outdated sequence number in an data packet, got %u expected %u", tcp->sequenceNumber, expectedSeqNumber); return NumCheckResult::OldSeq; } else { - Console.Error("DEV9: TCP: [PS2] Sent Unexpected Sequence Number From Data Packet, Got %u Expected %u", tcp->sequenceNumber, expectedSeqNumber); + Console.Error("DEV9: TCP: [PS2] Sent unexpected sequence number in a data packet, got %u expected %u", tcp->sequenceNumber, expectedSeqNumber); return NumCheckResult::Bad; } } @@ -482,7 +482,7 @@ namespace Sessions if (ResultFIN == NumCheckResult::Bad) { CloseByRemoteRST(); - Console.Error("DEV9: TCP: Bad TCP Numbers Received"); + Console.Error("DEV9: TCP: Bad TCP numbers received"); return true; } if (tcp->GetPayload()->GetLength() > 0) @@ -494,7 +494,7 @@ namespace Sessions return false; CloseByRemoteRST(); - Console.Error("DEV9: TCP: Invalid Packet, Packet Has Data"); + Console.Error("DEV9: TCP: Invalid packet, packet has data"); return true; } return false; @@ -516,7 +516,7 @@ namespace Sessions const int result = shutdown(client, SD_SEND); if (result == SOCKET_ERROR) - Console.Error("DEV9: TCP: Shutdown SD_SEND Error: %d", + Console.Error("DEV9: TCP: Shutdown SD_SEND error: %d", #ifdef _WIN32 WSAGetLastError()); #elif defined(__POSIX__) @@ -536,7 +536,7 @@ namespace Sessions bool TCP_Session::CloseByPS2Stage4(TCP_Packet* tcp) { //Close Part 4, Receive ACK from PS2 - //Console.WriteLn("DEV9: TCP: Completed Close By PS2"); + //Console.WriteLn("DEV9: TCP: Completed close by PS2"); if (ValidateEmptyPacket(tcp)) return true; @@ -555,7 +555,7 @@ namespace Sessions bool TCP_Session::CloseByRemoteStage2_ButAfter4(TCP_Packet* tcp) { - //Console.WriteLn("DEV9: TCP: Completed Close By PS2"); + //Console.WriteLn("DEV9: TCP: Completed close by PS2"); if (ValidateEmptyPacket(tcp)) return true; @@ -583,7 +583,7 @@ namespace Sessions int result = shutdown(client, SD_SEND); if (result == SOCKET_ERROR) - Console.Error("DEV9: TCP: Shutdown SD_SEND Error: %d", + Console.Error("DEV9: TCP: Shutdown SD_SEND error: %d", #ifdef _WIN32 WSAGetLastError()); #elif defined(__POSIX__) From 13c1856615d7cf90959d8a13231c22dd0fab83a6 Mon Sep 17 00:00:00 2001 From: TheLastRar Date: Sun, 21 Apr 2024 15:58:44 +0100 Subject: [PATCH 4/6] DEV9: Format comments in TCP session --- .../DEV9/Sessions/TCP_Session/TCP_Session.cpp | 5 +- pcsx2/DEV9/Sessions/TCP_Session/TCP_Session.h | 62 ++++++------- .../Sessions/TCP_Session/TCP_Session_In.cpp | 31 +++---- .../Sessions/TCP_Session/TCP_Session_Out.cpp | 89 +++++++++---------- 4 files changed, 93 insertions(+), 94 deletions(-) diff --git a/pcsx2/DEV9/Sessions/TCP_Session/TCP_Session.cpp b/pcsx2/DEV9/Sessions/TCP_Session/TCP_Session.cpp index 937c1933df936..4619c24e57d44 100644 --- a/pcsx2/DEV9/Sessions/TCP_Session/TCP_Session.cpp +++ b/pcsx2/DEV9/Sessions/TCP_Session/TCP_Session.cpp @@ -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; diff --git a/pcsx2/DEV9/Sessions/TCP_Session/TCP_Session.h b/pcsx2/DEV9/Sessions/TCP_Session/TCP_Session.h index 615f89aa4844b..28a68d42c5842 100644 --- a/pcsx2/DEV9/Sessions/TCP_Session/TCP_Session.h +++ b/pcsx2/DEV9/Sessions/TCP_Session/TCP_Session.h @@ -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 windowSize{1460}; //Make atomic instead + std::atomic 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 receivedPS2SeqNumbers; //Accesed By Out Thread Only + u32 expectedSeqNumber; // Accesed by out thread only + std::vector 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(); diff --git a/pcsx2/DEV9/Sessions/TCP_Session/TCP_Session_In.cpp b/pcsx2/DEV9/Sessions/TCP_Session/TCP_Session_In.cpp index 5199eb3cbaa9b..b6b857b184b9d 100644 --- a/pcsx2/DEV9/Sessions/TCP_Session/TCP_Session_In.cpp +++ b/pcsx2/DEV9/Sessions/TCP_Session/TCP_Session_In.cpp @@ -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; diff --git a/pcsx2/DEV9/Sessions/TCP_Session/TCP_Session_Out.cpp b/pcsx2/DEV9/Sessions/TCP_Session/TCP_Session_Out.cpp index b73c4dfce816e..3c9695dbc9139 100644 --- a/pcsx2/DEV9/Sessions/TCP_Session/TCP_Session_Out.cpp +++ b/pcsx2/DEV9/Sessions/TCP_Session/TCP_Session_Out.cpp @@ -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(tcp->options[i])->maxSegmentSize; break; - case 3: //WindowScale + case 3: // WindowScale windowScale = static_cast(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(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(&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(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(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(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(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(); From 5fb621e91fa0221d05e129b48ab5f40e3dc92e47 Mon Sep 17 00:00:00 2001 From: TheLastRar Date: Sun, 21 Apr 2024 16:08:38 +0100 Subject: [PATCH 5/6] DEV9: Fix incorrect error check on TCP send --- pcsx2/DEV9/Sessions/TCP_Session/TCP_Session_Out.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pcsx2/DEV9/Sessions/TCP_Session/TCP_Session_Out.cpp b/pcsx2/DEV9/Sessions/TCP_Session/TCP_Session_Out.cpp index 3c9695dbc9139..7316eddd7fb91 100644 --- a/pcsx2/DEV9/Sessions/TCP_Session/TCP_Session_Out.cpp +++ b/pcsx2/DEV9/Sessions/TCP_Session/TCP_Session_Out.cpp @@ -337,7 +337,7 @@ namespace Sessions { int ret = send(client, reinterpret_cast(&payload->data[sent]), payload->GetLength() - sent, 0); - if (sent == SOCKET_ERROR) + if (ret == SOCKET_ERROR) { #ifdef _WIN32 const int err = WSAGetLastError(); From b64a07ccae38d32aae69afe74e2018ee9eaff61a Mon Sep 17 00:00:00 2001 From: TheLastRar Date: Sun, 21 Apr 2024 16:43:29 +0100 Subject: [PATCH 6/6] DEV9: Add const to variables in TCP session --- pcsx2/DEV9/Sessions/TCP_Session/TCP_Session_In.cpp | 2 +- pcsx2/DEV9/Sessions/TCP_Session/TCP_Session_Out.cpp | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pcsx2/DEV9/Sessions/TCP_Session/TCP_Session_In.cpp b/pcsx2/DEV9/Sessions/TCP_Session/TCP_Session_In.cpp index b6b857b184b9d..ff0b712a6b912 100644 --- a/pcsx2/DEV9/Sessions/TCP_Session/TCP_Session_In.cpp +++ b/pcsx2/DEV9/Sessions/TCP_Session/TCP_Session_In.cpp @@ -141,7 +141,7 @@ namespace Sessions // Server closed the Socket if (recived == 0) { - int result = shutdown(client, SD_RECEIVE); + const int result = shutdown(client, SD_RECEIVE); if (result == SOCKET_ERROR) Console.Error("DEV9: TCP: Shutdown SD_RECEIVE error: %d", #ifdef _WIN32 diff --git a/pcsx2/DEV9/Sessions/TCP_Session/TCP_Session_Out.cpp b/pcsx2/DEV9/Sessions/TCP_Session/TCP_Session_Out.cpp index 7316eddd7fb91..e0fcf6222fd09 100644 --- a/pcsx2/DEV9/Sessions/TCP_Session/TCP_Session_Out.cpp +++ b/pcsx2/DEV9/Sessions/TCP_Session/TCP_Session_Out.cpp @@ -335,7 +335,7 @@ namespace Sessions PayloadPtr* payload = static_cast(tcp->GetPayload()); while (sent != payload->GetLength()) { - int ret = send(client, reinterpret_cast(&payload->data[sent]), payload->GetLength() - sent, 0); + const int ret = send(client, reinterpret_cast(&payload->data[sent]), payload->GetLength() - sent, 0); if (ret == SOCKET_ERROR) { @@ -475,7 +475,7 @@ namespace Sessions } bool TCP_Session::ValidateEmptyPacket(TCP_Packet* tcp, bool ignoreOld) { - NumCheckResult ResultFIN = CheckNumbers(tcp, !ignoreOld); + const NumCheckResult ResultFIN = CheckNumbers(tcp, !ignoreOld); if (ResultFIN == NumCheckResult::Bad) { CloseByRemoteRST(); @@ -578,7 +578,7 @@ namespace Sessions receivedPS2SeqNumbers.push_back(expectedSeqNumber); expectedSeqNumber += 1; - int result = shutdown(client, SD_SEND); + const int result = shutdown(client, SD_SEND); if (result == SOCKET_ERROR) Console.Error("DEV9: TCP: Shutdown SD_SEND error: %d", #ifdef _WIN32