mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
Refine code, use SrsConnectionManager
This commit is contained in:
parent
33f1cb87a2
commit
66c680edb1
8 changed files with 154 additions and 110 deletions
|
@ -216,6 +216,7 @@ SrsRtcServer::SrsRtcServer()
|
|||
{
|
||||
handler = NULL;
|
||||
hijacker = NULL;
|
||||
manager = new SrsConnectionManager();
|
||||
timer = new SrsHourGlass(this, 1 * SRS_UTIME_SECONDS);
|
||||
}
|
||||
|
||||
|
@ -231,20 +232,14 @@ SrsRtcServer::~SrsRtcServer()
|
|||
}
|
||||
}
|
||||
|
||||
if (true) {
|
||||
std::vector<SrsRtcConnection*>::iterator it;
|
||||
for (it = zombies_.begin(); it != zombies_.end(); ++it) {
|
||||
SrsRtcConnection* session = *it;
|
||||
srs_freep(session);
|
||||
}
|
||||
}
|
||||
srs_freep(manager);
|
||||
}
|
||||
|
||||
srs_error_t SrsRtcServer::initialize()
|
||||
{
|
||||
srs_error_t err = srs_success;
|
||||
|
||||
if ((err = timer->tick(1 * SRS_UTIME_SECONDS)) != srs_success) {
|
||||
if ((err = timer->tick(3 * SRS_UTIME_SECONDS)) != srs_success) {
|
||||
return srs_error_wrap(err, "hourglass tick");
|
||||
}
|
||||
|
||||
|
@ -256,6 +251,10 @@ srs_error_t SrsRtcServer::initialize()
|
|||
return srs_error_wrap(err, "black hole");
|
||||
}
|
||||
|
||||
if ((err = manager->start()) != srs_success) {
|
||||
return srs_error_wrap(err, "start manager");
|
||||
}
|
||||
|
||||
srs_trace("RTC server init ok");
|
||||
|
||||
return err;
|
||||
|
@ -312,14 +311,11 @@ srs_error_t SrsRtcServer::on_udp_packet(SrsUdpMuxSocket* skt)
|
|||
|
||||
SrsRtcConnection* session = NULL;
|
||||
if (true) {
|
||||
map<string, SrsRtcConnection*>::iterator it = map_id_session.find(peer_id);
|
||||
if (it != map_id_session.end()) {
|
||||
session = it->second;
|
||||
|
||||
ISrsConnection* conn = manager->find_by_id(peer_id);
|
||||
if (conn) {
|
||||
// Switch to the session to write logs to the context.
|
||||
if (session) {
|
||||
session->switch_to_context();
|
||||
}
|
||||
session = dynamic_cast<SrsRtcConnection*>(conn);
|
||||
session->switch_to_context();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -462,7 +458,7 @@ srs_error_t SrsRtcServer::do_create_session(
|
|||
local_ufrag = srs_random_str(8);
|
||||
|
||||
username = local_ufrag + ":" + remote_sdp.get_ice_ufrag();
|
||||
if (!map_username_session.count(username)) {
|
||||
if (!manager->find_by_name(username)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -508,7 +504,7 @@ srs_error_t SrsRtcServer::do_create_session(
|
|||
}
|
||||
|
||||
// We allows username is optional, but it never empty here.
|
||||
map_username_session.insert(make_pair(username, session));
|
||||
manager->add_with_name(username, session);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
@ -571,7 +567,7 @@ srs_error_t SrsRtcServer::setup_session2(SrsRtcConnection* session, SrsRequest*
|
|||
}
|
||||
|
||||
// We allows username is optional, but it never empty here.
|
||||
map_username_session.insert(make_pair(username, session));
|
||||
manager->add_with_name(username, session);
|
||||
|
||||
session->set_remote_sdp(remote_sdp);
|
||||
session->set_state(WAITING_STUN);
|
||||
|
@ -586,60 +582,38 @@ void SrsRtcServer::destroy(SrsRtcConnection* session)
|
|||
}
|
||||
session->disposing_ = true;
|
||||
|
||||
std::map<std::string, SrsRtcConnection*>::iterator it;
|
||||
|
||||
// We allows username is optional.
|
||||
string username = session->username();
|
||||
if (!username.empty() && (it = map_username_session.find(username)) != map_username_session.end()) {
|
||||
map_username_session.erase(it);
|
||||
}
|
||||
|
||||
vector<SrsUdpMuxSocket*> addresses = session->peer_addresses();
|
||||
for (int i = 0; i < (int)addresses.size(); i++) {
|
||||
SrsUdpMuxSocket* addr = addresses.at(i);
|
||||
map_id_session.erase(addr->peer_id());
|
||||
}
|
||||
|
||||
SrsContextRestore(_srs_context->get_id());
|
||||
session->switch_to_context();
|
||||
|
||||
string username = session->username();
|
||||
srs_trace("RTC: session destroy, username=%s, summary: %s", username.c_str(), session->stat_->summary().c_str());
|
||||
|
||||
zombies_.push_back(session);
|
||||
manager->remove(session);
|
||||
}
|
||||
|
||||
bool SrsRtcServer::insert_into_id_sessions(const string& peer_id, SrsRtcConnection* session)
|
||||
void SrsRtcServer::insert_into_id_sessions(const string& peer_id, SrsRtcConnection* session)
|
||||
{
|
||||
return map_id_session.insert(make_pair(peer_id, session)).second;
|
||||
manager->add_with_id(peer_id, session);
|
||||
}
|
||||
|
||||
void SrsRtcServer::check_and_clean_timeout_session()
|
||||
{
|
||||
map<string, SrsRtcConnection*>::iterator iter = map_username_session.begin();
|
||||
while (iter != map_username_session.end()) {
|
||||
SrsRtcConnection* session = iter->second;
|
||||
for (int i = 0; i < (int)manager->size(); i++) {
|
||||
SrsRtcConnection* session = dynamic_cast<SrsRtcConnection*>(manager->at(i));
|
||||
srs_assert(session);
|
||||
|
||||
if (!session->is_stun_timeout()) {
|
||||
++iter;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Now, we got the RTC session to cleanup, switch to its context
|
||||
// to make all logs write to the "correct" pid+cid.
|
||||
session->switch_to_context();
|
||||
srs_trace("RTC: session STUN timeout, summary: %s", session->stat_->summary().c_str());
|
||||
string username = session->username();
|
||||
srs_trace("RTC: session STUN timeout, username=%s, summary: %s", username.c_str(), session->stat_->summary().c_str());
|
||||
|
||||
session->disposing_ = true;
|
||||
zombies_.push_back(session);
|
||||
|
||||
// Use C++98 style: https://stackoverflow.com/a/4636230
|
||||
map_username_session.erase(iter++);
|
||||
|
||||
vector<SrsUdpMuxSocket*> addresses = session->peer_addresses();
|
||||
for (int i = 0; i < (int)addresses.size(); i++) {
|
||||
SrsUdpMuxSocket* addr = addresses.at(i);
|
||||
map_id_session.erase(addr->peer_id());
|
||||
}
|
||||
manager->remove(session);
|
||||
|
||||
if (handler) {
|
||||
handler->on_timeout(session);
|
||||
|
@ -649,35 +623,18 @@ void SrsRtcServer::check_and_clean_timeout_session()
|
|||
|
||||
SrsRtcConnection* SrsRtcServer::find_session_by_username(const std::string& username)
|
||||
{
|
||||
map<string, SrsRtcConnection*>::iterator iter = map_username_session.find(username);
|
||||
if (iter == map_username_session.end()) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return iter->second;
|
||||
ISrsConnection* conn = manager->find_by_name(username);
|
||||
return dynamic_cast<SrsRtcConnection*>(conn);
|
||||
}
|
||||
|
||||
srs_error_t SrsRtcServer::notify(int type, srs_utime_t interval, srs_utime_t tick)
|
||||
{
|
||||
srs_error_t err = srs_success;
|
||||
|
||||
// TODO: FIXME: Merge small functions.
|
||||
// Check session timeout, put to zombies queue.
|
||||
check_and_clean_timeout_session();
|
||||
|
||||
// Cleanup zombie sessions.
|
||||
if (zombies_.empty()) {
|
||||
return err;
|
||||
}
|
||||
|
||||
std::vector<SrsRtcConnection*> zombies;
|
||||
zombies.swap(zombies_);
|
||||
|
||||
std::vector<SrsRtcConnection*>::iterator it;
|
||||
for (it = zombies.begin(); it != zombies.end(); ++it) {
|
||||
SrsRtcConnection* session = *it;
|
||||
srs_freep(session);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue