blob: da40ac4547569da7bdf64a0131187671b4cef592 [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
Steve Anton10542f22019-01-11 09:11:00 -080011#ifndef RTC_BASE_TEST_ECHO_SERVER_H_
12#define RTC_BASE_TEST_ECHO_SERVER_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Yves Gerey3e707812018-11-28 16:47:49 +010014#include <stddef.h>
15#include <stdint.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020016
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020017#include <list>
18#include <memory>
Steve Anton9de3aac2017-10-24 10:08:26 -070019
Steve Anton2acd1632019-03-25 13:48:30 -070020#include "absl/algorithm/container.h"
Steve Anton10542f22019-01-11 09:11:00 -080021#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 16:47:49 +010026#include "rtc_base/third_party/sigslot/sigslot.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "rtc_base/thread.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000028
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020029namespace 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 10:08:26 -070035 TestEchoServer(Thread* thread, const SocketAddress& addr);
36 ~TestEchoServer() override;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020037
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 15:03:05 +020050 void OnPacket(AsyncPacketSocket* socket,
51 const char* buf,
52 size_t size,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020053 const SocketAddress& remote_addr,
Niels Möllere6933812018-11-05 13:01:41 +010054 const int64_t& /* packet_time_us */) {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020055 rtc::PacketOptions options;
56 socket->Send(buf, size, options);
57 }
58 void OnClose(AsyncPacketSocket* socket, int err) {
Steve Anton2acd1632019-03-25 13:48:30 -070059 ClientList::iterator it = absl::c_find(client_sockets_, socket);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020060 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:26 +000071
Steve Anton10542f22019-01-11 09:11:00 -080072#endif // RTC_BASE_TEST_ECHO_SERVER_H_