blob: 4710560644cc73aa4373a7e4770b5d2d3bc47807 [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>
16#include <algorithm>
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020017#include <list>
18#include <memory>
Steve Anton9de3aac2017-10-24 10:08:26 -070019
Steve Anton10542f22019-01-11 09:11:00 -080020#include "rtc_base/async_packet_socket.h"
21#include "rtc_base/async_socket.h"
22#include "rtc_base/async_tcp_socket.h"
23#include "rtc_base/constructor_magic.h"
24#include "rtc_base/socket_address.h"
Yves Gerey3e707812018-11-28 16:47:49 +010025#include "rtc_base/third_party/sigslot/sigslot.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "rtc_base/thread.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000027
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020028namespace rtc {
29
30// A test echo server, echoes back any packets sent to it.
31// Useful for unit tests.
32class TestEchoServer : public sigslot::has_slots<> {
33 public:
Steve Anton9de3aac2017-10-24 10:08:26 -070034 TestEchoServer(Thread* thread, const SocketAddress& addr);
35 ~TestEchoServer() override;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020036
37 SocketAddress address() const { return server_socket_->GetLocalAddress(); }
38
39 private:
40 void OnAccept(AsyncSocket* socket) {
41 AsyncSocket* raw_socket = socket->Accept(nullptr);
42 if (raw_socket) {
43 AsyncTCPSocket* packet_socket = new AsyncTCPSocket(raw_socket, false);
44 packet_socket->SignalReadPacket.connect(this, &TestEchoServer::OnPacket);
45 packet_socket->SignalClose.connect(this, &TestEchoServer::OnClose);
46 client_sockets_.push_back(packet_socket);
47 }
48 }
Yves Gerey665174f2018-06-19 15:03:05 +020049 void OnPacket(AsyncPacketSocket* socket,
50 const char* buf,
51 size_t size,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020052 const SocketAddress& remote_addr,
Niels Möllere6933812018-11-05 13:01:41 +010053 const int64_t& /* packet_time_us */) {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020054 rtc::PacketOptions options;
55 socket->Send(buf, size, options);
56 }
57 void OnClose(AsyncPacketSocket* socket, int err) {
58 ClientList::iterator it =
59 std::find(client_sockets_.begin(), client_sockets_.end(), socket);
60 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_