Skip to content

Commit 5d1803c

Browse files
authored
Adding network connectivity check for WinHttp stack (microsoft#454)
Adding network connectivity check for WinHttp stack
1 parent 20f0c27 commit 5d1803c

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

Include/httpClient/pal.h

+1
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,7 @@ typedef struct _LIST_ENTRY {
438438
#define E_HC_ALREADY_INITIALISED MAKE_E_HC(0x5004)
439439
#define E_HC_CONNECT_ALREADY_CALLED MAKE_E_HC(0x5005)
440440
#define E_HC_NO_NETWORK MAKE_E_HC(0x5006)
441+
#define E_HC_NETWORK_NOT_READY MAKE_E_HC(0x5007)
441442

442443
typedef uint32_t HCMemoryType;
443444
typedef struct HC_WEBSOCKET* HCWebsocketHandle;

Source/HTTP/WinHttp/winhttp_http_task.cpp

+40
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
#include "hcwebsocket.h"
1212
#endif
1313

14+
#if HC_PLATFORM == HC_PLATFORM_GSDK
15+
#include "XGameRuntimeFeature.h"
16+
#include <winsock2.h>
17+
#include <iphlpapi.h>
18+
#endif
19+
1420
#define CRLF L"\r\n"
1521

1622
using namespace xbox::httpclient;
@@ -1030,11 +1036,45 @@ HRESULT winhttp_http_task::connect_and_send_async()
10301036

10311037
NAMESPACE_XBOX_HTTP_CLIENT_END
10321038

1039+
#if HC_PLATFORM == HC_PLATFORM_GSDK
1040+
typedef DWORD(WINAPI *GetNetworkConnectivityHintProc)(NL_NETWORK_CONNECTIVITY_HINT*);
1041+
#endif
1042+
10331043
HRESULT Internal_InitializeHttpPlatform(HCInitArgs* args, PerformEnv& performEnv) noexcept
10341044
{
10351045
assert(args == nullptr);
10361046
UNREFERENCED_PARAMETER(args);
10371047

1048+
#if HC_PLATFORM == HC_PLATFORM_GSDK
1049+
if (XGameRuntimeIsFeatureAvailable(XGameRuntimeFeature::XNetworking))
1050+
{
1051+
HMODULE hModule = LoadLibrary(TEXT("iphlpapi.dll"));
1052+
1053+
if (hModule != nullptr)
1054+
{
1055+
GetNetworkConnectivityHintProc getNetworkConnectivityHint =
1056+
(GetNetworkConnectivityHintProc)GetProcAddress(hModule, "GetNetworkConnectivityHint");
1057+
1058+
HRESULT hr = S_OK;
1059+
bool networkNotReady = false;
1060+
1061+
if (getNetworkConnectivityHint != nullptr)
1062+
{
1063+
NL_NETWORK_CONNECTIVITY_HINT connectivityHint{};
1064+
hr = HRESULT_FROM_WIN32(getNetworkConnectivityHint(&connectivityHint));
1065+
networkNotReady = connectivityHint.ConnectivityLevel == NetworkConnectivityLevelHintUnknown;
1066+
}
1067+
1068+
FreeLibrary(hModule);
1069+
1070+
if (SUCCEEDED(hr) && networkNotReady)
1071+
{
1072+
return E_HC_NETWORK_NOT_READY;
1073+
}
1074+
}
1075+
}
1076+
#endif
1077+
10381078
performEnv.reset(new (std::nothrow) HC_PERFORM_ENV());
10391079
if (!performEnv) { return E_OUTOFMEMORY; }
10401080

libHttpClient.props

+2
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@
7979
</ClCompile>
8080
<Link>
8181
<AdditionalDependencies Condition="'$(HCLibPlatformType)'=='Win32'">%(AdditionalDependencies);winhttp.lib</AdditionalDependencies>
82+
<AdditionalLibraryDirectories Condition="'$(HCLibPlatformType)'=='GXDK'">$(GameDKLatest)\GXDK\gameKit\lib\amd64</AdditionalLibraryDirectories>
83+
<AdditionalDependencies Condition="'$(HCLibPlatformType)'=='GXDK'">%(AdditionalDependencies);XGamePlatform.lib</AdditionalDependencies>
8284
</Link>
8385
</ItemDefinitionGroup>
8486

0 commit comments

Comments
 (0)