1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

RTC: Refine resouce management

This commit is contained in:
winlin 2020-09-19 10:30:05 +08:00
parent 1173d35e33
commit 64705d1cc8
29 changed files with 364 additions and 177 deletions

View file

@ -37,24 +37,43 @@
class SrsWallClock;
// The connection manager remove connection and delete it asynchronously.
class SrsConnectionManager : virtual public ISrsCoroutineHandler, virtual public IConnectionManager
// Hooks for connection manager, to handle the event when disposing connections.
class ISrsDisposingHandler
{
public:
ISrsDisposingHandler();
virtual ~ISrsDisposingHandler();
public:
// When before disposing resource, trigger when manager.remove(c), sync API.
virtual void on_before_dispose(ISrsResource* c) = 0;
// When disposing resource, async API, c is freed after it.
virtual void on_disposing(ISrsResource* c) = 0;
};
// The resource manager remove resource and delete it asynchronously.
class SrsResourceManager : virtual public ISrsCoroutineHandler, virtual public ISrsResourceManager
{
private:
std::string label_;
SrsContextId cid_;
bool verbose_;
private:
SrsCoroutine* trd;
srs_cond_t cond;
// Callback handlers.
std::vector<ISrsDisposingHandler*> handlers_;
// The zombie connections, we will delete it asynchronously.
std::vector<ISrsConnection*> zombies_;
std::vector<ISrsResource*> zombies_;
private:
// The connections without any id.
std::vector<ISrsConnection*> conns_;
// The connections with connection id.
std::map<std::string, ISrsConnection*> conns_id_;
// The connections with connection name.
std::map<std::string, ISrsConnection*> conns_name_;
std::vector<ISrsResource*> conns_;
// The connections with resource id.
std::map<std::string, ISrsResource*> conns_id_;
// The connections with resource name.
std::map<std::string, ISrsResource*> conns_name_;
public:
SrsConnectionManager();
virtual ~SrsConnectionManager();
SrsResourceManager(const std::string& label, bool verbose = false);
virtual ~SrsResourceManager();
public:
srs_error_t start();
bool empty();
@ -63,18 +82,21 @@ public:
public:
virtual srs_error_t cycle();
public:
void add(ISrsConnection* conn);
void add_with_id(const std::string& id, ISrsConnection* conn);
void add_with_name(const std::string& name, ISrsConnection* conn);
ISrsConnection* at(int index);
ISrsConnection* find_by_id(std::string id);
ISrsConnection* find_by_name(std::string name);
// Interface IConnectionManager
void add(ISrsResource* conn);
void add_with_id(const std::string& id, ISrsResource* conn);
void add_with_name(const std::string& name, ISrsResource* conn);
ISrsResource* at(int index);
ISrsResource* find_by_id(std::string id);
ISrsResource* find_by_name(std::string name);
public:
virtual void remove(ISrsConnection* c);
void subscribe(ISrsDisposingHandler* h);
void unsubscribe(ISrsDisposingHandler* h);
// Interface ISrsResourceManager
public:
virtual void remove(ISrsResource* c);
private:
void clear();
void dispose(ISrsConnection* c);
void dispose(ISrsResource* c);
};
// The basic connection of SRS, for TCP based protocols,
@ -88,7 +110,7 @@ protected:
// when thread stop, the connection will be delete by server.
SrsCoroutine* trd;
// The manager object to manage the connection.
IConnectionManager* manager;
ISrsResourceManager* manager;
// The underlayer st fd handler.
srs_netfd_t stfd;
// The ip and port of client.
@ -106,7 +128,7 @@ protected:
// for current connection to log self create time and calculate the living time.
int64_t create_time;
public:
SrsTcpConnection(IConnectionManager* cm, srs_netfd_t c, std::string cip, int cport);
SrsTcpConnection(ISrsResourceManager* cm, srs_netfd_t c, std::string cip, int cport);
virtual ~SrsTcpConnection();
// Interface ISrsKbpsDelta
public:
@ -136,8 +158,11 @@ public:
// Get the srs id which identify the client.
// TODO: FIXME: Rename to cid.
virtual SrsContextId srs_id();
// Get the remote ip of peer.
// Interface ISrsConnection.
public:
virtual std::string remote_ip();
virtual const SrsContextId& get_id();
public:
// Set connection to expired.
virtual void expire();
protected: