-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathSharer.cpp
executable file
·188 lines (169 loc) · 4.37 KB
/
Sharer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* RDPShare: Windows Desktop Sharing remote control interface
*
* Copyright 2016 Simul Piscator
*
* RDPShare is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* RDPShare is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with RDPShare. If not, see <http://www.gnu.org/licenses/>.
*/
#include <Windows.h>
#include <RdpEncomAPI.h>
#include <Winerror.h>
#include "Sharer.h"
#include "Utils.h"
#include "RDPSessionEvents.h"
struct Sharer::Private
{
struct Events : RDPSessionEvents
{
Sharer* mpSelf;
Events(Sharer* self) : mpSelf(self) {}
void OnAttendeeConnected(ComPtr<IRDPSRAPIAttendee> p) override
{
BStr name;
HR_SucceedOrDie hr = p->get_RemoteName(&name);
if (name == L"Viewer")
hr = p->put_ControlLevel(CTRL_LEVEL_VIEW);
else
hr = p->put_ControlLevel(CTRL_LEVEL_INTERACTIVE);
if (mpSelf->p->mAttendees < 0)
mpSelf->p->mAttendees = 1;
else
++mpSelf->p->mAttendees;
}
void OnAttendeeDisconnected(ComPtr<IRDPSRAPIAttendee>) override
{
--mpSelf->p->mAttendees;
}
void OnControlLevelChangeRequest(ComPtr<IRDPSRAPIAttendee> p, CTRL_LEVEL level) override
{
HR_SucceedOrDie hr = p->put_ControlLevel(level);
}
};
ComPtr<IRDPSRAPISharingSession> mpSession;
ComPtr<Events> mpEvents;
RECT mRect;
int mPort;
std::string mConnectionString;
int mAttendees;
bool mStarted;
Private( Sharer* pSelf )
: mPort(0), mAttendees(-1), mStarted(false)
{
::memset(&mRect, 0, sizeof(mRect));
}
};
Sharer::Sharer()
: p( new Private(this) )
{
}
Sharer::~Sharer()
{
delete p;
}
Sharer&
Sharer::SetRect(const RECT& r)
{
p->mRect = r;
return *this;
}
const RECT&
Sharer::Rect() const
{
return p->mRect;
}
Sharer&
Sharer::SetPort(int i)
{
p->mPort = i;
return *this;
}
int
Sharer::Port() const
{
return p->mPort;
}
const std::string&
Sharer::ConnectionString() const
{
return p->mConnectionString;
}
bool
Sharer::LastAttendeeDisconnected() const
{
return p->mAttendees == 0;
}
Sharer&
Sharer::Start(int maxAttendees)
{
if (p->mStarted)
Stop();
HR_SucceedOrDie hr = ::CoCreateInstance(
CLSID_RDPSession, nullptr,
CLSCTX_INPROC_SERVER | CLSCTX_INPROC_HANDLER,
IID_IRDPSRAPISharingSession, (void**)&p->mpSession
);
ComPtr<IConnectionPointContainer> pCPC;
hr = p->mpSession->QueryInterface(&pCPC);
ComPtr<IConnectionPoint> pCP;
hr = pCPC->FindConnectionPoint(DIID__IRDPSessionEvents, &pCP);
DWORD dwCookie = 0;
p->mpEvents = ComPtr<Private::Events>(new Private::Events(this));
hr = pCP->Advise(p->mpEvents, &dwCookie);
p->mAttendees = -1;
p->mConnectionString.clear();
ComPtr<IRDPSRAPISessionProperties> pProperties;
hr = p->mpSession->get_Properties(&pProperties);
hr = pProperties->put_Property(BStr(L"DrvConAttach"), Variant(true));
hr = pProperties->put_Property(BStr(L"PortProtocol"), Variant(AF_INET));
hr = pProperties->put_Property(BStr(L"PortId"), Variant(p->mPort));
hr = p->mpSession->put_ColorDepth(24);
hr = p->mpSession->SetDesktopSharedRect(
p->mRect.left, p->mRect.top,
p->mRect.right, p->mRect.bottom
);
hr = p->mpSession->Open();
ComPtr<IRDPSRAPIInvitationManager> pInvitations;
hr = p->mpSession->get_Invitations(&pInvitations);
ComPtr<IRDPSRAPIInvitation> pInvitation;
hr = pInvitations->CreateInvitation(nullptr, BStr(L""), BStr(L""), maxAttendees, &pInvitation);
BStr connectionString;
hr = pInvitation->get_ConnectionString(&connectionString);
p->mConnectionString = connectionString.ToUtf8();
p->mStarted = true;
return *this;
}
Sharer&
Sharer::Stop()
{
if (p->mStarted)
{
p->mStarted = false;
p->mAttendees = 0;
p->mConnectionString.clear();
HR_SucceedOrDie hr = p->mpSession->Close();
}
return *this;
}
Sharer&
Sharer::Suspend()
{
HR_SucceedOrDie(p->mpSession->Pause());
return *this;
}
Sharer&
Sharer::Resume()
{
HR_SucceedOrDie(p->mpSession->Resume());
return *this;
}