Skip to content

Commit 2c3f7f6

Browse files
committed
Add a central location for managing HLE sockets
1 parent 3dc2a10 commit 2c3f7f6

File tree

12 files changed

+113
-72
lines changed

12 files changed

+113
-72
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2178,6 +2178,8 @@ add_library(${CoreLibName} ${CoreLinkType}
21782178
Core/HLE/AtracCtx2.h
21792179
Core/HLE/NetInetConstants.cpp
21802180
Core/HLE/NetInetConstants.h
2181+
Core/HLE/SocketManager.cpp
2182+
Core/HLE/SocketManager.h
21812183
Core/HLE/sceAtrac.cpp
21822184
Core/HLE/sceAtrac.h
21832185
Core/HLE/sceAudio.cpp

Core/Core.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,7 @@
580580
<ClCompile Include="HLE\sceUsbAcc.cpp" />
581581
<ClCompile Include="HLE\sceUsbCam.cpp" />
582582
<ClCompile Include="HLE\sceUsbMic.cpp" />
583+
<ClCompile Include="HLE\SocketManager.cpp" />
583584
<ClCompile Include="HW\Atrac3Standalone.cpp" />
584585
<ClCompile Include="HW\BufferQueue.cpp" />
585586
<ClCompile Include="HW\Camera.cpp" />
@@ -1199,6 +1200,7 @@
11991200
<ClInclude Include="HLE\sceUsbAcc.h" />
12001201
<ClInclude Include="HLE\sceUsbCam.h" />
12011202
<ClInclude Include="HLE\sceUsbMic.h" />
1203+
<ClInclude Include="HLE\SocketManager.h" />
12021204
<ClInclude Include="HW\Atrac3Standalone.h" />
12031205
<ClInclude Include="HW\Camera.h" />
12041206
<ClInclude Include="HW\Display.h" />

Core/Core.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,6 +1342,9 @@
13421342
<ClCompile Include="HLE\NetInetConstants.cpp">
13431343
<Filter>HLE\Libraries</Filter>
13441344
</ClCompile>
1345+
<ClCompile Include="HLE\SocketManager.cpp">
1346+
<Filter>HLE\Libraries</Filter>
1347+
</ClCompile>
13451348
</ItemGroup>
13461349
<ItemGroup>
13471350
<ClInclude Include="ELF\ElfReader.h">
@@ -2160,6 +2163,9 @@
21602163
<ClInclude Include="HLE\NetInetConstants.h">
21612164
<Filter>HLE\Libraries</Filter>
21622165
</ClInclude>
2166+
<ClInclude Include="HLE\SocketManager.h">
2167+
<Filter>HLE\Libraries</Filter>
2168+
</ClInclude>
21632169
</ItemGroup>
21642170
<ItemGroup>
21652171
<None Include="..\LICENSE.TXT" />

Core/HLE/SocketManager.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include "Core/HLE/SocketManager.h"
2+
#include "Common/Log.h"
3+
4+
#include <mutex>
5+
6+
// We use this array from 1 and forward. It's probably not a good idea to return 0 as a socket.
7+
InetSocket g_inetSockets[256];
8+
static std::mutex g_socketMutex; // TODO: Remove once the adhoc thread is gone
9+
10+
int AllocInetSocket() {
11+
std::lock_guard<std::mutex> guard(g_socketMutex);
12+
for (int i = MIN_VALID_INET_SOCKET; i < ARRAY_SIZE(g_inetSockets); i++) {
13+
if (g_inetSockets[i].state == SocketState::Unused) {
14+
return i;
15+
}
16+
}
17+
_dbg_assert_(false);
18+
ERROR_LOG(Log::sceNet, "Ran out of socket handles! This is BAD.");
19+
return 0;
20+
}
21+
22+
bool GetInetSocket(int sock, InetSocket **inetSocket) {
23+
std::lock_guard<std::mutex> guard(g_socketMutex);
24+
if (sock < MIN_VALID_INET_SOCKET || sock >= ARRAY_SIZE(g_inetSockets) || g_inetSockets[sock].state == SocketState::Unused) {
25+
*inetSocket = nullptr;
26+
return false;
27+
}
28+
*inetSocket = &g_inetSockets[sock];
29+
return true;
30+
}
31+
32+
// Simplified mappers, only really useful in select/poll
33+
SOCKET GetHostSocketFromInetSocket(int sock) {
34+
std::lock_guard<std::mutex> guard(g_socketMutex);
35+
if (sock < MIN_VALID_INET_SOCKET || sock >= ARRAY_SIZE(g_inetSockets) || g_inetSockets[sock].state == SocketState::Unused) {
36+
_dbg_assert_(false);
37+
return -1;
38+
}
39+
if (sock == 0) {
40+
// Map 0 to 0, special case.
41+
return 0;
42+
}
43+
return g_inetSockets[sock].sock;
44+
}
45+
46+
void CloseAllSockets() {
47+
for (auto &sock : g_inetSockets) {
48+
if (sock.state != SocketState::Unused) {
49+
closesocket(sock.sock);
50+
}
51+
sock.state = SocketState::Unused;
52+
sock.sock = 0;
53+
}
54+
}

Core/HLE/SocketManager.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#pragma once
2+
3+
#include "Common/Net/SocketCompat.h"
4+
5+
// Keep track of who's using a socket.
6+
enum class SocketState {
7+
Unused,
8+
UsedNetInet,
9+
UsedProAdhoc,
10+
};
11+
12+
// Internal socket state tracking
13+
struct InetSocket {
14+
SOCKET sock; // native socket
15+
SocketState state;
16+
// NOTE: These are the PSP types for now
17+
int domain;
18+
int type;
19+
int protocol;
20+
// These are the host types for convenience.
21+
int hostDomain;
22+
int hostType;
23+
int hostProtocol;
24+
};
25+
26+
#define MIN_VALID_INET_SOCKET 20
27+
#define VALID_INET_SOCKET_COUNT 256
28+
29+
extern InetSocket g_inetSockets[VALID_INET_SOCKET_COUNT];
30+
31+
int AllocInetSocket();
32+
bool GetInetSocket(int sock, InetSocket **inetSocket);
33+
SOCKET GetHostSocketFromInetSocket(int sock);
34+
void CloseAllSockets();

Core/HLE/proAdhoc.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
#include "Core/CoreTiming.h"
5050
#include "Core/Core.h"
5151
#include "Core/HLE/sceKernelInterrupt.h"
52-
#include "Core/HLE/sceKernelThread.h"
5352
#include "Core/HLE/sceKernelMemory.h"
5453
#include "Core/HLE/sceNetAdhoc.h"
5554
#include "Core/Instance.h"

Core/HLE/sceNetInet.cpp

Lines changed: 5 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "Common/Serialize/SerializeFuncs.h"
77
#include "Common/Serialize/SerializeMap.h"
88

9+
#include "Core/HLE/SocketManager.h"
910
#include "Core/HLE/HLE.h"
1011
#include "Core/HLE/FunctionWrappers.h"
1112
#include "Core/HLE/sceNet.h"
@@ -22,63 +23,17 @@
2223
#include "Core/Util/PortManager.h"
2324
#include "Core/Instance.h"
2425

25-
#define MIN_VALID_SOCKET 20
26-
2726
int inetLastErrno = 0; // TODO: since errno can only be read once, we should keep track the value to be used on sceNetInetGetErrno
2827

2928
bool netInetInited = false;
3029

31-
// We use this array from 1 and forward. It's probably not a good idea to return 0 as a socket.
32-
InetSocket g_inetSockets[256];
33-
34-
static int AllocInetSocket() {
35-
for (int i = MIN_VALID_SOCKET; i < ARRAY_SIZE(g_inetSockets); i++) {
36-
if (g_inetSockets[i].state == SocketState::Unused) {
37-
return i;
38-
}
39-
}
40-
_dbg_assert_(false);
41-
ERROR_LOG(Log::sceNet, "Ran out of socket handles! This is BAD.");
42-
return 0;
43-
}
44-
45-
static bool GetInetSocket(int sock, InetSocket **inetSocket) {
46-
if (sock < MIN_VALID_SOCKET || sock >= ARRAY_SIZE(g_inetSockets) || g_inetSockets[sock].state == SocketState::Unused) {
47-
*inetSocket = nullptr;
48-
return false;
49-
}
50-
*inetSocket = &g_inetSockets[sock];
51-
return true;
52-
}
53-
54-
// Simplified mappers, only really useful in select/poll
55-
static SOCKET GetHostSocketFromInetSocket(int sock) {
56-
if (sock < MIN_VALID_SOCKET || sock >= ARRAY_SIZE(g_inetSockets) || g_inetSockets[sock].state == SocketState::Unused) {
57-
_dbg_assert_(false);
58-
return -1;
59-
}
60-
if (sock == 0) {
61-
// Map 0 to 0, special case.
62-
return 0;
63-
}
64-
return g_inetSockets[sock].sock;
65-
}
66-
6730
void __NetInetShutdown() {
6831
if (!netInetInited) {
6932
return;
7033
}
7134

7235
netInetInited = false;
73-
74-
for (auto &sock : g_inetSockets) {
75-
if (sock.state != SocketState::Unused) {
76-
closesocket(sock.sock);
77-
}
78-
sock.state = SocketState::Unused;
79-
}
80-
81-
// TODO: Shut down any open sockets here.
36+
CloseAllSockets();
8237
}
8338

8439
static int sceNetInetInit() {
@@ -250,7 +205,7 @@ int sceNetInetSelect(int nfds, u32 readfdsPtr, u32 writefdsPtr, u32 exceptfdsPtr
250205
// Save the mapping during setup.
251206
SOCKET hostSockets[256]{};
252207

253-
for (int i = MIN_VALID_SOCKET; i < nfds; i++) {
208+
for (int i = MIN_VALID_INET_SOCKET; i < nfds; i++) {
254209
if (readfds && (NetInetFD_ISSET(i, readfds))) {
255210
SOCKET sock = GetHostSocketFromInetSocket(i);
256211
hostSockets[i] = sock;
@@ -299,7 +254,7 @@ int sceNetInetSelect(int nfds, u32 readfdsPtr, u32 writefdsPtr, u32 exceptfdsPtr
299254
if (readfds != NULL) NetInetFD_ZERO(readfds);
300255
if (writefds != NULL) NetInetFD_ZERO(writefds);
301256
if (exceptfds != NULL) NetInetFD_ZERO(exceptfds);
302-
for (int i = MIN_VALID_SOCKET; i < nfds; i++) {
257+
for (int i = MIN_VALID_INET_SOCKET; i < nfds; i++) {
303258
if (readfds && hostSockets[i] != 0 && FD_ISSET(hostSockets[i], &rdfds)) {
304259
NetInetFD_SET(i, readfds);
305260
}
@@ -455,7 +410,7 @@ static int sceNetInetSocket(int domain, int type, int protocol) {
455410
return hleLogError(Log::sceNet, ERROR_NET_INTERNAL);
456411
}
457412
InetSocket *inetSock = &g_inetSockets[socket];
458-
inetSock->state = SocketState::Used;
413+
inetSock->state = SocketState::UsedNetInet;
459414
inetSock->sock = hostSock;
460415
inetSock->domain = domain;
461416
inetSock->type = type;

Core/HLE/sceNetInet.h

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -186,24 +186,3 @@ int sceNetApctlConnect(int connIndex);
186186
int sceNetInetPoll(u32 fdsPtr, u32 nfds, int timeout);
187187
int sceNetApctlTerm();
188188
int sceNetApctlDisconnect();
189-
190-
enum class SocketState {
191-
Unused,
192-
Used,
193-
};
194-
195-
// Internal socket state tracking
196-
struct InetSocket {
197-
SOCKET sock; // native socket
198-
SocketState state;
199-
// NOTE: These are the PSP types for now
200-
int domain;
201-
int type;
202-
int protocol;
203-
// These are the host types for convenience.
204-
int hostDomain;
205-
int hostType;
206-
int hostProtocol;
207-
};
208-
209-
extern InetSocket g_inetSockets[256];

UWP/CoreUWP/CoreUWP.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@
182182
<ClInclude Include="..\..\Core\HLE\sceNetInet.h" />
183183
<ClInclude Include="..\..\Core\HLE\sceNetResolver.h" />
184184
<ClInclude Include="..\..\Core\HLE\sceNp2.h" />
185+
<ClInclude Include="..\..\Core\HLE\SocketManager.h" />
185186
<ClInclude Include="..\..\Core\Instance.h" />
186187
<ClInclude Include="..\..\Core\HLE\FunctionWrappers.h" />
187188
<ClInclude Include="..\..\Core\HLE\HLE.h" />
@@ -444,6 +445,7 @@
444445
<ClCompile Include="..\..\Core\HLE\sceNetInet.cpp" />
445446
<ClCompile Include="..\..\Core\HLE\sceNetResolver.cpp" />
446447
<ClCompile Include="..\..\Core\HLE\sceNp2.cpp" />
448+
<ClCompile Include="..\..\Core\HLE\SocketManager.cpp" />
447449
<ClCompile Include="..\..\Core\Instance.cpp" />
448450
<ClCompile Include="..\..\Core\HLE\HLE.cpp" />
449451
<ClCompile Include="..\..\Core\HLE\HLEHelperThread.cpp" />

UWP/CoreUWP/CoreUWP.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,6 +1232,9 @@
12321232
<ClCompile Include="..\..\Core\HLE\NetInetConstants.cpp">
12331233
<Filter>HLE</Filter>
12341234
</ClCompile>
1235+
<ClCompile Include="..\..\Core\HLE\SocketManager.cpp">
1236+
<Filter>HLE</Filter>
1237+
</ClCompile>
12351238
</ItemGroup>
12361239
<ItemGroup>
12371240
<ClInclude Include="pch.h" />
@@ -1948,6 +1951,9 @@
19481951
<ClInclude Include="..\..\Core\HLE\NetInetConstants.h">
19491952
<Filter>HLE</Filter>
19501953
</ClInclude>
1954+
<ClInclude Include="..\..\Core\HLE\SocketManager.h">
1955+
<Filter>HLE</Filter>
1956+
</ClInclude>
19511957
</ItemGroup>
19521958
<ItemGroup>
19531959
<None Include="..\..\ext\gason\LICENSE">

0 commit comments

Comments
 (0)