Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
Properties | Methods | Protected Attributes
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. More...
 
Object object
 The superclass. More...
 
Conditioncondition
 The condition. More...
 
ident handle
 The libcurl handle. More...
 
Locklock
 The Lock guarding access to tasks. More...
 
MutableArraytasks
 The URLSessionTasks. More...
 
Threadthread
 The backing Thread. More...
 
- Properties inherited from Object
Classclazz
 Every instance of Object begins with a pointer to its Class. More...
 

Methods

Class_URLSession (void)
 The URLSession archetype. More...
 
URLSessionDataTaskdataTaskWithRequest (URLSession *, URLRequest *, URLSessionTaskCompletion)
 Creates a URLSessionDataTask for the given URLRequest. More...
 
URLSessionDataTaskdataTaskWithURL (URLSession *, URL *, URLSessionTaskCompletion)
 Creates a URLSessionDataTask for the given URL. More...
 
URLSessionDownloadTaskdownloadTaskWithRequest (URLSession *, URLRequest *, URLSessionTaskCompletion)
 Creates a URLSessionDownloadTask for the given URLRequest. More...
 
URLSessionDownloadTaskdownloadTaskWithURL (URLSession *, URL *, URLSessionTaskCompletion)
 Creates a URLSessionDownloadTask for the given URL. More...
 
URLSessioninit (URLSession *)
 Initializes this URLSession with a default configuration. More...
 
URLSessioninitWithConfiguration (URLSession *, URLSessionConfiguration *)
 Initializes this URLSession with the given configuration. More...
 
void invalidateAndCancel (URLSession *)
 Invalidates this URLSession and cancels all pending tasks. More...
 
URLSessionsharedInstance ()
 
Arraytasks (const URLSession *)
 
URLSessionUploadTaskuploadTaskWithRequest (URLSession *, URLRequest *, URLSessionTaskCompletion)
 Creates a URLSessionUploadTask for the given URLRequest. More...
 
- Methods inherited from Object
Class_Object (void)
 The Object archetype. More...
 
Objectcopy (const Object *self)
 Creates a shallow copy of this Object. More...
 
void dealloc (Object *self)
 Frees all resources held by this Object. More...
 
Stringdescription (const Object *self)
 
int hash (const Object *self)
 
Objectinit (Object *self)
 Initializes this Object. More...
 
_Bool isEqual (const Object *self, const Object *other)
 Tests equality of the other Object. More...
 
_Bool isKindOfClass (const Object *self, const Class *clazz)
 Tests for Class hierarchy membership. More...
 

Protected Attributes

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

Property Details

◆ condition

Condition* URLSession::condition

The condition.

Definition at line 77 of file URLSession.h.

◆ configuration

URLSessionConfiguration* URLSession::configuration

The session configuration.

Definition at line 103 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.

◆ lock

Lock* URLSession::lock

The Lock guarding access to tasks.

Definition at line 87 of file URLSession.h.

◆ object

Object URLSession::object

The superclass.

Definition at line 62 of file URLSession.h.

◆ tasks

MutableArray* URLSession::tasks

The URLSessionTasks.

Definition at line 92 of file URLSession.h.

◆ thread

Thread* URLSession::thread

The backing Thread.

Definition at line 97 of file URLSession.h.

Method Details

◆ _URLSession()

Class * _URLSession ( void  )

The URLSession archetype.

Returns
The URLSession Class.

Definition at line 389 of file URLSession.c.

389 {
390 static Class *clazz;
391 static Once once;
392
393 do_once(&once, {
394 clazz = _initialize(&(const ClassDef) {
395 .name = "URLSession",
396 .superclass = _Object(),
397 .instanceSize = sizeof(URLSession),
398 .interfaceOffset = offsetof(URLSession, interface),
399 .interfaceSize = sizeof(URLSessionInterface),
401 .destroy = destroy,
402 });
403 });
404
405 return clazz;
406}
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition: Class.c:91
static void destroy(Class *clazz)
Definition: URLSession.c:378
static void initialize(Class *clazz)
Definition: URLSession.c:356
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:51
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
78 return taskWithRequest(self, alloc(URLSessionDataTask), request, completion);
79}
#define alloc(type)
Allocate and initialize and instance of type.
Definition: Class.h:159
static ident taskWithRequest(URLSession *self, ident task, URLRequest *request, URLSessionTaskCompletion completion)
URLSessionTask factory function.
Definition: URLSession.c:57
Use data tasks to send and receive Data in-memory.

