30#define _Class _URLSession
43 $(this->locals.thread,
join, NULL);
45 release(this->locals.condition);
53#pragma mark - URLSession
161 self->locals.
handle = curl_multi_init();
162 assert(self->locals.
handle);
186 if (task->locals.
handle) {
187 const CURLcode err = curl_easy_pause(task->locals.
handle, CURLPAUSE_ALL);
188 assert(err == CURLE_OK);
195 if (task->locals.
handle == NULL) {
198 assert(task->locals.
handle);
200 const CURLMcode merr = curl_multi_add_handle(self->locals.
handle, task->locals.
handle);
201 assert(merr == CURLM_OK);
203 const CURLcode err = curl_easy_pause(task->locals.
handle, CURLPAUSE_CONT);
204 assert(err == CURLE_OK);
211 if (task->locals.
handle) {
212 const CURLMcode merr = curl_multi_remove_handle(self->locals.
handle, task->locals.
handle);
213 assert(merr == CURLM_OK);
235 CURLMcode merr = curl_multi_wait(self->locals.
handle, NULL, 0, 0, NULL);
236 assert(merr == CURLM_OK);
238 merr = curl_multi_perform(self->locals.
handle, &ret);
239 assert(merr == CURLM_OK);
242 while ((message = curl_multi_info_read(self->locals.
handle, &ret))) {
248 if (t->locals.
handle == message->easy_handle) {
256 if (message->msg == CURLMSG_DONE) {
258 merr = curl_multi_remove_handle(self->locals.
handle, task->locals.
handle);
259 assert(merr == CURLM_OK);
269 if (task->completion) {
270 task->completion(task, message->data.result == CURLE_OK);
284 curl_multi_cleanup(self->locals.
handle);
295 assert(configuration);
376#pragma mark - Class lifecycle
385 curl_global_cleanup();
406 const CURLcode code = curl_global_init(CURL_GLOBAL_ALL);
407 assert(code == CURLE_OK);
420 .name =
"URLSession",
423 .interfaceOffset = offsetof(
URLSession, interface),
424 .interfaceSize =
sizeof(URLSessionInterface),
static ident objectAtIndex(const Array *self, size_t index)
static void removeObject(Array *self, const ident obj)
static Array * arrayWithArray(const Array *array)
static void addObject(Array *self, const ident obj)
static Array * array(void)
static void teardown(void)
Called atexit to teardown Objectively.
static void setup(void)
Called when initializing Object to setup Objectively.
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Class * _initialize(const ClassDef *def)
Initializes the given Class.
ident retain(ident obj)
Atomically increment the given Object's reference count.
#define alloc(type)
Allocate and initialize and instance of type.
#define super(type, obj, method,...)
static bool isKindOfClass(const Object *self, const Class *clazz)
static Operation * initWithFunction(Operation *self, OperationFunction function, ident data)
static void start(Operation *self)
static void cancel(Operation *self)
static int request(RESTClient *self, HTTPMethod method, const char *url_string, const Data *body, Data **out_data)
static void join(Thread *self, ident *status)
void * ident
The identity type, similar to Objective-C id.
static URLCachedResponse * cachedResponseForRequest(URLCache *self, const URLRequest *request)
static URLRequest * initWithURL(URLRequest *self, URL *url)
static URLSessionDataTask * dataTaskWithRequest(URLSession *self, URLRequest *request, URLSessionTaskCompletion completion)
static void destroy(Class *clazz)
static URLSessionUploadTask * uploadTaskWithRequest(URLSession *self, URLRequest *request, URLSessionTaskCompletion completion)
static URLSession * initWithConfiguration(URLSession *self, URLSessionConfiguration *configuration)
Class * _URLSession(void)
static URLSessionDownloadTask * downloadTaskWithURL(URLSession *self, URL *url, URLSessionTaskCompletion completion)
static Array * tasks(const URLSession *self)
static URLSession * init(URLSession *self)
static ident taskWithRequest(URLSession *self, ident task, URLRequest *request, URLSessionTaskCompletion completion)
URLSessionTask factory function.
static URLSession * sharedInstance(void)
static ident run(Thread *thread)
ThreadFunction for a URLSession.
static void dealloc(Object *self)
static URLSessionDataTask * dataTaskWithURL(URLSession *self, URL *url, URLSessionTaskCompletion completion)
static URLSession * _sharedInstance
static void invalidateAndCancel(URLSession *self)
static void initialize(Class *clazz)
static URLSessionDownloadTask * downloadTaskWithRequest(URLSession *self, URLRequest *request, URLSessionTaskCompletion completion)
A management context for loading resources via URLs.
Class * _URLSessionDataTask(void)
static void cacheResponse(URLSessionDataTask *self)
static URLSessionTask * initWithRequestInSession(URLSessionTask *self, struct URLRequest *request, struct URLSession *session, URLSessionTaskCompletion completion)
void(* URLSessionTaskCompletion)(URLSessionTask *task, bool success)
A function pointer for URLSessionTask completion.
@ URLSESSIONTASK_RESUMING
@ URLSESSIONTASK_CANCELED
@ URLSESSIONTASK_SUSPENDING
@ URLSESSIONTASK_SUSPENDED
@ URLSESSIONTASK_COMPLETED
@ URLSESSIONTASK_CANCELING
#define do_once(once, block)
Executes the given block at most one time.
size_t count
The count of elements.
ClassDefs are passed to _initialize via an archetype to initialize a Class.
The runtime representation of a Class.
ident interface
The interface of the Class.
POSIX Threads conditional variables.
Object is the root Class of The Objectively Class hierarchy.
Object * init(Object *self)
Initializes this Object.
bool isCancelled
true when this Thread has been cancelled, false otherwise.
A cache for HTTP responses.
Uniform Resource Locators (RFC 3986).
A protocol-agnostic abstraction for requesting resources via URLs.
int httpStatusCode
The HTTP response status code.
Configuration bundle for URLSession.
URLCache * urlCache
The cache for URL responses, or NULL to disable caching.
Use data tasks to send and receive Data in-memory.
URLCachedResponse * cachedResponse
A cached response, if this task was fulfilled from URLCache.
Use download tasks to save remote resources to file.
A management context for loading resources via URLs.
Array * tasks
The URLSessionTasks.
Condition * condition
The condition, and the Lock guarding access to tasks.
URLSessionConfiguration * configuration
The session configuration.
Thread * thread
The backing Thread.
void invalidateAndCancel(URLSession *)
Invalidates this URLSession and cancels all pending tasks.
URLSession * initWithConfiguration(URLSession *, URLSessionConfiguration *)
Initializes this URLSession with the given configuration.
ident handle
The libcurl handle.
URL session tasks are handles to pending URL operations.
struct URLResponse * response
The response.
ident handle
The backing libcurl handle.
URLSessionTaskState state
The state.
URLSessionTaskCompletion completion
The completion function.
Use upload tasks to send files directly from disk.