OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 499 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
510 | 510 |
511 return flock(fileno(file_), LOCK_UN) == 0; | 511 return flock(fileno(file_), LOCK_UN) == 0; |
512 } | 512 } |
513 | 513 |
514 #endif | 514 #endif |
515 | 515 |
516 void FileStream::DoClose() { | 516 void FileStream::DoClose() { |
517 fclose(file_); | 517 fclose(file_); |
518 } | 518 } |
519 | 519 |
| 520 CircularFileStream::CircularFileStream(size_t max_size) |
| 521 : max_write_size_(max_size), |
| 522 position_(0), |
| 523 marked_position_(max_size / 2), |
| 524 last_write_position_(0), |
| 525 read_segment_(READ_LATEST), |
| 526 read_segment_available_(0) { |
| 527 } |
| 528 |
| 529 bool CircularFileStream::Open(const std::string& filename, |
| 530 const char* mode, |
| 531 int* error) { |
| 532 if (!FileStream::Open(filename.c_str(), mode, error)) |
| 533 return false; |
| 534 |
| 535 if (strchr(mode, "r") != NULL) { // Opened in read mode. |
| 536 // Check if the buffer has been overwritten and determine how to read the |
| 537 // log in time sequence. |
| 538 size_t file_size; |
| 539 GetSize(&file_size); |
| 540 if (file_size == position_) { |
| 541 // The buffer has not been overwritten yet. Read 0 .. file_size |
| 542 read_segment_ = READ_LATEST; |
| 543 read_segment_available_ = file_size; |
| 544 } else { |
| 545 // The buffer has been over written. There are three segments: The first |
| 546 // one is 0 .. marked_position_, which is the marked earliest log. The |
| 547 // second one is position_ .. file_size, which is the middle log. The |
| 548 // last one is marked_position_ .. position_, which is the latest log. |
| 549 read_segment_ = READ_MARKED; |
| 550 read_segment_available_ = marked_position_; |
| 551 last_write_position_ = position_; |
| 552 } |
| 553 |
| 554 // Read from the beginning. |
| 555 position_ = 0; |
| 556 SetPosition(position_); |
| 557 } |
| 558 |
| 559 return true; |
| 560 } |
| 561 |
| 562 StreamResult CircularFileStream::Read(void* buffer, |
| 563 size_t buffer_len, |
| 564 size_t* read, |
| 565 int* error) { |
| 566 if (read_segment_available_ == 0) { |
| 567 size_t file_size; |
| 568 switch (read_segment_) { |
| 569 case READ_MARKED: // Finished READ_MARKED and start READ_MIDDLE. |
| 570 read_segment_ = READ_MIDDLE; |
| 571 position_ = last_write_position_; |
| 572 SetPosition(position_); |
| 573 GetSize(&file_size); |
| 574 read_segment_available_ = file_size - position_; |
| 575 break; |
| 576 |
| 577 case READ_MIDDLE: // Finished READ_MIDDLE and start READ_LATEST. |
| 578 read_segment_ = READ_LATEST; |
| 579 position_ = marked_position_; |
| 580 SetPosition(position_); |
| 581 read_segment_available_ = last_write_position_ - position_; |
| 582 break; |
| 583 |
| 584 default: // Finished READ_LATEST and return EOS. |
| 585 return rtc::SR_EOS; |
| 586 } |
| 587 } |
| 588 |
| 589 size_t local_read; |
| 590 if (!read) |
| 591 read = &local_read; |
| 592 |
| 593 size_t to_read = std::min(buffer_len, read_segment_available_); |
| 594 rtc::StreamResult result = |
| 595 rtc::FileStream::Read(buffer, to_read, read, error); |
| 596 if (result == rtc::SR_SUCCESS) { |
| 597 read_segment_available_ -= *read; |
| 598 position_ += *read; |
| 599 } |
| 600 return result; |
| 601 } |
| 602 |
| 603 StreamResult CircularFileStream::Write(const void* data, |
| 604 size_t data_len, |
| 605 size_t* written, |
| 606 int* error) { |
| 607 if (position_ >= max_write_size_) { |
| 608 ASSERT(position_ == max_write_size_); |
| 609 position_ = marked_position_; |
| 610 SetPosition(position_); |
| 611 } |
| 612 |
| 613 size_t local_written; |
| 614 if (!written) |
| 615 written = &local_written; |
| 616 |
| 617 size_t to_eof = max_write_size_ - position_; |
| 618 size_t to_write = std::min(data_len, to_eof); |
| 619 rtc::StreamResult result = |
| 620 rtc::FileStream::Write(data, to_write, written, error); |
| 621 if (result == rtc::SR_SUCCESS) { |
| 622 position_ += *written; |
| 623 } |
| 624 return result; |
| 625 } |
| 626 |
520 /////////////////////////////////////////////////////////////////////////////// | 627 /////////////////////////////////////////////////////////////////////////////// |
521 // MemoryStream | 628 // MemoryStream |
522 /////////////////////////////////////////////////////////////////////////////// | 629 /////////////////////////////////////////////////////////////////////////////// |
523 | 630 |
524 MemoryStreamBase::MemoryStreamBase() | 631 MemoryStreamBase::MemoryStreamBase() |
525 : buffer_(NULL), buffer_length_(0), data_length_(0), | 632 : buffer_(NULL), buffer_length_(0), data_length_(0), |
526 seek_position_(0) { | 633 seek_position_(0) { |
527 } | 634 } |
528 | 635 |
529 StreamState MemoryStreamBase::GetState() const { | 636 StreamState MemoryStreamBase::GetState() const { |
(...skipping 587 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1117 | 1224 |
1118 if (data_len) { | 1225 if (data_len) { |
1119 *data_len = 0; | 1226 *data_len = 0; |
1120 } | 1227 } |
1121 return SR_SUCCESS; | 1228 return SR_SUCCESS; |
1122 } | 1229 } |
1123 | 1230 |
1124 /////////////////////////////////////////////////////////////////////////////// | 1231 /////////////////////////////////////////////////////////////////////////////// |
1125 | 1232 |
1126 } // namespace rtc | 1233 } // namespace rtc |
OLD | NEW |