winlin

refine fast buffer, check available bytes to read.

@@ -60,7 +60,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -60,7 +60,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
60 #define ERROR_SYSTEM_PACKET_INVALID 1019 60 #define ERROR_SYSTEM_PACKET_INVALID 1019
61 #define ERROR_SYSTEM_CLIENT_INVALID 1020 61 #define ERROR_SYSTEM_CLIENT_INVALID 1020
62 #define ERROR_SYSTEM_ASSERT_FAILED 1021 62 #define ERROR_SYSTEM_ASSERT_FAILED 1021
63 -#define ERROR_SYSTEM_SIZE_NEGATIVE 1022 63 +#define ERROR_READER_BUFFER_OVERFLOW 1022
64 #define ERROR_SYSTEM_CONFIG_INVALID 1023 64 #define ERROR_SYSTEM_CONFIG_INVALID 1023
65 #define ERROR_SYSTEM_CONFIG_DIRECTIVE 1024 65 #define ERROR_SYSTEM_CONFIG_DIRECTIVE 1024
66 #define ERROR_SYSTEM_CONFIG_BLOCK_START 1025 66 #define ERROR_SYSTEM_CONFIG_BLOCK_START 1025
@@ -134,7 +134,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -134,7 +134,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
134 #define ERROR_OpenSslSha256DigestSize 2037 134 #define ERROR_OpenSslSha256DigestSize 2037
135 #define ERROR_OpenSslGetPeerPublicKey 2038 135 #define ERROR_OpenSslGetPeerPublicKey 2038
136 #define ERROR_OpenSslComputeSharedKey 2039 136 #define ERROR_OpenSslComputeSharedKey 2039
137 -#define ERROR_RTMP_BUFFER_OVERFLOW 2040  
138 // 137 //
139 // system control message, 138 // system control message,
140 // not an error, but special control logic. 139 // not an error, but special control logic.
@@ -169,18 +169,18 @@ int SrsFastBuffer::grow(ISrsBufferReader* reader, int required_size) @@ -169,18 +169,18 @@ int SrsFastBuffer::grow(ISrsBufferReader* reader, int required_size)
169 { 169 {
170 int ret = ERROR_SUCCESS; 170 int ret = ERROR_SUCCESS;
171 171
172 - if (required_size < 0) {  
173 - ret = ERROR_SYSTEM_SIZE_NEGATIVE;  
174 - srs_error("size is negative. size=%d, ret=%d", required_size, ret); 172 + // generally the required size is ok.
  173 + if (end - p >= required_size) {
175 return ret; 174 return ret;
176 } 175 }
177 176
178 - // when read payload and need to grow, reset buffer.  
179 - // or there is no space to read. 177 + // must be positive.
  178 + srs_assert(required_size > 0);
  179 +
  180 + // when read payload or there is no space to read,
  181 + // reset the buffer with exists bytes.
180 int max_to_read = buffer + nb_buffer - end; 182 int max_to_read = buffer + nb_buffer - end;
181 - if (end - p < required_size  
182 - && (required_size > SRS_RTMP_MAX_MESSAGE_HEADER || max_to_read < required_size)  
183 - ) { 183 + if (required_size > SRS_RTMP_MAX_MESSAGE_HEADER || max_to_read < required_size) {
184 int nb_cap = end - p; 184 int nb_cap = end - p;
185 srs_verbose("move fast buffer %d bytes", nb_cap); 185 srs_verbose("move fast buffer %d bytes", nb_cap);
186 if (nb_cap < nb_buffer) { 186 if (nb_cap < nb_buffer) {
@@ -190,16 +190,16 @@ int SrsFastBuffer::grow(ISrsBufferReader* reader, int required_size) @@ -190,16 +190,16 @@ int SrsFastBuffer::grow(ISrsBufferReader* reader, int required_size)
190 } 190 }
191 } 191 }
192 192
193 - while (end - p < required_size) {  
194 - // the max to read is the left bytes. 193 + // directly check the available bytes to read in buffer.
195 max_to_read = buffer + nb_buffer - end; 194 max_to_read = buffer + nb_buffer - end;
196 -  
197 - if (max_to_read <= 0) {  
198 - ret = ERROR_RTMP_BUFFER_OVERFLOW; 195 + if (max_to_read < required_size) {
  196 + ret = ERROR_READER_BUFFER_OVERFLOW;
199 srs_error("buffer overflow, required=%d, max=%d, ret=%d", required_size, nb_buffer, ret); 197 srs_error("buffer overflow, required=%d, max=%d, ret=%d", required_size, nb_buffer, ret);
200 return ret; 198 return ret;
201 } 199 }
202 200
  201 + // buffer is ok, read required size of bytes.
  202 + while (end - p < required_size) {
203 ssize_t nread; 203 ssize_t nread;
204 if ((ret = reader->read(end, max_to_read, &nread)) != ERROR_SUCCESS) { 204 if ((ret = reader->read(end, max_to_read, &nread)) != ERROR_SUCCESS) {
205 return ret; 205 return ret;