Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set identifying environment variable for new connections #897

Merged
merged 5 commits into from
May 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 45 additions & 26 deletions src/cascadia/TerminalConnection/ConhostConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,32 @@
#endif

#include <conpty-universal.h>
#include "../../types/inc/Utils.hpp"

using namespace ::Microsoft::Console;

namespace winrt::Microsoft::Terminal::TerminalConnection::implementation
{
ConhostConnection::ConhostConnection(hstring const& commandline,
hstring const& startingDirectory,
uint32_t initialRows,
uint32_t initialCols) :
_connected{ false },
_inPipe{ INVALID_HANDLE_VALUE },
_outPipe{ INVALID_HANDLE_VALUE },
_signalPipe{ INVALID_HANDLE_VALUE },
_outputThreadId{ 0 },
_hOutputThread{ INVALID_HANDLE_VALUE },
_piConhost{ 0 },
_closing{ false }
ConhostConnection::ConhostConnection(const hstring& commandline,
const hstring& startingDirectory,
const uint32_t initialRows,
const uint32_t initialCols,
const guid& initialGuid) :
_initialRows{ initialRows },
_initialCols{ initialCols },
_commandline{ commandline },
_startingDirectory{ startingDirectory },
_guid{ initialGuid }
{
_commandline = commandline;
_startingDirectory = startingDirectory;
_initialRows = initialRows;
_initialCols = initialCols;
if (_guid == guid())
{
_guid = Utils::CreateGuid();
}
}

winrt::guid ConhostConnection::Guid() const noexcept
{
return _guid;
}

winrt::event_token ConhostConnection::TerminalOutput(Microsoft::Terminal::TerminalConnection::TerminalOutputEventArgs const& handler)
Expand All @@ -57,27 +62,41 @@ namespace winrt::Microsoft::Terminal::TerminalConnection::implementation

void ConhostConnection::Start()
{
std::wstring cmdline = _commandline.c_str();
std::wstring cmdline{ _commandline.c_str() };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer std::wstring cmdline{ _commandline.begin(), _commandline.end() }; because then we get guarantees about the bounds o the string we're using to initialize and we don't need to dip into the behind-the-scenes data members of std::string

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

string has an explicit constructor from a T that has a data and size; maybe it can be used here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(std::wstring cmdline{_commandline})

std::optional<std::wstring> startingDirectory;
if (!_startingDirectory.empty())
{
startingDirectory = _startingDirectory;
}

CreateConPty(cmdline,
startingDirectory,
static_cast<short>(_initialCols),
static_cast<short>(_initialRows),
&_inPipe,
&_outPipe,
&_signalPipe,
&_piConhost);
EnvironmentVariableMapW extraEnvVars;
{
// Convert connection Guid to string and ignore the enclosing '{}'.
std::wstring wsGuid{ Utils::GuidToString(_guid) };
wsGuid.pop_back();

const wchar_t* const pwszGuid{ wsGuid.data() + 1 };

// Ensure every connection has the unique identifier in the environment.
extraEnvVars.emplace(L"WT_SESSION", pwszGuid);
}

THROW_IF_FAILED(
CreateConPty(cmdline,
startingDirectory,
static_cast<short>(_initialCols),
static_cast<short>(_initialRows),
&_inPipe,
&_outPipe,
&_signalPipe,
&_piConhost,
extraEnvVars));

_connected = true;

// Create our own output handling thread
// Each console needs to make sure to drain the output from its backing host.
_outputThreadId = (DWORD)-1;
_outputThreadId = static_cast<DWORD>(-1);
_hOutputThread = CreateThread(nullptr,
0,
StaticOutputThreadProc,
Expand Down
32 changes: 17 additions & 15 deletions src/cascadia/TerminalConnection/ConhostConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace winrt::Microsoft::Terminal::TerminalConnection::implementation
{
struct ConhostConnection : ConhostConnectionT<ConhostConnection>
{
ConhostConnection(const hstring& cmdline, const hstring& startingDirectory, uint32_t rows, uint32_t cols);
ConhostConnection(const hstring& cmdline, const hstring& startingDirectory, const uint32_t rows, const uint32_t cols, const guid& guid);

winrt::event_token TerminalOutput(TerminalConnection::TerminalOutputEventArgs const& handler);
void TerminalOutput(winrt::event_token const& token) noexcept;
Expand All @@ -20,24 +20,26 @@ namespace winrt::Microsoft::Terminal::TerminalConnection::implementation
void Resize(uint32_t rows, uint32_t columns);
void Close();

winrt::guid Guid() const noexcept;

private:
winrt::event<TerminalConnection::TerminalOutputEventArgs> _outputHandlers;
winrt::event<TerminalConnection::TerminalDisconnectedEventArgs> _disconnectHandlers;

uint32_t _initialRows;
uint32_t _initialCols;
hstring _commandline;
hstring _startingDirectory;

bool _connected;
HANDLE _inPipe; // The pipe for writing input to
HANDLE _outPipe; // The pipe for reading output from
HANDLE _signalPipe;
//HPCON _hPC;
DWORD _outputThreadId;
HANDLE _hOutputThread;
PROCESS_INFORMATION _piConhost;
bool _closing;
uint32_t _initialRows{};
uint32_t _initialCols{};
hstring _commandline{};
hstring _startingDirectory{};

bool _connected{};
HANDLE _inPipe{ INVALID_HANDLE_VALUE }; // The pipe for writing input to
HANDLE _outPipe{ INVALID_HANDLE_VALUE }; // The pipe for reading output from
HANDLE _signalPipe{ INVALID_HANDLE_VALUE };
DWORD _outputThreadId{};
HANDLE _hOutputThread{ INVALID_HANDLE_VALUE };
PROCESS_INFORMATION _piConhost{};
guid _guid{}; // A "unique" session identifier for connected client
bool _closing{};

static DWORD WINAPI StaticOutputThreadProc(LPVOID lpParameter);
DWORD _OutputThread();
Expand Down
4 changes: 3 additions & 1 deletion src/cascadia/TerminalConnection/ConhostConnection.idl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ namespace Microsoft.Terminal.TerminalConnection
[default_interface]
runtimeclass ConhostConnection : ITerminalConnection
{
ConhostConnection(String cmdline, String startingDirectory, UInt32 rows, UInt32 columns);
ConhostConnection(String cmdline, String startingDirectory, UInt32 rows, UInt32 columns, Guid guid);
binarycrusader marked this conversation as resolved.
Show resolved Hide resolved

Guid Guid { get; };
};

}
12 changes: 12 additions & 0 deletions src/cascadia/TerminalConnection/TerminalConnection.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@

</ItemDefinitionGroup>

<!-- ========================= Project References ======================== -->
<ItemGroup>
<!--
the packaging project won't recurse through our dependencies, you have to
make sure that if you add a cppwinrt dependency to any of these projects,
you also update all the consumers
-->
<ProjectReference Include="$(OpenConsoleDir)src\types\lib\types.vcxproj">
<Project>{18D09A24-8240-42D6-8CB6-236EEE820263}</Project>
</ProjectReference>

</ItemGroup>
<PropertyGroup>
<!--
DON'T REDIRECT OUR OUTPUT.
Expand Down
6 changes: 6 additions & 0 deletions src/cascadia/TerminalConnection/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,11 @@

#pragma once

#include <LibraryIncludes.h>

// Must be included before any WinRT headers.
#include <unknwn.h>

#include "winrt/Windows.Foundation.h"
#include <Windows.h>
#include <wil/result.h>
4 changes: 2 additions & 2 deletions src/cascadia/TerminalControl/TermControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
}

TermControl::TermControl(Settings::IControlSettings settings) :
_connection{ TerminalConnection::ConhostConnection(winrt::to_hstring("cmd.exe"), winrt::hstring(), 30, 80) },
_connection{ TerminalConnection::ConhostConnection(winrt::to_hstring("cmd.exe"), winrt::hstring(), 30, 80, winrt::guid()) },
_initializedTerminal{ false },
_root{ nullptr },
_controlRoot{ nullptr },
Expand Down Expand Up @@ -221,7 +221,7 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
// directory.
void TermControl::_ApplyConnectionSettings()
{
_connection = TerminalConnection::ConhostConnection(_settings.Commandline(), _settings.StartingDirectory(), 30, 80);
_connection = TerminalConnection::ConhostConnection(_settings.Commandline(), _settings.StartingDirectory(), 30, 80, winrt::guid());
}

TermControl::~TermControl()
Expand Down
Loading