|
| 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 |
0 commit comments