2014-05-13 18:00:26 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright 2004 The WebRTC Project Authors. All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
2019-01-11 09:11:00 -08:00
|
|
|
#ifndef RTC_BASE_TEST_ECHO_SERVER_H_
|
|
|
|
|
#define RTC_BASE_TEST_ECHO_SERVER_H_
|
2014-05-13 18:00:26 +00:00
|
|
|
|
2018-11-28 16:47:49 +01:00
|
|
|
#include <stddef.h>
|
|
|
|
|
#include <stdint.h>
|
2019-07-05 19:08:33 +02:00
|
|
|
|
2017-06-29 07:52:50 +02:00
|
|
|
#include <list>
|
|
|
|
|
#include <memory>
|
2017-10-24 10:08:26 -07:00
|
|
|
|
2019-03-25 13:48:30 -07:00
|
|
|
#include "absl/algorithm/container.h"
|
2022-08-24 18:35:45 +02:00
|
|
|
#include "absl/memory/memory.h"
|
2019-01-11 09:11:00 -08:00
|
|
|
#include "rtc_base/async_packet_socket.h"
|
|
|
|
|
#include "rtc_base/async_tcp_socket.h"
|
2021-08-12 10:32:30 +02:00
|
|
|
#include "rtc_base/socket.h"
|
2019-01-11 09:11:00 -08:00
|
|
|
#include "rtc_base/socket_address.h"
|
2018-11-28 16:47:49 +01:00
|
|
|
#include "rtc_base/third_party/sigslot/sigslot.h"
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "rtc_base/thread.h"
|
2014-05-13 18:00:26 +00:00
|
|
|
|
2017-06-29 07:52:50 +02:00
|
|
|
namespace rtc {
|
|
|
|
|
|
|
|
|
|
// A test echo server, echoes back any packets sent to it.
|
|
|
|
|
// Useful for unit tests.
|
|
|
|
|
class TestEchoServer : public sigslot::has_slots<> {
|
|
|
|
|
public:
|
2017-10-24 10:08:26 -07:00
|
|
|
TestEchoServer(Thread* thread, const SocketAddress& addr);
|
|
|
|
|
~TestEchoServer() override;
|
2017-06-29 07:52:50 +02:00
|
|
|
|
2022-01-12 05:24:58 +09:00
|
|
|
TestEchoServer(const TestEchoServer&) = delete;
|
|
|
|
|
TestEchoServer& operator=(const TestEchoServer&) = delete;
|
|
|
|
|
|
2017-06-29 07:52:50 +02:00
|
|
|
SocketAddress address() const { return server_socket_->GetLocalAddress(); }
|
|
|
|
|
|
|
|
|
|
private:
|
2021-08-12 10:32:30 +02:00
|
|
|
void OnAccept(Socket* socket) {
|
|
|
|
|
Socket* raw_socket = socket->Accept(nullptr);
|
2017-06-29 07:52:50 +02:00
|
|
|
if (raw_socket) {
|
2021-10-19 10:11:02 +02:00
|
|
|
AsyncTCPSocket* packet_socket = new AsyncTCPSocket(raw_socket);
|
2017-06-29 07:52:50 +02:00
|
|
|
packet_socket->SignalReadPacket.connect(this, &TestEchoServer::OnPacket);
|
2022-04-13 09:03:52 +00:00
|
|
|
packet_socket->SubscribeClose(
|
|
|
|
|
this, [this](AsyncPacketSocket* s, int err) { OnClose(s, err); });
|
2017-06-29 07:52:50 +02:00
|
|
|
client_sockets_.push_back(packet_socket);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
void OnPacket(AsyncPacketSocket* socket,
|
|
|
|
|
const char* buf,
|
|
|
|
|
size_t size,
|
|
|
|
|
const SocketAddress& remote_addr,
|
2018-11-05 13:01:41 +01:00
|
|
|
const int64_t& /* packet_time_us */) {
|
2017-06-29 07:52:50 +02:00
|
|
|
rtc::PacketOptions options;
|
|
|
|
|
socket->Send(buf, size, options);
|
|
|
|
|
}
|
|
|
|
|
void OnClose(AsyncPacketSocket* socket, int err) {
|
2019-03-25 13:48:30 -07:00
|
|
|
ClientList::iterator it = absl::c_find(client_sockets_, socket);
|
2017-06-29 07:52:50 +02:00
|
|
|
client_sockets_.erase(it);
|
2022-08-24 18:35:45 +02:00
|
|
|
// `OnClose` is triggered by socket Close callback, deleting `socket` while
|
|
|
|
|
// processing that callback might be unsafe.
|
|
|
|
|
Thread::Current()->PostTask([socket = absl::WrapUnique(socket)] {});
|
2017-06-29 07:52:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
typedef std::list<AsyncTCPSocket*> ClientList;
|
2021-08-12 10:32:30 +02:00
|
|
|
std::unique_ptr<Socket> server_socket_;
|
2017-06-29 07:52:50 +02:00
|
|
|
ClientList client_sockets_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace rtc
|
2014-05-13 18:00:26 +00:00
|
|
|
|
2019-01-11 09:11:00 -08:00
|
|
|
#endif // RTC_BASE_TEST_ECHO_SERVER_H_
|