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.hpp

108 lines
4.1 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
#ifndef SRS_APP_CONN_HPP
#define SRS_APP_CONN_HPP
2013-11-23 03:36:07 +00:00
#include <srs_core.hpp>
#include <string>
#include <srs_app_st.hpp>
#include <srs_app_thread.hpp>
2015-05-23 01:58:00 +00:00
#include <srs_protocol_kbps.hpp>
#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;
2019-04-28 01:08:05 +00:00
// The basic connection of SRS,
// all connections accept from listener must extends from this base class,
// server will add the connection to manager, and delete it when remove.
class SrsConnection : 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
{
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.
SrsCoroutine* trd;
2019-04-28 01:08:05 +00:00
// The manager object to manage the connection.
IConnectionManager* manager;
2019-04-28 01:08:05 +00:00
// The underlayer st fd handler.
srs_netfd_t stfd;
2020-07-04 09:19:08 +00:00
// The ip and port of client.
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-07-04 09:19:08 +00:00
SrsConnection(IConnectionManager* cm, srs_netfd_t c, std::string cip, int cport);
2014-03-18 03:32:58 +00:00
virtual ~SrsConnection();
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.
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).
virtual srs_error_t start();
// Set socket option TCP_NODELAY.
virtual srs_error_t set_tcp_nodelay(bool v);
// 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.
virtual srs_error_t cycle();
public:
2019-04-28 01:08:05 +00:00
// Get the srs id which identify the client.
// TODO: FIXME: Rename to cid.
virtual SrsContextId srs_id();
// 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