diff --git a/pcsx2/DEV9/Sessions/UDP_Session/UDP_FixedPort.cpp b/pcsx2/DEV9/Sessions/UDP_Session/UDP_FixedPort.cpp index eee6729a9a334..ee48284d4689a 100644 --- a/pcsx2/DEV9/Sessions/UDP_Session/UDP_FixedPort.cpp +++ b/pcsx2/DEV9/Sessions/UDP_Session/UDP_FixedPort.cpp @@ -45,10 +45,14 @@ namespace Sessions UDP_FixedPort::UDP_FixedPort(ConnectionKey parKey, IP_Address parAdapterIP, u16 parPort) : BaseSession(parKey, parAdapterIP) - , client{socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)} - , port(parPort) + , port{parPort} + { + } + + void UDP_FixedPort::Init() { int ret; + client = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (client == INVALID_SOCKET) { Console.Error("DEV9: UDP: Failed to open socket. Error: %d", @@ -57,11 +61,7 @@ namespace Sessions #elif defined(__POSIX__) errno); #endif - /* - * TODO: Currently error is not correctly handled here - * We would need to call RaiseEventConnectionClosed() - * and also deal with the follow up call to NewClientSession() - */ + RaiseEventConnectionClosed(); return; } @@ -90,17 +90,23 @@ namespace Sessions sockaddr_in endpoint{}; endpoint.sin_family = AF_INET; endpoint.sin_addr = std::bit_cast(adapterIP); - endpoint.sin_port = htons(parPort); + endpoint.sin_port = htons(port); ret = bind(client, reinterpret_cast(&endpoint), sizeof(endpoint)); if (ret == SOCKET_ERROR) + { Console.Error("DEV9: UDP: Failed to bind socket. Error: %d", #ifdef _WIN32 WSAGetLastError()); #elif defined(__POSIX__) errno); #endif + RaiseEventConnectionClosed(); + return; + } + + open.store(true); } IP_Payload* UDP_FixedPort::Recv() @@ -228,6 +234,9 @@ namespace Sessions UDP_Session* UDP_FixedPort::NewClientSession(ConnectionKey parNewKey, bool parIsBrodcast, bool parIsMulticast) { + if (!open.load()) + return nullptr; + UDP_Session* s = new UDP_Session(parNewKey, adapterIP, parIsBrodcast, parIsMulticast, client); s->AddConnectionClosedHandler([&](BaseSession* session) { HandleChildConnectionClosed(session); }); @@ -247,7 +256,10 @@ namespace Sessions { connections.erase(index); if (connections.size() == 0) + { + open.store(false); RaiseEventConnectionClosed(); + } } } diff --git a/pcsx2/DEV9/Sessions/UDP_Session/UDP_FixedPort.h b/pcsx2/DEV9/Sessions/UDP_Session/UDP_FixedPort.h index d66eaf9140010..58b46e9240047 100644 --- a/pcsx2/DEV9/Sessions/UDP_Session/UDP_FixedPort.h +++ b/pcsx2/DEV9/Sessions/UDP_Session/UDP_FixedPort.h @@ -20,7 +20,7 @@ namespace Sessions class UDP_FixedPort : public BaseSession { private: - std::atomic open{true}; + std::atomic open{false}; #ifdef _WIN32 SOCKET client = INVALID_SOCKET; @@ -38,6 +38,8 @@ namespace Sessions public: UDP_FixedPort(ConnectionKey parKey, PacketReader::IP::IP_Address parAdapterIP, u16 parPort); + void Init(); + virtual PacketReader::IP::IP_Payload* Recv(); virtual bool Send(PacketReader::IP::IP_Payload* payload); virtual void Reset(); diff --git a/pcsx2/DEV9/sockets.cpp b/pcsx2/DEV9/sockets.cpp index 4c1aae975db33..4243118774167 100644 --- a/pcsx2/DEV9/sockets.cpp +++ b/pcsx2/DEV9/sockets.cpp @@ -531,12 +531,20 @@ bool SocketAdapter::SendUDP(ConnectionKey Key, IP_Packet* ipPkt) connections.Add(fKey, fPort); fixedUDPPorts.Add(udp.sourcePort, fPort); + + fPort->Init(); } Console.WriteLn("DEV9: Socket: Creating New UDP Connection from FixedPort %d to %d", udp.sourcePort, udp.destinationPort); s = fPort->NewClientSession(Key, ipPkt->destinationIP == dhcpServer.broadcastIP || ipPkt->destinationIP == IP_Address{{{255, 255, 255, 255}}}, (ipPkt->destinationIP.bytes[0] & 0xF0) == 0xE0); + + if (s == nullptr) + { + Console.Error("DEV9: Socket: Failed to Create New UDP Connection from FixedPort"); + return false; + } } else {