Host Compute Network (HCN) scenarios | M·续

if (FAILED(result))

{

// UnMarshal the result Json

THROW_HR(result);

}

}


### Delete a HCN namespace

This sample shows how to use Host Compute Network Service API to delete a Host Compute Network Namespace.

```C

wil::unique_cotaskmem_string errorRecord;

GUID namespaceGuid; // Initialize it to appropriate namespace guid value

HRESULT hr = HcnDeleteNamespace(namespaceGuid, &errorRecord);

if (FAILED(hr))

{

// UnMarshal the result Json

THROW_HR(hr);

}

Modify a HCN namespace

This sample shows how to use Host Compute Network Service API to modify a Host Compute Network Namespace.


unique_hcn_namespace handle;

GUID namespaceGuid; // Initialize it to appropriate namespace guid value

HRESULT hr = HcnOpenNamespace(namespaceGuid, &handle, &errorRecord);

if (FAILED(hr))

{

// UnMarshal  the result Json

THROW_HR(hr);

}

wil::unique_cotaskmem_string errorRecord;

static std::wstring  ModifySettingAddEndpointJson = LR"(

{

"ResourceType" : 1,

"RequestType" : 0,

"Settings" : {

"EndpointId" : "87fdcf16-d210-426e-959d-2a1d4f41d6d1"

}

}

)";

hr = HcnModifyNamespace(handle.get(), ModifySettingAddEndpointJson.c_str(), &errorRecord);

if (FAILED(hr))

{

// UnMarshal the result Json

THROW_HR(hr);

}

hr = HcnCloseNamespace(handle.get());

if (FAILED(hr))

{

// UnMarshal  the result Json

THROW_HR(hr);

}

Enumerate all namespaces

This sample shows how to use Host Compute Network Service API to enumerate all Host Compute Network Namespaces.


wil::unique_cotaskmem_string resultNamespaces;

wil::unique_cotaskmem_string errorRecord;

std::wstring filter [] = LR"(

{

// Future

})";

HRESULT hr = HcnEnumerateNamespace(filter.c_str(), &resultNamespaces, &errorRecord);

if (FAILED(hr))

{

// UnMarshal  the result Json

THROW_HR(hr);

}

Query namespace properties

This sample shows how to use Host Compute Network Service API to query Host Compute Network Namespace properties


unique_hcn_namespace handle;

GUID namespaceGuid; // Initialize it to appropriate namespace guid value

HRESULT hr = HcnOpenNamespace(namespaceGuid, &handle, &errorRecord);

if (FAILED(hr))

{

// UnMarshal  the result Json

THROW_HR(hr);

}

wil::unique_cotaskmem_string errorRecord;

wil::unique_cotaskmem_string properties;

std:wstring query = LR"(

{

// Future

})";

HRESULT hr = HcnQueryNamespaceProperties(handle.get(), query.c_str(), &properties, &errorRecord);

if (FAILED(hr))

{

// UnMarshal  the result Json

THROW_HR(hr);

}

Scenario: HCN load balancer

Create a HCN load balancer

This sample shows how to use Host Compute Network Service API to create a Host Compute Network Load Balancer on the host that can be used to load balance Endpoint across compute.


using unique_hcn_loadbalancer = wil::unique_any<

HCN_LOADBALANCER,

decltype(&HcnCloseLoadBalancer),

HcnCloseLoadBalancer>;

/// Creates a simple HCN LoadBalancer, waiting synchronously to finish the task

void CreateHcnLoadBalancer()

{

unique_hcn_loadbalancer handle;

wil::unique_cotaskmem_string errorRecord;

std::wstring settings = LR"(

{

"SchemaVersion": {

"Major": 2,

"Minor": 0

},

"Owner" : "Sample",

"HostComputeEndpoints" : [

"87fdcf16-d210-426e-959d-2a1d4f41d6d1"

],

"VirtualIPs" : [ "10.0.0.10" ],

"PortMappings" : [

{

"Protocol" : 0,

"InternalPort" : 8080,

"ExternalPort" : 80,

}

],

"EnableDirectServerReturn" : true,

"InternalLoadBalancer" : false,

}

)";

GUID lbGuid;

HRESULT result = CoCreateGuid(&lbGuid);

HRESULT hr = HcnCreateLoadBalancer(

lbGuid,              // Unique ID

settings.c_str(),      // LoadBalancer settings document

&handle,

&errorRecord

);

if (FAILED(hr))

{

// UnMarshal  the result Json

// ErrorSchema

//   {

//    "ErrorCode" : &lt;uint32>,

//    "Error" : &lt;string>,

//    "Success" : &lt;bool>,

//   }

// Failed to create network

THROW_HR(hr);

}

hr = HcnCloseLoadBalancer(handle.get());

if (FAILED(hr))

{

// UnMarshal  the result Json

THROW_HR(hr);

}

}

Delete a HCN load balancer

This sample shows how to use Host Compute Network Service API to delete a Host Compute Network Load Balancer.


wil::unique_cotaskmem_string errorRecord;

GUID lbGuid; // Initialize it to appropriate loadbalancer guid value

HRESULT hr = HcnDeleteLoadBalancer(lbGuid , &errorRecord);

if (FAILED(hr))

{

// UnMarshal the result Json

THROW_HR(hr);

}

Modify a HCN load balancer

This sample shows how to use Host Compute Network Service API to modify a Host Compute Network Load Balancer.


unique_hcn_loadbalancer handle;

GUID lbGuid; // Initialize it to appropriate loadbalancer guid value

HRESULT hr = HcnOpenLoadBalancer(lbGuid, &handle, &errorRecord);

if (FAILED(hr))

{

// UnMarshal  the result Json

THROW_HR(hr);

}

wil::unique_cotaskmem_string errorRecord;

static std::wstring  ModifySettingAddEndpointJson = LR"(

{

"ResourceType" : 1,

"RequestType" : 0,

"Settings" : {

"EndpointId" : "87fdcf16-d210-426e-959d-2a1d4f41d6d1"

}

}

)";

hr = HcnModifyLoadBalancer(handle.get(), ModifySettingAddEndpointJson.c_str(), &errorRecord);

if (FAILED(hr))

{

// UnMarshal the result Json

THROW_HR(hr);

}

hr = HcnCloseLoadBalancer(handle.get());

if (FAILED(hr))

{

// UnMarshal  the result Json

THROW_HR(hr);

}

Enumerate all load balancers

This sample shows how to use Host Compute Network Service API to enumerate all Host Compute Network Load Balancer.


wil::unique_cotaskmem_string resultLoadBalancers;

wil::unique_cotaskmem_string errorRecord;

std::wstring filter [] = LR"(

{

// Future

})";

HRESULT result = HcnEnumerateLoadBalancers(filter.c_str(), & resultLoadbalancers, &errorRecord);

if (FAILED(result))

{

// UnMarshal  the result Json

THROW_HR(result);

}

Query load balancer properties

This sample shows how to use Host Compute Network Service API to query Host Compute Network Load Balancer properties.


unique_hcn_loadbalancer handle;

GUID lbGuid; // Initialize it to appropriate loadbalancer guid value

HRESULT hr = HcnOpenLoadBalancer(lbGuid, &handle, &errorRecord);

if (FAILED(hr))

{

// UnMarshal  the result Json

THROW_HR(hr);

}

wil::unique_cotaskmem_string errorRecord;

wil::unique_cotaskmem_string properties;

std:wstring query = LR"(

{

"ID"  : "",

"Type" : 0,

})";

hr = HcnQueryNProperties(handle.get(), query.c_str(), &properties, &errorRecord);

if (FAILED(hr))

{

// UnMarshal  the result Json

THROW_HR(hr);

}

Scenario: HCN notifications

Register and unregister service-wide notifications

This sample demonstrates how to use the Host Compute Network Service API to register and unregister for service-wide notifications. This enables the caller to receive a notification (via the callback function they specified during registration) whenever a service-wide operation such as a new network creation event has occurred.


using unique_hcn_callback = wil::unique_any<

HCN_CALLBACK,

decltype(&HcnUnregisterServiceCallback),

HcnUnregisterServiceCallback>;

// Callback handle returned by registration api. Kept at

// global or module scope as it will automatically be

// unregistered when it goes out of scope.

unique_hcn_callback g_Callback;

// Event notification callback function.

void

CALLBACK

ServiceCallback(

DWORD   NotificationType,

void*   Context,

HRESULT NotificationStatus,

PCWSTR  NotificationData)

{

// Optional client context

UNREFERENCED_PARAMETER(context);

// Reserved for future use

UNREFERENCED_PARAMETER(NotificationStatus);

switch (NotificationType)

{

case HcnNotificationNetworkCreate:

// TODO: UnMarshal the NotificationData

//

// // Notification

// {

//     "ID" : Guid,

//     "Flags" : &lt;uint32>,

// };

break;

case HcnNotificationNetworkDelete:

// TODO: UnMarshal the NotificationData

break;

Default:

// TODO: handle other events.

break;

}

}

/// Register for service-wide notifications

void RegisterForServiceNotifications()

{

THROW_IF_FAILED(HcnRegisterServiceCallback(

ServiceCallback,

nullptr,

&g_Callback));

}

/// Unregister from service-wide notifications

void UnregisterForServiceNotifications()

{

// As this is a unique_hcn_callback, this will cause HcnUnregisterServiceCallback to be invoked

g_Callback.reset();

}