Objectively
Ultra-lightweight object oriented framework for GNU C.
Loading...
Searching...
No Matches
URLSession Struct Reference

#include <URLSession.h>

Overview

A management context for loading resources via URLs.

Definition at line 57 of file URLSession.h.

Inheritance diagram for URLSession:
Object

Properties

URLSessionConfigurationconfiguration
 The session configuration.
 
Object object
 The superclass.
 
- 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_URLSession (void)
 The URLSession archetype.
 
URLSessionDataTaskdataTaskWithRequest (URLSession *, URLRequest *, URLSessionTaskCompletion)
 Creates a URLSessionDataTask for the given URLRequest.
 
URLSessionDataTaskdataTaskWithURL (URLSession *, URL *, URLSessionTaskCompletion)
 Creates a URLSessionDataTask for the given URL.
 
URLSessionDownloadTaskdownloadTaskWithRequest (URLSession *, URLRequest *, URLSessionTaskCompletion)
 Creates a URLSessionDownloadTask for the given URLRequest.
 
URLSessionDownloadTaskdownloadTaskWithURL (URLSession *, URL *, URLSessionTaskCompletion)
 Creates a URLSessionDownloadTask for the given URL.
 
URLSessioninit (URLSession *)
 Initializes this URLSession with a default configuration.
 
URLSessioninitWithConfiguration (URLSession *, URLSessionConfiguration *)
 Initializes this URLSession with the given configuration.
 
void invalidateAndCancel (URLSession *)
 Invalidates this URLSession and cancels all pending tasks.
 
URLSessionsharedInstance ()
 
Arraytasks (const URLSession *)
 
URLSessionUploadTaskuploadTaskWithRequest (URLSession *, URLRequest *, URLSessionTaskCompletion)
 Creates a URLSessionUploadTask for the given URLRequest.
 
- 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

URLSessionInterface * interface
 The interface.
 
- Protected Attributes inherited from Object
ObjectInterface * interface
 The interface.
 

Property Details

◆ condition

Condition* URLSession::condition

The condition, and the Lock guarding access to tasks.

Definition at line 77 of file URLSession.h.

◆ configuration

URLSessionConfiguration* URLSession::configuration

The session configuration.

Definition at line 98 of file URLSession.h.

◆ handle

ident URLSession::handle

The libcurl handle.

Definition at line 82 of file URLSession.h.

◆ interface

URLSessionInterface* URLSession::interface
protected

The interface.

Definition at line 68 of file URLSession.h.

◆ object

Object URLSession::object

The superclass.

Definition at line 62 of file URLSession.h.

◆ tasks

Array* URLSession::tasks

The URLSessionTasks.

Definition at line 87 of file URLSession.h.

◆ thread

Thread* URLSession::thread

The backing Thread.

Definition at line 92 of file URLSession.h.

Method Details

◆ _URLSession()

Class * _URLSession ( void  )

The URLSession archetype.

Returns
The URLSession Class.

Definition at line 414 of file URLSession.c.

414 {
415 static Class *clazz;
416 static Once once;
417
418 do_once(&once, {
419 clazz = _initialize(&(const ClassDef) {
420 .name = "URLSession",
421 .superclass = _Object(),
422 .instanceSize = sizeof(URLSession),
423 .interfaceOffset = offsetof(URLSession, interface),
424 .interfaceSize = sizeof(URLSessionInterface),
426 .destroy = destroy,
427 });
428 });
429
430 return clazz;
431}
static void initialize(Class *clazz)
Definition Array.c:710
static void destroy(Class *clazz)
Definition Boole.c:102
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
A management context for loading resources via URLs.
Definition URLSession.h:57
URLSessionInterface * interface
The interface.
Definition URLSession.h:68

◆ dataTaskWithRequest()

URLSessionDataTask * dataTaskWithRequest ( URLSession self,
URLRequest request,
URLSessionTaskCompletion  completion 
)

Creates a URLSessionDataTask for the given URLRequest.

Parameters
selfThe URLSession.
requestThe URLRequest to perform.
completionThe completion handler.
Returns
The URLSessionDataTask, or NULL on error.

Definition at line 76 of file URLSession.c.

