henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 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 Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #ifndef RTC_BASE_TEST_ECHO_SERVER_H_ |
| 12 | #define RTC_BASE_TEST_ECHO_SERVER_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 13 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 14 | #include <stddef.h> |
| 15 | #include <stdint.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 16 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 17 | #include <list> |
| 18 | #include <memory> |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 19 | |
Steve Anton | 2acd163 | 2019-03-25 13:48:30 -0700 | [diff] [blame] | 20 | #include "absl/algorithm/container.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 21 | #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 Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 26 | #include "rtc_base/third_party/sigslot/sigslot.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 27 | #include "rtc_base/thread.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 28 | |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 29 | namespace rtc { |
| 30 | |
| 31 | // A test echo server, echoes back any packets sent to it. |
| 32 | // Useful for unit tests. |
| 33 | class TestEchoServer : public sigslot::has_slots<> { |
| 34 | public: |
Steve Anton | 9de3aac | 2017-10-24 10:08:26 -0700 | [diff] [blame] | 35 | TestEchoServer(Thread* thread, const SocketAddress& addr); |
| 36 | ~TestEchoServer() override; |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 37 | |
| 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 Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 50 | void OnPacket(AsyncPacketSocket* socket, |
| 51 | const char* buf, |
| 52 | size_t size, |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 53 | const SocketAddress& remote_addr, |
Niels Möller | e693381 | 2018-11-05 13:01:41 +0100 | [diff] [blame] | 54 | const int64_t& /* packet_time_us */) { |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 55 | rtc::PacketOptions options; |
| 56 | socket->Send(buf, size, options); |
| 57 | } |
| 58 | void OnClose(AsyncPacketSocket* socket, int err) { |
Steve Anton | 2acd163 | 2019-03-25 13:48:30 -0700 | [diff] [blame] | 59 | ClientList::iterator it = absl::c_find(client_sockets_, socket); |
Henrik Kjellander | ec78f1c | 2017-06-29 07:52:50 +0200 | [diff] [blame] | 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.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 71 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 72 | #endif // RTC_BASE_TEST_ECHO_SERVER_H_ |