Skip to content

Commit

Permalink
for #742, refine the io interfaces.
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Jan 30, 2017
1 parent eaa222f commit d3bb8b6
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 33 deletions.
4 changes: 2 additions & 2 deletions trunk/src/kernel/srs_kernel_file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
/**
* file writer, to write to file.
*/
class SrsFileWriter : public ISrsBufferWriter
class SrsFileWriter : public ISrsWriter
{
private:
std::string path;
Expand Down Expand Up @@ -85,7 +85,7 @@ class SrsFileWriter : public ISrsBufferWriter
/**
* file reader, to read from file.
*/
class SrsFileReader : public ISrsBufferReader
class SrsFileReader : public ISrsReader
{
private:
std::string path;
Expand Down
24 changes: 20 additions & 4 deletions trunk/src/kernel/srs_kernel_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,35 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#include <srs_kernel_io.hpp>

ISrsBufferReader::ISrsBufferReader()
ISrsReader::ISrsReader()
{
}

ISrsBufferReader::~ISrsBufferReader()
ISrsReader::~ISrsReader()
{
}

ISrsBufferWriter::ISrsBufferWriter()
ISrsStreamWriter::ISrsStreamWriter()
{
}

ISrsBufferWriter::~ISrsBufferWriter()
ISrsStreamWriter::~ISrsStreamWriter()
{
}

ISrsVectorWriter::ISrsVectorWriter()
{
}

ISrsVectorWriter::~ISrsVectorWriter()
{
}

ISrsWriter::ISrsWriter()
{
}

ISrsWriter::~ISrsWriter()
{
}

39 changes: 29 additions & 10 deletions trunk/src/kernel/srs_kernel_io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,39 +36,58 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#endif

/**
* the reader for the buffer to read from whatever channel.
* The reader to read data from channel.
*/
class ISrsBufferReader
class ISrsReader
{
public:
ISrsBufferReader();
virtual ~ISrsBufferReader();
// for protocol/amf0/msg-codec
ISrsReader();
virtual ~ISrsReader();
public:
virtual int read(void* buf, size_t size, ssize_t* nread) = 0;
};

/**
* the writer for the buffer to write to whatever channel.
* The writer to write stream data to channel.
*/
class ISrsBufferWriter
class ISrsStreamWriter
{
public:
ISrsBufferWriter();
virtual ~ISrsBufferWriter();
// for protocol
ISrsStreamWriter();
virtual ~ISrsStreamWriter();
public:
/**
* write bytes over writer.
* @nwrite the actual written bytes. NULL to ignore.
*/
virtual int write(void* buf, size_t size, ssize_t* nwrite) = 0;
};

/**
* The vector writer to write vector(iovc) to channel.
*/
class ISrsVectorWriter
{
public:
ISrsVectorWriter();
virtual ~ISrsVectorWriter();
public:
/**
* write iov over writer.
* @nwrite the actual written bytes. NULL to ignore.
*/
virtual int writev(const iovec *iov, int iov_size, ssize_t* nwrite) = 0;
};

/**
* The generally writer, stream and vector writer.
*/
class ISrsWriter : virtual public ISrsStreamWriter, virtual public ISrsVectorWriter
{
public:
ISrsWriter();
virtual ~ISrsWriter();
};

#endif

2 changes: 1 addition & 1 deletion trunk/src/libs/srs_lib_simple_socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ int SimpleSocketStream::connect(const char* server_ip, int port)
return srs_hijack_io_connect(io, server_ip, port);
}

// ISrsBufferReader
// ISrsReader
int SimpleSocketStream::read(void* buf, size_t size, ssize_t* nread)
{
srs_assert(io);
Expand Down
2 changes: 1 addition & 1 deletion trunk/src/libs/srs_lib_simple_socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class SimpleSocketStream : public ISrsProtocolReaderWriter
virtual srs_hijack_io_t hijack_io();
virtual int create_socket(srs_rtmp_t owner);
virtual int connect(const char* server, int port);
// ISrsBufferReader
// ISrsReader
public:
virtual int read(void* buf, size_t size, ssize_t* nread);
// ISrsProtocolReader
Expand Down
27 changes: 16 additions & 11 deletions trunk/src/protocol/srs_protocol_io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,20 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

/**
* the system io reader/writer architecture:
+---------------+ +--------------------+ +---------------+
| IBufferReader | | IStatistic | | IBufferWriter |
+---------------+ +--------------------+ +---------------+
| + read() | | + get_recv_bytes() | | + write() |
+------+--------+ | + get_send_bytes() | | + writev() |
/ \ +---+--------------+-+ +-------+-------+
| / \ / \ / \
| | | |
+------+------------------+-+ +-----+----------------+--+
+---------------+ +---------------+
| IStreamWriter | | IVectorWriter |
+---------------+ +---------------+
| + write() | | + writev() |
+-------------+-+ ++--------------+
+----------+ +--------------------+ /\ /\
| IReader | | IStatistic | \ /
+----------+ +--------------------+ V
| + read() | | + get_recv_bytes() | +------+----+
+------+---+ | + get_send_bytes() | | IWriter |
/ \ +---+--------------+-+ +-------+---+
| / \ / \ / \
| | | |
+------+-------------+------+ ++---------------------+--+
| IProtocolReader | | IProtocolWriter |
+---------------------------+ +-------------------------+
| + readfully() | | + set_send_timeout() |
Expand Down Expand Up @@ -80,7 +85,7 @@ class ISrsProtocolStatistic
/**
* the reader for the protocol to read from whatever channel.
*/
class ISrsProtocolReader : public virtual ISrsBufferReader, public virtual ISrsProtocolStatistic
class ISrsProtocolReader : public virtual ISrsReader, public virtual ISrsProtocolStatistic
{
public:
ISrsProtocolReader();
Expand Down Expand Up @@ -108,7 +113,7 @@ class ISrsProtocolReader : public virtual ISrsBufferReader, public virtual ISrsP
/**
* the writer for the protocol to write to whatever channel.
*/
class ISrsProtocolWriter : public virtual ISrsBufferWriter, public virtual ISrsProtocolStatistic
class ISrsProtocolWriter : public virtual ISrsWriter, public virtual ISrsProtocolStatistic
{
public:
ISrsProtocolWriter();
Expand Down
2 changes: 1 addition & 1 deletion trunk/src/protocol/srs_protocol_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ void SrsFastStream::skip(int size)
p += size;
}

int SrsFastStream::grow(ISrsBufferReader* reader, int required_size)
int SrsFastStream::grow(ISrsReader* reader, int required_size)
{
int ret = ERROR_SUCCESS;

Expand Down
4 changes: 2 additions & 2 deletions trunk/src/protocol/srs_protocol_stream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class IMergeReadHandler
* the buffer provices bytes cache for protocol. generally,
* protocol recv data from socket, put into buffer, decode to RTMP message.
* Usage:
* ISrsBufferReader* r = ......;
* ISrsReader* r = ......;
* SrsFastStream* fb = ......;
* fb->grow(r, 1024);
* char* header = fb->read_slice(100);
Expand Down Expand Up @@ -138,7 +138,7 @@ class SrsFastStream
* @return an int error code, error if required_size negative.
* @remark, we actually maybe read more than required_size, maybe 4k for example.
*/
virtual int grow(ISrsBufferReader* reader, int required_size);
virtual int grow(ISrsReader* reader, int required_size);
public:
#ifdef SRS_PERF_MERGED_READ
/**
Expand Down
2 changes: 1 addition & 1 deletion trunk/src/utest/srs_utest_kernel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <srs_kernel_file.hpp>
#include <srs_protocol_stream.hpp>

class MockBufferReader: public ISrsBufferReader
class MockBufferReader: public ISrsReader
{
private:
std::string str;
Expand Down

0 comments on commit d3bb8b6

Please sign in to comment.