76 {
77
79 initWithRequestInSession, request, self, completion);
80 if (task) {
81
82 URLCache *cache = self->configuration->urlCache;
83 if (cache) {
85 if (task->cachedResponse) {
86 return task;
87 }
88 }
89
90 synchronized(self->locals.condition, {
91 $(self->locals.tasks, addObject, task);
92 $(self->locals.condition, signal);
93 });
94 }
95
96 return task;
97}
static void addObject(Array *self, const ident obj)
Definition Array.c:181
#define alloc(type)
Allocate and initialize and instance of type.
Definition Class.h:176
static int request(RESTClient *self, HTTPMethod method, const char *url_string, const Data *body, Data **out_data)
Definition RESTClient.c:57
static URLCachedResponse * cachedResponseForRequest(URLCache *self, const URLRequest *request)
Definition URLCache.c:268
static URLSessionTask * initWithRequestInSession(URLSessionTask *self, struct URLRequest *request, struct URLSession *session, URLSessionTaskCompletion completion)
A cache for HTTP responses.
Definition URLCache.h:44
Use data tasks to send and receive Data in-memory.
URLCachedResponse * cachedResponse
A cached response, if this task was fulfilled from URLCache.
URL session tasks are handles to pending URL operations.

◆ dataTaskWithURL()

URLSessionDataTask * dataTaskWithURL ( URLSession self,
URL url,
URLSessionTaskCompletion  completion 
)

Creates a URLSessionDataTask for the given URL.

Parameters
selfThe URLSession.
urlThe URL to GET.
completionThe completion handler.
Returns
The URLSessionDataTask, or NULL on error.

Definition at line 103 of file URLSession.c.

103 {
104
106 request->httpMethod = HTTP_GET;
107
108 URLSessionDataTask *task = $(self, dataTaskWithRequest, request, completion);
109
111
112 return task;
113}
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition Class.c:195
static URLRequest * initWithURL(URLRequest *self, URL *url)
Definition URLRequest.c:150
@ HTTP_GET
Definition URLRequest.h:46
A protocol-agnostic abstraction for requesting resources via URLs.
Definition URLRequest.h:58
URLSessionDataTask * dataTaskWithRequest(URLSession *, URLRequest *, URLSessionTaskCompletion)
Creates a URLSessionDataTask for the given URLRequest.
Definition URLSession.c:76

◆ downloadTaskWithRequest()

URLSessionDownloadTask * downloadTaskWithRequest ( URLSession self,
URLRequest request,
URLSessionTaskCompletion  completion 
)

Creates a URLSessionDownloadTask for the given URLRequest.

Parameters
selfThe URLSession.
requestThe URLRequest to perform.
completionThe completion handler.
Returns
The URLSessionDownloadTask, or NULL on error.

Definition at line 119 of file URLSession.c.

119 {
120
121 return taskWithRequest(self, alloc(URLSessionDownloadTask), request, completion);
122}
static ident taskWithRequest(URLSession *self, ident task, URLRequest *request, URLSessionTaskCompletion completion)
URLSessionTask factory function.
Definition URLSession.c:58
Use download tasks to save remote resources to file.

◆ downloadTaskWithURL()

URLSessionDownloadTask * downloadTaskWithURL ( URLSession self,
URL url,
URLSessionTaskCompletion  completion 
)

Creates a URLSessionDownloadTask for the given URL.

Parameters
selfThe URLSession.
urlThe URL to GET.
completionThe completion handler.
Returns
The URLSessionDownloadTask, or NULL on error.

Definition at line 128 of file URLSession.c.

128 {
129
131
132 URLSessionDownloadTask *task = $(self, downloadTaskWithRequest, request, completion);
133
135
136 return task;
137}
URLSessionDownloadTask * downloadTaskWithRequest(URLSession *, URLRequest *, URLSessionTaskCompletion)
Creates a URLSessionDownloadTask for the given URLRequest.
Definition URLSession.c:119

◆ init()

URLSession * init ( URLSession self)

Initializes this URLSession with a default configuration.

Parameters
selfThe URLSession.
Returns
The initialized URLSession, or NULL on error.

Definition at line 143 of file URLSession.c.

143 {
144
146
147 self = $(self, initWithConfiguration, configuration);
148
150
151 return self;
152}
Configuration bundle for URLSession.
URLSessionConfiguration * configuration
The session configuration.
Definition URLSession.h:98
URLSession * init(URLSession *)
Initializes this URLSession with a default configuration.
Definition URLSession.c:143
URLSession * initWithConfiguration(URLSession *, URLSessionConfiguration *)
Initializes this URLSession with the given configuration.
Definition URLSession.c:293

