9#include <zypp/APIConfig.h>
14#include <zypp-core/zyppng/base/Timer>
15#include <zypp-core/zyppng/base/SocketNotifier>
16#include <zypp-core/zyppng/base/EventDispatcher>
22#include <zypp-core/base/DtorReset>
37 static const std::string
_value(
49 , _timer(
Timer::create() )
64 _timer->setSingleShot(
true );
80 if ( timeout_ms >= 0 ) {
81 that->_timer->start(
static_cast<uint64_t>(timeout_ms) );
103 std::shared_ptr<SocketNotifier>
socketp;
137 WAR <<
"Cleaning up unassigned easy handle" << std::endl;
214 WAR <<
"Unable to get CURLINFO_PRIVATE" << std::endl;
220 WAR <<
"Cleaning up unassigned easy handle" << std::endl;
230 std::string
errBuf =
"Broken easy handle in request";
240 errBuf =
"Failed to reinitialize the request";
280 auto it = std::find_if( list.begin(), list.end(), [ &req ](
const std::shared_ptr<NetworkRequest> &
r ) {
281 return req.d_func() == r->d_func();
283 if (
it != list.end() ) {
294 auto rmode = std::get_if<NetworkRequestPrivate::running_t>( &req.d_func()->_runningMode );
296 if (
rmode->_isInCallback ) {
298 if ( !
rmode->_cachedResult )
299 rmode->_cachedResult = result;
301 }
else if (
rmode->_cachedResult ) {
302 result =
rmode->_cachedResult.value();
310 void *easyHandle = req.d_func()->_easyHandle;
314 req.d_func()->_dispatcher =
nullptr;
318 req.d_func()->setResult( std::move(result) );
347 std::string
errBuf =
"Failed to initialize easy handle";
348 if ( !req->d_func()->initialize(
errBuf ) ) {
357 req->d_func()->aboutToStart();
373NetworkRequestDispatcher::NetworkRequestDispatcher( )
379bool NetworkRequestDispatcher::supportsProtocol(
const Url &url )
381 curl_version_info_data *curl_info =
nullptr;
382 curl_info = curl_version_info(CURLVERSION_NOW);
384 if (curl_info->protocols)
386 const char *
const *proto =
nullptr;
387 std::string scheme( url.getScheme() );
389 for(proto=curl_info->protocols; !found && *proto; ++proto) {
390 if( scheme == std::string((
const char *)*proto))
398void NetworkRequestDispatcher::setMaximumConcurrentConnections(
const int maxConn )
400 d_func()->_maxConnections = maxConn;
403int NetworkRequestDispatcher::maximumConcurrentConnections ()
const
405 return d_func()->_maxConnections;
408void NetworkRequestDispatcher::enqueue(
const std::shared_ptr<NetworkRequest> &req )
414 if ( std::find( d->_runningDownloads.begin(), d->_runningDownloads.end(), req ) != d->_runningDownloads.end() ) {
415 WAR <<
"Ignoring request to enqueue download " << req->url().asString() <<
" request is already running " << std::endl;
419 if ( std::find( d->_pendingDownloads.begin(), d->_pendingDownloads.end(), req ) != d->_pendingDownloads.end() ) {
420 WAR <<
"Ignoring request to enqueue download " << req->url().asString() <<
" request is already enqueued " << std::endl;
424 req->d_func()->_dispatcher =
this;
426 d->_pendingDownloads.push_back( req );
428 auto it = std::find_if( d->_pendingDownloads.begin(), d->_pendingDownloads.end(), [ prio = req->priority() ](
const auto &pendingReq ){
429 return pendingReq->priority() < prio;
433 if ( it != d->_pendingDownloads.end() && it != d->_pendingDownloads.begin() )
435 d->_pendingDownloads.insert( it, req );
442void NetworkRequestDispatcher::setAgentString(
const std::string &agent )
448 d->_userAgent = agent;
451const std::string &NetworkRequestDispatcher::agentString()
const
453 return d_func()->_userAgent;
456void NetworkRequestDispatcher::setHostSpecificHeader(
const std::string &host,
const std::string &headerName,
const std::string &value )
459 if ( value.empty() ) {
460 if (
auto i = d->_customHeaders.find( host ); i != d->_customHeaders.end() ) {
461 if (
auto v = i->second.find( headerName ); v != i->second.end() ) {
464 if ( i->second.empty() )
465 d->_customHeaders.erase(i);
469 d->_customHeaders[host][headerName] = value;
472const NetworkRequestDispatcher::SpecificHeaderMap &NetworkRequestDispatcher::hostSpecificHeaders()
const
474 return d_func()->_customHeaders;
477void NetworkRequestDispatcher::cancel( NetworkRequest &req, std::string reason )
482void NetworkRequestDispatcher::cancel(NetworkRequest &req,
const NetworkRequestError &err)
486 if ( req.d_func()->_dispatcher !=
this ) {
491 d->setFinished( req, err );
494void NetworkRequestDispatcher::run()
497 d->_isRunning =
true;
499 if ( d->_pendingDownloads.size() )
503void NetworkRequestDispatcher::reschedule()
506 if ( !d->_pendingDownloads.size() )
509 std::stable_sort( d->_pendingDownloads.begin(), d->_pendingDownloads.end(), [](
const auto &
a,
const auto &
b ){
510 return a->priority() < b->priority();
516size_t NetworkRequestDispatcher::count()
519 return d->_pendingDownloads.size() + d->_runningDownloads.size();
524 return d_func()->_lastError;
527SignalProxy<void (NetworkRequestDispatcher &, NetworkRequest &)> NetworkRequestDispatcher::sigDownloadStarted()
529 return d_func()->_sigDownloadStarted;
532SignalProxy<void (NetworkRequestDispatcher &, NetworkRequest &)> NetworkRequestDispatcher::sigDownloadFinished()
534 return d_func()->_sigDownloadFinished;
537SignalProxy<void ( NetworkRequestDispatcher &)> NetworkRequestDispatcher::sigQueueFinished()
539 return d_func()->_sigQueueFinished;
542SignalProxy<void ( NetworkRequestDispatcher &)> NetworkRequestDispatcher::sigError()
544 return d_func()->_sigError;
Reference counted access to a Tp object calling a custom Dispose function when the last AutoDispose h...
Assign a vaiable a certain value when going out of scope.
NetworkRequestDispatcherPrivate(NetworkRequestDispatcher &p)
static int static_socket_callback(CURL *easy, curl_socket_t s, int what, void *userp, SocketNotifier *socketp)
int socketCallback(CURL *easy, curl_socket_t s, int what, void *)
Signal< void(NetworkRequestDispatcher &, NetworkRequest &) _sigDownloadFinished)
Signal< void(NetworkRequestDispatcher &) _sigQueueFinished)
Signal< void(NetworkRequestDispatcher &) _sigError)
void setFinished(NetworkRequest &req, NetworkRequestError result)
void multiTimerTimout(const Timer &t)
NetworkRequestError _lastError
void handleMultiSocketAction(curl_socket_t nativeSocket, int evBitmask)
Signal< void(NetworkRequestDispatcher &, NetworkRequest &) _sigDownloadStarted)
~NetworkRequestDispatcherPrivate() override
std::shared_ptr< Timer > _timer
std::map< curl_socket_t, std::shared_ptr< SocketNotifier > > _socketHandler
bool addRequestToMultiHandle(NetworkRequest &req)
void onSocketActivated(const SocketNotifier &listener, int events)
std::deque< std::shared_ptr< NetworkRequest > > _pendingDownloads
static int multi_timer_cb(CURLM *multi, long timeout_ms, void *g)
void cancelAll(const NetworkRequestError &result)
std::vector< std::shared_ptr< NetworkRequest > > _runningDownloads
static zyppng::NetworkRequestError fromCurlMError(int nativeCode)
static zyppng::NetworkRequestError fromCurlError(NetworkRequest &req, int nativeCode, const std::string &nativeError)
static zyppng::NetworkRequestError customError(NetworkRequestError::Type t, std::string &&errorMsg="", std::map< std::string, boost::any > &&extraInfo={})
The NetworkRequestError class Represents a error that occured in.
SignalProxy< void(const SocketNotifier &sock, int evTypes) sigActivated)()
static Ptr create(int socket, int evTypes, bool enable=true)
The Timer class provides repetitive and single-shot timers.
SignalProxy< void(Timer &t) sigExpired)()
This signal is always emitted when the timer expires.
void globalInitCurlOnce()
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
static const std::string & defaultAgentString()
#define L_ENV_CONSTR_DEFINE_FUNC(ENV)
#define ZYPP_IMPL_PRIVATE(Class)