TWIN server: accept inbound twin connections on TWIN_LISTEN_PORT

Replace the initTwinServer() stub with a real Async::TcpServer that
listens on TWIN_LISTEN_PORT (default 5304). Inbound connections are
held in m_twin_pending_cons with a 10-second hello timeout; on receipt
of a valid MsgTrunkHello with ROLE_TWIN the connection is handed off to
TwinLink::acceptInboundConnection(). Pending entries are cleaned up on
timeout, disconnect, and successful handoff. No server is created when
no [TWIN] section is configured.
This commit is contained in:
Audric committed 2026-04-15 20:54:51 +02:00
1 parent 9f349fff5c
commit 4b6346d4ab
2 files changed
+147 -15

No files matched your search

+141 -15
View File
@@ -327,6 +327,13 @@ Reflector::~Reflector(void)
m_satellite_client = nullptr;
delete m_twin_link;
m_twin_link = nullptr;
for (auto& kv : m_twin_pending_cons)
{
delete kv.second;
}
m_twin_pending_cons.clear();
delete m_twin_srv;
m_twin_srv = nullptr;
m_client_con_map.clear();
ReflectorClient::cleanup();
delete TGHandler::instance();
@@ -3905,30 +3912,149 @@ void Reflector::initTwinLink(void)
void Reflector::initTwinServer(void)
{
bool found = false;
for (const auto& section : m_cfg->listSections())
{
if (section == "TWIN")
{
found = true;
break;
}
}
if (!found)
{
return;
}
if (m_twin_link == nullptr) return; // no [TWIN] section — no server needed
std::string port_str;
if (m_cfg->getValue("GLOBAL", "TWIN_LISTEN_PORT", port_str)
&& !port_str.empty())
{
m_twin_listen_port = static_cast<uint16_t>(std::atoi(port_str.c_str()));
}
std::cout << "TWIN: listen port = " << m_twin_listen_port
<< " (server stub, not bound yet)" << std::endl;
m_twin_srv = new TcpServer<FramedTcpConnection>(
std::to_string(m_twin_listen_port));
m_twin_srv->clientConnected.connect(
sigc::mem_fun(*this, &Reflector::twinClientConnected));
m_twin_srv->clientDisconnected.connect(
sigc::mem_fun(*this, &Reflector::twinClientDisconnected));
std::cout << "TWIN: server listening on port " << m_twin_listen_port
<< std::endl;
} /* Reflector::initTwinServer */
void Reflector::twinClientConnected(Async::FramedTcpConnection* con)
{
std::cout << "TWIN: inbound connection from "
<< con->remoteHost() << ":" << con->remotePort()
<< " (awaiting hello)" << std::endl;
auto* timer = new Async::Timer(10000, Async::Timer::TYPE_ONESHOT);
timer->expired.connect(
sigc::mem_fun(*this, &Reflector::twinPendingTimeout));
m_twin_pending_cons[con] = timer;
con->setMaxFrameSize(ReflectorMsg::MAX_SSL_SETUP_FRAME_SIZE);
con->frameReceived.connect(
sigc::mem_fun(*this, &Reflector::twinPendingFrameReceived));
} /* Reflector::twinClientConnected */
void Reflector::twinClientDisconnected(Async::FramedTcpConnection* con,
Async::FramedTcpConnection::DisconnectReason /*reason*/)
{
auto pit = m_twin_pending_cons.find(con);
if (pit != m_twin_pending_cons.end())
{
delete pit->second; // timer
m_twin_pending_cons.erase(pit);
std::cout << "TWIN: pending inbound from "
<< con->remoteHost() << " disconnected" << std::endl;
}
// If already handed off to m_twin_link, TwinLink owns the connection.
} /* Reflector::twinClientDisconnected */
void Reflector::twinPendingFrameReceived(Async::FramedTcpConnection* con,
std::vector<uint8_t>& data)
{
auto pit = m_twin_pending_cons.find(con);
if (pit == m_twin_pending_cons.end())
{
return; // Already handed off — TwinLink handles frames now
}
auto buf = reinterpret_cast<const char*>(data.data());
std::stringstream ss;
ss.write(buf, data.size());
// Helper: clean up pending entry and disconnect.
// TcpConnection::disconnect() does NOT emit the disconnected signal,
// so twinClientDisconnected would not fire — we clean up here explicitly.
auto rejectPending = [&]()
{
delete pit->second; // timer
m_twin_pending_cons.erase(pit);
con->disconnect();
};
ReflectorMsg header;
if (!header.unpack(ss))
{
std::cerr << "*** ERROR: TWIN inbound: failed to unpack message header"
<< std::endl;
rejectPending();
return;
}
if (header.type() != MsgTrunkHello::TYPE)
{
std::cerr << "*** WARNING: TWIN inbound: expected MsgTrunkHello, got type="
<< header.type() << std::endl;
rejectPending();
return;
}
MsgTrunkHello msg;
if (!msg.unpack(ss))
{
std::cerr << "*** ERROR: TWIN inbound: failed to unpack MsgTrunkHello"
<< std::endl;
rejectPending();
return;
}
if (msg.role() != MsgTrunkHello::ROLE_TWIN)
{
std::cerr << "*** ERROR: TWIN inbound: peer sent role=" << int(msg.role())
<< ", expected ROLE_TWIN (" << int(MsgTrunkHello::ROLE_TWIN)
<< ")" << std::endl;
rejectPending();
return;
}
// Secret/prefix validation lives inside TwinLink::acceptInboundConnection.
// Clean up our pending entry and hand off.
delete pit->second; // timer
m_twin_pending_cons.erase(pit);
con->frameReceived.clear();
con->setMaxFrameSize(ReflectorMsg::MAX_POSTAUTH_FRAME_SIZE);
m_twin_link->acceptInboundConnection(con, msg);
} /* Reflector::twinPendingFrameReceived */
void Reflector::twinPendingTimeout(Async::Timer* t)
{
for (auto it = m_twin_pending_cons.begin();
it != m_twin_pending_cons.end(); ++it)
{
if (it->second == t)
{
std::cerr << "*** WARNING: TWIN inbound from "
<< it->first->remoteHost()
<< ": hello timeout — disconnecting" << std::endl;
auto* con = it->first;
delete it->second; // timer
m_twin_pending_cons.erase(it);
con->disconnect();
return;
}
}
} /* Reflector::twinPendingTimeout */
/*
* This file has not been truncated
*/