◆ initWithConfiguration()

URLSession * initWithConfiguration ( URLSession self,
URLSessionConfiguration configuration 
)

Initializes this URLSession with the given configuration.

Parameters
selfThe URLSession.
configurationThe URLSessionConfiguration.
Returns
The initialized URLSession, or NULL on error.

Definition at line 293 of file URLSession.c.

293 {
294
295 assert(configuration);
296
297 self = (URLSession *) super(Object, self, init);
298 if (self) {
300
301 self->locals.condition = $(alloc(Condition), init);
302 self->locals.tasks = $(alloc(Array), init);
303 self->locals.thread = $(alloc(Thread), initWithFunction, run, self);
304
305 $(self->locals.thread, start);
306 }
307
308 return self;
309}
ident retain(ident obj)
Atomically increment the given Object's reference count.
Definition Class.c:210
#define super(type, obj, method,...)
static Operation * initWithFunction(Operation *self, OperationFunction function, ident data)
Definition Operation.c:119
static void start(Operation *self)
Definition Operation.c:171
static ident run(Thread *thread)
ThreadFunction for the OperationQueue Thread.
Arrays.
Definition Array.h:56
POSIX Threads conditional variables.
Definition Condition.h:44
Object is the root Class of The Objectively Class hierarchy.
Definition Object.h:46
POSIX Threads.
Definition Thread.h:53
Array * tasks
The URLSessionTasks.
Definition URLSession.h:87
Condition * condition
The condition, and the Lock guarding access to tasks.
Definition URLSession.h:77
Thread * thread
The backing Thread.
Definition URLSession.h:92

◆ invalidateAndCancel()

void invalidateAndCancel ( URLSession self)

Invalidates this URLSession and cancels all pending tasks.

Parameters
selfThe URLSession.

Definition at line 315 of file URLSession.c.

315 {
316
317 if (self->locals.thread->isCancelled) {
318 return;
319 }
320
321 Array *tasks = $(self, tasks);
322
323 for (size_t i = 0; i < tasks->count; i++) {
324
325 URLSessionTask *task = $(tasks, objectAtIndex, i);
326 $(task, cancel);
327 }
328
329 release(tasks);
330
331 $(self->locals.thread, cancel);
332 synchronized(self->locals.condition, $(self->locals.condition, signal));
333}
static ident objectAtIndex(const Array *self, size_t index)
Definition Array.c:578
static void cancel(Operation *self)
Definition Operation.c:74
size_t count
The count of elements.
Definition Array.h:72
bool isCancelled
true when this Thread has been cancelled, false otherwise.
Definition Thread.h:79

◆ sharedInstance()

URLSession * sharedInstance ( void  )
Returns
The shared URLSession instance.

Definition at line 212 of file Log.c.

212 {
213
214 static Once once;
215
216 do_once(&once, {
218 });
219
220 return _sharedInstance;
221}
static Log * _sharedInstance
Definition Log.c:206
A Log4J-inspired log appender.
Definition Log.h:61

◆ tasks()

Array * tasks ( const URLSession self)
Parameters
selfThe URLSession.
Returns
An instantaneous copy of this URLSession's URLSessionTasks.

Definition at line 356 of file URLSession.c.

356 {
357
358 Array *array;
359
360 synchronized(self->locals.condition, {
361 array = $$(Array, arrayWithArray, (Array *) self->locals.tasks);
362 });
363
364 return array;
365}
static Array * arrayWithArray(const Array *array)
Definition Array.c:243
static Array * array(void)
Definition Array.c:234

◆ uploadTaskWithRequest()

URLSessionUploadTask * uploadTaskWithRequest ( URLSession self,
URLRequest request,
URLSessionTaskCompletion  completion 
)

Creates a URLSessionUploadTask for the given URLRequest.

Parameters
selfThe URLSession.
requestThe URLRequest to perform.
completionThe completion handler.
Returns
The URLSessionUploadTask, or NULL on error.

Definition at line 371 of file URLSession.c.

371 {
372
373 return taskWithRequest(self, alloc(URLSessionUploadTask), request, completion);
374}
Use upload tasks to send files directly from disk.

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