private Task handlePropertyRequestAsync(
     MethodCallOptions methodCallOptions,
     ReceivedMessage receivedMessage,
     CancellationToken cancellationToken
     ) => methodCallOptions.Member switch
 {
     "GetAll" => handleGetAllAsync(methodCallOptions, receivedMessage, cancellationToken),
        private void handleMethodCall(
            MethodCallOptions methodCallOptions,
            ReceivedMessage receivedMessage,
            CancellationToken cancellationToken
            )
        {
            async Task withExceptionHandling(Func <MethodCallOptions, ReceivedMessage, CancellationToken, Task> work, CancellationToken localCancellationToken)
            {
                try
                {
                    using (receivedMessage)
                        await work(methodCallOptions, receivedMessage, localCancellationToken);
                }
                catch (DbusException dbusException)
                {
                    await sendMethodCallErrorAsync(
                        methodCallOptions,
                        dbusException.ErrorName,
                        dbusException.ErrorMessage,
                        localCancellationToken
                        );
                }
                catch (Exception e)
                {
                    await sendMethodCallErrorAsync(
                        methodCallOptions,
                        DbusException.CreateErrorName("General"),
                        e.Message,
                        localCancellationToken
                        );
                }
            }

            if (methodCallOptions.InterfaceName == "org.freedesktop.DBus.Properties")
            {
                Task.Run(() => withExceptionHandling(handlePropertyRequestAsync, cancellationToken));
                return;
            }

            var dictionaryEntry = methodCallOptions.Path + "\0" + methodCallOptions.InterfaceName;

            if (objectProxies.TryGetValue(dictionaryEntry, out var proxy))
            {
                Task.Run(() => withExceptionHandling(proxy.HandleMethodCallAsync, cancellationToken));
                return;
            }

            receivedMessage.Dispose();
            Task.Run(() => sendMethodCallErrorAsync(
                         methodCallOptions,
                         DbusException.CreateErrorName("MethodCallTargetNotFound"),
                         "The requested method call isn't mapped to an actual object",
                         cancellationToken
                         ));
        }
Exemplo n.º 3
0
        public Task HandleMethodCallAsync(
            MethodCallOptions methodCallOptions,
            ReceivedMessage message,
            CancellationToken cancellationToken
            )
        {
            switch (methodCallOptions.Member)
            {
            case "GetManagedObjects":
                return(handleGetManagedObjectsAsync(methodCallOptions, message, cancellationToken));

            default:
                throw new DbusException(
                          DbusException.CreateErrorName("UnknownMethod"),
                          "Method not supported"
                          );
            }
        }
Exemplo n.º 4
0
        private async Task handleGetManagedObjectsAsync(
            MethodCallOptions methodCallOptions,
            ReceivedMessage message,
            CancellationToken cancellationToken
            )
        {
            message.AssertSignature("");
            var managedObjects = await target.GetManagedObjectsAsync(cancellationToken).ConfigureAwait(false);

            if (!methodCallOptions.ShouldSendReply)
            {
                return;
            }
            var sendBody = new Encoder();

            sendBody.AddArray(() =>
            {
                foreach (var managedObject in managedObjects)
                {
                    sendBody.StartCompoundValue();
                    sendBody.Add(managedObject.Key);
                    sendBody.AddArray(() =>
                    {
                        foreach (var proxy in managedObject.Value)
                        {
                            sendBody.StartCompoundValue();
                            sendBody.Add(proxy.InterfaceName);
                            proxy.EncodeProperties(sendBody);
                        }
                        sendBody.StartCompoundValue();
                        sendBody.Add("org.freedesktop.DBus.Properties");
                        sendBody.AddArray(() => { }, storesCompoundValues: true);  // empty properties for the properties interface
                    }, storesCompoundValues: true);
                }
            }, storesCompoundValues: true);
            await connection.SendMethodReturnAsync(
                methodCallOptions,
                sendBody,
                "a{oa{sa{sv}}}",
                cancellationToken
                ).ConfigureAwait(false);
        }
        public async Task SendMethodReturnAsync(
            MethodCallOptions methodCallOptions,
            Encoder body,
            Signature signature,
            CancellationToken cancellationToken
            )
        {
            var bodySegments = await body.CompleteWritingAsync(cancellationToken).ConfigureAwait(false);

            var bodyLength = 0;

            foreach (var bodySegment in bodySegments)
            {
                bodyLength += bodySegment.Length;
            }

            var header = createHeader(
                DbusMessageType.MethodReturn,
                DbusMessageFlags.NoReplyExpected,
                bodyLength,
                e =>
            {
                addHeader(e, DbusHeaderType.Destination, methodCallOptions.Sender);
                addHeader(e, methodCallOptions.ReplySerial);
                if (bodyLength > 0)
                {
                    addHeader(e, signature);
                }
            }
                );

            var headerSegments = await header.CompleteWritingAsync(cancellationToken).ConfigureAwait(false);

            await serializedWriteToStream(
                headerSegments,
                bodySegments,
                cancellationToken
                ).ConfigureAwait(false);

            body.CompleteReading(bodySegments);
            header.CompleteReading(headerSegments);
        }