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
|
|
|
#ifndef SRS_APP_CONN_HPP
|
|
|
|
#define SRS_APP_CONN_HPP
|
2013-11-23 03:36:07 +00:00
|
|
|
|
|
|
|
#include <srs_core.hpp>
|
|
|
|
|
2014-05-27 08:45:02 +00:00
|
|
|
#include <string>
|
2020-09-11 08:44:40 +00:00
|
|
|
#include <vector>
|
2020-09-11 10:55:18 +00:00
|
|
|
#include <map>
|
2014-05-27 08:45:02 +00:00
|
|
|
|
2014-03-02 13:49:09 +00:00
|
|
|
#include <srs_app_st.hpp>
|
2015-05-23 01:58:00 +00:00
|
|
|
#include <srs_protocol_kbps.hpp>
|
2016-12-15 08:22:04 +00:00
|
|
|
#include <srs_app_reload.hpp>
|
2017-03-26 05:40:39 +00:00
|
|
|
#include <srs_service_conn.hpp>
|
2014-07-16 03:23:49 +00:00
|
|
|
|
2018-12-23 12:47:17 +00:00
|
|
|
class SrsWallClock;
|
|
|
|
|
2020-09-11 09:20:41 +00:00
|
|
|
// The connection manager remove connection and delete it asynchronously.
|
|
|
|
class SrsConnectionManager : virtual public ISrsCoroutineHandler, virtual public IConnectionManager
|
2020-09-11 08:44:40 +00:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
SrsCoroutine* trd;
|
|
|
|
srs_cond_t cond;
|
2020-09-11 10:55:18 +00:00
|
|
|
// The zombie connections, we will delete it asynchronously.
|
|
|
|
std::vector<ISrsConnection*> 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_;
|
2020-09-11 08:44:40 +00:00
|
|
|
public:
|
2020-09-11 09:20:41 +00:00
|
|
|
SrsConnectionManager();
|
|
|
|
virtual ~SrsConnectionManager();
|
2020-09-11 08:44:40 +00:00
|
|
|
public:
|
|
|
|
srs_error_t start();
|
2020-09-11 10:55:18 +00:00
|
|
|
bool empty();
|
|
|
|
size_t size();
|
2020-09-11 08:44:40 +00:00
|
|
|
// Interface ISrsCoroutineHandler
|
|
|
|
public:
|
|
|
|
virtual srs_error_t cycle();
|
2020-09-11 10:55:18 +00:00
|
|
|
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);
|
2020-09-11 08:44:40 +00:00
|
|
|
// Interface IConnectionManager
|
|
|
|
public:
|
|
|
|
virtual void remove(ISrsConnection* c);
|
|
|
|
private:
|
|
|
|
void clear();
|
2020-09-11 10:55:18 +00:00
|
|
|
void dispose(ISrsConnection* c);
|
2020-09-11 08:44:40 +00:00
|
|
|
};
|
|
|
|
|
2020-09-11 08:59:22 +00:00
|
|
|
// The basic connection of SRS, for TCP based protocols,
|
2019-04-28 01:08:05 +00:00
|
|
|
// all connections accept from listener must extends from this base class,
|
|
|
|
// server will add the connection to manager, and delete it when remove.
|
2020-09-11 08:59:22 +00:00
|
|
|
class SrsTcpConnection : virtual public ISrsConnection, virtual public ISrsCoroutineHandler
|
2019-01-01 09:36:27 +00:00
|
|
|
, virtual public ISrsKbpsDelta, virtual public ISrsReloadHandler
|
2013-11-23 03:36:07 +00:00
|
|
|
{
|
2017-05-29 11:45:19 +00:00
|
|
|
protected:
|
2019-04-28 01:08:05 +00:00
|
|
|
// Each connection start a green thread,
|
|
|
|
// when thread stop, the connection will be delete by server.
|
2017-05-29 11:45:19 +00:00
|
|
|
SrsCoroutine* trd;
|
2019-04-28 01:08:05 +00:00
|
|
|
// The manager object to manage the connection.
|
2015-05-03 15:34:59 +00:00
|
|
|
IConnectionManager* manager;
|
2019-04-28 01:08:05 +00:00
|
|
|
// The underlayer st fd handler.
|
2017-05-30 01:05:02 +00:00
|
|
|
srs_netfd_t stfd;
|
2020-07-04 09:19:08 +00:00
|
|
|
// The ip and port of client.
|
2014-05-27 08:45:02 +00:00
|
|
|
std::string ip;
|
2020-07-04 09:19:08 +00:00
|
|
|
int port;
|
2019-04-28 01:08:05 +00:00
|
|
|
// The underlayer socket.
|
2015-09-17 05:36:02 +00:00
|
|
|
SrsStSocket* skt;
|
2019-04-28 01:08:05 +00:00
|
|
|
// The connection total kbps.
|
|
|
|
// not only the rtmp or http connection, all type of connection are
|
|
|
|
// need to statistic the kbps of io.
|
|
|
|
// The SrsStatistic will use it indirectly to statistic the bytes delta of current connection.
|
2015-09-17 05:36:02 +00:00
|
|
|
SrsKbps* kbps;
|
2018-12-23 12:47:17 +00:00
|
|
|
SrsWallClock* clk;
|
2019-04-28 01:08:05 +00:00
|
|
|
// The create time in milliseconds.
|
|
|
|
// for current connection to log self create time and calculate the living time.
|
2015-09-17 05:36:02 +00:00
|
|
|
int64_t create_time;
|
2013-11-23 03:36:07 +00:00
|
|
|
public:
|
2020-09-11 08:59:22 +00:00
|
|
|
SrsTcpConnection(IConnectionManager* cm, srs_netfd_t c, std::string cip, int cport);
|
|
|
|
virtual ~SrsTcpConnection();
|
2019-04-30 00:30:13 +00:00
|
|
|
// Interface ISrsKbpsDelta
|
2015-09-17 05:36:02 +00:00
|
|
|
public:
|
2019-01-01 09:36:27 +00:00
|
|
|
virtual void remark(int64_t* in, int64_t* out);
|
2013-11-23 03:36:07 +00:00
|
|
|
public:
|
2019-04-28 01:08:05 +00:00
|
|
|
// To dipose the connection.
|
2015-06-08 06:03:16 +00:00
|
|
|
virtual void dispose();
|
2019-04-28 01:08:05 +00:00
|
|
|
// Start the client green thread.
|
|
|
|
// when server get a client from listener,
|
|
|
|
// 1. server will create an concrete connection(for instance, RTMP connection),
|
|
|
|
// 2. then add connection to its connection manager,
|
|
|
|
// 3. start the client thread by invoke this start()
|
|
|
|
// when client cycle thread stop, invoke the on_thread_stop(), which will use server
|
|
|
|
// To remove the client by server->remove(this).
|
2017-06-11 10:44:20 +00:00
|
|
|
virtual srs_error_t start();
|
2018-08-05 12:30:04 +00:00
|
|
|
// Set socket option TCP_NODELAY.
|
|
|
|
virtual srs_error_t set_tcp_nodelay(bool v);
|
2019-04-17 23:58:37 +00:00
|
|
|
// Set socket option SO_SNDBUF in srs_utime_t.
|
2019-04-09 01:20:44 +00:00
|
|
|
virtual srs_error_t set_socket_buffer(srs_utime_t buffer_v);
|
2019-04-30 00:30:13 +00:00
|
|
|
// Interface ISrsOneCycleThreadHandler
|
2015-05-23 01:20:16 +00:00
|
|
|
public:
|
2019-04-28 01:08:05 +00:00
|
|
|
// The thread cycle function,
|
|
|
|
// when serve connection completed, terminate the loop which will terminate the thread,
|
|
|
|
// thread will invoke the on_thread_stop() when it terminated.
|
2017-06-11 10:44:20 +00:00
|
|
|
virtual srs_error_t cycle();
|
2014-06-19 07:28:05 +00:00
|
|
|
public:
|
2019-04-28 01:08:05 +00:00
|
|
|
// Get the srs id which identify the client.
|
2020-07-05 15:26:55 +00:00
|
|
|
// TODO: FIXME: Rename to cid.
|
|
|
|
virtual SrsContextId srs_id();
|
2019-12-26 02:37:16 +00:00
|
|
|
// Get the remote ip of peer.
|
|
|
|
virtual std::string remote_ip();
|
2019-04-28 01:08:05 +00:00
|
|
|
// Set connection to expired.
|
2015-08-22 05:36:15 +00:00
|
|
|
virtual void expire();
|
2013-11-23 03:36:07 +00:00
|
|
|
protected:
|
2019-04-28 01:08:05 +00:00
|
|
|
// For concrete connection to do the cycle.
|
2017-07-29 13:39:57 +00:00
|
|
|
virtual srs_error_t do_cycle() = 0;
|
2013-11-23 03:36:07 +00:00
|
|
|
};
|
|
|
|
|
2014-08-02 14:18:39 +00:00
|
|
|
#endif
|