michaelt | fcea39d | 2017-04-20 05:39:30 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 10 | #include "rtc_tools/network_tester/packet_logger.h" |
michaelt | fcea39d | 2017-04-20 05:39:30 -0700 | [diff] [blame] | 11 | |
Mirko Bonadei | e45c688 | 2019-02-16 09:59:29 +0100 | [diff] [blame] | 12 | #include <string> |
| 13 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 14 | #include "rtc_base/checks.h" |
michaelt | fcea39d | 2017-04-20 05:39:30 -0700 | [diff] [blame] | 15 | |
| 16 | namespace webrtc { |
| 17 | |
| 18 | PacketLogger::PacketLogger(const std::string& log_file_path) |
| 19 | : packet_logger_stream_(log_file_path, |
| 20 | std::ios_base::out | std::ios_base::binary) { |
| 21 | RTC_DCHECK(packet_logger_stream_.is_open()); |
| 22 | RTC_DCHECK(packet_logger_stream_.good()); |
| 23 | } |
| 24 | |
| 25 | PacketLogger::~PacketLogger() = default; |
| 26 | |
| 27 | void PacketLogger::LogPacket(const NetworkTesterPacket& packet) { |
| 28 | // The protobuffer message will be saved in the following format to the file: |
| 29 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 30 | // | 1 byte | X byte | 1 byte | ... | X byte | |
| 31 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 32 | // | Size of the next | proto | Size of the next | ... | proto | |
| 33 | // | proto message | message | proto message | | message | |
| 34 | // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
Mirko Bonadei | e45c688 | 2019-02-16 09:59:29 +0100 | [diff] [blame] | 35 | std::string packet_data; |
michaelt | fcea39d | 2017-04-20 05:39:30 -0700 | [diff] [blame] | 36 | packet.SerializeToString(&packet_data); |
| 37 | RTC_DCHECK_LE(packet_data.length(), 255); |
| 38 | RTC_DCHECK_GE(packet_data.length(), 0); |
| 39 | char proto_size = packet_data.length(); |
| 40 | packet_logger_stream_.write(&proto_size, sizeof(proto_size)); |
| 41 | packet_logger_stream_.write(packet_data.data(), packet_data.length()); |
| 42 | } |
| 43 | |
| 44 | } // namespace webrtc |