Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
mpd
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Иван Мажукин
mpd
Commits
a159299a
Commit
a159299a
authored
Jun 04, 2018
by
Max Kellermann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
lib/dbus/AsyncRequest: new helper class
parent
d048545f
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
121 additions
and
19 deletions
+121
-19
Makefile.am
Makefile.am
+1
-0
AsyncRequest.hxx
src/lib/dbus/AsyncRequest.hxx
+111
-0
UdisksNeighborPlugin.cxx
src/neighbor/plugins/UdisksNeighborPlugin.cxx
+9
-19
No files found.
Makefile.am
View file @
a159299a
...
...
@@ -285,6 +285,7 @@ if ENABLE_DBUS
noinst_LIBRARIES
+=
libodbus.a
libodbus_a_SOURCES
=
\
src/lib/dbus/AppendIter.hxx
\
src/lib/dbus/AsyncRequest.hxx
\
src/lib/dbus/Connection.cxx src/lib/dbus/Connection.hxx
\
src/lib/dbus/Error.cxx src/lib/dbus/Error.hxx
\
src/lib/dbus/Iter.hxx
\
...
...
src/lib/dbus/AsyncRequest.hxx
0 → 100644
View file @
a159299a
/*
* Copyright (C) 2018 Max Kellermann <max.kellermann@gmail.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef ODBUS_ASYNC_REQUEST_HXX
#define ODBUS_ASYNC_REQUEST_HXX
#include "PendingCall.hxx"
#include <functional>
#include <assert.h>
namespace
ODBus
{
class
Message
;
/**
* Helper class which makes sending messages and receiving the
* response asynchronously easy.
*
* Remember to always cancel pending operations before destructing
* this class.
*/
class
AsyncRequest
{
PendingCall
pending_call
;
std
::
function
<
void
(
Message
)
noexcept
>
callback
;
public
:
operator
bool
()
const
noexcept
{
return
pending_call
;
}
/**
* Send a message on the specified connection and invoke the
* given callback upon completion (or error).
*
* The callback should invoke Message::CheckThrowError() to
* check for errors.
*
* This object must be kept around until the operation
* completes. It can only be reused after completion.
*/
template
<
typename
F
>
void
Send
(
DBusConnection
&
connection
,
DBusMessage
&
message
,
int
timeout_milliseconds
,
F
&&
_callback
)
{
assert
(
!
pending_call
);
callback
=
std
::
forward
<
F
>
(
_callback
);
pending_call
=
PendingCall
::
SendWithReply
(
&
connection
,
&
message
,
timeout_milliseconds
);
pending_call
.
SetNotify
(
Notify
,
this
);
}
template
<
typename
F
>
void
Send
(
DBusConnection
&
connection
,
DBusMessage
&
message
,
F
&&
_callback
)
{
Send
(
connection
,
message
,
-
1
,
std
::
forward
<
F
>
(
_callback
));
}
void
Cancel
()
{
pending_call
.
Cancel
();
}
private
:
void
Notify
(
DBusPendingCall
*
pending
)
noexcept
{
assert
(
pending_call
.
Get
()
==
pending
);
pending_call
=
{};
std
::
exchange
(
callback
,
{})(
Message
::
StealReply
(
*
pending
));
}
static
void
Notify
(
DBusPendingCall
*
pending
,
void
*
user_data
)
noexcept
{
auto
&
ar
=
*
(
AsyncRequest
*
)
user_data
;
ar
.
Notify
(
pending
);
}
};
}
/* namespace ODBus */
#endif
src/neighbor/plugins/UdisksNeighborPlugin.cxx
View file @
a159299a
...
...
@@ -23,7 +23,7 @@
#include "lib/dbus/Error.hxx"
#include "lib/dbus/Glue.hxx"
#include "lib/dbus/Message.hxx"
#include "lib/dbus/
PendingCall
.hxx"
#include "lib/dbus/
AsyncRequest
.hxx"
#include "lib/dbus/ReadIter.hxx"
#include "lib/dbus/ObjectManager.hxx"
#include "lib/dbus/UDisks2.hxx"
...
...
@@ -82,7 +82,7 @@ class UdisksNeighborExplorer final
Manual
<
SafeSingleton
<
ODBus
::
Glue
>>
dbus_glue
;
ODBus
::
PendingCall
pending_list_call
;
ODBus
::
AsyncRequest
list_request
;
/**
* Protects #by_uri, #by_path.
...
...
@@ -118,13 +118,7 @@ private:
void
Insert
(
UdisksObject
&&
o
)
noexcept
;
void
Remove
(
const
std
::
string
&
path
)
noexcept
;
void
OnListNotify
(
DBusPendingCall
*
pending
)
noexcept
;
static
void
OnListNotify
(
DBusPendingCall
*
pending
,
void
*
user_data
)
noexcept
{
auto
&
e
=
*
(
UdisksNeighborExplorer
*
)
user_data
;
e
.
OnListNotify
(
pending
);
}
void
OnListNotify
(
ODBus
::
Message
reply
)
noexcept
;
DBusHandlerResult
HandleMessage
(
DBusConnection
*
dbus_connection
,
DBusMessage
*
message
)
noexcept
;
...
...
@@ -159,8 +153,9 @@ UdisksNeighborExplorer::DoOpen()
UDISKS2_PATH
,
DBUS_OM_INTERFACE
,
"GetManagedObjects"
);
pending_list_call
=
PendingCall
::
SendWithReply
(
connection
,
msg
.
Get
());
pending_list_call
.
SetNotify
(
OnListNotify
,
this
);
list_request
.
Send
(
connection
,
*
msg
.
Get
(),
std
::
bind
(
&
UdisksNeighborExplorer
::
OnListNotify
,
this
,
std
::
placeholders
::
_1
));
}
catch
(...)
{
dbus_glue
.
Destruct
();
throw
;
...
...
@@ -176,8 +171,8 @@ UdisksNeighborExplorer::Open()
inline
void
UdisksNeighborExplorer
::
DoClose
()
noexcept
{
if
(
pending_list_call
)
{
pending_list_call
.
Cancel
();
if
(
list_request
)
{
list_request
.
Cancel
();
}
// TODO: remove_match
...
...
@@ -317,14 +312,9 @@ UdisksNeighborExplorer::Remove(const std::string &path) noexcept
}
inline
void
UdisksNeighborExplorer
::
OnListNotify
(
DBusPendingCall
*
pending
)
noexcept
UdisksNeighborExplorer
::
OnListNotify
(
ODBus
::
Message
reply
)
noexcept
{
assert
(
pending
==
pending_list_call
.
Get
());
pending_list_call
=
{};
using
namespace
ODBus
;
Message
reply
=
Message
::
StealReply
(
*
pending
);
try
{
reply
.
CheckThrowError
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment