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

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

Issue 2779233002: Add first part of the network_tester functionality. (Closed)
Patch Set: Add proto files 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::HI);
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 switch (packet.type()) {
52 case NetworkTesterPacket::HI: {
53 local_test_done_ = false;
54 remote_test_done_ = false;
55 NetworkTesterPacket packet;
56 packet.set_type(NetworkTesterPacket::START_SEND);
57 udp_transport_->SetRemoteAddress(remote_addr);
58 SendData(packet, rtc::Optional<size_t>());
59 packet_sender_.reset(new PacketSender(this, config_file_path_));
60 packet_sender_->StartSending();
61 break;
62 }
63 case NetworkTesterPacket::START_SEND: {
64 packet_sender_.reset(new PacketSender(this, config_file_path_));
65 packet_sender_->StartSending();
66 break;
67 }
68 case NetworkTesterPacket::TEST_DATA: {
69 packet.set_arrival_timestamp(packet_time.timestamp);
70 packet.set_packet_size(len);
71 // log packet
72 break;
73 }
74 case NetworkTesterPacket::TEST_DONE: {
75 remote_test_done_ = true;
76 break;
77 }
78 default: { RTC_NOTREACHED(); }
79 }
80 }
81
82 void TestController::SendData(const NetworkTesterPacket& packet,
83 rtc::Optional<size_t> data_size) {
84 size_t packet_size = packet.ByteSize();
85 send_data_[0] = packet_size;
86 packet.SerializeToArray(&send_data_[1], std::numeric_limits<char>::max());
87 if (data_size && *data_size > packet_size)
88 packet_size = *data_size;
89 udp_transport_->SendPacket(send_data_.data(), packet_size + 1,
90 rtc::PacketOptions(), 0);
91 }
92
93 void TestController::OnTestDone() {
94 NetworkTesterPacket packet;
95 packet.set_type(NetworkTesterPacket::TEST_DONE);
96 SendData(packet, rtc::Optional<size_t>());
97 local_test_done_ = true;
98 }
99
100 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698