2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using System.Collections.Generic;
19 using System.Threading.Tasks;
24 /// Base class for map service request
26 /// <typeparam name="T"></typeparam>
27 public abstract class MapServiceRequest<T>
29 protected TaskCompletionSource<IEnumerable<T>> _requestTask;
30 protected string errMessage;
31 protected int? _requestID;
32 protected ServiceRequestType _type;
34 internal Action startExecutionAction;
35 internal Interop.ErrorCode errorCode;
36 internal Interop.ServiceHandle _service;
39 /// Creates map service request
41 /// <param name="service">map service object</param>
42 /// <param name="type">Request type</param>
43 internal MapServiceRequest(MapService service, ServiceRequestType type)
45 _service = service.handle;
50 /// Sends request to map service provider
52 /// <returns>Response from map service provider</returns>
53 /// <exception cref="TaskCanceledException">Throws if request is canceled</exception>
54 /// <exception cref="InvalidOperationException">Throws if native operation failed</exception>
55 public async Task<IEnumerable<T>> GetResponseAsync()
57 if (_requestTask == null || _requestTask.Task.IsCanceled)
59 _requestTask = new TaskCompletionSource<IEnumerable<T>>();
60 startExecutionAction();
61 await _requestTask.Task;
63 errorCode.WarnIfFailed(errMessage);
64 return await _requestTask.Task;
68 /// Cancel this map service request
72 if (_requestTask?.Task.IsCompleted == false)
74 _requestTask?.SetCanceled();
75 if (_requestID != null)
77 var err = Interop.Service.CancelRequest(_service, (int)_requestID);
78 err.ThrowIfFailed(string.Format("Unable to cancel service request, Type: {0}, ID: {1}", _type, _requestID));
81 errorCode = Interop.ErrorCode.Canceled;