Objectively
Ultra-lightweight object oriented framework for GNU 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 URLRequestrequest
 The request.
 
struct URLResponseresponse
 The response.
 
struct URLSessionsession
 The session.
 
URLSessionTaskState state
 The state.
 
- Properties inherited from Object
Classclazz
 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.
 
URLSessionTaskinitWithRequestInSession (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.
 
Objectcopy (const Object *self)
 Creates a shallow copy of this Object.
 
void dealloc (Object *self)
 Frees all resources held by this Object.
 
Stringdescription (const Object *self)
 
int hash (const Object *self)
 
Objectinit (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
 The interface.
 
- Protected Attributes inherited from Object
ObjectInterface * interface
 The interface.
 

Property Details

◆ bytesExpectedToReceive

size_t URLSessionTask::bytesExpectedToReceive

The count of bytes this task expects to receive.

Definition at line 99 of file URLSessionTask.h.

◆ bytesExpectedToSend

size_t URLSessionTask::bytesExpectedToSend

The count of bytes this task expects to send.

Definition at line 104 of file URLSessionTask.h.

◆ bytesReceived

size_t URLSessionTask::bytesReceived

The count of bytes received.

Definition at line 109 of file URLSessionTask.h.

◆ bytesSent

size_t URLSessionTask::bytesSent

The count of bytes sent.

Definition at line 114 of file URLSessionTask.h.

◆ completion

URLSessionTaskCompletion URLSessionTask::completion

The completion function.

Definition at line 119 of file URLSessionTask.h.

◆ data

ident URLSessionTask::data

User data.

Definition at line 124 of file URLSessionTask.h.

◆ error

char* URLSessionTask::error

The error buffer.

Definition at line 129 of file URLSessionTask.h.

◆ handle

ident URLSessionTask::handle

The backing libcurl handle.

Definition at line 88 of file URLSessionTask.h.

◆ interface

URLSessionTaskInterface* URLSessionTask::interface
protected

The interface.

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 134 of file URLSessionTask.h.

◆ request

struct URLRequest* URLSessionTask::request

The request.

Definition at line 139 of file URLSessionTask.h.

◆ requestHeaders

ident URLSessionTask::requestHeaders

HTTP headers, in libcurl list structure.

Definition at line 93 of file URLSessionTask.h.

◆ response

struct URLResponse* URLSessionTask::response

The response.

Definition at line 144 of file URLSessionTask.h.

◆ session

struct URLSession* URLSessionTask::session

The session.

Definition at line 149 of file URLSessionTask.h.

◆ state

URLSessionTaskState URLSessionTask::state

The state.

Definition at line 154 of file URLSessionTask.h.

Method Details

◆ _URLSessionTask()

Class * _URLSessionTask ( void  )

The URLSessionTask archetype.

Returns
The URLSessionTask Class.

Definition at line 342 of file URLSessionTask.c.

342 {
343 static Class *clazz;
344 static Once once;
345
346 do_once(&once, {
347 clazz = _initialize(&(const ClassDef) {
348 .name = "URLSessionTask",
349 .superclass = _Object(),
350 .instanceSize = sizeof(URLSessionTask),
351 .interfaceOffset = offsetof(URLSessionTask, interface),
352 .interfaceSize = sizeof(URLSessionTaskInterface),
354 });
355 });
356
357 return clazz;
358}
static void initialize(Class *clazz)
Definition Array.c:710
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition Class.c:86
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:95
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.
URLSessionTaskInterface * interface
The interface.

◆ cancel()

void cancel ( URLSessionTask self)

Cancels this task.

Parameters
selfThe URLSessionTask.

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:195
#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.

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 $(self, setup);
133
134 CURLcode code = curl_easy_perform(self->locals.handle);
135
136 curl_easy_getinfo(self->locals.handle, CURLINFO_RESPONSE_CODE, (long *) &self->response->httpStatusCode);
137
138 this->urlSessionTask.state = URLSESSIONTASK_COMPLETED;
139
140 if (code == CURLE_OK) {
141 $(this, cacheResponse);
142 }
143
144 $(self, teardown);
145}
static void teardown(void)
Called atexit to teardown Objectively.
Definition Class.c:46
static void setup(void)
Called when initializing Object to setup Objectively.
Definition Class.c:73
static void materializeCachedResponse(URLSessionDataTask *self)
Copies a cached response into this task's live response and data fields.
static void cacheResponse(URLSessionDataTask *self)
int httpStatusCode
The HTTP response status code.
Definition URLResponse.h:63
struct URLResponse * response
The response.
ident handle
The backing libcurl handle.

◆ 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 105 of file URLSessionTask.c.

106 {
107
108 assert(request);
109 assert(session);
110
111 self = (URLSessionTask *) super(Object, self, init);
112 if (self) {
113
114 self->error = calloc(1, CURL_ERROR_SIZE);
115 assert(self->error);
116
117 self->request = retain(request);
118 self->session = retain(session);
119
120 self->response = $(alloc(URLResponse), init);
121
122 self->completion = completion;
123
125 }
126
127 return self;
128}
ident retain(ident obj)
Atomically increment the given Object's reference count.
Definition Class.c:210
#define alloc(type)
Allocate and initialize and instance of type.
Definition Class.h:176
@ 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.
struct URLRequest * request
The request.
struct URLSession * session
The session.
URLSessionTaskState state
The state.
URLSessionTaskCompletion completion
The completion function.

◆ resume()

void resume ( URLSessionTask self)

Starts or resumes this task.

Parameters
selfThe URLSessionTask.

Definition at line 150 of file URLSessionDataTask.c.

150 {
151
152 URLSessionDataTask *this = (URLSessionDataTask *) self;
153
154 if (this->cachedResponse) {
155 switch (this->urlSessionTask.state) {
160 this->urlSessionTask.state = URLSESSIONTASK_COMPLETED;
161
162 if (this->urlSessionTask.completion) {
163 this->urlSessionTask.completion((URLSessionTask *) this, true);
164 }
165 break;
166 default:
167 break;
168 }
169
170 return;
171 }
172
174}
@ 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 199 of file URLSessionDataTask.c.

199 {
200
201 super(URLSessionTask, self, setup);
202
203 curl_easy_setopt(self->locals.handle, CURLOPT_WRITEFUNCTION, writeFunction);
204 curl_easy_setopt(self->locals.handle, CURLOPT_WRITEDATA, self);
205}
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 290 of file URLSessionTask.c.

290 {
291
292 switch (self->state) {
296 break;
297 default:
298 break;
299 }
300}
@ URLSESSIONTASK_RESUMED

◆ teardown()

void teardown ( URLSessionTask self)

Tears down this task.

Parameters
selfThe URLSessionTask.

Definition at line 306 of file URLSessionTask.c.

306 {
307
308 if (self->locals.handle) {
309 curl_easy_cleanup(self->locals.handle);
310 self->locals.handle = NULL;
311 }
312
313 if (self->locals.requestHeaders) {
314 curl_slist_free_all(self->locals.requestHeaders);
315 self->locals.requestHeaders = NULL;
316 }
317}
ident requestHeaders
HTTP headers, in libcurl list structure.

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