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

Side by Side Diff: webrtc/base/testclient.h

Issue 2859373003: Refactor TestClient to use std::unique_ptr, and fix VirtualSocketServerTest leaks. (Closed)
Patch Set: Created 3 years, 7 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
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
11 #ifndef WEBRTC_BASE_TESTCLIENT_H_ 11 #ifndef WEBRTC_BASE_TESTCLIENT_H_
12 #define WEBRTC_BASE_TESTCLIENT_H_ 12 #define WEBRTC_BASE_TESTCLIENT_H_
13 13
14 #include <memory>
14 #include <vector> 15 #include <vector>
15 #include "webrtc/base/asyncudpsocket.h" 16 #include "webrtc/base/asyncudpsocket.h"
16 #include "webrtc/base/constructormagic.h" 17 #include "webrtc/base/constructormagic.h"
17 #include "webrtc/base/criticalsection.h" 18 #include "webrtc/base/criticalsection.h"
18 19
19 namespace rtc { 20 namespace rtc {
20 21
21 // A simple client that can send TCP or UDP data and check that it receives 22 // A simple client that can send TCP or UDP data and check that it receives
22 // what it expects to receive. Useful for testing server functionality. 23 // what it expects to receive. Useful for testing server functionality.
23 class TestClient : public sigslot::has_slots<> { 24 class TestClient : public sigslot::has_slots<> {
(...skipping 11 matching lines...) Expand all
35 char* buf; 36 char* buf;
36 size_t size; 37 size_t size;
37 PacketTime packet_time; 38 PacketTime packet_time;
38 }; 39 };
39 40
40 // Default timeout for NextPacket reads. 41 // Default timeout for NextPacket reads.
41 static const int kTimeoutMs = 5000; 42 static const int kTimeoutMs = 5000;
42 43
43 // Creates a client that will send and receive with the given socket and 44 // Creates a client that will send and receive with the given socket and
44 // will post itself messages with the given thread. 45 // will post itself messages with the given thread.
45 explicit TestClient(AsyncPacketSocket* socket); 46 explicit TestClient(std::unique_ptr<AsyncPacketSocket> socket);
46 ~TestClient() override; 47 ~TestClient() override;
47 48
48 SocketAddress address() const { return socket_->GetLocalAddress(); } 49 SocketAddress address() const { return socket_->GetLocalAddress(); }
49 SocketAddress remote_address() const { return socket_->GetRemoteAddress(); } 50 SocketAddress remote_address() const { return socket_->GetRemoteAddress(); }
50 51
51 // Checks that the socket moves to the specified connect state. 52 // Checks that the socket moves to the specified connect state.
52 bool CheckConnState(AsyncPacketSocket::State state); 53 bool CheckConnState(AsyncPacketSocket::State state);
53 54
54 // Checks that the socket is connected to the remote side. 55 // Checks that the socket is connected to the remote side.
55 bool CheckConnected() { 56 bool CheckConnected() {
56 return CheckConnState(AsyncPacketSocket::STATE_CONNECTED); 57 return CheckConnState(AsyncPacketSocket::STATE_CONNECTED);
57 } 58 }
58 59
59 // Sends using the clients socket. 60 // Sends using the clients socket.
60 int Send(const char* buf, size_t size); 61 int Send(const char* buf, size_t size);
61 62
62 // Sends using the clients socket to the given destination. 63 // Sends using the clients socket to the given destination.
63 int SendTo(const char* buf, size_t size, const SocketAddress& dest); 64 int SendTo(const char* buf, size_t size, const SocketAddress& dest);
64 65
65 // Returns the next packet received by the client or 0 if none is received 66 // Returns the next packet received by the client or 0 if none is received
66 // within the specified timeout. The caller must delete the packet 67 // within the specified timeout. The caller must delete the packet
67 // when done with it. 68 // when done with it.
kwiberg-webrtc 2017/05/05 11:04:42 The last sentence of the comment is no longer nece
nisse-webrtc 2017/05/05 12:12:56 Done.
68 Packet* NextPacket(int timeout_ms); 69 std::unique_ptr<Packet> NextPacket(int timeout_ms);
69 70
70 // Checks that the next packet has the given contents. Returns the remote 71 // Checks that the next packet has the given contents. Returns the remote
71 // address that the packet was sent from. 72 // address that the packet was sent from.
72 bool CheckNextPacket(const char* buf, size_t len, SocketAddress* addr); 73 bool CheckNextPacket(const char* buf, size_t len, SocketAddress* addr);
73 74
74 // Checks that no packets have arrived or will arrive in the next second. 75 // Checks that no packets have arrived or will arrive in the next second.
75 bool CheckNoPacket(); 76 bool CheckNoPacket();
76 77
77 int GetError(); 78 int GetError();
78 int SetOption(Socket::Option opt, int value); 79 int SetOption(Socket::Option opt, int value);
79 80
80 bool ready_to_send() const { return ready_to_send_count() > 0; } 81 bool ready_to_send() const { return ready_to_send_count() > 0; }
81 82
82 // How many times SignalReadyToSend has been fired. 83 // How many times SignalReadyToSend has been fired.
83 int ready_to_send_count() const { return ready_to_send_count_; } 84 int ready_to_send_count() const { return ready_to_send_count_; }
84 85
85 private: 86 private:
86 // Timeout for reads when no packet is expected. 87 // Timeout for reads when no packet is expected.
87 static const int kNoPacketTimeoutMs = 1000; 88 static const int kNoPacketTimeoutMs = 1000;
88 // Workaround for the fact that AsyncPacketSocket::GetConnState doesn't exist. 89 // Workaround for the fact that AsyncPacketSocket::GetConnState doesn't exist.
89 Socket::ConnState GetState(); 90 Socket::ConnState GetState();
90 // Slot for packets read on the socket. 91 // Slot for packets read on the socket.
91 void OnPacket(AsyncPacketSocket* socket, const char* buf, size_t len, 92 void OnPacket(AsyncPacketSocket* socket, const char* buf, size_t len,
92 const SocketAddress& remote_addr, 93 const SocketAddress& remote_addr,
93 const PacketTime& packet_time); 94 const PacketTime& packet_time);
94 void OnReadyToSend(AsyncPacketSocket* socket); 95 void OnReadyToSend(AsyncPacketSocket* socket);
95 bool CheckTimestamp(int64_t packet_timestamp); 96 bool CheckTimestamp(int64_t packet_timestamp);
96 97
97 CriticalSection crit_; 98 CriticalSection crit_;
98 AsyncPacketSocket* socket_; 99 std::unique_ptr<AsyncPacketSocket> socket_;
99 std::vector<Packet*>* packets_; 100 std::vector<std::unique_ptr<Packet>> packets_;
100 int ready_to_send_count_ = 0; 101 int ready_to_send_count_ = 0;
101 int64_t prev_packet_timestamp_; 102 int64_t prev_packet_timestamp_;
102 RTC_DISALLOW_COPY_AND_ASSIGN(TestClient); 103 RTC_DISALLOW_COPY_AND_ASSIGN(TestClient);
103 }; 104 };
104 105
105 } // namespace rtc 106 } // namespace rtc
106 107
107 #endif // WEBRTC_BASE_TESTCLIENT_H_ 108 #endif // WEBRTC_BASE_TESTCLIENT_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698