blob: b4703828eb84a36ccec117254dc5489b7ae9ebc2 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
2 * Copyright 2004 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#ifndef WEBRTC_BASE_TESTCLIENT_H_
12#define WEBRTC_BASE_TESTCLIENT_H_
13
nisse32f25052017-05-08 01:57:18 -070014#include <memory>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000015#include <vector>
16#include "webrtc/base/asyncudpsocket.h"
kwiberg4485ffb2016-04-26 08:14:39 -070017#include "webrtc/base/constructormagic.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000018#include "webrtc/base/criticalsection.h"
19
20namespace rtc {
21
22// A simple client that can send TCP or UDP data and check that it receives
23// what it expects to receive. Useful for testing server functionality.
24class TestClient : public sigslot::has_slots<> {
25 public:
26 // Records the contents of a packet that was received.
27 struct Packet {
Stefan Holmer9131efd2016-05-23 18:19:26 +020028 Packet(const SocketAddress& a,
29 const char* b,
30 size_t s,
31 const PacketTime& packet_time);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000032 Packet(const Packet& p);
33 virtual ~Packet();
34
35 SocketAddress addr;
36 char* buf;
37 size_t size;
Stefan Holmer9131efd2016-05-23 18:19:26 +020038 PacketTime packet_time;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000039 };
40
jlmiller@webrtc.orgec499be2015-02-07 22:37:59 +000041 // Default timeout for NextPacket reads.
42 static const int kTimeoutMs = 5000;
43
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000044 // Creates a client that will send and receive with the given socket and
45 // will post itself messages with the given thread.
nisse32f25052017-05-08 01:57:18 -070046 explicit TestClient(std::unique_ptr<AsyncPacketSocket> socket);
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000047 ~TestClient() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000048
49 SocketAddress address() const { return socket_->GetLocalAddress(); }
50 SocketAddress remote_address() const { return socket_->GetRemoteAddress(); }
51
52 // Checks that the socket moves to the specified connect state.
53 bool CheckConnState(AsyncPacketSocket::State state);
54
55 // Checks that the socket is connected to the remote side.
56 bool CheckConnected() {
57 return CheckConnState(AsyncPacketSocket::STATE_CONNECTED);
58 }
59
60 // Sends using the clients socket.
61 int Send(const char* buf, size_t size);
62
63 // Sends using the clients socket to the given destination.
64 int SendTo(const char* buf, size_t size, const SocketAddress& dest);
65
nisse32f25052017-05-08 01:57:18 -070066 // Returns the next packet received by the client or null if none is received
67 // within the specified timeout.
68 std::unique_ptr<Packet> NextPacket(int timeout_ms);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000069
70 // Checks that the next packet has the given contents. Returns the remote
71 // address that the packet was sent from.
72 bool CheckNextPacket(const char* buf, size_t len, SocketAddress* addr);
73
74 // Checks that no packets have arrived or will arrive in the next second.
75 bool CheckNoPacket();
76
77 int GetError();
78 int SetOption(Socket::Option opt, int value);
79
Taylor Brandstettere7536412016-09-09 13:16:15 -070080 bool ready_to_send() const { return ready_to_send_count() > 0; }
81
82 // How many times SignalReadyToSend has been fired.
83 int ready_to_send_count() const { return ready_to_send_count_; }
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000084
85 private:
jlmiller@webrtc.orgec499be2015-02-07 22:37:59 +000086 // Timeout for reads when no packet is expected.
87 static const int kNoPacketTimeoutMs = 1000;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000088 // Workaround for the fact that AsyncPacketSocket::GetConnState doesn't exist.
89 Socket::ConnState GetState();
90 // Slot for packets read on the socket.
91 void OnPacket(AsyncPacketSocket* socket, const char* buf, size_t len,
92 const SocketAddress& remote_addr,
93 const PacketTime& packet_time);
94 void OnReadyToSend(AsyncPacketSocket* socket);
Stefan Holmer9131efd2016-05-23 18:19:26 +020095 bool CheckTimestamp(int64_t packet_timestamp);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000096
97 CriticalSection crit_;
nisse32f25052017-05-08 01:57:18 -070098 std::unique_ptr<AsyncPacketSocket> socket_;
99 std::vector<std::unique_ptr<Packet>> packets_;
Taylor Brandstettere7536412016-09-09 13:16:15 -0700100 int ready_to_send_count_ = 0;
Stefan Holmer9131efd2016-05-23 18:19:26 +0200101 int64_t prev_packet_timestamp_;
henrikg3c089d72015-09-16 05:37:44 -0700102 RTC_DISALLOW_COPY_AND_ASSIGN(TestClient);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000103};
104
105} // namespace rtc
106
107#endif // WEBRTC_BASE_TESTCLIENT_H_