-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappleNetwork.cpp
More file actions
359 lines (326 loc) · 13.7 KB
/
appleNetwork.cpp
File metadata and controls
359 lines (326 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#if defined(WITH_APPLE_NETWORK)
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <cstring>
#include <thread>
#include <condition_variable>
#include <mutex>
// Objective-C includes
#include <Network/Network.h>
#include <Foundation/Foundation.h>
#include "utility.hpp"
#include "propsync/propsyncTypes.h"
#include "propsync/cpp/propsyncError.hpp"
#include "propsync/cpp/serializerFactory.hpp"
#include "httpSynchronizer.hpp"
using namespace std::chrono_literals;
// This implementation uses some Objectiv-C to interop with Apple's Network framework API.
// Of note are "blocks" which you can read more about Objective-C blocks here:
// https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithBlocks/WorkingwithBlocks.html
// TODO: May need to enable '-fblocks', see https://stackoverflow.com/questions/52683798/what-does-typedef-void-something-mean
namespace ps
{
struct httpSynchronizer::PIMPL
{
std::string _url;
std::string _initDataString;
std::string _filename;
size_t _bytesRead = 0;
size_t _bytesWritten = 0;
std::optional< std::reference_wrapper< class propProvider > > _propProvider;
std::unique_ptr< serializer_t > _serializer;
bool _domParsing = true;
std::vector< unsigned char > _buffer;
volatile bool _isOpening = false;
volatile bool _isOpen = false;
std::unique_ptr< property > _config;
// Objective-C objects
NSURLSession * session = nullptr;
// nw_connection_t _connection = nullptr;
// static constexpr int MIN_LENGTH = 1024;
// static constexpr int MAX_LENGTH = UINT32_MAX;
// using receiveDelegate = std::function<void(bool)>;
// void receive( receiveDelegate completionCallback );
};
httpSynchronizer::httpSynchronizer()
: _impl( new PIMPL() )
{
_impl->_serializer = ps::createSerializer( this->defaultSerializerType() );
}
httpSynchronizer::~httpSynchronizer()
{
close();
}
const std::string & httpSynchronizer::url() const
{
return _impl->_url;
}
void httpSynchronizer::url( std::string value )
{
_impl->_url = value;
}
const serializer_t & httpSynchronizer::serializer() const
{
return *_impl->_serializer;
}
void httpSynchronizer::serializer( std::unique_ptr< serializer_t > serializer )
{
_impl->_serializer = std::move(serializer);
}
void httpSynchronizer::initData( const std::string & initData )
{
_impl->_initDataString = std::ref(initData);
}
void httpSynchronizer::initData( std::istream & initData )
{
// GUARD
if ( initData.bad() )
throw propsyncError( API_TYPE_NAME(INVALID_SOURCE), "Bad istream" );
// Read everything now into our member string
_impl->_initDataString = std::string(std::istreambuf_iterator<char>(initData), {});
}
void httpSynchronizer::initData( const uint8_t * initData, size_t len )
{
this->initData( std::string(reinterpret_cast<const char *>(initData), len) );
}
void httpSynchronizer::domParsing( bool v )
{
_impl->_domParsing = v;
}
synchronizer & httpSynchronizer::open( std::unique_ptr< property > config )
{
// GUARD
if ( isOpening() )
return *this;
if (!_impl->_propProvider)
throw propsyncError("Synchronizer requires a property provider before calling open()");
// Let's go
_impl->_isOpen = false;
_impl->_isOpening = true;
// Nice cleanup
bool failed = true;
nw_endpoint_t endpoint = nullptr;
nw_parameters_t params = nullptr;
RAII raii( [this, &failed, &endpoint, ¶ms] {
_impl->_isOpening = false;
if ( !failed ) _impl->_isOpen = true;
if ( endpoint ) nw_release(endpoint);
if ( params ) nw_release(params);
});
// Use the default config if not provided
if ( !config )
config = templateConfig();
else
config->upsert( templateConfig(), ps::UPSERT_POLICY::MERGE_KEEP_EXISTING );
_impl->_config = std::move(config);
// Use these for emulating DOM parsing
simplePropProvider tempProps;
std::vector< property * > parseOrder;
std::mutex m;
std::unique_lock<std::mutex> ul(m);
std::condition_variable syncLock;
std::unique_ptr<propsyncError> error = nullptr;
auto continueWithError = [&syncLock, &error](std::string errorStr = "") {
if ( errorStr != "" )
error = std::make_unique<propsyncError>( errorStr );
syncLock.notify_one();
};
// Create or reuse our session object
if ( _impl->session == nullptr )
{
_impl->session = [NSURLSession sharedSession];
}
NSString *urlString = [NSString stringWithCString: _impl->_url.c_str() encoding:[NSString defaultCStringEncoding]];
NSURLSessionDataTask *dataTask = [_impl->session dataTaskWithURL: [NSURL URLWithString: urlString] completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) {
// Guard
if ( error ) {
continueWithError( std::string(error.localizedDescription.UTF8String) );
return;
}
// Guard
auto httpResponse = (NSHTTPURLResponse *)response;
if ( httpResponse.statusCode != 200 ) {
continueWithError( std::string([NSHTTPURLResponse localizedStringForStatusCode: httpResponse.statusCode ].UTF8String) );
return;
}
// All good, copy the data
const size_t dataSize = data.length;
const uint8_t * bytes = static_cast<const uint8_t *>(data.bytes);
std::copy( bytes, bytes + dataSize, std::back_inserter(this->_impl->_buffer) );
continueWithError();
}];
[dataTask resume];
syncLock.wait( ul );
if ( error ) throw *error;
// TODO: Keeping this here for
// -----------------------------------------------
//
// // Build and set our custom header
// struct curl_slist * chunk = nullptr;
// chunk = curl_slist_append( chunk, _impl->_config->at("header").valueAsString().c_str() );
// curl_easy_setopt( _impl->_curlHandle, CURLOPT_HTTPHEADER, chunk );
// // Build the request
// std::stringstream request;
// request << utility::str_toupper(_impl->_config->at("method").valueAsString()) << " " << _impl->_url.c_str() << " HTTP/1.1";
// if ( utility::str_toupper(_impl->_config->at("method").valueAsString()) == "POST" )
// {
//// if ( _impl->_initDataString.size() )
//// curl_easy_setopt( _impl->_curlHandle, CURLOPT_POSTFIELDS, _impl->_initDataString.c_str() );
// }
//
// // Create an endpoint using the URL, then create the connection
// endpoint = nw_endpoint_create_url( _impl->_url.c_str() );
// params = nw_parameters_create_secure_tcp(NW_PARAMETERS_DEFAULT_CONFIGURATION,
// NW_PARAMETERS_DEFAULT_CONFIGURATION );
// _impl->_connection = nw_connection_create( endpoint, params );
// if ( !_impl->_connection )
// {
// std::stringstream ss;
// ss << "TODO: PUT ERROR HERE. Docs say 'Fails due to invalid parameters'";
// throw propsyncError(ss.str());
// }
//
// // Set a worker queue
// //auto queue = dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0);
// auto queue = dispatch_queue_create("HTTP Download", DISPATCH_QUEUE_SERIAL);
// if ( !queue )
// throw propsyncError("Could not access global dispatch queue");
// nw_connection_set_queue( _impl->_connection, queue );
//
// // Monitor connection state changes, update our synchronization variable and error
// std::mutex m;
// std::unique_lock<std::mutex> ul(m);
// std::condition_variable syncLock;
// std::unique_ptr<propsyncError> error = nullptr;
// auto continueWithError = [&syncLock, &error](std::string errorStr = "") {
// if ( errorStr != "" )
// error = std::make_unique<propsyncError>( errorStr );
// syncLock.notify_one();
// };
// nw_connection_set_state_changed_handler(_impl->_connection, ^(nw_connection_state_t state, nw_error_t e) {
// switch (state) {
// case nw_connection_state_waiting:
// std::cout << "waiting" << std::endl; break;
// case nw_connection_state_failed: continueWithError("Connection failed"); break;
// case nw_connection_state_ready: continueWithError(); break;
// case nw_connection_state_cancelled:
// std::cout << "connection is cancelled" << std::endl; break;
// default: break;
// }
// });
//
// // Start the connection
// nw_connection_start( _impl->_connection );
// syncLock.wait( ul );
// if ( error ) throw *error;
//
// // Be ready to receive before sending the HTTP request
// _impl->receive([continueWithError](bool success) {
// continueWithError();
// });
//
// // Send our request
// NSString * nsRequest = [NSString stringWithCString: request.str().c_str() encoding:[NSString defaultCStringEncoding]];
// NSData * rawData = [nsRequest dataUsingEncoding:NSUTF8StringEncoding];
// dispatch_data_t dispatch_data = dispatch_data_create([rawData bytes], [rawData length], dispatch_get_main_queue(), DISPATCH_DATA_DESTRUCTOR_DEFAULT);
// nw_connection_send(_impl->_connection,
// dispatch_data,
// NW_CONNECTION_DEFAULT_MESSAGE_CONTEXT,
// true,
// ^(nw_error_t _Nullable error) {
// if (error != NULL)
// continueWithError("Send HTTP/S request failed");
// // Nothing more to do here, continuation will happen in our receive() completion callback
// }
// );
// auto cvs = syncLock.wait_for( ul, _impl->_config->at("timeout").value<int>() * 1s );
// if ( error ) throw *error;
// else if ( cvs == std::cv_status::timeout ) throw propsyncError( "HTTP timeout" );
//
// // TODO: Move this to close()
// nw_connection_cancel(_impl->_connection);
// -----------------------------------------------
// CURLcode res = curl_easy_perform( _impl->_curlHandle );
// if ( res != CURLE_OK )
// {
// std::stringstream ss;
// ss << "HTTP download failed with cURL error " << res << ": " << curl_easy_strerror( res );
// throw propsyncError(API_TYPE_NAME( BAD_URL ), ss.str());
// }
// Force a NULL terminator
_impl->_buffer.push_back( 0 );
// Convert the data to propsync
if ( _impl->_domParsing )
_impl->_bytesRead = _impl->_serializer->deserialize( _impl->_buffer.data(), _impl->_buffer.size(), tempProps, UPSERT_POLICY::KEEP_NEW, parseOrder );
else
_impl->_bytesRead = _impl->_serializer->deserialize( _impl->_buffer.data(), _impl->_buffer.size(), _impl->_propProvider->get() );
// Handle the delayed behavior of emulated DOM parsing, even though we always use SAX parsing internally
if ( _impl->_domParsing )
{
// Move the now-completed temp props to the real prop provider.
// We are not replacing the original root, just modifying it.
// TODO: For legacy support, trigger the rootChanging and rootChanged callbacks. This may be deprecated in the future
auto eventId = std::hash<property>{}(_impl->_propProvider->get().root());
_impl->_propProvider->get().rootChanging.invoke( rootChangeEventModel( eventId, &_impl->_propProvider->get(), &_impl->_propProvider->get(), std::ref(_impl->_propProvider->get().root())) );
if ( _impl->_propProvider->get().root().key() != tempProps.root().key() )
_impl->_propProvider->get().root().key( tempProps.root().key() );
_impl->_propProvider->get().rootChanged.invoke( rootChangeEventModel( eventId, &_impl->_propProvider->get(), &_impl->_propProvider->get(), std::ref(_impl->_propProvider->get().root())) );
_impl->_propProvider->get().root().upsert( tempProps.extractRoot(), UPSERT_POLICY::MERGE_KEEP_NEW, false );
_impl->_propProvider->get().root().triggerAllEvents( parseOrder );
}
failed = false;
synced.invoke();
return *this;
}
void httpSynchronizer::close()
{
// Supposedly ARC will do the right thing here in terms of releasing
_impl->session = nullptr;
}
bool httpSynchronizer::isOpen() const
{
return _impl->_isOpen;
}
bool httpSynchronizer::isOpening() const
{
return _impl->_isOpening;
}
void httpSynchronizer::propProvider( std::optional< std::reference_wrapper< class propProvider > > propProvider )
{
_impl->_propProvider = propProvider;
}
size_t httpSynchronizer::numConnections() const
{
return 0;
}
size_t httpSynchronizer::numBytesRead() const
{
return _impl->_bytesRead;
}
size_t httpSynchronizer::numBytesWritten() const
{
return _impl->_bytesWritten;
}
// void httpSynchronizer::PIMPL::receive( receiveDelegate completionCallback )
// {
// // Recursively receive the data asynchronously, stopping only when the data is complete.
// // Once complete, make the completion callback.
// nw_connection_receive( _connection,
// MIN_LENGTH,
// MAX_LENGTH,
// ^(_Nullable dispatch_data_t content, _Nullable nw_content_context_t context, bool is_complete, _Nullable nw_error_t error) {
// const size_t dataSize = ((NSData *)content).length;
// const uint8_t * bytes = static_cast<const uint8_t *>(((NSData *)content).bytes);
// std::copy( bytes, bytes + dataSize, std::back_inserter(this->_buffer) );
//
// if ( is_complete )
// completionCallback(is_complete);
// else
// this->receive( completionCallback );
// }
// );
// }
}
#endif