Exemple #1
0
        /// <summary>
        /// Sends payload asynchronously to the connected cion server.
        /// </summary>
        /// <param name="payload">The payload to send.</param>
        /// <exception cref="ArgumentException">Thrown when the payload is not valid.</exception>
        /// <exception cref="InvalidOperationException">Thrown when there is no connected cion server or failed to send payload.</exception>
        /// <since_tizen> 9 </since_tizen>
        public Task <PayloadAsyncResult> SendPayloadAsync(Payload payload)
        {
            if (payload == null || payload.Id.Length == 0)
            {
                throw new ArgumentException("Payload is invalid.");
            }

            if (_tcsDictionary.ContainsKey(payload.Id))
            {
                throw new InvalidOperationException("Payload is already sent.");
            }

            TaskCompletionSource <PayloadAsyncResult> tcs = new TaskCompletionSource <PayloadAsyncResult>();

            _tcsDictionary[payload.Id] = tcs;

            Interop.CionClient.CionClientPayloadAsyncResultCb cb = new Interop.CionClient.CionClientPayloadAsyncResultCb(
                (IntPtr result, IntPtr userData) =>
            {
                TaskCompletionSource <PayloadAsyncResult> tcsToReturn = _tcsDictionary[payload.Id];
                PayloadAsyncResult resultPayload = null;
                try
                {
                    resultPayload = PayloadAsyncResult.CreateFromHandle(result);
                }
                catch (Exception e)
                {
                    Log.Error(LogTag, string.Format("Failed to create PayloadAsyncResult from result handle: {0}.", e.Message));
                    tcsToReturn.SetException(e);
                    return;
                }
                tcsToReturn.SetResult(resultPayload);
                _payloadAsyncCbDictionary.Remove(resultPayload.PayloadId);
                _tcsDictionary.Remove(resultPayload.PayloadId);
            });

            Interop.Cion.ErrorCode ret = Interop.CionClient.CionClientSendPayloadAsync(_handle, payload?._handle, cb, IntPtr.Zero);
            if (ret != Interop.Cion.ErrorCode.None)
            {
                throw CionErrorFactory.GetException(ret, "Failed to send payload.");
            }

            _payloadAsyncCbDictionary[payload?.Id] = cb;

            return(tcs.Task);
        }
Exemple #2
0
        /// <summary>
        /// Sends the payload to a peer asynchronously.
        /// </summary>
        /// <param name="payload">The payload to send.</param>
        /// <param name="peerInfo">The peer to send payload.</param>
        /// <exception cref="ArgumentException">Thrown when the payload is not valid.</exception>
        /// <exception cref="InvalidOperationException">Thrown when there is no such connected cion client or failed to send payload.</exception>
        /// <since_tizen> 9 </since_tizen>
        public Task <PayloadAsyncResult> SendPayloadAsync(Payload payload, PeerInfo peerInfo)
        {
            if (payload == null || payload.Id.Length == 0 || peerInfo == null || peerInfo.UUID.Length == 0)
            {
                throw new ArgumentException("Payload or peerinfo is invalid.");
            }

            TaskCompletionSource <PayloadAsyncResult> tcs = new TaskCompletionSource <PayloadAsyncResult>();

            _tcsDictionary[Tuple.Create(payload.Id, peerInfo.UUID)] = tcs;

            if (_payloadAsyncResultCb == null)
            {
                Interop.CionServer.CionServerPayloadAsyncResultCb cb = new Interop.CionServer.CionServerPayloadAsyncResultCb(
                    (IntPtr result, IntPtr userData) =>
                {
                    PayloadAsyncResult resultPayload = null;
                    try
                    {
                        resultPayload = PayloadAsyncResult.CreateFromHandle(result);
                    }
                    catch (Exception e)
                    {
                        Log.Error(LogTag, string.Format("Failed to create PayloadAsyncResult from result handle: {0}.", e.Message));
                        return;
                    }
                    TaskCompletionSource <PayloadAsyncResult> tcsToReturn = _tcsDictionary[Tuple.Create(resultPayload.PayloadId, resultPayload.PeerInfo.UUID)];
                    tcsToReturn.SetResult(resultPayload);
                    _tcsDictionary.Remove(Tuple.Create(resultPayload.PayloadId, resultPayload.PeerInfo.UUID));
                });
                _payloadAsyncResultCb = cb;
            }

            Interop.Cion.ErrorCode ret = Interop.CionServer.CionServerSendPayloadAsync(_handle, peerInfo?._handle, payload?._handle, _payloadAsyncResultCb, IntPtr.Zero);
            if (ret != Interop.Cion.ErrorCode.None)
            {
                throw CionErrorFactory.GetException(ret, "Failed to send payload.");
            }

            return(tcs.Task);
        }