OLD | NEW |
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 Loading... |
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 null if none is received |
66 // within the specified timeout. The caller must delete the packet | 67 // within the specified timeout. |
67 // when done with it. | 68 std::unique_ptr<Packet> NextPacket(int timeout_ms); |
68 Packet* NextPacket(int timeout_ms); | |
69 | 69 |
70 // Checks that the next packet has the given contents. Returns the remote | 70 // Checks that the next packet has the given contents. Returns the remote |
71 // address that the packet was sent from. | 71 // address that the packet was sent from. |
72 bool CheckNextPacket(const char* buf, size_t len, SocketAddress* addr); | 72 bool CheckNextPacket(const char* buf, size_t len, SocketAddress* addr); |
73 | 73 |
74 // Checks that no packets have arrived or will arrive in the next second. | 74 // Checks that no packets have arrived or will arrive in the next second. |
75 bool CheckNoPacket(); | 75 bool CheckNoPacket(); |
76 | 76 |
77 int GetError(); | 77 int GetError(); |
78 int SetOption(Socket::Option opt, int value); | 78 int SetOption(Socket::Option opt, int value); |
79 | 79 |
80 bool ready_to_send() const { return ready_to_send_count() > 0; } | 80 bool ready_to_send() const { return ready_to_send_count() > 0; } |
81 | 81 |
82 // How many times SignalReadyToSend has been fired. | 82 // How many times SignalReadyToSend has been fired. |
83 int ready_to_send_count() const { return ready_to_send_count_; } | 83 int ready_to_send_count() const { return ready_to_send_count_; } |
84 | 84 |
85 private: | 85 private: |
86 // Timeout for reads when no packet is expected. | 86 // Timeout for reads when no packet is expected. |
87 static const int kNoPacketTimeoutMs = 1000; | 87 static const int kNoPacketTimeoutMs = 1000; |
88 // Workaround for the fact that AsyncPacketSocket::GetConnState doesn't exist. | 88 // Workaround for the fact that AsyncPacketSocket::GetConnState doesn't exist. |
89 Socket::ConnState GetState(); | 89 Socket::ConnState GetState(); |
90 // Slot for packets read on the socket. | 90 // Slot for packets read on the socket. |
91 void OnPacket(AsyncPacketSocket* socket, const char* buf, size_t len, | 91 void OnPacket(AsyncPacketSocket* socket, const char* buf, size_t len, |
92 const SocketAddress& remote_addr, | 92 const SocketAddress& remote_addr, |
93 const PacketTime& packet_time); | 93 const PacketTime& packet_time); |
94 void OnReadyToSend(AsyncPacketSocket* socket); | 94 void OnReadyToSend(AsyncPacketSocket* socket); |
95 bool CheckTimestamp(int64_t packet_timestamp); | 95 bool CheckTimestamp(int64_t packet_timestamp); |
96 | 96 |
97 CriticalSection crit_; | 97 CriticalSection crit_; |
98 AsyncPacketSocket* socket_; | 98 std::unique_ptr<AsyncPacketSocket> socket_; |
99 std::vector<Packet*>* packets_; | 99 std::vector<std::unique_ptr<Packet>> packets_; |
100 int ready_to_send_count_ = 0; | 100 int ready_to_send_count_ = 0; |
101 int64_t prev_packet_timestamp_; | 101 int64_t prev_packet_timestamp_; |
102 RTC_DISALLOW_COPY_AND_ASSIGN(TestClient); | 102 RTC_DISALLOW_COPY_AND_ASSIGN(TestClient); |
103 }; | 103 }; |
104 | 104 |
105 } // namespace rtc | 105 } // namespace rtc |
106 | 106 |
107 #endif // WEBRTC_BASE_TESTCLIENT_H_ | 107 #endif // WEBRTC_BASE_TESTCLIENT_H_ |
OLD | NEW |