Commit 422d6a48 authored by François Cartegnie's avatar François Cartegnie 🤞

adaptive: move connection to connection factory

parent 61c3a2a6
......@@ -37,7 +37,7 @@ namespace adaptive
{
namespace http
{
class HTTPConnection;
class AbstractConnection;
class HTTPConnectionManager;
class AbstractChunk;
......@@ -92,7 +92,7 @@ namespace adaptive
protected:
virtual bool prepare();
HTTPConnection *connection;
AbstractConnection *connection;
HTTPConnectionManager *connManager;
size_t consumed; /* read pointer */
bool prepared;
......
......@@ -294,3 +294,35 @@ std::string HTTPConnection::extraRequestHeaders() const
}
return ss.str();
}
ConnectionFactory::ConnectionFactory()
{
}
ConnectionFactory::~ConnectionFactory()
{
}
AbstractConnection * ConnectionFactory::createConnection(vlc_object_t *p_object,
const ConnectionParams &params)
{
if((params.getScheme() != "http" && params.getScheme() != "https") || params.getHostname().empty())
return NULL;
const int sockettype = (params.getScheme() == "https") ? TLSSocket::TLS : Socket::REGULAR;
Socket *socket = (sockettype == TLSSocket::TLS) ? new (std::nothrow) TLSSocket()
: new (std::nothrow) Socket();
if(!socket)
return NULL;
/* disable pipelined tls until we have ticket/resume session support */
HTTPConnection *conn = new (std::nothrow)
HTTPConnection(p_object, socket, sockettype != TLSSocket::TLS);
if(!conn)
{
delete socket;
return NULL;
}
return conn;
}
......@@ -102,6 +102,14 @@ namespace adaptive
private:
Socket *socket;
};
class ConnectionFactory
{
public:
ConnectionFactory();
virtual ~ConnectionFactory();
virtual AbstractConnection * createConnection(vlc_object_t *, const ConnectionParams &);
};
}
}
......
......@@ -34,17 +34,22 @@
using namespace adaptive::http;
HTTPConnectionManager::HTTPConnectionManager (vlc_object_t *stream) :
HTTPConnectionManager::HTTPConnectionManager (vlc_object_t *stream, ConnectionFactory *factory_) :
stream (stream),
rateObserver (NULL)
{
vlc_mutex_init(&lock);
downloader = new (std::nothrow) Downloader();
downloader->start();
if(!factory_)
factory = new (std::nothrow) ConnectionFactory();
else
factory = factory_;
}
HTTPConnectionManager::~HTTPConnectionManager ()
{
delete downloader;
delete factory;
this->closeAllConnections();
vlc_mutex_destroy(&lock);
}
......@@ -59,14 +64,14 @@ void HTTPConnectionManager::closeAllConnections ()
void HTTPConnectionManager::releaseAllConnections()
{
std::vector<HTTPConnection *>::iterator it;
std::vector<AbstractConnection *>::iterator it;
for(it = connectionPool.begin(); it != connectionPool.end(); ++it)
(*it)->setUsed(false);
}
HTTPConnection * HTTPConnectionManager::reuseConnection(ConnectionParams &params)
AbstractConnection * HTTPConnectionManager::reuseConnection(ConnectionParams &params)
{
std::vector<HTTPConnection *>::const_iterator it;
std::vector<AbstractConnection *>::const_iterator it;
for(it = connectionPool.begin(); it != connectionPool.end(); ++it)
{
AbstractConnection *conn = *it;
......@@ -76,31 +81,16 @@ HTTPConnection * HTTPConnectionManager::reuseConnection(ConnectionParams &params
return NULL;
}
HTTPConnection * HTTPConnectionManager::getConnection(ConnectionParams &params)
AbstractConnection * HTTPConnectionManager::getConnection(ConnectionParams &params)
{
if((params.getScheme() != "http" && params.getScheme() != "https") || params.getHostname().empty())
if(unlikely(!factory || !downloader))
return NULL;
const int sockettype = (params.getScheme() == "https") ? TLSSocket::TLS : Socket::REGULAR;
vlc_mutex_lock(&lock);
HTTPConnection *conn = reuseConnection(params);
AbstractConnection *conn = reuseConnection(params);
if(!conn)
{
Socket *socket = (sockettype == TLSSocket::TLS) ? new (std::nothrow) TLSSocket()
: new (std::nothrow) Socket();
if(!socket)
{
vlc_mutex_unlock(&lock);
return NULL;
}
/* disable pipelined tls until we have ticket/resume session support */
conn = new (std::nothrow) HTTPConnection(stream, socket, sockettype != TLSSocket::TLS);
if(!conn)
{
delete socket;
vlc_mutex_unlock(&lock);
return NULL;
}
conn = factory->createConnection(stream, params);
connectionPool.push_back(conn);
......
......@@ -40,17 +40,18 @@ namespace adaptive
namespace http
{
class ConnectionParams;
class HTTPConnection;
class ConnectionFactory;
class AbstractConnection;
class Downloader;
class HTTPConnectionManager : public IDownloadRateObserver
{
public:
HTTPConnectionManager (vlc_object_t *stream);
HTTPConnectionManager (vlc_object_t *stream, ConnectionFactory * = NULL);
virtual ~HTTPConnectionManager ();
void closeAllConnections ();
HTTPConnection * getConnection(ConnectionParams &);
AbstractConnection * getConnection(ConnectionParams &);
virtual void updateDownloadRate(size_t, mtime_t); /* reimpl */
void setDownloadRateObserver(IDownloadRateObserver *);
......@@ -59,10 +60,11 @@ namespace adaptive
private:
void releaseAllConnections ();
vlc_mutex_t lock;
std::vector<HTTPConnection *> connectionPool;
std::vector<AbstractConnection *> connectionPool;
vlc_object_t *stream;
IDownloadRateObserver *rateObserver;
HTTPConnection * reuseConnection(ConnectionParams &);
ConnectionFactory *factory;
AbstractConnection * reuseConnection(ConnectionParams &);
};
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment