winlin

refine srs buffer, min interfaces

... ... @@ -552,7 +552,7 @@ char* SrsHttpMessage::http_ts_send_buffer()
void SrsHttpMessage::reset()
{
_state = SrsHttpParseStateInit;
_body->clear();
_body->erase(_body->length());
_url = "";
}
... ... @@ -666,8 +666,8 @@ string SrsHttpMessage::body()
{
std::string b;
if (_body && !_body->empty()) {
b.append(_body->bytes(), _body->size());
if (_body && _body->length() > 0) {
b.append(_body->bytes(), _body->length());
}
return b;
... ... @@ -675,16 +675,12 @@ string SrsHttpMessage::body()
char* SrsHttpMessage::body_raw()
{
if (_body && !_body->empty()) {
return _body->bytes();
}
return NULL;
return _body? _body->bytes() : NULL;
}
int64_t SrsHttpMessage::body_size()
{
return (int64_t)_body->size();
return (int64_t)_body->length();
}
int64_t SrsHttpMessage::content_length()
... ...
... ... @@ -44,43 +44,38 @@ SrsBuffer::~SrsBuffer()
{
}
int SrsBuffer::size()
int SrsBuffer::length()
{
return (int)data.size();
}
bool SrsBuffer::empty()
{
return size() <= 0;
int len = (int)data.size();
srs_assert(len >= 0);
return len;
}
char* SrsBuffer::bytes()
{
return &data.at(0);
return (length() == 0)? NULL : &data.at(0);
}
void SrsBuffer::erase(int _size)
void SrsBuffer::erase(int size)
{
if (_size == size()) {
clear();
srs_assert(size > 0);
if (size == length()) {
data.clear();
return;
}
data.erase(data.begin(), data.begin() + _size);
}
void SrsBuffer::clear()
{
data.clear();
data.erase(data.begin(), data.begin() + size);
}
void SrsBuffer::append(const char* bytes, int size)
{
srs_assert(size > 0);
data.insert(data.end(), bytes, bytes + size);
}
int SrsBuffer::ensure_buffer_bytes(ISrsBufferReader* skt, int required_size)
int SrsBuffer::grow(ISrsBufferReader* reader, int required_size)
{
int ret = ERROR_SUCCESS;
... ... @@ -90,11 +85,11 @@ int SrsBuffer::ensure_buffer_bytes(ISrsBufferReader* skt, int required_size)
return ret;
}
while (size() < required_size) {
while (length() < required_size) {
char buffer[SOCKET_READ_SIZE];
ssize_t nread;
if ((ret = skt->read(buffer, SOCKET_READ_SIZE, &nread)) != ERROR_SUCCESS) {
if ((ret = reader->read(buffer, SOCKET_READ_SIZE, &nread)) != ERROR_SUCCESS) {
return ret;
}
... ...
... ... @@ -50,7 +50,7 @@ public:
* protocol recv data from socket, put into buffer, decode to RTMP message.
* protocol encode RTMP message to bytes, put into buffer, send to socket.
*/
class SrsBuffer
class SrsBuffer
{
private:
std::vector<char> data;
... ... @@ -58,14 +58,34 @@ public:
SrsBuffer();
virtual ~SrsBuffer();
public:
virtual int size();
virtual bool empty();
/**
* get the length of buffer.
* never negative, empty if zero.
*/
virtual int length();
/**
* get the buffer bytes.
* @return the bytes, NULL if empty.
*/
virtual char* bytes();
/**
* erase size of bytes from begin.
* if size equals to length(), clear buffer.
* @param size
*/
virtual void erase(int size);
virtual void clear();
/**
* append specified bytes to buffer.
* @param size the size of bytes, assert positive.
*/
virtual void append(const char* bytes, int size);
public:
virtual int ensure_buffer_bytes(ISrsBufferReader* skt, int required_size);
/**
* grow buffer to the required size, loop to read from skt to fill.
* @param reader, read more bytes from reader to fill the buffer to required size.
* @param required_size, loop to fill to ensure buffer size to required.
*/
virtual int grow(ISrsBufferReader* reader, int required_size);
};
#endif
\ No newline at end of file
... ...
... ... @@ -869,7 +869,7 @@ int SrsProtocol::read_basic_header(char& fmt, int& cid, int& bh_size)
int ret = ERROR_SUCCESS;
int required_size = 1;
if ((ret = buffer->ensure_buffer_bytes(skt, required_size)) != ERROR_SUCCESS) {
if ((ret = buffer->grow(skt, required_size)) != ERROR_SUCCESS) {
if (ret != ERROR_SOCKET_TIMEOUT && !srs_is_client_gracefully_close(ret)) {
srs_error("read 1bytes basic header failed. required_size=%d, ret=%d", required_size, ret);
}
... ... @@ -889,7 +889,7 @@ int SrsProtocol::read_basic_header(char& fmt, int& cid, int& bh_size)
if (cid == 0) {
required_size = 2;
if ((ret = buffer->ensure_buffer_bytes(skt, required_size)) != ERROR_SUCCESS) {
if ((ret = buffer->grow(skt, required_size)) != ERROR_SUCCESS) {
if (ret != ERROR_SOCKET_TIMEOUT && !srs_is_client_gracefully_close(ret)) {
srs_error("read 2bytes basic header failed. required_size=%d, ret=%d", required_size, ret);
}
... ... @@ -902,7 +902,7 @@ int SrsProtocol::read_basic_header(char& fmt, int& cid, int& bh_size)
srs_verbose("%dbytes basic header parsed. fmt=%d, cid=%d", bh_size, fmt, cid);
} else if (cid == 1) {
required_size = 3;
if ((ret = buffer->ensure_buffer_bytes(skt, 3)) != ERROR_SUCCESS) {
if ((ret = buffer->grow(skt, 3)) != ERROR_SUCCESS) {
if (ret != ERROR_SOCKET_TIMEOUT && !srs_is_client_gracefully_close(ret)) {
srs_error("read 3bytes basic header failed. required_size=%d, ret=%d", required_size, ret);
}
... ... @@ -982,7 +982,7 @@ int SrsProtocol::read_message_header(SrsChunkStream* chunk, char fmt, int bh_siz
srs_verbose("calc chunk message header size. fmt=%d, mh_size=%d", fmt, mh_size);
int required_size = bh_size + mh_size;
if ((ret = buffer->ensure_buffer_bytes(skt, required_size)) != ERROR_SUCCESS) {
if ((ret = buffer->grow(skt, required_size)) != ERROR_SUCCESS) {
if (ret != ERROR_SOCKET_TIMEOUT && !srs_is_client_gracefully_close(ret)) {
srs_error("read %dbytes message header failed. required_size=%d, ret=%d", mh_size, required_size, ret);
}
... ... @@ -1092,7 +1092,7 @@ int SrsProtocol::read_message_header(SrsChunkStream* chunk, char fmt, int bh_siz
mh_size += 4;
required_size = bh_size + mh_size;
srs_verbose("read header ext time. fmt=%d, ext_time=%d, mh_size=%d", fmt, chunk->extended_timestamp, mh_size);
if ((ret = buffer->ensure_buffer_bytes(skt, required_size)) != ERROR_SUCCESS) {
if ((ret = buffer->grow(skt, required_size)) != ERROR_SUCCESS) {
if (ret != ERROR_SOCKET_TIMEOUT && !srs_is_client_gracefully_close(ret)) {
srs_error("read %dbytes message header failed. required_size=%d, ret=%d", mh_size, required_size, ret);
}
... ... @@ -1219,7 +1219,7 @@ int SrsProtocol::read_message_payload(SrsChunkStream* chunk, int bh_size, int mh
// read payload to buffer
int required_size = bh_size + mh_size + payload_size;
if ((ret = buffer->ensure_buffer_bytes(skt, required_size)) != ERROR_SUCCESS) {
if ((ret = buffer->grow(skt, required_size)) != ERROR_SUCCESS) {
if (ret != ERROR_SOCKET_TIMEOUT && !srs_is_client_gracefully_close(ret)) {
srs_error("read payload failed. required_size=%d, ret=%d", required_size, ret);
}
... ...