blob: df831fefb75e880533a1038812c6512e08bc8ca3 [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
14#include <vector>
15#include "webrtc/base/asyncudpsocket.h"
kwiberg4485ffb2016-04-26 08:14:39 -070016#include "webrtc/base/constructormagic.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000017#include "webrtc/base/criticalsection.h"
18
19namespace rtc {
20
21// 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.
23class TestClient : public sigslot::has_slots<> {
24 public:
25 // Records the contents of a packet that was received.
26 struct Packet {
27 Packet(const SocketAddress& a, const char* b, size_t s);
28 Packet(const Packet& p);
29 virtual ~Packet();
30
31 SocketAddress addr;
32 char* buf;
33 size_t size;
34 };
35
jlmiller@webrtc.orgec499be2015-02-07 22:37:59 +000036 // Default timeout for NextPacket reads.
37 static const int kTimeoutMs = 5000;
38
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000039 // Creates a client that will send and receive with the given socket and
40 // will post itself messages with the given thread.
41 explicit TestClient(AsyncPacketSocket* socket);
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000042 ~TestClient() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000043
44 SocketAddress address() const { return socket_->GetLocalAddress(); }
45 SocketAddress remote_address() const { return socket_->GetRemoteAddress(); }
46
47 // Checks that the socket moves to the specified connect state.
48 bool CheckConnState(AsyncPacketSocket::State state);
49
50 // Checks that the socket is connected to the remote side.
51 bool CheckConnected() {
52 return CheckConnState(AsyncPacketSocket::STATE_CONNECTED);
53 }
54
55 // Sends using the clients socket.
56 int Send(const char* buf, size_t size);
57
58 // Sends using the clients socket to the given destination.
59 int SendTo(const char* buf, size_t size, const SocketAddress& dest);
60
61 // Returns the next packet received by the client or 0 if none is received
jlmiller@webrtc.orgec499be2015-02-07 22:37:59 +000062 // within the specified timeout. The caller must delete the packet
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000063 // when done with it.
jlmiller@webrtc.orgec499be2015-02-07 22:37:59 +000064 Packet* NextPacket(int timeout_ms);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000065
66 // Checks that the next packet has the given contents. Returns the remote
67 // address that the packet was sent from.
68 bool CheckNextPacket(const char* buf, size_t len, SocketAddress* addr);
69
70 // Checks that no packets have arrived or will arrive in the next second.
71 bool CheckNoPacket();
72
73 int GetError();
74 int SetOption(Socket::Option opt, int value);
75
76 bool ready_to_send() const;
77
78 private:
jlmiller@webrtc.orgec499be2015-02-07 22:37:59 +000079 // Timeout for reads when no packet is expected.
80 static const int kNoPacketTimeoutMs = 1000;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000081 // Workaround for the fact that AsyncPacketSocket::GetConnState doesn't exist.
82 Socket::ConnState GetState();
83 // Slot for packets read on the socket.
84 void OnPacket(AsyncPacketSocket* socket, const char* buf, size_t len,
85 const SocketAddress& remote_addr,
86 const PacketTime& packet_time);
87 void OnReadyToSend(AsyncPacketSocket* socket);
88
89 CriticalSection crit_;
90 AsyncPacketSocket* socket_;
91 std::vector<Packet*>* packets_;
92 bool ready_to_send_;
henrikg3c089d72015-09-16 05:37:44 -070093 RTC_DISALLOW_COPY_AND_ASSIGN(TestClient);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000094};
95
96} // namespace rtc
97
98#endif // WEBRTC_BASE_TESTCLIENT_H_