VirtualBox

source: vbox/trunk/src/VBox/HostServices/common/client.cpp@ 85714

Last change on this file since 85714 was 85714, checked in by vboxsync, 4 years ago

HGCM / DnD: Removed the protocol version from the generic HGCM client implementation and integrated it into the DnD host service instead (for keeping backwards compatibility).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.8 KB
Line 
1/* $Id: client.cpp 85714 2020-08-12 12:40:56Z vboxsync $ */
2/** @file
3 * Base class for a host-guest service.
4 */
5
6/*
7 * Copyright (C) 2011-2020 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#include <VBox/log.h>
19#include <VBox/hgcmsvc.h>
20
21#include <iprt/assert.h>
22#include <iprt/alloc.h>
23#include <iprt/cpp/utils.h>
24
25#include <VBox/HostServices/Service.h>
26
27using namespace HGCM;
28
29Client::Client(uint32_t idClient)
30 : m_idClient(idClient)
31 , m_fDeferred(false)
32{
33 RT_ZERO(m_Deferred);
34 RT_ZERO(m_SvcCtx);
35}
36
37Client::~Client(void)
38{
39
40}
41
42/**
43 * Completes a guest call by returning the control back to the guest side,
44 * together with a status code, internal version.
45 *
46 * @returns IPRT status code.
47 * @param hHandle Call handle to complete guest call for.
48 * @param rcOp Return code to return to the guest side.
49 */
50int Client::completeInternal(VBOXHGCMCALLHANDLE hHandle, int rcOp) RT_NOEXCEPT
51{
52 LogFlowThisFunc(("idClient=%RU32\n", m_idClient));
53
54 if ( m_SvcCtx.pHelpers
55 && m_SvcCtx.pHelpers->pfnCallComplete)
56 {
57 m_SvcCtx.pHelpers->pfnCallComplete(hHandle, rcOp);
58
59 reset();
60 return VINF_SUCCESS;
61 }
62
63 return VERR_NOT_AVAILABLE;
64}
65
66/**
67 * Resets the client's internal state.
68 */
69void Client::reset(void) RT_NOEXCEPT
70{
71 m_fDeferred = false;
72
73 RT_ZERO(m_Deferred);
74}
75
76/**
77 * Completes a guest call by returning the control back to the guest side,
78 * together with a status code.
79 *
80 * @returns IPRT status code.
81 * @param hHandle Call handle to complete guest call for.
82 * @param rcOp Return code to return to the guest side.
83 */
84int Client::Complete(VBOXHGCMCALLHANDLE hHandle, int rcOp /* = VINF_SUCCESS */) RT_NOEXCEPT
85{
86 return completeInternal(hHandle, rcOp);
87}
88
89/**
90 * Completes a deferred guest call by returning the control back to the guest side,
91 * together with a status code.
92 *
93 * @returns IPRT status code. VERR_INVALID_STATE if the client is not in deferred mode.
94 * @param rcOp Return code to return to the guest side.
95 */
96int Client::CompleteDeferred(int rcOp) RT_NOEXCEPT
97{
98 if (m_fDeferred)
99 {
100 Assert(m_Deferred.hHandle != NULL);
101
102 int rc = completeInternal(m_Deferred.hHandle, rcOp);
103 if (RT_SUCCESS(rc))
104 m_fDeferred = false;
105
106 return rc;
107 }
108
109 AssertMsg(m_fDeferred, ("Client %RU32 is not in deferred mode\n", m_idClient));
110 return VERR_INVALID_STATE;
111}
112
113/**
114 * Returns the HGCM call handle of the client.
115 *
116 * @returns HGCM handle.
117 */
118VBOXHGCMCALLHANDLE Client::GetHandle(void) const RT_NOEXCEPT
119{
120 return m_Deferred.hHandle;
121}
122
123/**
124 * Returns the HGCM call handle of the client.
125 *
126 * @returns HGCM handle.
127 */
128uint32_t Client::GetMsgType(void) const RT_NOEXCEPT
129{
130 return m_Deferred.uType;
131}
132
133uint32_t Client::GetMsgParamCount(void) const RT_NOEXCEPT
134{
135 return m_Deferred.cParms;
136}
137
138/**
139 * Returns the client's (HGCM) ID.
140 *
141 * @returns The client's (HGCM) ID.
142 */
143uint32_t Client::GetClientID(void) const RT_NOEXCEPT
144{
145 return m_idClient;
146}
147
148/**
149 * Returns whether the client currently is in deferred mode or not.
150 *
151 * @returns \c True if in deferred mode, \c False if not.
152 */
153bool Client::IsDeferred(void) const RT_NOEXCEPT
154{
155 return m_fDeferred;
156}
157
158/**
159 * Set the client's status to deferred, meaning that it does not return to the caller
160 * until CompleteDeferred() has been called.
161 */
162void Client::SetDeferred(VBOXHGCMCALLHANDLE hHandle, uint32_t u32Function, uint32_t cParms, VBOXHGCMSVCPARM paParms[]) RT_NOEXCEPT
163{
164 LogFlowThisFunc(("uClient=%RU32\n", m_idClient));
165
166#ifndef DEBUG_bird /** r=bird: This bugger triggers in the DnD service when restoring saved state. Not tested? */
167 AssertMsg(m_fDeferred == false, ("Client already in deferred mode\n"));
168#endif
169 m_fDeferred = true;
170
171 m_Deferred.hHandle = hHandle;
172 m_Deferred.uType = u32Function;
173 m_Deferred.cParms = cParms;
174 m_Deferred.paParms = paParms;
175}
176
177/**
178 * Sets the HGCM service context.
179 *
180 * @param SvcCtx Service context to set.
181 */
182void Client::SetSvcContext(const VBOXHGCMSVCTX &SvcCtx) RT_NOEXCEPT
183{
184 m_SvcCtx = SvcCtx;
185}
186
187/**
188 * Sets the deferred parameters to a specific message type and
189 * required parameters. That way the client can re-request that message with
190 * the right amount of parameters from the service.
191 *
192 * @returns IPRT status code.
193 * @param uMsg Message type (number) to set.
194 * @param cParms Number of parameters the message needs.
195 */
196int Client::SetDeferredMsgInfo(uint32_t uMsg, uint32_t cParms) RT_NOEXCEPT
197{
198 if (m_fDeferred)
199 {
200 if (m_Deferred.cParms < 2)
201 return VERR_INVALID_PARAMETER;
202
203 AssertPtrReturn(m_Deferred.paParms, VERR_BUFFER_OVERFLOW);
204
205 HGCMSvcSetU32(&m_Deferred.paParms[0], uMsg);
206 HGCMSvcSetU32(&m_Deferred.paParms[1], cParms);
207
208 return VINF_SUCCESS;
209 }
210
211 AssertFailed();
212 return VERR_INVALID_STATE;
213}
214
215/**
216 * Sets the deferred parameters to a specific message type and
217 * required parameters. That way the client can re-request that message with
218 * the right amount of parameters from the service.
219 *
220 * @returns IPRT status code.
221 * @param pMessage Message to get message type and required parameters from.
222 */
223int Client::SetDeferredMsgInfo(const Message *pMessage) RT_NOEXCEPT
224{
225 AssertPtrReturn(pMessage, VERR_INVALID_POINTER);
226 return SetDeferredMsgInfo(pMessage->GetType(), pMessage->GetParamCount());
227}
228
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette