Allow MIST testing after and during a GameStream session

This commit is contained in:
Cameron Gutman 2018-10-20 18:34:36 -07:00
parent fe4d895d88
commit 727e8c0339

View File

@ -137,7 +137,12 @@ bool IsGameStreamEnabled()
} }
} }
bool TestPort(PSOCKADDR_STORAGE addr, int proto, int port, bool withServer) enum PortTestStatus {
PortTestOk,
PortTestError,
PortTestUnknown
};
PortTestStatus TestPort(PSOCKADDR_STORAGE addr, int proto, int port, bool withServer)
{ {
SOCKET clientSock = INVALID_SOCKET, serverSock = INVALID_SOCKET; SOCKET clientSock = INVALID_SOCKET, serverSock = INVALID_SOCKET;
int err; int err;
@ -145,7 +150,7 @@ bool TestPort(PSOCKADDR_STORAGE addr, int proto, int port, bool withServer)
clientSock = socket(addr->ss_family, proto == IPPROTO_TCP ? SOCK_STREAM : SOCK_DGRAM, proto); clientSock = socket(addr->ss_family, proto == IPPROTO_TCP ? SOCK_STREAM : SOCK_DGRAM, proto);
if (clientSock == INVALID_SOCKET) { if (clientSock == INVALID_SOCKET) {
printf("socket() failed: %d\n", WSAGetLastError()); printf("socket() failed: %d\n", WSAGetLastError());
return false; return PortTestError;
} }
if (withServer) { if (withServer) {
@ -153,7 +158,7 @@ bool TestPort(PSOCKADDR_STORAGE addr, int proto, int port, bool withServer)
if (serverSock == INVALID_SOCKET) { if (serverSock == INVALID_SOCKET) {
printf("socket() failed: %d\n", WSAGetLastError()); printf("socket() failed: %d\n", WSAGetLastError());
closesocket(clientSock); closesocket(clientSock);
return false; return PortTestError;
} }
SOCKADDR_IN sin = {}; SOCKADDR_IN sin = {};
@ -161,24 +166,36 @@ bool TestPort(PSOCKADDR_STORAGE addr, int proto, int port, bool withServer)
sin.sin_port = htons(port); sin.sin_port = htons(port);
err = bind(serverSock, (struct sockaddr*)&sin, sizeof(sin)); err = bind(serverSock, (struct sockaddr*)&sin, sizeof(sin));
if (err == SOCKET_ERROR) { if (err == SOCKET_ERROR) {
printf("bind() failed: %d\n", WSAGetLastError()); if (WSAGetLastError() == WSAEADDRINUSE) {
// If someone is already listening (perhaps GFE is currently streaming),
// If someone is already listening (perhaps GFE is currently streaming), // we can proceed if it's a TCP connection.
// we can proceed if it's a TCP connection if (proto == IPPROTO_TCP) {
if (WSAGetLastError() != WSAEADDRINUSE || proto == IPPROTO_UDP) { closesocket(serverSock);
serverSock = INVALID_SOCKET;
}
else {
// We can't continue to test for UDP ports.
printf("Unknown (in use)\n");
closesocket(clientSock);
closesocket(serverSock);
return PortTestUnknown;
}
}
else {
printf("bind() failed: %d\n", WSAGetLastError());
closesocket(clientSock); closesocket(clientSock);
closesocket(serverSock); closesocket(serverSock);
return false; return PortTestError;
} }
} }
if (proto == IPPROTO_TCP) { if (proto == IPPROTO_TCP && serverSock != INVALID_SOCKET) {
err = listen(serverSock, 1); err = listen(serverSock, 1);
if (err == SOCKET_ERROR) { if (err == SOCKET_ERROR) {
printf("listen() failed: %d\n", WSAGetLastError()); printf("listen() failed: %d\n", WSAGetLastError());
closesocket(clientSock); closesocket(clientSock);
closesocket(serverSock); closesocket(serverSock);
return false; return PortTestError;
} }
} }
} }
@ -191,7 +208,7 @@ bool TestPort(PSOCKADDR_STORAGE addr, int proto, int port, bool withServer)
if (serverSock != INVALID_SOCKET) { if (serverSock != INVALID_SOCKET) {
closesocket(serverSock); closesocket(serverSock);
} }
return false; return PortTestError;
} }
SOCKADDR_IN6 sin6; SOCKADDR_IN6 sin6;
@ -233,7 +250,7 @@ bool TestPort(PSOCKADDR_STORAGE addr, int proto, int port, bool withServer)
closesocket(serverSock); closesocket(serverSock);
} }
return err == 1; return err == 1 ? PortTestOk : PortTestError;
} }
else { else {
const char testMsg[] = "mist-test"; const char testMsg[] = "mist-test";
@ -242,7 +259,7 @@ bool TestPort(PSOCKADDR_STORAGE addr, int proto, int port, bool withServer)
printf("sendto() failed: %d\n", WSAGetLastError()); printf("sendto() failed: %d\n", WSAGetLastError());
closesocket(clientSock); closesocket(clientSock);
closesocket(serverSock); closesocket(serverSock);
return false; return PortTestError;
} }
struct timeval timeout = {}; struct timeval timeout = {};
@ -268,7 +285,7 @@ bool TestPort(PSOCKADDR_STORAGE addr, int proto, int port, bool withServer)
closesocket(clientSock); closesocket(clientSock);
closesocket(serverSock); closesocket(serverSock);
return err == 1; return err == 1 ? PortTestOk : PortTestError;
} }
} }
@ -283,15 +300,20 @@ bool TestAllPorts(PSOCKADDR_STORAGE addr, const char* baseMessage, char* message
printf("Testing %s %d...", printf("Testing %s %d...",
k_Ports[i].proto == IPPROTO_TCP ? "TCP" : "UDP", k_Ports[i].proto == IPPROTO_TCP ? "TCP" : "UDP",
k_Ports[i].port); k_Ports[i].port);
if (!TestPort(addr, k_Ports[i].proto, k_Ports[i].port, k_Ports[i].withServer)) { PortTestStatus status = TestPort(addr, k_Ports[i].proto, k_Ports[i].port, k_Ports[i].withServer);
int msgLen = snprintf(message, messageLength, "%s %d\n", if (status != PortTestOk) {
k_Ports[i].proto == IPPROTO_TCP ? "TCP" : "UDP", // If we got an unknown result, assume it matches with whatever
k_Ports[i].port); // we've gotten so far.
message += msgLen; if (status == PortTestError || !ret) {
messageLength -= msgLen; int msgLen = snprintf(message, messageLength, "%s %d\n",
ret = false; k_Ports[i].proto == IPPROTO_TCP ? "TCP" : "UDP",
k_Ports[i].port);
message += msgLen;
messageLength -= msgLen;
// Keep going to check all ports and report the failing ones // Keep going to check all ports and report the failing ones
ret = false;
}
} }
} }