1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-02-15 04:42:04 +00:00
srs/trunk/src/app/srs_app_conn.cpp

283 lines
7.3 KiB
C++
Raw Normal View History

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
#include <srs_app_conn.hpp>
2013-11-23 03:36:07 +00:00
#include <netinet/tcp.h>
2015-12-24 09:25:05 +00:00
using namespace std;
#include <srs_kernel_log.hpp>
#include <srs_kernel_error.hpp>
#include <srs_app_utility.hpp>
2015-09-17 05:36:02 +00:00
#include <srs_kernel_utility.hpp>
2013-11-23 03:36:07 +00:00
SrsCoroutineManager::SrsCoroutineManager()
{
cond = srs_cond_new();
trd = new SrsSTCoroutine("manager", this);
}
SrsCoroutineManager::~SrsCoroutineManager()
{
srs_freep(trd);
srs_cond_destroy(cond);
clear();
}
srs_error_t SrsCoroutineManager::start()
{
srs_error_t err = srs_success;
if ((err = trd->start()) != srs_success) {
return srs_error_wrap(err, "coroutine manager");
}
return err;
}
srs_error_t SrsCoroutineManager::cycle()
{
srs_error_t err = srs_success;
while (true) {
if ((err = trd->pull()) != srs_success) {
return srs_error_wrap(err, "coroutine mansger");
}
srs_cond_wait(cond);
clear();
}
return err;
}
void SrsCoroutineManager::remove(ISrsConnection* c)
{
if (::find(conns.begin(), conns.end(), c) == conns.end()) {
conns.push_back(c);
}
srs_cond_signal(cond);
}
void SrsCoroutineManager::clear()
{
// To prevent thread switch when delete connection,
// we copy all connections then free one by one.
vector<ISrsConnection*> copy;
copy.swap(conns);
vector<ISrsConnection*>::iterator it;
for (it = copy.begin(); it != copy.end(); ++it) {
ISrsConnection* conn = *it;
srs_freep(conn);
}
}
2020-07-04 09:19:08 +00:00
SrsConnection::SrsConnection(IConnectionManager* cm, srs_netfd_t c, string cip, int cport)
2013-11-23 03:36:07 +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;
create_time = srsu2ms(srs_get_system_time());
2017-03-25 09:21:39 +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
trd = new SrsSTCoroutine("conn", this);
2013-11-23 03:36:07 +00:00
}
SrsConnection::~SrsConnection()
{
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);
srs_freep(trd);
srs_close_stfd(stfd);
}
2019-01-01 09:36:27 +00:00
void SrsConnection::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
}
void SrsConnection::dispose()
{
trd->interrupt();
2013-11-23 03:36:07 +00:00
}
srs_error_t SrsConnection::start()
{
srs_error_t err = srs_success;
if ((err = skt->initialize(stfd)) != srs_success) {
return srs_error_wrap(err, "init socket");
}
if ((err = trd->start()) != srs_success) {
return srs_error_wrap(err, "coroutine");
}
return err;
}
srs_error_t SrsConnection::set_tcp_nodelay(bool v)
{
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);
}
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;
}
2019-04-09 01:20:44 +00:00
srs_error_t SrsConnection::set_socket_buffer(srs_utime_t buffer_v)
{
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;
int iv = srsu2ms(buffer_v) * kbps / 8;
// 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);
}
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);
}
srs_trace("set fd=%d, SO_SNDBUF=%d=>%d, buffer=%dms", fd, ov, iv, srsu2ms(buffer_v));
return err;
}
srs_error_t SrsConnection::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
// Notify manager to remove it.
manager->remove(this);
2014-03-18 03:32:58 +00:00
// success.
2017-07-29 13:39:57 +00:00
if (err == srs_success) {
srs_trace("client finished.");
2017-07-29 13:39:57 +00:00
return err;
}
// 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;
}
// client close peer.
// TODO: FIXME: Only reset the error when client closed it.
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));
} 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 {
srs_error("serve error %s", srs_error_desc(err).c_str());
}
2017-03-25 09:21:39 +00:00
2017-09-23 14:12:33 +00:00
srs_freep(err);
return srs_success;
}
SrsContextId SrsConnection::srs_id()
{
return trd->cid();
}
2020-07-04 09:19:08 +00:00
string SrsConnection::remote_ip()
{
return ip;
}
2015-08-22 05:36:15 +00:00
void SrsConnection::expire()
{
trd->interrupt();
2015-08-22 05:36:15 +00:00
}
2014-08-02 14:18:39 +00:00