• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
10 #include "rtc_tools/network_tester/packet_logger.h"
11 
12 #include <string>
13 
14 #include "rtc_base/checks.h"
15 
16 namespace webrtc {
17 
PacketLogger(const std::string & log_file_path)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 
LogPacket(const NetworkTesterPacket & packet)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   // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
35   std::string packet_data;
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
45