Objectively
Object oriented framework for C.
Loading...
Searching...
No Matches
URLSessionTask Struct Reference

#include <URLSessionTask.h>

Overview

URL session tasks are handles to pending URL operations.

Definition at line 68 of file URLSessionTask.h.

Inheritance diagram for URLSessionTask:
Object URLSessionDataTask URLSessionDownloadTask URLSessionUploadTask

Properties

size_t bytesExpectedToReceive
 The count of bytes this task expects to receive.
 
size_t bytesExpectedToSend
 The count of bytes this task expects to send.
 
size_t bytesReceived
 The count of bytes received.
 
size_t bytesSent
 The count of bytes sent.
 
URLSessionTaskCompletion completion
 The completion function.
 
ident data
 User data.
 
char * error
 The error buffer.
 
Object object
 The superclass.
 
URLSessionTaskProgress progress
 The progress function.
 
struct URLRequest * request
 The request.
 
struct URLResponse * response
 The response.
 
int result
 The libcurl result code (CURLcode) of the transfer, 0 on success.
 
struct URLSession * session
 The session, which is not retained.
 
URLSessionTaskState state
 The state.
 
- Properties inherited from Object
Class * clazz
 Every instance of Object begins with a pointer to its Class.
 
unsigned int magic
 A header to allow introspection of Object types.
 

Methods

Class * _URLSessionTask (void)
 The URLSessionTask archetype.
 
void cancel (URLSessionTask *)
 Cancels this task.
 
void execute (URLSessionTask *)
 Executes this task synchronously, on the calling thread.
 
URLSessionTask * initWithRequestInSession (URLSessionTask *, struct URLRequest *, struct URLSession *, URLSessionTaskCompletion)
 Initializes this task with the given URLRequest.
 
void resume (URLSessionTask *)
 Starts or resumes this task.
 
void setup (URLSessionTask *)
 Sets up this task.
 
void suspend (URLSessionTask *)
 Suspends this task.
 
void teardown (URLSessionTask *)
 Tears down this task.
 
- Methods inherited from Object
Class * _Object (void)
 The Object archetype.
 
Object * copy (const Object *self)
 Creates a shallow copy of this Object.
 
void dealloc (Object *self)
 Frees all resources held by this Object.
 
String * description (const Object *self)
 
int hash (const Object *self)
 
Object * init (Object *self)
 Initializes this Object.
 
bool isEqual (const Object *self, const Object *other)
 Tests equality of the other Object.
 
bool isKindOfClass (const Object *self, const Class *clazz)
 Tests for Class hierarchy membership.
 

Protected Attributes

URLSessionTaskInterface * interface [0]
 The interface type.
 
- Protected Attributes inherited from Object
ObjectInterface * interface [0]
 The interface type.
 

Property Details

◆ bytesExpectedToReceive

size_t URLSessionTask::bytesExpectedToReceive

The count of bytes this task expects to receive.

Definition at line 84 of file URLSessionTask.h.

◆ bytesExpectedToSend

size_t URLSessionTask::bytesExpectedToSend

The count of bytes this task expects to send.

Definition at line 89 of file URLSessionTask.h.

◆ bytesReceived

size_t URLSessionTask::bytesReceived

The count of bytes received.

Definition at line 94 of file URLSessionTask.h.

◆ bytesSent

size_t URLSessionTask::bytesSent

The count of bytes sent.

Definition at line 99 of file URLSessionTask.h.

◆ completion

URLSessionTaskCompletion URLSessionTask::completion

The completion function.

Definition at line 104 of file URLSessionTask.h.

◆ data

ident URLSessionTask::data

User data.

Definition at line 109 of file URLSessionTask.h.

◆ error

char* URLSessionTask::error

The error buffer.

Definition at line 114 of file URLSessionTask.h.

◆ interface

URLSessionTaskInterface* URLSessionTask::interface[0]
protected

The interface type.

Definition at line 79 of file URLSessionTask.h.

◆ object

Object URLSessionTask::object

The superclass.

Definition at line 73 of file URLSessionTask.h.

◆ progress

URLSessionTaskProgress URLSessionTask::progress

The progress function.

Definition at line 132 of file URLSessionTask.h.

◆ request

struct URLRequest* URLSessionTask::request

The request.

Definition at line 137 of file URLSessionTask.h.

◆ response

struct URLResponse* URLSessionTask::response

The response.

Definition at line 148 of file URLSessionTask.h.

