2017-03-25 09:21:39 +00:00
|
|
|
/**
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2019-12-30 02:10:35 +00:00
|
|
|
* Copyright (c) 2013-2020 Winlin
|
2017-03-25 09:21:39 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
* this software and associated documentation files (the "Software"), to deal in
|
|
|
|
* the Software without restriction, including without limitation the rights to
|
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
* subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
2013-11-23 03:36:07 +00:00
|
|
|
|
2014-03-02 13:49:09 +00:00
|
|
|
#include <srs_app_conn.hpp>
|
2013-11-23 03:36:07 +00:00
|
|
|
|
2018-08-05 12:30:04 +00:00
|
|
|
#include <netinet/tcp.h>
|
2020-09-13 03:01:34 +00:00
|
|
|
#include <algorithm>
|
2015-12-24 09:25:05 +00:00
|
|
|
using namespace std;
|
|
|
|
|
2014-03-01 02:42:55 +00:00
|
|
|
#include <srs_kernel_log.hpp>
|
2014-03-01 02:30:16 +00:00
|
|
|
#include <srs_kernel_error.hpp>
|
2014-05-27 08:45:02 +00:00
|
|
|
#include <srs_app_utility.hpp>
|
2015-09-17 05:36:02 +00:00
|
|
|
#include <srs_kernel_utility.hpp>
|
2020-09-19 02:30:05 +00:00
|
|
|
#include <srs_service_log.hpp>
|
2020-10-02 01:13:41 +00:00
|
|
|
#include <srs_app_log.hpp>
|
2013-11-23 03:36:07 +00:00
|
|
|
|
2020-09-19 02:30:05 +00:00
|
|
|
ISrsDisposingHandler::ISrsDisposingHandler()
|
2020-09-11 08:44:40 +00:00
|
|
|
{
|
2020-09-19 02:30:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ISrsDisposingHandler::~ISrsDisposingHandler()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SrsResourceManager::SrsResourceManager(const std::string& label, bool verbose)
|
|
|
|
{
|
|
|
|
verbose_ = verbose;
|
|
|
|
label_ = label;
|
2020-09-11 08:44:40 +00:00
|
|
|
cond = srs_cond_new();
|
2020-09-19 02:30:05 +00:00
|
|
|
trd = NULL;
|
2020-09-20 14:09:03 +00:00
|
|
|
p_disposing_ = NULL;
|
2020-10-02 01:13:41 +00:00
|
|
|
removing_ = false;
|
2020-09-11 08:44:40 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 02:30:05 +00:00
|
|
|
SrsResourceManager::~SrsResourceManager()
|
2020-09-11 08:44:40 +00:00
|
|
|
{
|
2020-09-19 02:30:05 +00:00
|
|
|
if (trd) {
|
|
|
|
srs_cond_signal(cond);
|
|
|
|
trd->stop();
|
2020-09-11 09:26:02 +00:00
|
|
|
|
2020-09-19 02:30:05 +00:00
|
|
|
srs_freep(trd);
|
|
|
|
srs_cond_destroy(cond);
|
|
|
|
}
|
2020-09-11 08:44:40 +00:00
|
|
|
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2020-09-19 02:30:05 +00:00
|
|
|
srs_error_t SrsResourceManager::start()
|
2020-09-11 08:44:40 +00:00
|
|
|
{
|
|
|
|
srs_error_t err = srs_success;
|
|
|
|
|
2020-09-19 02:30:05 +00:00
|
|
|
cid_ = _srs_context->generate_id();
|
|
|
|
trd = new SrsSTCoroutine("manager", this, cid_);
|
|
|
|
|
2020-09-11 08:44:40 +00:00
|
|
|
if ((err = trd->start()) != srs_success) {
|
2020-09-11 09:26:02 +00:00
|
|
|
return srs_error_wrap(err, "conn manager");
|
2020-09-11 08:44:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2020-09-19 02:30:05 +00:00
|
|
|
bool SrsResourceManager::empty()
|
2020-09-11 10:55:18 +00:00
|
|
|
{
|
|
|
|
return conns_.empty();
|
|
|
|
}
|
|
|
|
|
2020-09-19 02:30:05 +00:00
|
|
|
size_t SrsResourceManager::size()
|
2020-09-11 10:55:18 +00:00
|
|
|
{
|
|
|
|
return conns_.size();
|
|
|
|
}
|
|
|
|
|
2020-09-19 02:30:05 +00:00
|
|
|
srs_error_t SrsResourceManager::cycle()
|
2020-09-11 08:44:40 +00:00
|
|
|
{
|
|
|
|
srs_error_t err = srs_success;
|
|
|
|
|
2020-09-19 02:30:05 +00:00
|
|
|
srs_trace("%s connection manager run", label_.c_str());
|
|
|
|
|
2020-09-11 08:44:40 +00:00
|
|
|
while (true) {
|
|
|
|
if ((err = trd->pull()) != srs_success) {
|
2020-09-11 09:26:02 +00:00
|
|
|
return srs_error_wrap(err, "conn manager");
|
2020-09-11 08:44:40 +00:00
|
|
|
}
|
|
|
|
|
2020-09-16 13:50:24 +00:00
|
|
|
// Clear all zombies, because we may switch context and lost signal
|
|
|
|
// when we clear zombie connection.
|
|
|
|
while (!zombies_.empty()) {
|
|
|
|
clear();
|
|
|
|
}
|
2020-09-11 09:26:02 +00:00
|
|
|
|
|
|
|
srs_cond_wait(cond);
|
2020-09-11 08:44:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2020-09-19 02:30:05 +00:00
|
|
|
void SrsResourceManager::add(ISrsResource* conn)
|
2020-09-11 10:55:18 +00:00
|
|
|
{
|
2020-09-13 02:41:37 +00:00
|
|
|
if (std::find(conns_.begin(), conns_.end(), conn) == conns_.end()) {
|
2020-09-11 10:55:18 +00:00
|
|
|
conns_.push_back(conn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-19 02:30:05 +00:00
|
|
|
void SrsResourceManager::add_with_id(const std::string& id, ISrsResource* conn)
|
2020-09-11 10:55:18 +00:00
|
|
|
{
|
|
|
|
add(conn);
|
2020-09-20 13:10:48 +00:00
|
|
|
conns_id_[id] = conn;
|
2020-09-11 10:55:18 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 02:30:05 +00:00
|
|
|
void SrsResourceManager::add_with_name(const std::string& name, ISrsResource* conn)
|
2020-09-11 10:55:18 +00:00
|
|
|
{
|
|
|
|
add(conn);
|
2020-09-20 13:10:48 +00:00
|
|
|
conns_name_[name] = conn;
|
2020-09-11 10:55:18 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 02:30:05 +00:00
|
|
|
ISrsResource* SrsResourceManager::at(int index)
|
2020-09-11 10:55:18 +00:00
|
|
|
{
|
2020-09-19 02:30:05 +00:00
|
|
|
return (index < (int)conns_.size())? conns_.at(index) : NULL;
|
2020-09-11 10:55:18 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 02:30:05 +00:00
|
|
|
ISrsResource* SrsResourceManager::find_by_id(std::string id)
|
2020-09-11 10:55:18 +00:00
|
|
|
{
|
2020-09-19 02:30:05 +00:00
|
|
|
map<string, ISrsResource*>::iterator it = conns_id_.find(id);
|
2020-09-11 10:55:18 +00:00
|
|
|
return (it != conns_id_.end())? it->second : NULL;
|
|
|
|
}
|
|
|
|
|
2020-09-19 02:30:05 +00:00
|
|
|
ISrsResource* SrsResourceManager::find_by_name(std::string name)
|
2020-09-11 10:55:18 +00:00
|
|
|
{
|
2020-09-19 02:30:05 +00:00
|
|
|
map<string, ISrsResource*>::iterator it = conns_name_.find(name);
|
2020-09-11 10:55:18 +00:00
|
|
|
return (it != conns_name_.end())? it->second : NULL;
|
|
|
|
}
|
|
|
|
|
2020-09-19 02:30:05 +00:00
|
|
|
void SrsResourceManager::subscribe(ISrsDisposingHandler* h)
|
|
|
|
{
|
|
|
|
if (std::find(handlers_.begin(), handlers_.end(), h) == handlers_.end()) {
|
|
|
|
handlers_.push_back(h);
|
|
|
|
}
|
2020-10-02 01:13:41 +00:00
|
|
|
|
|
|
|
// Restore the handler from unsubscribing handlers.
|
|
|
|
vector<ISrsDisposingHandler*>::iterator it;
|
|
|
|
if ((it = std::find(unsubs_.begin(), unsubs_.end(), h)) != unsubs_.end()) {
|
|
|
|
unsubs_.erase(it);
|
|
|
|
}
|
2020-09-19 02:30:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SrsResourceManager::unsubscribe(ISrsDisposingHandler* h)
|
|
|
|
{
|
|
|
|
vector<ISrsDisposingHandler*>::iterator it = find(handlers_.begin(), handlers_.end(), h);
|
|
|
|
if (it != handlers_.end()) {
|
|
|
|
handlers_.erase(it);
|
|
|
|
}
|
2020-10-02 01:13:41 +00:00
|
|
|
|
|
|
|
// Put it to the unsubscribing handlers.
|
|
|
|
if (std::find(unsubs_.begin(), unsubs_.end(), h) == unsubs_.end()) {
|
|
|
|
unsubs_.push_back(h);
|
|
|
|
}
|
2020-09-19 02:30:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SrsResourceManager::remove(ISrsResource* c)
|
2020-10-02 01:13:41 +00:00
|
|
|
{
|
|
|
|
removing_ = true;
|
|
|
|
do_remove(c);
|
|
|
|
removing_ = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SrsResourceManager::do_remove(ISrsResource* c)
|
2020-09-11 08:44:40 +00:00
|
|
|
{
|
2020-09-19 02:30:05 +00:00
|
|
|
SrsContextRestore(cid_);
|
|
|
|
if (verbose_) {
|
|
|
|
_srs_context->set_id(c->get_id());
|
|
|
|
srs_trace("before dispose resource(%s), zombies=%d", c->desc().c_str(), (int)zombies_.size());
|
|
|
|
}
|
|
|
|
|
2020-09-20 14:09:03 +00:00
|
|
|
// Only notify when not removed(in zombies_).
|
|
|
|
vector<ISrsResource*>::iterator it = std::find(zombies_.begin(), zombies_.end(), c);
|
|
|
|
if (it != zombies_.end()) {
|
|
|
|
return;
|
2020-09-11 08:44:40 +00:00
|
|
|
}
|
2020-09-19 02:30:05 +00:00
|
|
|
|
2020-09-20 14:09:03 +00:00
|
|
|
// Also ignore when we are disposing it.
|
|
|
|
if (p_disposing_) {
|
|
|
|
it = std::find(p_disposing_->begin(), p_disposing_->end(), c);
|
|
|
|
if (it != p_disposing_->end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push to zombies, we will free it in another coroutine.
|
|
|
|
zombies_.push_back(c);
|
|
|
|
|
2020-10-02 01:13:41 +00:00
|
|
|
// We should copy all handlers, because it may change during callback.
|
|
|
|
vector<ISrsDisposingHandler*> handlers = handlers_;
|
|
|
|
|
2020-09-20 14:09:03 +00:00
|
|
|
// Notify other handlers to handle the before-dispose event.
|
2020-10-02 01:13:41 +00:00
|
|
|
for (int i = 0; i < (int)handlers.size(); i++) {
|
|
|
|
ISrsDisposingHandler* h = handlers.at(i);
|
|
|
|
|
|
|
|
// Ignore if handler is unsubscribing.
|
|
|
|
if (!unsubs_.empty() && std::find(unsubs_.begin(), unsubs_.end(), h) != unsubs_.end()) {
|
|
|
|
srs_warn2(TAG_RESOURCE_UNSUB, "ignore before-dispose for %p", h);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-09-19 02:30:05 +00:00
|
|
|
h->on_before_dispose(c);
|
|
|
|
}
|
|
|
|
|
2020-09-20 14:09:03 +00:00
|
|
|
// Notify the coroutine to free it.
|
2020-09-19 02:30:05 +00:00
|
|
|
srs_cond_signal(cond);
|
2020-09-11 08:44:40 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 02:30:05 +00:00
|
|
|
void SrsResourceManager::clear()
|
2020-09-11 08:44:40 +00:00
|
|
|
{
|
2020-09-19 02:30:05 +00:00
|
|
|
if (zombies_.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SrsContextRestore(cid_);
|
|
|
|
if (verbose_) {
|
|
|
|
srs_trace("clear zombies=%d connections", (int)zombies_.size());
|
|
|
|
}
|
|
|
|
|
2020-10-02 01:13:41 +00:00
|
|
|
// Clear all unsubscribing handlers, if not removing any resource.
|
|
|
|
if (!removing_ && !unsubs_.empty()) {
|
|
|
|
vector<ISrsDisposingHandler*>().swap(unsubs_);
|
|
|
|
}
|
|
|
|
|
2020-09-20 14:09:03 +00:00
|
|
|
do_clear();
|
|
|
|
|
|
|
|
// Reset it for it points to a local object.
|
|
|
|
p_disposing_ = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SrsResourceManager::do_clear()
|
|
|
|
{
|
2020-09-11 08:44:40 +00:00
|
|
|
// To prevent thread switch when delete connection,
|
|
|
|
// we copy all connections then free one by one.
|
2020-09-19 02:30:05 +00:00
|
|
|
vector<ISrsResource*> copy;
|
2020-09-11 10:55:18 +00:00
|
|
|
copy.swap(zombies_);
|
2020-09-20 14:09:03 +00:00
|
|
|
p_disposing_ = ©
|
2020-09-11 08:44:40 +00:00
|
|
|
|
2020-09-19 02:30:05 +00:00
|
|
|
vector<ISrsResource*>::iterator it;
|
2020-09-11 08:44:40 +00:00
|
|
|
for (it = copy.begin(); it != copy.end(); ++it) {
|
2020-09-19 02:30:05 +00:00
|
|
|
ISrsResource* conn = *it;
|
|
|
|
|
|
|
|
if (verbose_) {
|
|
|
|
_srs_context->set_id(conn->get_id());
|
|
|
|
srs_trace("disposing resource(%s), zombies=%d/%d", conn->desc().c_str(),
|
|
|
|
(int)copy.size(), (int)zombies_.size());
|
|
|
|
}
|
|
|
|
|
2020-09-11 10:55:18 +00:00
|
|
|
dispose(conn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-19 02:30:05 +00:00
|
|
|
void SrsResourceManager::dispose(ISrsResource* c)
|
2020-09-11 10:55:18 +00:00
|
|
|
{
|
2020-09-19 02:30:05 +00:00
|
|
|
for (map<string, ISrsResource*>::iterator it = conns_name_.begin(); it != conns_name_.end();) {
|
2020-09-11 10:55:18 +00:00
|
|
|
if (c != it->second) {
|
|
|
|
++it;
|
|
|
|
} else {
|
|
|
|
// Use C++98 style: https://stackoverflow.com/a/4636230
|
|
|
|
conns_name_.erase(it++);
|
|
|
|
}
|
2020-09-11 08:44:40 +00:00
|
|
|
}
|
2020-09-11 10:55:18 +00:00
|
|
|
|
2020-09-19 02:30:05 +00:00
|
|
|
for (map<string, ISrsResource*>::iterator it = conns_id_.begin(); it != conns_id_.end();) {
|
2020-09-11 10:55:18 +00:00
|
|
|
if (c != it->second) {
|
|
|
|
++it;
|
|
|
|
} else {
|
|
|
|
// Use C++98 style: https://stackoverflow.com/a/4636230
|
|
|
|
conns_id_.erase(it++);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-19 02:30:05 +00:00
|
|
|
vector<ISrsResource*>::iterator it = std::find(conns_.begin(), conns_.end(), c);
|
2020-09-11 10:55:18 +00:00
|
|
|
if (it != conns_.end()) {
|
|
|
|
conns_.erase(it);
|
|
|
|
}
|
|
|
|
|
2020-10-02 01:13:41 +00:00
|
|
|
// We should copy all handlers, because it may change during callback.
|
|
|
|
vector<ISrsDisposingHandler*> handlers = handlers_;
|
|
|
|
|
|
|
|
// Notify other handlers to handle the disposing event.
|
|
|
|
for (int i = 0; i < (int)handlers.size(); i++) {
|
|
|
|
ISrsDisposingHandler* h = handlers.at(i);
|
|
|
|
|
|
|
|
// Ignore if handler is unsubscribing.
|
|
|
|
if (!unsubs_.empty() && std::find(unsubs_.begin(), unsubs_.end(), h) != unsubs_.end()) {
|
|
|
|
srs_warn2(TAG_RESOURCE_UNSUB, "ignore disposing for %p", h);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-09-19 02:30:05 +00:00
|
|
|
h->on_disposing(c);
|
|
|
|
}
|
|
|
|
|
2020-09-11 10:55:18 +00:00
|
|
|
srs_freep(c);
|
2020-09-11 08:44:40 +00:00
|
|
|
}
|
|
|
|
|
2020-11-04 12:57:07 +00:00
|
|
|
ISrsStartableConneciton::ISrsStartableConneciton()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ISrsStartableConneciton::~ISrsStartableConneciton()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-09-19 02:30:05 +00:00
|
|
|
SrsTcpConnection::SrsTcpConnection(ISrsResourceManager* cm, srs_netfd_t c, string cip, int cport)
|
2013-11-23 03:36:07 +00:00
|
|
|
{
|
2015-05-03 15:34:59 +00:00
|
|
|
manager = cm;
|
|
|
|
stfd = c;
|
2015-12-24 09:25:05 +00:00
|
|
|
ip = cip;
|
2020-07-04 09:19:08 +00:00
|
|
|
port = cport;
|
2019-04-10 01:07:03 +00:00
|
|
|
create_time = srsu2ms(srs_get_system_time());
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2017-03-02 08:43:06 +00:00
|
|
|
skt = new SrsStSocket();
|
2018-12-23 12:47:17 +00:00
|
|
|
clk = new SrsWallClock();
|
|
|
|
kbps = new SrsKbps(clk);
|
2015-09-17 05:36:02 +00:00
|
|
|
kbps->set_io(skt, skt);
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2017-06-04 11:13:56 +00:00
|
|
|
trd = new SrsSTCoroutine("conn", this);
|
2013-11-23 03:36:07 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 08:59:22 +00:00
|
|
|
SrsTcpConnection::~SrsTcpConnection()
|
2013-11-23 03:36:07 +00:00
|
|
|
{
|
2015-06-08 06:03:16 +00:00
|
|
|
dispose();
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2015-09-17 05:36:02 +00:00
|
|
|
srs_freep(kbps);
|
2018-12-23 12:47:17 +00:00
|
|
|
srs_freep(clk);
|
2015-09-17 05:36:02 +00:00
|
|
|
srs_freep(skt);
|
2017-05-29 11:45:19 +00:00
|
|
|
srs_freep(trd);
|
|
|
|
|
|
|
|
srs_close_stfd(stfd);
|
2015-06-08 06:03:16 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 08:59:22 +00:00
|
|
|
void SrsTcpConnection::remark(int64_t* in, int64_t* out)
|
2015-09-17 05:36:02 +00:00
|
|
|
{
|
2019-01-01 09:36:27 +00:00
|
|
|
kbps->remark(in, out);
|
2015-09-17 05:36:02 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 08:59:22 +00:00
|
|
|
void SrsTcpConnection::dispose()
|
2015-06-08 06:03:16 +00:00
|
|
|
{
|
2017-05-29 11:45:19 +00:00
|
|
|
trd->interrupt();
|
2013-11-23 03:36:07 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 08:59:22 +00:00
|
|
|
srs_error_t SrsTcpConnection::start()
|
2014-05-03 14:59:21 +00:00
|
|
|
{
|
2017-06-11 10:44:20 +00:00
|
|
|
srs_error_t err = srs_success;
|
2017-03-02 08:43:06 +00:00
|
|
|
|
2017-09-22 11:54:50 +00:00
|
|
|
if ((err = skt->initialize(stfd)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "init socket");
|
2017-03-02 08:43:06 +00:00
|
|
|
}
|
|
|
|
|
2017-06-11 10:44:20 +00:00
|
|
|
if ((err = trd->start()) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "coroutine");
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
2014-05-03 14:59:21 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 08:59:22 +00:00
|
|
|
srs_error_t SrsTcpConnection::set_tcp_nodelay(bool v)
|
2018-08-05 12:30:04 +00:00
|
|
|
{
|
|
|
|
srs_error_t err = srs_success;
|
|
|
|
|
|
|
|
int r0 = 0;
|
|
|
|
socklen_t nb_v = sizeof(int);
|
|
|
|
int fd = srs_netfd_fileno(stfd);
|
|
|
|
|
|
|
|
int ov = 0;
|
|
|
|
if ((r0 = getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &ov, &nb_v)) != 0) {
|
|
|
|
return srs_error_new(ERROR_SOCKET_NO_NODELAY, "getsockopt fd=%d, r0=%d", fd, r0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef SRS_PERF_TCP_NODELAY
|
|
|
|
srs_warn("ignore TCP_NODELAY, fd=%d, ov=%d", fd, ov);
|
|
|
|
return err;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int iv = (v? 1:0);
|
|
|
|
if ((r0 = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &iv, nb_v)) != 0) {
|
2020-04-21 04:18:49 +00:00
|
|
|
return srs_error_new(ERROR_SOCKET_NO_NODELAY, "setsockopt fd=%d, r0=%d", fd, r0);
|
2018-08-05 12:30:04 +00:00
|
|
|
}
|
|
|
|
if ((r0 = getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &iv, &nb_v)) != 0) {
|
|
|
|
return srs_error_new(ERROR_SOCKET_NO_NODELAY, "getsockopt fd=%d, r0=%d", fd, r0);
|
|
|
|
}
|
|
|
|
|
|
|
|
srs_trace("set fd=%d TCP_NODELAY %d=>%d", fd, ov, iv);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2020-09-11 08:59:22 +00:00
|
|
|
srs_error_t SrsTcpConnection::set_socket_buffer(srs_utime_t buffer_v)
|
2018-08-05 12:30:04 +00:00
|
|
|
{
|
|
|
|
srs_error_t err = srs_success;
|
|
|
|
|
|
|
|
int r0 = 0;
|
|
|
|
int fd = srs_netfd_fileno(stfd);
|
|
|
|
socklen_t nb_v = sizeof(int);
|
|
|
|
|
|
|
|
int ov = 0;
|
|
|
|
if ((r0 = getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &ov, &nb_v)) != 0) {
|
|
|
|
return srs_error_new(ERROR_SOCKET_SNDBUF, "getsockopt fd=%d, r0=%d", fd, r0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef SRS_PERF_MW_SO_SNDBUF
|
|
|
|
srs_warn("ignore SO_SNDBUF, fd=%d, ov=%d", fd, ov);
|
|
|
|
return err;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// the bytes:
|
|
|
|
// 4KB=4096, 8KB=8192, 16KB=16384, 32KB=32768, 64KB=65536,
|
|
|
|
// 128KB=131072, 256KB=262144, 512KB=524288
|
|
|
|
// the buffer should set to sleep*kbps/8,
|
|
|
|
// for example, your system delivery stream in 1000kbps,
|
|
|
|
// sleep 800ms for small bytes, the buffer should set to:
|
|
|
|
// 800*1000/8=100000B(about 128KB).
|
|
|
|
// other examples:
|
|
|
|
// 2000*3000/8=750000B(about 732KB).
|
|
|
|
// 2000*5000/8=1250000B(about 1220KB).
|
|
|
|
int kbps = 4000;
|
2019-04-09 23:53:09 +00:00
|
|
|
int iv = srsu2ms(buffer_v) * kbps / 8;
|
2018-08-05 12:30:04 +00:00
|
|
|
|
|
|
|
// socket send buffer, system will double it.
|
|
|
|
iv = iv / 2;
|
|
|
|
|
|
|
|
// override the send buffer by macro.
|
|
|
|
#ifdef SRS_PERF_SO_SNDBUF_SIZE
|
|
|
|
iv = SRS_PERF_SO_SNDBUF_SIZE / 2;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// set the socket send buffer when required larger buffer
|
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &iv, nb_v) < 0) {
|
2020-04-21 04:18:49 +00:00
|
|
|
return srs_error_new(ERROR_SOCKET_SNDBUF, "setsockopt fd=%d, r0=%d", fd, r0);
|
2018-08-05 12:30:04 +00:00
|
|
|
}
|
|
|
|
if ((r0 = getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &iv, &nb_v)) != 0) {
|
|
|
|
return srs_error_new(ERROR_SOCKET_SNDBUF, "getsockopt fd=%d, r0=%d", fd, r0);
|
|
|
|
}
|
|
|
|
|
2019-04-09 23:53:09 +00:00
|
|
|
srs_trace("set fd=%d, SO_SNDBUF=%d=>%d, buffer=%dms", fd, ov, iv, srsu2ms(buffer_v));
|
2018-08-05 12:30:04 +00:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2020-09-11 08:59:22 +00:00
|
|
|
srs_error_t SrsTcpConnection::cycle()
|
2013-11-23 03:36:07 +00:00
|
|
|
{
|
2017-07-29 13:39:57 +00:00
|
|
|
srs_error_t err = do_cycle();
|
2013-11-23 03:36:07 +00:00
|
|
|
|
2017-06-11 10:44:20 +00:00
|
|
|
// Notify manager to remove it.
|
|
|
|
manager->remove(this);
|
2014-03-18 03:32:58 +00:00
|
|
|
|
2014-05-03 14:59:21 +00:00
|
|
|
// success.
|
2017-07-29 13:39:57 +00:00
|
|
|
if (err == srs_success) {
|
2014-07-12 12:48:03 +00:00
|
|
|
srs_trace("client finished.");
|
2017-07-29 13:39:57 +00:00
|
|
|
return err;
|
2014-05-03 14:59:21 +00:00
|
|
|
}
|
2020-03-17 03:40:05 +00:00
|
|
|
|
|
|
|
// It maybe success with message.
|
|
|
|
if (srs_error_code(err) == ERROR_SUCCESS) {
|
|
|
|
srs_trace("client finished%s.", srs_error_summary(err).c_str());
|
|
|
|
srs_freep(err);
|
|
|
|
return err;
|
|
|
|
}
|
2014-05-03 14:59:21 +00:00
|
|
|
|
|
|
|
// client close peer.
|
2017-06-11 10:44:20 +00:00
|
|
|
// TODO: FIXME: Only reset the error when client closed it.
|
2019-12-17 08:54:06 +00:00
|
|
|
if (srs_is_client_gracefully_close(err)) {
|
2017-07-29 13:39:57 +00:00
|
|
|
srs_warn("client disconnect peer. ret=%d", srs_error_code(err));
|
2019-12-17 08:54:06 +00:00
|
|
|
} else if (srs_is_server_gracefully_close(err)) {
|
|
|
|
srs_warn("server disconnect. ret=%d", srs_error_code(err));
|
2017-09-23 14:12:33 +00:00
|
|
|
} else {
|
2019-12-17 08:54:06 +00:00
|
|
|
srs_error("serve error %s", srs_error_desc(err).c_str());
|
2014-05-03 14:59:21 +00:00
|
|
|
}
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2017-09-23 14:12:33 +00:00
|
|
|
srs_freep(err);
|
|
|
|
return srs_success;
|
2014-05-03 14:59:21 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 08:59:22 +00:00
|
|
|
string SrsTcpConnection::remote_ip()
|
2020-07-04 09:19:08 +00:00
|
|
|
{
|
2019-12-26 02:37:16 +00:00
|
|
|
return ip;
|
|
|
|
}
|
|
|
|
|
2020-09-19 02:30:05 +00:00
|
|
|
const SrsContextId& SrsTcpConnection::get_id()
|
|
|
|
{
|
|
|
|
return trd->cid();
|
|
|
|
}
|
|
|
|
|
2020-09-11 08:59:22 +00:00
|
|
|
void SrsTcpConnection::expire()
|
2015-08-22 05:36:15 +00:00
|
|
|
{
|
2017-05-29 11:45:19 +00:00
|
|
|
trd->interrupt();
|
2015-08-22 05:36:15 +00:00
|
|
|
}
|
|
|
|
|
2014-08-02 14:18:39 +00:00
|
|
|
|