Skip to content

Commit c34fed7

Browse files
committed
Implement most of WinRT API
1 parent d8850cc commit c34fed7

41 files changed

Lines changed: 2480 additions & 91 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/windows/WslcSDK/Defaults.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Module Name:
1313
--*/
1414

1515
#pragma once
16+
#include <cstdint>
1617

1718
constexpr uint32_t s_DefaultCPUCount = 2;
1819
constexpr uint32_t s_DefaultMemoryMB = 2000;

src/windows/WslcSDK/winrt/CMakeLists.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,46 @@ add_idl_winrt(wslcsdkwinrtidl "wslcsdk.idl")
22
set_target_properties(wslcsdkwinrtidl PROPERTIES FOLDER windows)
33

44
set(SOURCES
5+
Container.cpp
6+
ContainerNamedVolume.cpp
7+
ContainerPortMapping.cpp
8+
ContainerSettings.cpp
9+
ContainerVolume.cpp
10+
ImageInfo.cpp
11+
ImageProgress.cpp
12+
InstallProgress.cpp
13+
Process.cpp
14+
ProcessSettings.cpp
15+
PullImageOptions.cpp
16+
PushImageOptions.cpp
17+
ServiceVersion.cpp
518
Session.cpp
619
SessionSettings.cpp
20+
TagImageOptions.cpp
721
VhdRequirements.cpp
22+
WslcService.cpp
823
)
924

1025
set(HEADERS
26+
Container.h
27+
ContainerNamedVolume.h
28+
ContainerPortMapping.h
29+
ContainerSettings.h
30+
ContainerVolume.h
1131
Helpers.h
32+
ImageInfo.h
33+
ImageProgress.h
34+
InstallProgress.h
35+
Process.h
36+
ProcessSettings.h
37+
PullImageOptions.h
38+
PushImageOptions.h
39+
ServiceVersion.h
1240
Session.h
1341
SessionSettings.h
42+
TagImageOptions.h
1443
VhdRequirements.h
44+
WslcService.h
1545
)
1646

1747
add_library(wslcsdkwinrt STATIC ${SOURCES} ${HEADERS})
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*++
2+
3+
Copyright (c) Microsoft. All rights reserved.
4+
5+
Module Name:
6+
7+
Container.cpp
8+
9+
Abstract:
10+
11+
This file contains the implementation of the WinRT wrapper for the WSLC SDK Container class.
12+
13+
--*/
14+
15+
#include "precomp.h"
16+
#include "Container.h"
17+
#include "Microsoft.WSL.Containers.Container.g.cpp"
18+
#include "Process.h"
19+
20+
namespace winrt::Microsoft::WSL::Containers::implementation {
21+
Container::Container(WslcContainer container)
22+
: m_container(container)
23+
{
24+
}
25+
26+
winrt::Microsoft::WSL::Containers::Container Container::Create(
27+
winrt::Microsoft::WSL::Containers::Session const& session, winrt::Microsoft::WSL::Containers::ContainerSettings const& containerSettings)
28+
{
29+
wil::unique_cotaskmem_string errorMessage;
30+
WslcContainer containerHandle;
31+
auto hr = WslcCreateContainer(GetHandle(session), GetStructPointer(containerSettings), &containerHandle, errorMessage.put());
32+
THROW_MSG_IF_FAILED(hr, errorMessage);
33+
return *winrt::make_self<implementation::Container>(containerHandle);
34+
}
35+
36+
void Container::Start(winrt::Microsoft::WSL::Containers::ContainerStartFlags const& flags)
37+
{
38+
wil::unique_cotaskmem_string errorMessage;
39+
auto hr = WslcStartContainer(ToHandle(), static_cast<WslcContainerStartFlags>(flags), errorMessage.put());
40+
THROW_MSG_IF_FAILED(hr, errorMessage);
41+
}
42+
43+
void Container::Stop(winrt::Microsoft::WSL::Containers::Signal const& signal, uint32_t timeoutSeconds)
44+
{
45+
wil::unique_cotaskmem_string errorMessage;
46+
auto hr = WslcStopContainer(ToHandle(), static_cast<WslcSignal>(signal), timeoutSeconds, errorMessage.put());
47+
THROW_MSG_IF_FAILED(hr, errorMessage);
48+
}
49+
50+
void Container::Delete(winrt::Microsoft::WSL::Containers::DeleteContainerFlags const& flags)
51+
{
52+
wil::unique_cotaskmem_string errorMessage;
53+
auto hr = WslcDeleteContainer(ToHandle(), static_cast<WslcDeleteContainerFlags>(flags), errorMessage.put());
54+
THROW_MSG_IF_FAILED(hr, errorMessage);
55+
}
56+
57+
winrt::Microsoft::WSL::Containers::Process Container::CreateProcess(winrt::Microsoft::WSL::Containers::ProcessSettings const& newProcessSettings)
58+
{
59+
wil::unique_cotaskmem_string errorMessage;
60+
WslcProcess process;
61+
auto hr = WslcCreateContainerProcess(ToHandle(), GetStructPointer(newProcessSettings), &process, errorMessage.put());
62+
THROW_MSG_IF_FAILED(hr, errorMessage);
63+
return *winrt::make_self<implementation::Process>(process);
64+
}
65+
66+
hstring Container::Inspect()
67+
{
68+
wil::unique_cotaskmem_ansistring inspectData;
69+
winrt::check_hresult(WslcInspectContainer(ToHandle(), inspectData.put()));
70+
return winrt::to_hstring(inspectData.get());
71+
}
72+
73+
hstring Container::Id()
74+
{
75+
CHAR id[WSLC_CONTAINER_ID_BUFFER_SIZE];
76+
winrt::check_hresult(WslcGetContainerID(ToHandle(), id));
77+
return winrt::to_hstring(id);
78+
}
79+
80+
winrt::Microsoft::WSL::Containers::Process Container::InitProcess()
81+
{
82+
WslcProcess initProcess;
83+
winrt::check_hresult(WslcGetContainerInitProcess(ToHandle(), &initProcess));
84+
return *winrt::make_self<implementation::Process>(initProcess);
85+
}
86+
87+
winrt::Microsoft::WSL::Containers::ContainerState Container::State()
88+
{
89+
WslcContainerState state;
90+
winrt::check_hresult(WslcGetContainerState(ToHandle(), &state));
91+
return static_cast<winrt::Microsoft::WSL::Containers::ContainerState>(state);
92+
}
93+
94+
WslcContainer Container::ToHandle()
95+
{
96+
return m_container.get();
97+
}
98+
99+
WslcContainer* Container::ToHandlePointer()
100+
{
101+
return m_container.addressof();
102+
}
103+
104+
} // namespace winrt::Microsoft::WSL::Containers::implementation
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*++
2+
3+
Copyright (c) Microsoft. All rights reserved.
4+
5+
Module Name:
6+
7+
Container.h
8+
9+
Abstract:
10+
11+
This file contains the definition of the WinRT wrapper for the WSLC SDK Container class.
12+
13+
--*/
14+
15+
#pragma once
16+
#include "Microsoft.WSL.Containers.Container.g.h"
17+
#include "Helpers.h"
18+
19+
namespace winrt::Microsoft::WSL::Containers::implementation {
20+
struct Container : ContainerT<Container>
21+
{
22+
Container() = default;
23+
Container(WslcContainer container);
24+
25+
static winrt::Microsoft::WSL::Containers::Container Create(
26+
winrt::Microsoft::WSL::Containers::Session const& session, winrt::Microsoft::WSL::Containers::ContainerSettings const& containerSettings);
27+
void Start(winrt::Microsoft::WSL::Containers::ContainerStartFlags const& flags);
28+
void Stop(winrt::Microsoft::WSL::Containers::Signal const& signal, uint32_t timeoutSeconds);
29+
void Delete(winrt::Microsoft::WSL::Containers::DeleteContainerFlags const& flags);
30+
winrt::Microsoft::WSL::Containers::Process CreateProcess(winrt::Microsoft::WSL::Containers::ProcessSettings const& newProcessSettings);
31+
hstring Inspect();
32+
hstring Id();
33+
winrt::Microsoft::WSL::Containers::Process InitProcess();
34+
winrt::Microsoft::WSL::Containers::ContainerState State();
35+
36+
WslcContainer* ToHandlePointer();
37+
WslcContainer ToHandle();
38+
39+
private:
40+
wil::unique_any<WslcContainer, decltype(&WslcReleaseContainer), &WslcReleaseContainer> m_container;
41+
};
42+
43+
} // namespace winrt::Microsoft::WSL::Containers::implementation
44+
namespace winrt::Microsoft::WSL::Containers::factory_implementation {
45+
struct Container : ContainerT<Container, implementation::Container>
46+
{
47+
};
48+
} // namespace winrt::Microsoft::WSL::Containers::factory_implementation
49+
50+
DEFINE_TYPE_HELPERS(Container);
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*++
2+
3+
Copyright (c) Microsoft. All rights reserved.
4+
5+
Module Name:
6+
7+
ContainerNamedVolume.cpp
8+
9+
Abstract:
10+
11+
This file contains the implementation of the WinRT wrapper for the WSLC SDK ContainerNamedVolume class.
12+
13+
--*/
14+
15+
#include "precomp.h"
16+
#include "ContainerNamedVolume.h"
17+
#include "Microsoft.WSL.Containers.ContainerNamedVolume.g.cpp"
18+
19+
namespace winrt::Microsoft::WSL::Containers::implementation {
20+
21+
ContainerNamedVolume::ContainerNamedVolume(hstring const& name, hstring const& containerPath, bool readOnly) :
22+
m_name(winrt::to_string(name)), m_containerPath(winrt::to_string(containerPath)), m_readOnly(readOnly)
23+
{
24+
if (name.empty() || containerPath.empty())
25+
{
26+
throw hresult_invalid_argument();
27+
}
28+
}
29+
30+
hstring ContainerNamedVolume::Name()
31+
{
32+
return winrt::to_hstring(m_name);
33+
}
34+
35+
void ContainerNamedVolume::Name(hstring const& value)
36+
{
37+
if (m_containerNamedVolume)
38+
{
39+
throw hresult_illegal_state_change();
40+
}
41+
42+
if (value.empty())
43+
{
44+
throw hresult_invalid_argument();
45+
}
46+
47+
m_name = winrt::to_string(value);
48+
}
49+
50+
hstring ContainerNamedVolume::ContainerPath()
51+
{
52+
return winrt::to_hstring(m_containerPath);
53+
}
54+
55+
void ContainerNamedVolume::ContainerPath(hstring const& value)
56+
{
57+
if (m_containerNamedVolume)
58+
{
59+
throw hresult_illegal_state_change();
60+
}
61+
62+
if (value.empty())
63+
{
64+
throw hresult_invalid_argument();
65+
}
66+
67+
m_containerPath = winrt::to_string(value);
68+
}
69+
70+
bool ContainerNamedVolume::ReadOnly()
71+
{
72+
return m_readOnly;
73+
}
74+
75+
void ContainerNamedVolume::ReadOnly(bool value)
76+
{
77+
if (m_containerNamedVolume)
78+
{
79+
throw hresult_illegal_state_change();
80+
}
81+
82+
m_readOnly = value;
83+
}
84+
85+
WslcContainerNamedVolume ContainerNamedVolume::ToStruct()
86+
{
87+
if (!m_containerNamedVolume)
88+
{
89+
m_containerNamedVolume = std::make_unique<WslcContainerNamedVolume>();
90+
m_containerNamedVolume->name = m_name.c_str();
91+
m_containerNamedVolume->containerPath = m_containerPath.c_str();
92+
m_containerNamedVolume->readOnly = m_readOnly;
93+
}
94+
95+
return *m_containerNamedVolume;
96+
}
97+
98+
} // namespace winrt::Microsoft::WSL::Containers::implementation
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*++
2+
3+
Copyright (c) Microsoft. All rights reserved.
4+
5+
Module Name:
6+
7+
ContainerNamedVolume.h
8+
9+
Abstract:
10+
11+
This file contains the definition of the WinRT wrapper for the WSLC SDK ContainerNamedVolume class.
12+
13+
--*/
14+
15+
#pragma once
16+
#include "Microsoft.WSL.Containers.ContainerNamedVolume.g.h"
17+
#include "Helpers.h"
18+
19+
namespace winrt::Microsoft::WSL::Containers::implementation {
20+
struct ContainerNamedVolume : ContainerNamedVolumeT<ContainerNamedVolume>
21+
{
22+
ContainerNamedVolume() = default;
23+
24+
ContainerNamedVolume(hstring const& name, hstring const& containerPath, bool readOnly);
25+
hstring Name();
26+
void Name(hstring const& value);
27+
hstring ContainerPath();
28+
void ContainerPath(hstring const& value);
29+
bool ReadOnly();
30+
void ReadOnly(bool value);
31+
32+
WslcContainerNamedVolume ToStruct();
33+
34+
private:
35+
std::string m_name;
36+
std::string m_containerPath;
37+
bool m_readOnly{};
38+
39+
std::unique_ptr<WslcContainerNamedVolume> m_containerNamedVolume;
40+
};
41+
} // namespace winrt::Microsoft::WSL::Containers::implementation
42+
43+
namespace winrt::Microsoft::WSL::Containers::factory_implementation {
44+
struct ContainerNamedVolume : ContainerNamedVolumeT<ContainerNamedVolume, implementation::ContainerNamedVolume>
45+
{
46+
};
47+
} // namespace winrt::Microsoft::WSL::Containers::factory_implementation
48+
49+
DEFINE_TYPE_HELPERS(ContainerNamedVolume);

0 commit comments

Comments
 (0)