◆ result

int URLSessionTask::result

The libcurl result code (CURLcode) of the transfer, 0 on success.

Remarks
Only meaningful once the task is URLSESSIONTASK_COMPLETED. A transfer that ends early, for example with fewer bytes than its Content-Length, completes with a nonzero result.

Definition at line 155 of file URLSessionTask.h.

◆ session

struct URLSession* URLSessionTask::session

The session, which is not retained.

Remarks
The session's Thread releases tasks, so a task that retained its session could release the session's last reference on that Thread, which then could not join itself. A task MUST NOT be used after its session is deallocated.

Definition at line 163 of file URLSessionTask.h.

◆ state

URLSessionTaskState URLSessionTask::state

The state.

Definition at line 168 of file URLSessionTask.h.

Method Details

◆ _URLSessionTask()

Class * _URLSessionTask ( void  )

The URLSessionTask archetype.

Returns
The URLSessionTask Class.

Definition at line 386 of file URLSessionTask.c.

386 {
387 static Class *clazz;
388 static Once once;
389
390 do_once(&once, {
391 clazz = _initialize(&(const ClassDef) {
392 .name = "URLSessionTask",
393 .superclass = _Object(),
394 .instanceSize = sizeof(URLSessionTask),
395 .interfaceSize = sizeof(URLSessionTaskInterface),
396 .initialize = initialize,
397 });
398 });
399
400 return clazz;
401}
static void initialize(Class *clazz)
Definition Array.c:710
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition Class.c:151
long Once
The Once type.
Definition Once.h:37
#define do_once(once, block)
Executes the given block at most one time.
Definition Once.h:43
ClassDefs are passed to _initialize via an archetype to initialize a Class.
Definition Class.h:41
The runtime representation of a Class.
Definition Class.h:90
Class * clazz
Every instance of Object begins with a pointer to its Class.
Definition Object.h:55
Class * _Object(void)
The Object archetype.
Definition Object.c:136
URL session tasks are handles to pending URL operations.

◆ cancel()

void cancel ( URLSessionTask *  self)

Cancels this task.

Parameters
selfThe URLSessionTask.
Remarks
This MAY be called from any thread. It also interrupts a task that execute is running on another thread: execute then returns within about one second, and the task is URLSESSIONTASK_CANCELED.

Definition at line 97 of file URLSessionDataTask.c.

97 {
98
100
101 if (this->cachedResponse) {
102 if (this->urlSessionTask.state != URLSESSIONTASK_COMPLETED) {
103 release(this->cachedResponse);
104 this->cachedResponse = NULL;
105 this->urlSessionTask.state = URLSESSIONTASK_CANCELED;
106 }
107
108 return;
109 }
110
112}
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition Class.c:356
#define super(type, obj, method,...)
@ URLSESSIONTASK_CANCELED
@ URLSESSIONTASK_COMPLETED
Use data tasks to send and receive Data in-memory.
void cancel(URLSessionTask *)
Cancels this task.

◆ execute()

void execute ( URLSessionTask *  self)

Executes this task synchronously, on the calling thread.

Parameters
selfThe URLSessionTask.
Remarks
Do not mix calls to execute with calls to resume. A task that is cancelled before or while it executes ends URLSESSIONTASK_CANCELED, otherwise URLSESSIONTASK_COMPLETED.

Definition at line 117 of file URLSessionDataTask.c.

117 {
118
119 URLSessionDataTask *this = (URLSessionDataTask *) self;
120
121 if (this->urlSessionTask.state == URLSESSIONTASK_CANCELED
122 || this->urlSessionTask.state == URLSESSIONTASK_COMPLETED) {
123 return;
124 }
125
126 if (this->cachedResponse) {
128 this->urlSessionTask.state = URLSESSIONTASK_COMPLETED;
129 return;
130 }
131
132 URLCache *cache = self->session->configuration->urlCache;
133 if (cache) {
134 retain(cache);
135 }
136
138
139 if (cache && self->state == URLSESSIONTASK_COMPLETED && self->result == CURLE_OK) {
140 $(cache, storeCachedResponseForRequest, self->request, self->response, this->data);
141 }
142
143 release(cache);
144}
ident retain(ident obj)
Atomically increment the given Object's reference count.
Definition Class.c:372
static void storeCachedResponseForRequest(URLCache *self, const URLRequest *request, const URLResponse *response, const Data *data)
Definition URLCache.c:356
static void materializeCachedResponse(URLSessionDataTask *self)
Copies a cached response into this task's live response and data fields.
A cache for HTTP responses.
Definition URLCache.h:44
URLCache * urlCache
The cache for URL responses, or NULL to disable caching.
URLSessionConfiguration * configuration
The session configuration.
Definition URLSession.h:81
struct URLResponse * response
The response.
int result
The libcurl result code (CURLcode) of the transfer, 0 on success.
void execute(URLSessionTask *)
Executes this task synchronously, on the calling thread.
struct URLRequest * request
The request.
struct URLSession * session
The session, which is not retained.
URLSessionTaskState state
The state.

