[Lldb-commits] [lldb] [lldb] Add a callback version of TCPSocket::Accept (PR #106955)
Dmitry Vasilyev via lldb-commits
lldb-commits at lists.llvm.org
Mon Sep 2 16:23:33 PDT 2024
================
@@ -254,67 +255,60 @@ void TCPSocket::CloseListenSockets() {
m_listen_sockets.clear();
}
-Status TCPSocket::Accept(Socket *&conn_socket) {
- Status error;
- if (m_listen_sockets.size() == 0) {
- error = Status::FromErrorString("No open listening sockets!");
- return error;
- }
+llvm::Expected<std::vector<MainLoopBase::ReadHandleUP>> TCPSocket::Accept(
+ MainLoopBase &loop,
+ std::function<void(std::unique_ptr<TCPSocket> socket)> sock_cb) {
+ if (m_listen_sockets.size() == 0)
+ return llvm::createStringError("No open listening sockets!");
- NativeSocket sock = kInvalidSocketValue;
- NativeSocket listen_sock = kInvalidSocketValue;
- lldb_private::SocketAddress AcceptAddr;
- MainLoop accept_loop;
std::vector<MainLoopBase::ReadHandleUP> handles;
for (auto socket : m_listen_sockets) {
auto fd = socket.first;
- auto inherit = this->m_child_processes_inherit;
- auto io_sp = IOObjectSP(new TCPSocket(socket.first, false, inherit));
- handles.emplace_back(accept_loop.RegisterReadObject(
- io_sp, [fd, inherit, &sock, &AcceptAddr, &error,
- &listen_sock](MainLoopBase &loop) {
- socklen_t sa_len = AcceptAddr.GetMaxLength();
- sock = AcceptSocket(fd, &AcceptAddr.sockaddr(), &sa_len, inherit,
- error);
- listen_sock = fd;
- loop.RequestTermination();
- }, error));
- if (error.Fail())
- return error;
- }
+ auto io_sp =
+ std::make_shared<TCPSocket>(fd, false, this->m_child_processes_inherit);
+ auto cb = [this, fd, sock_cb](MainLoopBase &loop) {
+ lldb_private::SocketAddress AcceptAddr;
+ socklen_t sa_len = AcceptAddr.GetMaxLength();
+ Status error;
+ NativeSocket sock = AcceptSocket(fd, &AcceptAddr.sockaddr(), &sa_len,
+ m_child_processes_inherit, error);
+ Log *log = GetLog(LLDBLog::Host);
+ if (error.Fail())
+ LLDB_LOG(log, "AcceptSocket({0}): {1}", fd, error);
+
+ const lldb_private::SocketAddress &AddrIn = m_listen_sockets[fd];
+ if (!AddrIn.IsAnyAddr() && AcceptAddr != AddrIn) {
+ CLOSE_SOCKET(sock);
+ LLDB_LOG(log, "rejecting incoming connection from {0} (expecting {1})",
+ AcceptAddr.GetIPAddress(), AddrIn.GetIPAddress());
+ }
+ std::unique_ptr<TCPSocket> sock_up(new TCPSocket(sock, *this));
- bool accept_connection = false;
- std::unique_ptr<TCPSocket> accepted_socket;
- // Loop until we are happy with our connection
- while (!accept_connection) {
- accept_loop.Run();
+ // Keep our TCP packets coming without any delays.
+ sock_up->SetOptionNoDelay();
+ sock_cb(std::move(sock_up));
----------------
slydiman wrote:
Avoid to close or create the instance of TCPSocket with invalid sock
```
if (error.Fail())
LLDB_LOG(log, "AcceptSocket({0}): {1}", fd, error);
else {
const lldb_private::SocketAddress &AddrIn = m_listen_sockets[fd];
if (!AddrIn.IsAnyAddr() && AcceptAddr != AddrIn) {
CLOSE_SOCKET(sock);
LLDB_LOG(log,
"rejecting incoming connection from {0} (expecting {1})",
AcceptAddr.GetIPAddress(), AddrIn.GetIPAddress());
} else {
std::unique_ptr<TCPSocket> sock_up(new TCPSocket(sock, *this));
// Keep our TCP packets coming without any delays.
sock_up->SetOptionNoDelay();
sock_cb(std::move(sock_up));
}
}
```
https://github.com/llvm/llvm-project/pull/106955
More information about the lldb-commits
mailing list