blob: da40ac4547569da7bdf64a0131187671b4cef592 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:261/*
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
Steve Anton10542f22019-01-11 17:11:0011#ifndef RTC_BASE_TEST_ECHO_SERVER_H_
12#define RTC_BASE_TEST_ECHO_SERVER_H_
henrike@webrtc.orgf0488722014-05-13 18:00:2613
Yves Gerey3e707812018-11-28 15:47:4914#include <stddef.h>
15#include <stdint.h>
Jonas Olssona4d87372019-07-05 17:08:3316
Henrik Kjellanderec78f1c2017-06-29 05:52:5017#include <list>
18#include <memory>
Steve Anton9de3aac2017-10-24 17:08:2619
Steve Anton2acd1632019-03-25 20:48:3020#include "absl/algorithm/container.h"
Steve Anton10542f22019-01-11 17:11:0021#include "rtc_base/async_packet_socket.h"
22#include "rtc_base/async_socket.h"
23#include "rtc_base/async_tcp_socket.h"
24#include "rtc_base/constructor_magic.h"
25#include "rtc_base/socket_address.h"
Yves Gerey3e707812018-11-28 15:47:4926#include "rtc_base/third_party/sigslot/sigslot.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3127#include "rtc_base/thread.h"
henrike@webrtc.orgf0488722014-05-13 18:00:2628
Henrik Kjellanderec78f1c2017-06-29 05:52:5029namespace rtc {
30
31// A test echo server, echoes back any packets sent to it.
32// Useful for unit tests.
33class TestEchoServer : public sigslot::has_slots<> {
34 public:
Steve Anton9de3aac2017-10-24 17:08:2635 TestEchoServer(Thread* thread, const SocketAddress& addr);
36 ~TestEchoServer() override;
Henrik Kjellanderec78f1c2017-06-29 05:52:5037
38 SocketAddress address() const { return server_socket_->GetLocalAddress(); }
39
40 private:
41 void OnAccept(AsyncSocket* socket) {
42 AsyncSocket* raw_socket = socket->Accept(nullptr);
43 if (raw_socket) {
44 AsyncTCPSocket* packet_socket = new AsyncTCPSocket(raw_socket, false);
45 packet_socket->SignalReadPacket.connect(this, &TestEchoServer::OnPacket);
46 packet_socket->SignalClose.connect(this, &TestEchoServer::OnClose);
47 client_sockets_.push_back(packet_socket);
48 }
49 }
Yves Gerey665174f2018-06-19 13:03:0550 void OnPacket(AsyncPacketSocket* socket,
51 const char* buf,
52 size_t size,
Henrik Kjellanderec78f1c2017-06-29 05:52:5053 const SocketAddress& remote_addr,
Niels Möllere6933812018-11-05 12:01:4154 const int64_t& /* packet_time_us */) {
Henrik Kjellanderec78f1c2017-06-29 05:52:5055 rtc::PacketOptions options;
56 socket->Send(buf, size, options);
57 }
58 void OnClose(AsyncPacketSocket* socket, int err) {
Steve Anton2acd1632019-03-25 20:48:3059 ClientList::iterator it = absl::c_find(client_sockets_, socket);
Henrik Kjellanderec78f1c2017-06-29 05:52:5060 client_sockets_.erase(it);
61 Thread::Current()->Dispose(socket);
62 }
63
64 typedef std::list<AsyncTCPSocket*> ClientList;
65 std::unique_ptr<AsyncSocket> server_socket_;
66 ClientList client_sockets_;
67 RTC_DISALLOW_COPY_AND_ASSIGN(TestEchoServer);
68};
69
70} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:2671
Steve Anton10542f22019-01-11 17:11:0072#endif // RTC_BASE_TEST_ECHO_SERVER_H_