◆ 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 85 of file URLSession.c.

85 {
86
87 URLRequest *request = $(alloc(URLRequest), initWithURL, url);
88
89 URLSessionDataTask *task = $(self, dataTaskWithRequest, request, completion);
90
91 release(request);
92
93 return task;
94}
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition: Class.c:196
static URLRequest * initWithURL(URLRequest *self, URL *url)
Definition: URLRequest.c:73
A protocol-agnostic abstraction for requesting resources via URLs.
Definition: URLRequest.h:56
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 100 of file URLSession.c.

100 {
101
102 return taskWithRequest(self, alloc(URLSessionDownloadTask), request, completion);
103}
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 109 of file URLSession.c.

109 {
110
111 URLRequest *request = $(alloc(URLRequest), initWithURL, url);
112
113 URLSessionDownloadTask *task = $(self, downloadTaskWithRequest, request, completion);
114
115 release(request);
116
117 return task;
118}
URLSessionDownloadTask * downloadTaskWithRequest(URLSession *, URLRequest *, URLSessionTaskCompletion)
Creates a URLSessionDownloadTask for the given URLRequest.
Definition: URLSession.c:100

◆ 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 124 of file URLSession.c.

124 {
125
127
128 self = $(self, initWithConfiguration, configuration);
129
131
132 return self;
133}
Configuration bundle for URLSession.
URLSessionConfiguration * configuration
The session configuration.
Definition: URLSession.h:103
URLSession * init(URLSession *)
Initializes this URLSession with a default configuration.
Definition: URLSession.c:124
URLSession * initWithConfiguration(URLSession *, URLSessionConfiguration *)
Initializes this URLSession with the given configuration.
Definition: URLSession.c:267

◆ 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 267 of file URLSession.c.

267 {
268
269 assert(configuration);
270
271 self = (URLSession *) super(Object, self, init);
272 if (self) {
274
275 self->locals.condition = $(alloc(Condition), init);
276 self->locals.lock = $(alloc(Lock), init);
277 self->locals.tasks = $(alloc(MutableArray), init);
278 self->locals.thread = $(alloc(Thread), initWithFunction, run, self);
279
280 $(self->locals.thread, start);
281 }
282
283 return self;
284}
ident retain(ident obj)
Atomically increment the given Object's reference count.
Definition: Class.c:211
#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 a URLSession.
Definition: URLSession.c:138
POSIX Threads conditional variables.
Definition: Condition.h:44
POSIX Threads locks.
Definition: Lock.h:42
Mutable arrays.
Definition: MutableArray.h:40
Object is the root Class of The Objectively Class hierarchy.
Definition: Object.h:46
POSIX Threads.
Definition: Thread.h:53
Lock * lock
The Lock guarding access to tasks.
Definition: URLSession.h:87
Condition * condition
The condition.
Definition: URLSession.h:77
Thread * thread
The backing Thread.
Definition: URLSession.h:97
MutableArray * tasks
The URLSessionTasks.
Definition: URLSession.h:92

◆ invalidateAndCancel()

void invalidateAndCancel ( URLSession self)

Invalidates this URLSession and cancels all pending tasks.

Parameters
selfThe URLSession.

Definition at line 290 of file URLSession.c.

290 {
291
292 if (self->locals.thread->isCancelled) {
293 return;
294 }
295
296 Array *tasks = $(self, tasks);
297
298 for (size_t i = 0; i < tasks->count; i++) {
299
300 URLSessionTask *task = $(tasks, objectAtIndex, i);
301 $(task, cancel);
302 }
303
304 release(tasks);
305
306 $(self->locals.thread, cancel);
307 $(self->locals.condition, signal);
308}
static ident objectAtIndex(const Array *self, size_t index)
Definition: Array.c:394
static void cancel(Operation *self)
Definition: Operation.c:74
Immutable arrays.
Definition: Array.h:56
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
URL session tasks are handles to pending URL operations.

◆ 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 331 of file URLSession.c.

331 {
332
333 Array *array;
334
335 synchronized(self->locals.lock, {
336 array = $$(Array, arrayWithArray, (Array *) self->locals.tasks);
337 });
338
339 return array;
340}
static Array * arrayWithArray(const Array *array)
Definition: Array.c:133
static MutableArray * array(void)
Definition: MutableArray.c:153

◆ 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 346 of file URLSession.c.

346 {
347
348 return taskWithRequest(self, alloc(URLSessionUploadTask), request, completion);
349}
Use upload tasks to send files directly from disk.

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