◆ initWithRequestInSession()

URLSessionTask * initWithRequestInSession ( URLSessionTask *  self,
struct URLRequest *  request,
struct URLSession *  session,
URLSessionTaskCompletion  completion 
)

Initializes this task with the given URLRequest.

Parameters
selfThe URLSessionTask.
requestThe request.
sessionThe session.
completionThe completion handler.
Returns
The initialized task, or NULL on error.

Definition at line 137 of file URLSessionTask.c.

138 {
139
140 assert(request);
141 assert(session);
142
143 self = (URLSessionTask *) super(Object, self, init);
144 if (self) {
145
146 self->error = calloc(1, CURL_ERROR_SIZE);
147 assert(self->error);
148
149 self->request = retain(request);
150 self->session = session;
151
152 self->response = $(alloc(URLResponse), init);
153
154 self->completion = completion;
155
157 }
158
159 return self;
160}
#define alloc(type)
Allocate and initialize and instance of type.
Definition Class.h:226
@ URLSESSIONTASK_SUSPENDED
Object is the root Class of The Objectively Class hierarchy.
Definition Object.h:46
Object * init(Object *self)
Initializes this Object.
Definition Object.c:83
A protocol-agnostic abstraction for URLSessionTask responses.
Definition URLResponse.h:42
char * error
The error buffer.
URLSessionTaskCompletion completion
The completion function.

◆ resume()

void resume ( URLSessionTask *  self)

Starts or resumes this task.

Parameters
selfThe URLSessionTask.

Definition at line 149 of file URLSessionDataTask.c.

149 {
150
151 URLSessionDataTask *this = (URLSessionDataTask *) self;
152
153 if (this->cachedResponse) {
154 switch (this->urlSessionTask.state) {
159 this->urlSessionTask.state = URLSESSIONTASK_COMPLETED;
160
161 if (this->urlSessionTask.completion) {
162 this->urlSessionTask.completion((URLSessionTask *) this, true);
163 }
164 break;
165 default:
166 break;
167 }
168
169 return;
170 }
171
173}
@ URLSESSIONTASK_RESUMING
@ URLSESSIONTASK_SUSPENDING
void resume(URLSessionTask *)
Starts or resumes this task.

◆ setup()

void setup ( URLSessionTask *  self)

Sets up this task.

Parameters
selfThe URLSessionTask.

Definition at line 198 of file URLSessionDataTask.c.

198 {
199
200 super(URLSessionTask, self, setup);
201
202 curl_easy_setopt(self->handle, CURLOPT_WRITEFUNCTION, writeFunction);
203 curl_easy_setopt(self->handle, CURLOPT_WRITEDATA, self);
204}
static void setup(void)
Called when initializing Object to setup Objectively.
Definition Class.c:113
static size_t writeFunction(char *data, size_t size, size_t count, ident self)
The CURLOPT_WRITEFUNCTION callback.

◆ suspend()

void suspend ( URLSessionTask *  self)

Suspends this task.

Parameters
selfThe URLSessionTask.

Definition at line 331 of file URLSessionTask.c.

331 {
332
333 synchronized(self->session->condition, {
334 switch (self->state) {
338 $(self->session->condition, signal);
339 break;
340 default:
341 break;
342 }
343 });
344}
@ URLSESSIONTASK_RESUMED

◆ teardown()

void teardown ( URLSessionTask *  self)

Tears down this task.

Parameters
selfThe URLSessionTask.

Definition at line 350 of file URLSessionTask.c.

350 {
351
352 if (self->handle) {
353 curl_easy_cleanup(self->handle);
354 self->handle = NULL;
355 }
356
357 if (self->requestHeaders) {
358 curl_slist_free_all(self->requestHeaders);
359 self->requestHeaders = NULL;
360 }
361}

The documentation for this struct was generated from the following files: