| 1429 | #endif |
| 1430 | |
| 1431 | Uint64 Http::sendAsyncRequest( const Http::AsyncResponseCallback& cb, const Http::Request& request, |
| 1432 | Time timeout ) { |
| 1433 | Uint64 id = Http::AsyncRequest::IdCounter.fetch_add( 1, std::memory_order_relaxed ); |
| 1434 | #if EE_PLATFORM == EE_PLATFORM_EMSCRIPTEN |
| 1435 | WGetAsyncRequest* wget = new WGetAsyncRequest(); |
| 1436 | wget->http = this; |
| 1437 | wget->cb = cb; |
| 1438 | wget->request = Http::Request( request ); |
| 1439 | emscripten_async_wget2_data( ( getURI().toString() + request.getUri() ).c_str(), |
| 1440 | Request::methodToString( request.getMethod() ).c_str(), |
| 1441 | URI( request.getUri() ).getQuery().c_str(), wget, 1, |
| 1442 | emscripten_async_wget2_got_data, |
| 1443 | emscripten_async_wget2_got_error_data, NULL ); |
| 1444 | return id; |
| 1445 | #else |
| 1446 | if ( sGlobalThreadPool ) { |
| 1447 | sGlobalThreadPool->run( [this, cb, request, timeout, id] { |
| 1448 | AsyncRequest asyncRequest( id, this, cb, request, timeout, false ); |
| 1449 | { |
| 1450 | Lock l( mCurRequestsMutex ); |
| 1451 | mCurRequests[id] = &asyncRequest; |
| 1452 | } |
| 1453 | asyncRequest.run(); |
| 1454 | { |
| 1455 | Lock l( mCurRequestsMutex ); |
| 1456 | mCurRequests.erase( id ); |
| 1457 | } |
| 1458 | } ); |
| 1459 | return id; |
| 1460 | } |
| 1461 | AsyncRequest* thread = eeNew( AsyncRequest, ( id, this, cb, request, timeout, true ) ); |
| 1462 | { |
| 1463 | Lock l( mCurRequestsMutex ); |
| 1464 | mCurRequests[id] = thread; |
| 1465 | } |
| 1466 | thread->launch(); |
| 1467 | { |
| 1468 | Lock l( mThreadsMutex ); |
| 1469 | mThreads.push_back( thread ); |
| 1470 | } |
| 1471 | return id; |
| 1472 | #endif |
| 1473 | } |
| 1474 | |
| 1475 | Uint64 Http::downloadAsyncRequest( const Http::AsyncResponseCallback& cb, |
| 1476 | const Http::Request& request, IOStream& writeTo, Time timeout ) { |