Commit 61c3a2a6 authored by François Cartegnie's avatar François Cartegnie 🤞

demux: adaptive: refactor connections

parent 30c32ef9
......@@ -203,9 +203,7 @@ bool HTTPChunkSource::prepare()
if(!connection)
{
connection = connManager->getConnection(params.getScheme(),
params.getHostname(),
params.getPort());
connection = connManager->getConnection(params);
if(!connection)
return false;
}
......
......@@ -19,25 +19,55 @@
*****************************************************************************/
#include "HTTPConnection.hpp"
#include "ConnectionParams.hpp"
#include "Sockets.hpp"
#include "../adaptive/tools/Helper.h"
#include <sstream>
#include <vlc_stream.h>
using namespace adaptive::http;
HTTPConnection::HTTPConnection(vlc_object_t *p_object_, Socket *socket_, bool persistent)
AbstractConnection::AbstractConnection(vlc_object_t *p_object_)
{
socket = socket_;
p_object = p_object_;
psz_useragent = var_InheritString(p_object, "http-user-agent");
available = true;
bytesRead = 0;
contentLength = 0;
}
AbstractConnection::~AbstractConnection()
{
}
bool AbstractConnection::prepare(const ConnectionParams &params_)
{
if (!available)
return false;
params = params_;
available = false;
return true;
}
bool AbstractConnection::isAvailable() const
{
return available;
}
size_t AbstractConnection::getContentLength() const
{
return contentLength;
}
HTTPConnection::HTTPConnection(vlc_object_t *p_object_, Socket *socket_, bool persistent)
: AbstractConnection( p_object_ )
{
socket = socket_;
psz_useragent = var_InheritString(p_object_, "http-user-agent");
queryOk = false;
retries = 0;
connectionClose = !persistent;
port = 80;
available = true;
}
HTTPConnection::~HTTPConnection()
......@@ -46,22 +76,17 @@ HTTPConnection::~HTTPConnection()
delete socket;
}
bool HTTPConnection::compare(const std::string &hostname, uint16_t port, int type) const
bool HTTPConnection::canReuse(const ConnectionParams &params_) const
{
return ( hostname == this->hostname &&
(socket && socket->getType() == type) &&
port == this->port );
return ( params.getHostname() == params_.getHostname() &&
params.getScheme() == params_.getScheme() &&
params.getPort() == params_.getPort() );
}
bool HTTPConnection::connect(const std::string &hostname, uint16_t port)
bool HTTPConnection::connect()
{
if(!socket->connect(p_object, hostname.c_str(), port))
return false;
this->hostname = hostname;
this->port = port;
return true;
return socket->connect(p_object, params.getHostname().c_str(),
params.getPort());
}
bool HTTPConnection::connected() const
......@@ -82,10 +107,13 @@ int HTTPConnection::query(const std::string &path, const BytesRange &range)
{
queryOk = false;
msg_Dbg(p_object, "Retrieving ://%s:%u%s @%zu", hostname.c_str(), port, path.c_str(),
range.isValid() ? range.getStartByte() : 0);
/* Set new path for this query */
params.setPath(path);
if(!connected() && ( hostname.empty() || !connect(hostname, port) ))
msg_Dbg(p_object, "Retrieving %s @%zu", params.getUrl().c_str(),
range.isValid() ? range.getStartByte() : 0);
if(!connected() && ( params.getHostname().empty() || !connect() ))
return VLC_EGENERIC;
bytesRange = range;
......@@ -207,11 +235,6 @@ std::string HTTPConnection::readLine()
return socket->readline(p_object);
}
bool HTTPConnection::isAvailable() const
{
return available;
}
void HTTPConnection::setUsed( bool b )
{
available = !b;
......@@ -229,11 +252,6 @@ void HTTPConnection::setUsed( bool b )
}
}
size_t HTTPConnection::getContentLength() const
{
return contentLength;
}
void HTTPConnection::onHeader(const std::string &key,
const std::string &value)
{
......@@ -255,7 +273,7 @@ std::string HTTPConnection::buildRequestHeader(const std::string &path) const
{
std::stringstream req;
req << "GET " << path << " HTTP/1.1\r\n" <<
"Host: " << hostname << "\r\n" <<
"Host: " << params.getHostname() << "\r\n" <<
"Cache-Control: no-cache" << "\r\n" <<
"Accept-Encoding: " << "\r\n" <<
"User-Agent: " << std::string(psz_useragent) << "\r\n";
......
......@@ -29,6 +29,7 @@
# include "config.h"
#endif
#include "ConnectionParams.hpp"
#include "BytesRange.hpp"
#include <vlc_common.h>
#include <string>
......@@ -39,26 +40,50 @@ namespace adaptive
{
class Socket;
class HTTPConnection
class AbstractConnection
{
public:
AbstractConnection(vlc_object_t *);
virtual ~AbstractConnection();
virtual bool prepare (const ConnectionParams &);
virtual bool canReuse (const ConnectionParams &) const = 0;
virtual bool connect () = 0;
virtual int query (const std::string& path, const BytesRange & = BytesRange()) = 0;
virtual ssize_t read (void *p_buffer, size_t len) = 0;
bool isAvailable () const;
virtual size_t getContentLength() const;
virtual void setUsed( bool ) = 0;
protected:
vlc_object_t *p_object;
ConnectionParams params;
bool available;
size_t contentLength;
BytesRange bytesRange;
size_t bytesRead;
};
class HTTPConnection : public AbstractConnection
{
public:
HTTPConnection(vlc_object_t *stream, Socket *, bool = false);
virtual ~HTTPConnection();
virtual bool compare (const std::string &, uint16_t, int) const;
virtual bool connect (const std::string& hostname, uint16_t port = 80);
virtual bool connected () const;
virtual bool canReuse (const ConnectionParams &) const;
virtual bool connect ();
virtual int query (const std::string& path, const BytesRange & = BytesRange());
virtual bool send (const void *buf, size_t size);
virtual ssize_t read (void *p_buffer, size_t len);
virtual void disconnect ();
virtual bool send (const std::string &data);
size_t getContentLength() const;
bool isAvailable () const;
void setUsed( bool );
protected:
virtual bool connected () const;
virtual void disconnect ();
virtual bool send (const void *buf, size_t size);
virtual bool send (const std::string &data);
virtual void onHeader (const std::string &line,
const std::string &value);
......@@ -67,14 +92,7 @@ namespace adaptive
int parseReply();
std::string readLine();
std::string hostname;
uint16_t port;
char * psz_useragent;
vlc_object_t *p_object;
size_t bytesRead;
size_t contentLength;
BytesRange bytesRange;
bool available;
bool connectionClose;
bool queryOk;
......
......@@ -27,6 +27,7 @@
#include "HTTPConnectionManager.h"
#include "HTTPConnection.hpp"
#include "ConnectionParams.hpp"
#include "Sockets.hpp"
#include "Downloader.hpp"
#include <vlc_url.h>
......@@ -63,28 +64,26 @@ void HTTPConnectionManager::releaseAllConnections()
(*it)->setUsed(false);
}
HTTPConnection * HTTPConnectionManager::getConnection(const std::string &hostname, uint16_t port, int sockettype)
HTTPConnection * HTTPConnectionManager::reuseConnection(ConnectionParams &params)
{
std::vector<HTTPConnection *>::const_iterator it;
for(it = connectionPool.begin(); it != connectionPool.end(); ++it)
{
HTTPConnection *conn = *it;
if(conn->isAvailable() && conn->compare(hostname, port, sockettype))
AbstractConnection *conn = *it;
if(conn->isAvailable() && conn->canReuse(params))
return conn;
}
return NULL;
}
HTTPConnection * HTTPConnectionManager::getConnection(const std::string &scheme,
const std::string &hostname,
uint16_t port)
HTTPConnection * HTTPConnectionManager::getConnection(ConnectionParams &params)
{
if((scheme != "http" && scheme != "https") || hostname.empty())
if((params.getScheme() != "http" && params.getScheme() != "https") || params.getHostname().empty())
return NULL;
const int sockettype = (scheme == "https") ? TLSSocket::TLS : Socket::REGULAR;
const int sockettype = (params.getScheme() == "https") ? TLSSocket::TLS : Socket::REGULAR;
vlc_mutex_lock(&lock);
HTTPConnection *conn = getConnection(hostname, port, sockettype);
HTTPConnection *conn = reuseConnection(params);
if(!conn)
{
Socket *socket = (sockettype == TLSSocket::TLS) ? new (std::nothrow) TLSSocket()
......@@ -105,7 +104,7 @@ HTTPConnection * HTTPConnectionManager::getConnection(const std::string &scheme,
connectionPool.push_back(conn);
if (!conn->connect(hostname, port))
if (!conn->prepare(params) || !conn->connect())
{
vlc_mutex_unlock(&lock);
return NULL;
......
......@@ -39,6 +39,7 @@ namespace adaptive
{
namespace http
{
class ConnectionParams;
class HTTPConnection;
class Downloader;
......@@ -49,9 +50,7 @@ namespace adaptive
virtual ~HTTPConnectionManager ();
void closeAllConnections ();
HTTPConnection * getConnection(const std::string &scheme,
const std::string &hostname,
uint16_t port);
HTTPConnection * getConnection(ConnectionParams &);
virtual void updateDownloadRate(size_t, mtime_t); /* reimpl */
void setDownloadRateObserver(IDownloadRateObserver *);
......@@ -63,7 +62,7 @@ namespace adaptive
std::vector<HTTPConnection *> connectionPool;
vlc_object_t *stream;
IDownloadRateObserver *rateObserver;
HTTPConnection * getConnection(const std::string &hostname, uint16_t port, int);
HTTPConnection * 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