30#define _Class _URLSession
43 $(this->thread,
join, NULL);
53#pragma mark - URLSession
65 synchronized(self->condition, {
67 if (isInvalidated ==
false) {
69 $(self->condition, signal);
93 synchronized(self->condition, {
109 synchronized(self->condition, {
111 if (isInvalidated ==
false) {
113 $(self->condition, signal);
207 self->handle = curl_multi_init();
208 assert(self->handle);
214 synchronized(self->condition, {
216 $(self->condition, wait);
232 synchronized(self->condition, {
233 isExecuting = task->isExecuting;
243 const CURLcode err = curl_easy_pause(task->handle, CURLPAUSE_ALL);
244 assert(err == CURLE_OK);
247 synchronized(self->condition, {
255 if (task->handle == NULL) {
258 assert(task->handle);
260 const CURLMcode merr = curl_multi_add_handle(self->handle, task->handle);
261 assert(merr == CURLM_OK);
263 const CURLcode err = curl_easy_pause(task->handle, CURLPAUSE_CONT);
264 assert(err == CURLE_OK);
267 synchronized(self->condition, {
276 const CURLMcode merr = curl_multi_remove_handle(self->handle, task->handle);
277 assert(merr == CURLM_OK);
280 task->
result = CURLE_ABORTED_BY_CALLBACK;
289 synchronized(self->condition, {
294 synchronized(self->condition, {
300 CURLMcode merr = curl_multi_wait(self->handle, NULL, 0, 0, NULL);
301 assert(merr == CURLM_OK);
303 merr = curl_multi_perform(self->handle, &ret);
304 assert(merr == CURLM_OK);
307 while ((message = curl_multi_info_read(self->handle, &ret))) {
313 if (t->handle == message->easy_handle) {
321 if (message->msg == CURLMSG_DONE) {
323 merr = curl_multi_remove_handle(self->handle, task->handle);
324 assert(merr == CURLM_OK);
326 task->
result = message->data.result;
328 synchronized(self->condition, {
344 if (task->completion) {
345 task->completion(task, success);
350 synchronized(self->condition, {
359 curl_multi_cleanup(self->handle);
370 assert(configuration);
380 $(self->thread,
start);
394 synchronized(self->condition, {
396 if (isInvalidated ==
false) {
398 $(self->condition, signal);
442 synchronized(self->condition, {
458#pragma mark - Class lifecycle
467 curl_global_cleanup();
488 const CURLcode code = curl_global_init(CURL_GLOBAL_ALL);
489 assert(code == CURLE_OK);
502 .name =
"URLSession",
505 .interfaceSize =
sizeof(URLSessionInterface),
static ident objectAtIndex(const Array *self, size_t index)
static void removeObject(Array *self, const ident obj)
static Array * arrayWithArray(const Array *array)
static void addObject(Array *self, const ident obj)
static Array * array(void)
static void teardown(void)
Called atexit to teardown Objectively.
static void setup(void)
Called when initializing Object to setup Objectively.
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Class * _initialize(const ClassDef *def)
Initializes the given Class.
ident retain(ident obj)
Atomically increment the given Object's reference count.
#define alloc(type)
Allocate and initialize and instance of type.
#define super(type, obj, method,...)
static bool isKindOfClass(const Object *self, const Class *clazz)
static bool isCancelled(const Operation *self)
static Operation * initWithFunction(Operation *self, OperationFunction function, ident data)
static void start(Operation *self)
static void cancel(Operation *self)
static int request(RESTClient *self, HTTPMethod method, const char *url_string, const Data *body, const char **headers, Data **out_data)
static void join(Thread *self, ident *status)
void * ident
The identity type, similar to Objective-C id.
static URLCachedResponse * cachedResponseForRequest(URLCache *self, const URLRequest *request)
static URLRequest * initWithURL(URLRequest *self, URL *url)
static URLSessionDataTask * dataTaskWithRequest(URLSession *self, URLRequest *request, URLSessionTaskCompletion completion)
static void destroy(Class *clazz)
static URLSessionUploadTask * uploadTaskWithRequest(URLSession *self, URLRequest *request, URLSessionTaskCompletion completion)
static bool hasWork(const URLSession *self)
Checks whether any task needs the run loop.
static URLSession * initWithConfiguration(URLSession *self, URLSessionConfiguration *configuration)
Class * _URLSession(void)
static URLSessionDownloadTask * downloadTaskWithURL(URLSession *self, URL *url, URLSessionTaskCompletion completion)
static Array * tasks(const URLSession *self)
static URLSession * init(URLSession *self)
static ident taskWithRequest(URLSession *self, ident task, URLRequest *request, URLSessionTaskCompletion completion)
URLSessionTask factory function.
static URLSession * sharedInstance(void)
static ident run(Thread *thread)
ThreadFunction for a URLSession.
static void dealloc(Object *self)
static URLSessionDataTask * dataTaskWithURL(URLSession *self, URL *url, URLSessionTaskCompletion completion)
static URLSession * _sharedInstance
static void invalidateAndCancel(URLSession *self)
static void initialize(Class *clazz)
static URLSessionDownloadTask * downloadTaskWithRequest(URLSession *self, URLRequest *request, URLSessionTaskCompletion completion)
A management context for loading resources via URLs.
Class * _URLSessionDataTask(void)
static void cacheResponse(URLSessionDataTask *self)
static URLSessionTask * initWithRequestInSession(URLSessionTask *self, struct URLRequest *request, struct URLSession *session, URLSessionTaskCompletion completion)
void(* URLSessionTaskCompletion)(URLSessionTask *task, bool success)
A function pointer for URLSessionTask completion.
@ URLSESSIONTASK_RESUMING
@ URLSESSIONTASK_CANCELED
@ URLSESSIONTASK_SUSPENDING
@ URLSESSIONTASK_SUSPENDED
@ URLSESSIONTASK_COMPLETED
@ URLSESSIONTASK_CANCELING
#define do_once(once, block)
Executes the given block at most one time.
size_t count
The count of elements.
ClassDefs are passed to _initialize via an archetype to initialize a Class.
The runtime representation of a Class.
ident interface
The interface of the Class.
POSIX Threads conditional variables.
Object is the root Class of The Objectively Class hierarchy.
Object * init(Object *self)
Initializes this Object.
A cache for HTTP responses.
Uniform Resource Locators (RFC 3986).
A protocol-agnostic abstraction for requesting resources via URLs.
int httpStatusCode
The HTTP response status code.
Configuration bundle for URLSession.
URLCache * urlCache
The cache for URL responses, or NULL to disable caching.
Use data tasks to send and receive Data in-memory.
URLCachedResponse * cachedResponse
A cached response, if this task was fulfilled from URLCache.
Use download tasks to save remote resources to file.
A management context for loading resources via URLs.
URLSessionConfiguration * configuration
The session configuration.
void invalidateAndCancel(URLSession *)
Invalidates this URLSession and cancels all pending tasks.
URLSession * initWithConfiguration(URLSession *, URLSessionConfiguration *)
Initializes this URLSession with the given configuration.
URL session tasks are handles to pending URL operations.
struct URLResponse * response
The response.
int result
The libcurl result code (CURLcode) of the transfer, 0 on success.
URLSessionTaskState state
The state.
URLSessionTaskCompletion completion
The completion function.
Use upload tasks to send files directly from disk.