Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(631)

Side by Side Diff: webrtc/tools/network_tester/test_controller.cc

Issue 2779233002: Add first part of the network_tester functionality. (Closed)
Patch Set: Respond to comments Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
11 #include "webrtc/tools/network_tester/test_controller.h"
12
13 namespace webrtc {
14
15 TestController::TestController(int min_port,
16 int max_port,
17 const std::string& config_file_path)
18 : config_file_path_(config_file_path),
19 local_test_done_(false),
20 remote_test_done_(false) {
21 auto socket =
22 std::unique_ptr<rtc::AsyncPacketSocket>(socket_factory_.CreateUdpSocket(
23 rtc::SocketAddress(rtc::GetAnyIP(AF_INET), 0), min_port, max_port));
24 socket->SignalReadPacket.connect(this, &TestController::OnReadPacket);
25 udp_transport_.reset(
26 new cricket::UdpTransport("signaling transport", std::move(socket)));
27 }
28
29 void TestController::SendConnectTo(const std::string& hostname, int port) {
30 udp_transport_->SetRemoteAddress(rtc::SocketAddress(hostname, port));
31 NetworkTesterPacket packet;
32 packet.set_type(NetworkTesterPacket::HAND_SHAKING);
33 SendData(packet, rtc::Optional<size_t>());
34 local_test_done_ = false;
35 remote_test_done_ = false;
36 }
37
38 void TestController::Run() {
39 rtc::Thread::Current()->ProcessMessages(0);
40 }
41
42 void TestController::OnReadPacket(rtc::AsyncPacketSocket* socket,
43 const char* data,
44 size_t len,
45 const rtc::SocketAddress& remote_addr,
46 const rtc::PacketTime& packet_time) {
47 size_t packet_size = data[0];
48 std::string receive_data(&data[1], packet_size);
49 NetworkTesterPacket packet;
50 packet.ParseFromString(receive_data);
51 RTC_CHECK(packet.has_type());
52 switch (packet.type()) {
53 case NetworkTesterPacket::HAND_SHAKING: {
54 local_test_done_ = false;
55 remote_test_done_ = false;
56 NetworkTesterPacket packet;
57 packet.set_type(NetworkTesterPacket::TEST_START);
58 udp_transport_->SetRemoteAddress(remote_addr);
59 SendData(packet, rtc::Optional<size_t>());
60 packet_sender_.reset(new PacketSender(this, config_file_path_));
61 packet_sender_->StartSending();
62 break;
63 }
64 case NetworkTesterPacket::TEST_START: {
65 packet_sender_.reset(new PacketSender(this, config_file_path_));
66 packet_sender_->StartSending();
67 break;
68 }
69 case NetworkTesterPacket::TEST_DATA: {
70 packet.set_arrival_timestamp(packet_time.timestamp);
71 packet.set_packet_size(len);
72 // log packet
73 break;
74 }
75 case NetworkTesterPacket::TEST_DONE: {
76 remote_test_done_ = true;
77 break;
78 }
79 default: { RTC_NOTREACHED(); }
80 }
81 }
82
83 void TestController::SendData(const NetworkTesterPacket& packet,
84 rtc::Optional<size_t> data_size) {
85 size_t packet_size = packet.ByteSize();
86 send_data_[0] = packet_size;
87 packet.SerializeToArray(&send_data_[1], std::numeric_limits<char>::max());
88 if (data_size && *data_size > packet_size)
89 packet_size = *data_size;
90 udp_transport_->SendPacket(send_data_.data(), packet_size + 1,
91 rtc::PacketOptions(), 0);
92 }
93
94 void TestController::OnTestDone() {
95 NetworkTesterPacket packet;
96 packet.set_type(NetworkTesterPacket::TEST_DONE);
97 SendData(packet, rtc::Optional<size_t>());
98 local_test_done_ = true;
99 }
100
101 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698