Blame view

trunk/src/app/srs_app_conn.hpp 3.6 KB
winlin authored
1 2 3
/*
The MIT License (MIT)
4
Copyright (c) 2013-2015 SRS(simple-rtmp-server)
winlin authored
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

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.
*/
24 25
#ifndef SRS_APP_CONN_HPP
#define SRS_APP_CONN_HPP
winlin authored
26 27

/*
28
#include <srs_app_conn.hpp>
winlin authored
29 30 31 32
*/

#include <srs_core.hpp>
33 34
#include <string>
35
#include <srs_app_st.hpp>
36
#include <srs_app_thread.hpp>
37
#include <srs_app_kbps.hpp>
winlin authored
38
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
class SrsConnection;

/**
 * the manager for connection.
 */
class IConnectionManager
{
public:
    IConnectionManager();
    virtual ~IConnectionManager();
public:
    /**
     * remove the specified connection.
     */
    virtual void remove(SrsConnection* c) = 0;
};
winlin authored
55 56 57 58 59 60

/**
* 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.
*/
61
class SrsConnection : public virtual ISrsThreadHandler, public virtual IKbpsDelta
winlin authored
62
{
63
private:
winlin authored
64 65 66 67
    /**
    * each connection start a green thread,
    * when thread stop, the connection will be delete by server.
    */
68
    SrsThread* pthread;
69 70 71 72
    /**
    * the id of connection.
    */
    int id;
winlin authored
73
protected:
winlin authored
74
    /**
75
    * the manager object to manage the connection.
winlin authored
76
    */
77
    IConnectionManager* manager;
winlin authored
78 79 80
    /**
    * the underlayer st fd handler.
    */
81
    st_netfd_t stfd;
winlin authored
82 83 84
    /**
    * the ip of client.
    */
85
    std::string ip;
winlin authored
86
public:
87
    SrsConnection(IConnectionManager* cm, st_netfd_t c);
88
    virtual ~SrsConnection();
winlin authored
89
public:
winlin authored
90 91 92 93 94 95 96 97 98
    /**
    * 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).
    */
99
    virtual int start();
winlin authored
100 101 102 103 104
    /**
    * 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.
    */
105
    virtual int cycle();
winlin authored
106 107 108 109 110
    /**
    * when the thread cycle finished, thread will invoke the on_thread_stop(),
    * which will remove self from server, server will remove the connection from manager 
    * then delete the connection.
    */
111
    virtual void on_thread_stop();
112
public:
winlin authored
113
    /**
114
    * get the srs id which identify the client.
winlin authored
115
    */
116
    virtual int srs_id();
winlin authored
117
protected:
winlin authored
118 119 120
    /**
    * for concrete connection to do the cycle.
    */
121
    virtual int do_cycle() = 0;
winlin authored
122 123 124 125 126
private:
    /**
    * when delete the connection, stop the connection,
    * close the underlayer socket, delete the thread.
    */
127
    virtual void stop();
winlin authored
128 129
};
130
#endif