Blame view

trunk/src/rtmp/srs_protocol_io.hpp 5.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
The MIT License (MIT)

Copyright (c) 2013-2014 winlin

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_RTMP_PROTOCOL_IO_HPP
#define SRS_RTMP_PROTOCOL_IO_HPP
26 27 28 29 30 31 32

/*
#include <srs_protocol_io.hpp>
*/

#include <srs_core.hpp>
33 34
// for srs-librtmp, @see https://github.com/winlinvip/simple-rtmp-server/issues/213
#ifndef _WIN32
35
#include <sys/uio.h>
36
#endif
37 38 39 40

#include <srs_kernel_buffer.hpp>

/**
41 42 43 44 45
* the system io reader/writer architecture:
+---------------+     +--------------------+      +---------------+
| IBufferReader |     |    IStatistic      |      | IBufferWriter |
+---------------+     +--------------------+      +---------------+
| + read()      |     | + get_recv_bytes() |      | + write()     |
46 47 48
+------+--------+     | + get_recv_bytes() |      | + writev()    |
      / \             +---+--------------+-+      +-------+-------+
       |                 / \            / \              / \
49 50 51 52
       |                  |              |                |
+------+------------------+-+      +-----+----------------+--+
| IProtocolReader           |      | IProtocolWriter         |
+---------------------------+      +-------------------------+
53 54 55 56
| + readfully()             |      | + set_send_timeout()    |
| + set_recv_timeout()      |      +-------+-----------------+
+------------+--------------+             / \     
            / \                            |   
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
             |                             | 
          +--+-----------------------------+-+
          |       IProtocolReaderWriter      |
          +----------------------------------+
          | + is_never_timeout()             |
          +----------------------------------+
*/

/**
* the writer for the buffer to write to whatever channel.
*/
class ISrsBufferWriter
{
public:
    ISrsBufferWriter();
    virtual ~ISrsBufferWriter();
// for protocol
public:
75 76 77 78
    /**
    * write bytes over writer.
    * @nwrite the actual written bytes. NULL to ignore.
    */
79
    virtual int write(void* buf, size_t size, ssize_t* nwrite) = 0;
80 81 82 83
    /**
    * write iov over writer.
    * @nwrite the actual written bytes. NULL to ignore.
    */
84 85 86 87 88 89 90 91 92 93 94 95 96
    virtual int writev(const iovec *iov, int iov_size, ssize_t* nwrite) = 0;
};

/**
* get the statistic of channel.
*/
class ISrsProtocolStatistic
{
public:
    ISrsProtocolStatistic();
    virtual ~ISrsProtocolStatistic();
// for protocol
public:
97 98 99
    /**
    * get the total recv bytes over underlay fd.
    */
100
    virtual int64_t get_recv_bytes() = 0;
101 102 103
    /**
    * get the total send bytes over underlay fd.
    */
104 105 106 107
    virtual int64_t get_send_bytes() = 0;
};

/**
108 109
* the reader for the protocol to read from whatever channel.
*/
110
class ISrsProtocolReader : public virtual ISrsBufferReader, public virtual ISrsProtocolStatistic
111 112
{
public:
113 114
    ISrsProtocolReader();
    virtual ~ISrsProtocolReader();
115 116
// for protocol
public:
117 118 119 120
    /**
    * set the recv timeout in us, recv will error when timeout.
    * @remark, if not set, use ST_UTIME_NO_TIMEOUT, never timeout.
    */
121
    virtual void set_recv_timeout(int64_t timeout_us) = 0;
122 123 124
    /**
    * get the recv timeout in us.
    */
125
    virtual int64_t get_recv_timeout() = 0;
126 127 128 129 130 131 132
// for handshake.
public:
    /**
    * read specified size bytes of data
    * @param nread, the actually read size, NULL to ignore.
    */
    virtual int read_fully(void* buf, size_t size, ssize_t* nread) = 0;
133 134 135 136 137
};

/**
* the writer for the protocol to write to whatever channel.
*/
138
class ISrsProtocolWriter : public virtual ISrsBufferWriter, public virtual ISrsProtocolStatistic
139 140
{
public:
141 142
    ISrsProtocolWriter();
    virtual ~ISrsProtocolWriter();
143 144
// for protocol
public:
145 146 147 148
    /**
    * set the send timeout in us, send will error when timeout.
    * @remark, if not set, use ST_UTIME_NO_TIMEOUT, never timeout.
    */
149
    virtual void set_send_timeout(int64_t timeout_us) = 0;
150 151 152
    /**
    * get the send timeout in us.
    */
153
    virtual int64_t get_send_timeout() = 0;
154 155
};
156 157 158 159
/**
* the reader and writer.
*/
class ISrsProtocolReaderWriter : public virtual ISrsProtocolReader, public virtual ISrsProtocolWriter
160 161
{
public:
162 163
    ISrsProtocolReaderWriter();
    virtual ~ISrsProtocolReaderWriter();
164 165
// for protocol
public:
166 167 168 169
    /**
    * whether the specified timeout_us is never timeout.
    */
    virtual bool is_never_timeout(int64_t timeout_us) = 0;
170 171 172
};

#endif
173