Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
URLSessionDataTask.c
Go to the documentation of this file.
1/*
2 * Objectively: Ultra-lightweight object oriented framework for GNU C.
3 * Copyright (C) 2014 Jay Dolan <jay@jaydolan.com>
4 *
5 * This software is provided 'as-is', without any express or implied
6 * warranty. In no event will the authors be held liable for any damages
7 * arising from the use of this software.
8 *
9 * Permission is granted to anyone to use this software for any purpose,
10 * including commercial applications, and to alter it and redistribute it
11 * freely, subject to the following restrictions:
12 *
13 * 1. The origin of this software must not be misrepresented; you must not
14 * claim that you wrote the original software. If you use this software
15 * in a product, an acknowledgment in the product documentation would be
16 * appreciated but is not required.
17 *
18 * 2. Altered source versions must be plainly marked as such, and must not be
19 * misrepresented as being the original software.
20 *
21 * 3. This notice may not be removed or altered from any source distribution.
22 */
23
24#include <assert.h>
25#include <stdlib.h>
26#include <string.h>
27#include <curl/curl.h>
28
29#include "MutableData.h"
30#include "URLSessionDataTask.h"
31
32#define _Class _URLSessionDataTask
33
34#pragma mark - Object
35
39static void dealloc(Object *self) {
40
42
43 release(this->data);
44
45 super(Object, self, dealloc);
46}
47
48#pragma mark - URLSessionTask
49
50#define CURL_WRITEFUNC_ABORT 0
51
55static size_t writeFunction(char *data, size_t size, size_t count, ident self) {
56
58
59 const uint8_t *bytes = (uint8_t *) data;
60 const size_t bytesReceived = size * count;
61
62 if (this->data == NULL) {
63 this->data = (Data *) $(alloc(MutableData), init);
64 }
65
66 $((MutableData *) this->data, appendBytes, bytes, bytesReceived);
67
68 this->urlSessionTask.bytesReceived += bytesReceived;
69 return bytesReceived;
70}
71
75static void setup(URLSessionTask *self) {
76
78
79 curl_easy_setopt(self->locals.handle, CURLOPT_WRITEFUNCTION, writeFunction);
80 curl_easy_setopt(self->locals.handle, CURLOPT_WRITEDATA, self);
81}
82
83#pragma mark - Class lifecycle
84
88static void initialize(Class *clazz) {
89
90 ((ObjectInterface *) clazz->interface)->dealloc = dealloc;
91
92 ((URLSessionTaskInterface *) clazz->interface)->setup = setup;
93}
94
100 static Class *clazz;
101 static Once once;
102
103 do_once(&once, {
104 clazz = _initialize(&(const ClassDef) {
105 .name = "URLSessionDataTask",
106 .superclass = _URLSessionTask(),
107 .instanceSize = sizeof(URLSessionDataTask),
108 .interfaceOffset = offsetof(URLSessionDataTask, interface),
109 .interfaceSize = sizeof(URLSessionDataTaskInterface),
111 });
112 });
113
114 return clazz;
115}
116
117#undef _Class
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition: Class.c:196
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition: Class.c:91
#define alloc(type)
Allocate and initialize and instance of type.
Definition: Class.h:159
#define super(type, obj, method,...)
Mutable data buffers.
Immutable UTF-8 strings.
void * ident
The identity type, similar to Objective-C id.
Definition: Types.h:49
static size_t writeFunction(char *data, size_t size, size_t count, ident self)
The CURLOPT_WRITEFUNCTION callback.
static void initialize(Class *clazz)
Use data tasks to send and receive Data in-memory.
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
ident interface
The interface of the Class.
Definition: Class.h:105
Condition * init(Condition *self)
Initializes this Condition.
Definition: Condition.c:67
Immutable data buffers.
Definition: Data.h:50
Mutable data buffers.
Definition: MutableData.h:40
MutableData * data(void)
Returns a new MutableData.
Definition: MutableData.c:75
void appendBytes(MutableData *self, const uint8_t *bytes, size_t length)
Appends the given bytes to this Data.
Definition: MutableData.c:53
Object is the root Class of The Objectively Class hierarchy.
Definition: Object.h:46
void dealloc(Object *self)
Frees all resources held by this Object.
Definition: Array.c:50
Use data tasks to send and receive Data in-memory.
Class * _URLSessionDataTask(void)
The URLSessionDataTask archetype.
URL session tasks are handles to pending URL operations.
ident handle
The backing libcurl handle.
void setup(URLSessionTask *)
Sets up this task.
Class * _URLSessionTask(void)
The URLSessionTask archetype.