mirror of
https://github.com/eclipse-openvehicle-api/openvehicle-api.git
synced 2026-04-21 03:38:15 +00:00
tunnel component & update vehicle abstraction example (#8)
This commit is contained in:
@@ -212,8 +212,10 @@ add_subdirectory(task_timer)
|
||||
add_subdirectory(ipc_com)
|
||||
add_subdirectory(ipc_connect)
|
||||
add_subdirectory(ipc_shared_mem)
|
||||
add_subdirectory(uds_win_sockets)
|
||||
add_subdirectory(uds_unix_sockets)
|
||||
add_subdirectory(uds_unix_tunnel)
|
||||
add_subdirectory(uds_win_sockets)
|
||||
add_subdirectory(uds_win_tunnel)
|
||||
add_subdirectory(process_control)
|
||||
add_subdirectory(hardware_ident)
|
||||
add_subdirectory(manifest_util)
|
||||
|
||||
@@ -82,7 +82,7 @@ public:
|
||||
private:
|
||||
sdv::TInterfaceAccessPtr m_ptrObject; ///< Smart pointer to the object.
|
||||
sdv::IObjectControl* m_pObjectControl = nullptr; ///< Pointer to the object control of the application
|
||||
sdv::EObjectState m_eObjectState = sdv::EObjectState::initialization_pending; ///< Object status (in case there is no object control).
|
||||
sdv::EObjectState m_eObjectState = sdv::EObjectState::initialization_pending; ///< Object state (in case there is no object control).
|
||||
};
|
||||
|
||||
#endif // !defined ISOLATION_OBJECT_MONITOR_H
|
||||
@@ -55,7 +55,7 @@ CChannelConnector::~CChannelConnector()
|
||||
sdv::ipc::IConnect* pConnection = m_ptrChannelEndpoint.GetInterface<sdv::ipc::IConnect>();
|
||||
if (pConnection)
|
||||
{
|
||||
if (m_uiConnectionStatusCookie) pConnection->UnregisterStatusEventCallback(m_uiConnectionStatusCookie);
|
||||
if (m_uiConnectStateCookie) pConnection->UnregisterStateEventCallback(m_uiConnectStateCookie);
|
||||
pConnection->Disconnect();
|
||||
}
|
||||
|
||||
@@ -78,8 +78,8 @@ bool CChannelConnector::ServerConnect(sdv::IInterfaceAccess* pObject, bool bAllo
|
||||
// Establish connection...
|
||||
sdv::ipc::IConnect* pConnection = m_ptrChannelEndpoint.GetInterface<sdv::ipc::IConnect>();
|
||||
if (!pConnection) return false;
|
||||
m_uiConnectionStatusCookie = pConnection->RegisterStatusEventCallback(this);
|
||||
return m_uiConnectionStatusCookie != 0 && pConnection->AsyncConnect(this);
|
||||
m_uiConnectStateCookie = pConnection->RegisterStateEventCallback(this);
|
||||
return m_uiConnectStateCookie != 0 && pConnection->AsyncConnect(this);
|
||||
}
|
||||
|
||||
sdv::IInterfaceAccess* CChannelConnector::ClientConnect(uint32_t uiTimeoutMs)
|
||||
@@ -99,8 +99,8 @@ sdv::IInterfaceAccess* CChannelConnector::ClientConnect(uint32_t uiTimeoutMs)
|
||||
|
||||
// Establish connection...
|
||||
sdv::ipc::IConnect* pConnection = m_ptrChannelEndpoint.GetInterface<sdv::ipc::IConnect>();
|
||||
if (pConnection) m_uiConnectionStatusCookie = pConnection->RegisterStatusEventCallback(this);
|
||||
if (!pConnection || m_uiConnectionStatusCookie == 0 || !pConnection->AsyncConnect(this) ||
|
||||
if (pConnection) m_uiConnectStateCookie = pConnection->RegisterStateEventCallback(this);
|
||||
if (!pConnection || m_uiConnectStateCookie == 0 || !pConnection->AsyncConnect(this) ||
|
||||
!pConnection->WaitForConnection(uiTimeoutMs))
|
||||
{
|
||||
SDV_LOG_ERROR("Could not establish a connection!");
|
||||
@@ -113,17 +113,17 @@ sdv::IInterfaceAccess* CChannelConnector::ClientConnect(uint32_t uiTimeoutMs)
|
||||
|
||||
bool CChannelConnector::IsConnected() const
|
||||
{
|
||||
return m_eConnectStatus == sdv::ipc::EConnectStatus::connected;
|
||||
return m_eConnectState == sdv::ipc::EConnectState::connected;
|
||||
}
|
||||
|
||||
void CChannelConnector::SetStatus(/*in*/ sdv::ipc::EConnectStatus eConnectStatus)
|
||||
void CChannelConnector::SetConnectState(/*in*/ sdv::ipc::EConnectState eConnectState)
|
||||
{
|
||||
auto eConnectStatusTemp = m_eConnectStatus;
|
||||
m_eConnectStatus = eConnectStatus;
|
||||
switch (m_eConnectStatus)
|
||||
auto eConnectStateTemp = m_eConnectState;
|
||||
m_eConnectState = eConnectState;
|
||||
switch (m_eConnectState)
|
||||
{
|
||||
case sdv::ipc::EConnectStatus::disconnected:
|
||||
case sdv::ipc::EConnectStatus::disconnected_forced:
|
||||
case sdv::ipc::EConnectState::disconnected:
|
||||
case sdv::ipc::EConnectState::disconnected_forced:
|
||||
// Invalidate the proxy objects.
|
||||
for (auto& rvtProxyObject : m_mapProxyObjects)
|
||||
rvtProxyObject.second.reset();
|
||||
@@ -131,7 +131,7 @@ void CChannelConnector::SetStatus(/*in*/ sdv::ipc::EConnectStatus eConnectStatus
|
||||
if (m_eEndpointType == EEndpointType::server)
|
||||
{
|
||||
// Report information (but only once)
|
||||
if (sdv::app::ConsoleIsVerbose() && eConnectStatusTemp != sdv::ipc::EConnectStatus::disconnected)
|
||||
if (sdv::app::ConsoleIsVerbose() && eConnectStateTemp != sdv::ipc::EConnectState::disconnected)
|
||||
std::cout << "Client disconnected (ID#" << m_tConnectionID.uiIdent << ")" << std::endl;
|
||||
|
||||
// Remove the connection if reconnection is not enabled (normal case).
|
||||
@@ -139,7 +139,7 @@ void CChannelConnector::SetStatus(/*in*/ sdv::ipc::EConnectStatus eConnectStatus
|
||||
m_rcontrol.RemoveConnection(m_tConnectionID);
|
||||
}
|
||||
break;
|
||||
case sdv::ipc::EConnectStatus::connected:
|
||||
case sdv::ipc::EConnectState::connected:
|
||||
if (m_eEndpointType == EEndpointType::server)
|
||||
{
|
||||
// Report information
|
||||
|
||||
@@ -71,10 +71,10 @@ public:
|
||||
bool IsConnected() const;
|
||||
|
||||
/**
|
||||
* @brief Set the current status. Overload of sdv::ipc::IConnectEventCallback::SetStatus.
|
||||
* @param[in] eConnectStatus The connection status.
|
||||
* @brief Set the current connect statue. Overload of sdv::ipc::IConnectEventCallback::SetConnectState.
|
||||
* @param[in] eConnectState The connection state.
|
||||
*/
|
||||
virtual void SetStatus(/*in*/ sdv::ipc::EConnectStatus eConnectStatus) override;
|
||||
virtual void SetConnectState(/*in*/ sdv::ipc::EConnectState eConnectState) override;
|
||||
|
||||
/**
|
||||
* @brief Callback to be called by the IPC connection when receiving a data packet. Overload of
|
||||
@@ -139,14 +139,14 @@ private:
|
||||
} eState = EState::initialized; ///< Data processing state.
|
||||
sdv::sequence<sdv::pointer<uint8_t>> seqResult; ///< The result data.
|
||||
std::mutex mtxWaitForResult; ///< Mutex to protect result processing.
|
||||
std::condition_variable cvWaitForResult; ///< Condition variable to trigger result processing.
|
||||
std::condition_variable cvWaitForResult; ///< Condition var to trigger result processing.
|
||||
};
|
||||
|
||||
CCommunicationControl& m_rcontrol; ///< Reference to the communication control class.
|
||||
sdv::TObjectPtr m_ptrChannelEndpoint; ///< Managed pointer to the channel endpoint.
|
||||
uint64_t m_uiConnectionStatusCookie = 0; ///< Connection status cookie (received after registration).
|
||||
uint64_t m_uiConnectStateCookie = 0; ///< Connection state cookie (received after registration).
|
||||
std::shared_ptr<CMarshallObject> m_ptrInitialMarshallObject; ///< Initial marshall object used after a connect event.
|
||||
sdv::ipc::EConnectStatus m_eConnectStatus = sdv::ipc::EConnectStatus::uninitialized; ///< Current connection status.
|
||||
sdv::ipc::EConnectState m_eConnectState = sdv::ipc::EConnectState::uninitialized; ///< Current connection state.
|
||||
bool m_bAllowReconnect = false; ///< When set, allow reconnection of the server.
|
||||
EEndpointType m_eEndpointType = EEndpointType::client; ///< Endpoint type of this connector.
|
||||
std::recursive_mutex m_mtxMarshallObjects; ///< Synchronize access to the marshall object vector.
|
||||
|
||||
@@ -25,21 +25,21 @@ inline sdv::process::TProcessID GetProcessID()
|
||||
return pProcessInfo ? pProcessInfo->GetProcessID() : 0;
|
||||
}
|
||||
|
||||
inline std::string ConnectState(sdv::ipc::EConnectStatus eState)
|
||||
inline std::string ConnectState2String(sdv::ipc::EConnectState eState)
|
||||
{
|
||||
switch (eState)
|
||||
{
|
||||
case sdv::ipc::EConnectStatus::uninitialized: return "uninitialized";
|
||||
case sdv::ipc::EConnectStatus::initializing: return "initializing";
|
||||
case sdv::ipc::EConnectStatus::initialized: return "initialized";
|
||||
case sdv::ipc::EConnectStatus::connecting: return "connecting";
|
||||
case sdv::ipc::EConnectStatus::negotiating: return "negotiating";
|
||||
case sdv::ipc::EConnectStatus::connection_error: return "connection_error";
|
||||
case sdv::ipc::EConnectStatus::connected: return "connected";
|
||||
case sdv::ipc::EConnectStatus::communication_error: return "communication_error";
|
||||
case sdv::ipc::EConnectStatus::disconnected: return "disconnected";
|
||||
case sdv::ipc::EConnectStatus::disconnected_forced: return "disconnected_forced";
|
||||
case sdv::ipc::EConnectStatus::terminating: return "terminating";
|
||||
case sdv::ipc::EConnectState::uninitialized: return "uninitialized";
|
||||
case sdv::ipc::EConnectState::initializing: return "initializing";
|
||||
case sdv::ipc::EConnectState::initialized: return "initialized";
|
||||
case sdv::ipc::EConnectState::connecting: return "connecting";
|
||||
case sdv::ipc::EConnectState::negotiating: return "negotiating";
|
||||
case sdv::ipc::EConnectState::connection_error: return "connection_error";
|
||||
case sdv::ipc::EConnectState::connected: return "connected";
|
||||
case sdv::ipc::EConnectState::communication_error: return "communication_error";
|
||||
case sdv::ipc::EConnectState::disconnected: return "disconnected";
|
||||
case sdv::ipc::EConnectState::disconnected_forced: return "disconnected_forced";
|
||||
case sdv::ipc::EConnectState::terminating: return "terminating";
|
||||
default: return "unknown";
|
||||
}
|
||||
}
|
||||
@@ -86,11 +86,11 @@ CConnection::~CConnection()
|
||||
#endif
|
||||
|
||||
// If still connected, disconnect.
|
||||
if (m_eStatus == sdv::ipc::EConnectStatus::connected)
|
||||
if (m_eConnectState == sdv::ipc::EConnectState::connected)
|
||||
Disconnect();
|
||||
|
||||
// Set to terminating to allow the threads to shut down.
|
||||
m_eStatus = sdv::ipc::EConnectStatus::terminating;
|
||||
m_eConnectState = sdv::ipc::EConnectState::terminating;
|
||||
|
||||
// Stop the receive thread to prevent accepting any more messages from the server.
|
||||
if (m_threadReceive.joinable())
|
||||
@@ -146,19 +146,19 @@ uint32_t CConnection::Send(const void* pData, uint32_t uiDataLength)
|
||||
#if ENABLE_REPORTING >= 2
|
||||
switch (((SMsgHdr*)pData)->eType)
|
||||
{
|
||||
case EMsgType::sync_request: TRACE(m_bServer ? "SERVER" : "CLIENT", " SEND SYNC_REQUEST (", ConnectState(m_eStatus), ")"); break;
|
||||
case EMsgType::sync_answer: TRACE(m_bServer ? "SERVER" : "CLIENT", " SEND SYNC_ANSWER (", ConnectState(m_eStatus), ")"); break;
|
||||
case EMsgType::connect_request: TRACE(m_bServer ? "SERVER" : "CLIENT", " SEND CONNECT_REQUEST (", ConnectState(m_eStatus), ")"); break;
|
||||
case EMsgType::connect_answer: TRACE(m_bServer ? "SERVER" : "CLIENT", " SEND CONNECT_ANSWER (", ConnectState(m_eStatus), ")"); break;
|
||||
case EMsgType::connect_term: TRACE(m_bServer ? "SERVER" : "CLIENT", " SEND CONNECT_TERM (", ConnectState(m_eStatus), ")"); break;
|
||||
case EMsgType::sync_request: TRACE(m_bServer ? "SERVER" : "CLIENT", " SEND SYNC_REQUEST (", ConnectState2String(m_eConnectState), ")"); break;
|
||||
case EMsgType::sync_answer: TRACE(m_bServer ? "SERVER" : "CLIENT", " SEND SYNC_ANSWER (", ConnectState2String(m_eConnectState), ")"); break;
|
||||
case EMsgType::connect_request: TRACE(m_bServer ? "SERVER" : "CLIENT", " SEND CONNECT_REQUEST (", ConnectState2String(m_eConnectState), ")"); break;
|
||||
case EMsgType::connect_answer: TRACE(m_bServer ? "SERVER" : "CLIENT", " SEND CONNECT_ANSWER (", ConnectState2String(m_eConnectState), ")"); break;
|
||||
case EMsgType::connect_term: TRACE(m_bServer ? "SERVER" : "CLIENT", " SEND CONNECT_TERM (", ConnectState2String(m_eConnectState), ")"); break;
|
||||
#if ENABLE_REPORTING >= 3
|
||||
case EMsgType::data: TRACE(m_bServer ? "SERVER" : "CLIENT", " SEND DATA ", uiDataLength - sizeof(SMsgHdr), " bytes (", ConnectState(m_eStatus), ")"); break;
|
||||
case EMsgType::data_fragment: TRACE(m_bServer ? "SERVER" : "CLIENT", " RECEIVE DATA FRAGMENT ", uiDataLength - sizeof(SFragmentedMsgHdr), " bytes (", ConnectState(m_eStatus), ")"); break;
|
||||
case EMsgType::data: TRACE(m_bServer ? "SERVER" : "CLIENT", " SEND DATA ", uiDataLength - sizeof(SMsgHdr), " bytes (", ConnectState2String(m_eConnectState), ")"); break;
|
||||
case EMsgType::data_fragment: TRACE(m_bServer ? "SERVER" : "CLIENT", " RECEIVE DATA FRAGMENT ", uiDataLength - sizeof(SFragmentedMsgHdr), " bytes (", ConnectState2String(m_eConnectState), ")"); break;
|
||||
#else
|
||||
case EMsgType::data: break;
|
||||
case EMsgType::data_fragment: break;
|
||||
#endif
|
||||
default: TRACE(m_bServer ? "SERVER" : "CLIENT", " SEND UNKNOWN (", ConnectState(m_eStatus), ")"); break;
|
||||
default: TRACE(m_bServer ? "SERVER" : "CLIENT", " SEND UNKNOWN (", ConnectState2String(m_eConnectState), ")"); break;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -186,10 +186,10 @@ bool CConnection::SendData(/*inout*/ sdv::sequence<sdv::pointer<uint8_t>>& seqDa
|
||||
}
|
||||
TRACE("Send a sequence of data with ", seqData.size(), " pointers with the length {", sstreamReport.str(), "} bytes");
|
||||
#endif
|
||||
// Only allow sending messages when the status is connected
|
||||
if (m_eStatus != sdv::ipc::EConnectStatus::connected)
|
||||
// Only allow sending messages when the state is connected
|
||||
if (m_eConnectState != sdv::ipc::EConnectState::connected)
|
||||
{
|
||||
SetStatus(sdv::ipc::EConnectStatus::communication_error);
|
||||
SetConnectState(sdv::ipc::EConnectState::communication_error);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -260,7 +260,7 @@ bool CConnection::SendData(/*inout*/ sdv::sequence<sdv::pointer<uint8_t>>& seqDa
|
||||
auto optPacket = m_sender.Reserve(uiAllocSize);
|
||||
if (!optPacket)
|
||||
{
|
||||
if (m_eStatus == sdv::ipc::EConnectStatus::connected)
|
||||
if (m_eConnectState == sdv::ipc::EConnectState::connected)
|
||||
SDV_LOG_ERROR("Could not reserve a buffer to send a message of ", uiDataSize, " bytes.");
|
||||
return false;
|
||||
}
|
||||
@@ -277,7 +277,7 @@ bool CConnection::SendData(/*inout*/ sdv::sequence<sdv::pointer<uint8_t>>& seqDa
|
||||
uiMsgOffset = sizeof(SFragmentedMsgHdr);
|
||||
|
||||
#if ENABLE_REPORTING >= 3
|
||||
TRACE(m_bServer ? "SERVER" : "CLIENT", " SEND DATA FRAGMENT ", uiOffset, "-", uiOffset + uiDataSize - 1, " of ", uiRequiredSize, " bytes (", ConnectState(m_eStatus), ")");
|
||||
TRACE(m_bServer ? "SERVER" : "CLIENT", " SEND DATA FRAGMENT ", uiOffset, "-", uiOffset + uiDataSize - 1, " of ", uiRequiredSize, " bytes (", ConnectState2String(m_eConnectState), ")");
|
||||
#endif
|
||||
}
|
||||
else
|
||||
@@ -288,7 +288,7 @@ bool CConnection::SendData(/*inout*/ sdv::sequence<sdv::pointer<uint8_t>>& seqDa
|
||||
uiMsgOffset = sizeof(SMsgHdr);
|
||||
|
||||
#if ENABLE_REPORTING >= 3
|
||||
TRACE(m_bServer ? "SERVER" : "CLIENT", " SEND DATA ", uiRequiredSize, " bytes (", ConnectState(m_eStatus), ")");
|
||||
TRACE(m_bServer ? "SERVER" : "CLIENT", " SEND DATA ", uiRequiredSize, " bytes (", ConnectState2String(m_eConnectState), ")");
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -352,29 +352,29 @@ bool CConnection::AsyncConnect(sdv::IInterfaceAccess* pReceiver)
|
||||
std::unique_lock<std::mutex> lock(m_mtxConnect);
|
||||
|
||||
// Allowed to connect?
|
||||
if (m_eStatus != sdv::ipc::EConnectStatus::uninitialized)
|
||||
if (m_eConnectState != sdv::ipc::EConnectState::uninitialized)
|
||||
{
|
||||
for (auto& rprEventCallback : m_lstEventCallbacks)
|
||||
if (rprEventCallback.pCallback && rprEventCallback.uiCookie)
|
||||
rprEventCallback.pCallback->SetStatus(sdv::ipc::EConnectStatus::connection_error);
|
||||
rprEventCallback.pCallback->SetConnectState(sdv::ipc::EConnectState::connection_error);
|
||||
return false;
|
||||
}
|
||||
|
||||
SetStatus(sdv::ipc::EConnectStatus::initializing);
|
||||
SetConnectState(sdv::ipc::EConnectState::initializing);
|
||||
|
||||
// Initialized?
|
||||
if (!m_sender.IsValid() && !m_receiver.IsValid())
|
||||
{
|
||||
SetStatus(sdv::ipc::EConnectStatus::connection_error);
|
||||
SetConnectState(sdv::ipc::EConnectState::connection_error);
|
||||
SDV_LOG_ERROR("Could not establish connection: sender(", (m_sender.IsValid() ? "valid" : "invalid"), ") receiver(",
|
||||
(m_receiver.IsValid() ? "valid" : "invalid"), ")");
|
||||
SetStatus(sdv::ipc::EConnectStatus::uninitialized);
|
||||
SetConnectState(sdv::ipc::EConnectState::uninitialized);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Assign the receiver
|
||||
m_pReceiver = sdv::TInterfaceAccessPtr(pReceiver).GetInterface<sdv::ipc::IDataReceiveCallback>();
|
||||
SetStatus(sdv::ipc::EConnectStatus::initialized);
|
||||
SetConnectState(sdv::ipc::EConnectState::initialized);
|
||||
|
||||
// Start the receiving thread (wait until started).
|
||||
m_threadReceive = std::thread(&CConnection::ReceiveMessages, this);
|
||||
@@ -397,13 +397,13 @@ bool CConnection::AsyncConnect(sdv::IInterfaceAccess* pReceiver)
|
||||
bool CConnection::WaitForConnection(/*in*/ uint32_t uiWaitMs)
|
||||
{
|
||||
#if ENABLE_REPORTING >= 1
|
||||
if (m_eStatus == sdv::ipc::EConnectStatus::connected)
|
||||
if (m_eConnectState == sdv::ipc::EConnectState::connected)
|
||||
TRACE("Not waiting for a connection - already connected");
|
||||
else
|
||||
TRACE("Waiting for a connection of ", uiWaitMs, "ms");
|
||||
#endif
|
||||
|
||||
if (m_eStatus == sdv::ipc::EConnectStatus::connected) return true;
|
||||
if (m_eConnectState == sdv::ipc::EConnectState::connected) return true;
|
||||
|
||||
std::unique_lock<std::mutex> lock(m_mtxConnect);
|
||||
|
||||
@@ -413,13 +413,13 @@ bool CConnection::WaitForConnection(/*in*/ uint32_t uiWaitMs)
|
||||
m_cvConnect.wait_for(lock, std::chrono::milliseconds(uiWaitMs));
|
||||
|
||||
#if ENABLE_REPORTING >= 1
|
||||
if (m_eStatus == sdv::ipc::EConnectStatus::connected)
|
||||
if (m_eConnectState == sdv::ipc::EConnectState::connected)
|
||||
TRACE("Waiting finished - connection established");
|
||||
else
|
||||
TRACE("Waiting finished - timeout occurred");
|
||||
#endif
|
||||
|
||||
return m_eStatus == sdv::ipc::EConnectStatus::connected;
|
||||
return m_eConnectState == sdv::ipc::EConnectState::connected;
|
||||
}
|
||||
|
||||
void CConnection::CancelWait()
|
||||
@@ -440,19 +440,19 @@ void CConnection::Disconnect()
|
||||
// Cancel any waits, just in case
|
||||
CancelWait();
|
||||
|
||||
// Set the disconnect status
|
||||
sdv::ipc::EConnectStatus eStatus = m_eStatus;
|
||||
SetStatus(sdv::ipc::EConnectStatus::disconnected);
|
||||
// Set the disconnect state
|
||||
sdv::ipc::EConnectState eConnectState = m_eConnectState;
|
||||
SetConnectState(sdv::ipc::EConnectState::disconnected);
|
||||
|
||||
// Release the interface
|
||||
m_pReceiver = nullptr;
|
||||
|
||||
// If connected, send termination message.
|
||||
switch (eStatus)
|
||||
switch (eConnectState)
|
||||
{
|
||||
case sdv::ipc::EConnectStatus::connecting:
|
||||
case sdv::ipc::EConnectStatus::negotiating:
|
||||
case sdv::ipc::EConnectStatus::connected:
|
||||
case sdv::ipc::EConnectState::connecting:
|
||||
case sdv::ipc::EConnectState::negotiating:
|
||||
case sdv::ipc::EConnectState::connected:
|
||||
Send(SMsgHdr{ SDVFrameworkInterfaceVersion, EMsgType::connect_term });
|
||||
break;
|
||||
default:
|
||||
@@ -464,7 +464,7 @@ void CConnection::Disconnect()
|
||||
#endif
|
||||
}
|
||||
|
||||
uint64_t CConnection::RegisterStatusEventCallback(/*in*/ sdv::IInterfaceAccess* pEventCallback)
|
||||
uint64_t CConnection::RegisterStateEventCallback(/*in*/ sdv::IInterfaceAccess* pEventCallback)
|
||||
{
|
||||
if (!pEventCallback) return 0;
|
||||
sdv::ipc::IConnectEventCallback* pCallback = pEventCallback->GetInterface<sdv::ipc::IConnectEventCallback>();
|
||||
@@ -477,7 +477,7 @@ uint64_t CConnection::RegisterStatusEventCallback(/*in*/ sdv::IInterfaceAccess*
|
||||
return uiCookie;
|
||||
}
|
||||
|
||||
void CConnection::UnregisterStatusEventCallback(/*in*/ uint64_t uiCookie)
|
||||
void CConnection::UnregisterStateEventCallback(/*in*/ uint64_t uiCookie)
|
||||
{
|
||||
std::shared_lock<std::shared_mutex> lock(m_mtxEventCallbacks);
|
||||
auto itEventCallback = std::find_if(m_lstEventCallbacks.begin(), m_lstEventCallbacks.end(),
|
||||
@@ -486,9 +486,9 @@ void CConnection::UnregisterStatusEventCallback(/*in*/ uint64_t uiCookie)
|
||||
itEventCallback->pCallback = nullptr;
|
||||
}
|
||||
|
||||
sdv::ipc::EConnectStatus CConnection::GetStatus() const
|
||||
sdv::ipc::EConnectState CConnection::GetConnectState() const
|
||||
{
|
||||
return m_eStatus;
|
||||
return m_eConnectState;
|
||||
}
|
||||
|
||||
void CConnection::DestroyObject()
|
||||
@@ -500,8 +500,8 @@ void CConnection::DestroyObject()
|
||||
// Disconnect
|
||||
Disconnect();
|
||||
|
||||
// Set termination status.
|
||||
SetStatus(sdv::ipc::EConnectStatus::terminating);
|
||||
// Set termination state.
|
||||
SetConnectState(sdv::ipc::EConnectState::terminating);
|
||||
|
||||
// Clear all events callbacks (if not done so already)
|
||||
std::shared_lock<std::shared_mutex> lock(m_mtxEventCallbacks);
|
||||
@@ -523,32 +523,32 @@ void CConnection::DestroyObject()
|
||||
#endif
|
||||
}
|
||||
|
||||
void CConnection::SetStatus(sdv::ipc::EConnectStatus eStatus)
|
||||
void CConnection::SetConnectState(sdv::ipc::EConnectState eConnectState)
|
||||
{
|
||||
#if ENABLE_REPORTING >= 1
|
||||
TRACE(m_bServer ? "SERVER" : "CLIENT", " Changing connect status from '", ConnectState(m_eStatus), "' to '", ConnectState(eStatus), "'");
|
||||
TRACE(m_bServer ? "SERVER" : "CLIENT", " Changing connect state from '", ConnectState2String(m_eConnectState), "' to '", ConnectState2String(eConnectState), "'");
|
||||
#endif
|
||||
|
||||
// Do not change the status when terminated.
|
||||
if (m_eStatus == sdv::ipc::EConnectStatus::terminating)
|
||||
// Do not change the state when terminated.
|
||||
if (m_eConnectState == sdv::ipc::EConnectState::terminating)
|
||||
return;
|
||||
|
||||
// Only set the member variable if the status is not communication_error
|
||||
if (eStatus != sdv::ipc::EConnectStatus::communication_error)
|
||||
m_eStatus = eStatus;
|
||||
// Only set the member variable if the state is not communication_error
|
||||
if (eConnectState != sdv::ipc::EConnectState::communication_error)
|
||||
m_eConnectState = eConnectState;
|
||||
std::shared_lock<std::shared_mutex> lock(m_mtxEventCallbacks);
|
||||
for (auto& rprEventCallback : m_lstEventCallbacks)
|
||||
{
|
||||
if (rprEventCallback.pCallback && rprEventCallback.uiCookie)
|
||||
rprEventCallback.pCallback->SetStatus(eStatus);
|
||||
rprEventCallback.pCallback->SetConnectState(eConnectState);
|
||||
}
|
||||
|
||||
// If disconnected by force update the disconnect status.
|
||||
if (m_eStatus == sdv::ipc::EConnectStatus::disconnected_forced)
|
||||
m_eStatus = sdv::ipc::EConnectStatus::disconnected;
|
||||
// If disconnected by force update the disconnect state.
|
||||
if (m_eConnectState == sdv::ipc::EConnectState::disconnected_forced)
|
||||
m_eConnectState = sdv::ipc::EConnectState::disconnected;
|
||||
|
||||
#if ENABLE_REPORTING >= 1
|
||||
TRACE("Status updated...");
|
||||
TRACE("State updated...");
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -574,7 +574,7 @@ void CConnection::ReceiveMessages()
|
||||
|
||||
if (!m_sender.IsValid() || !m_receiver.IsValid())
|
||||
{
|
||||
SetStatus(sdv::ipc::EConnectStatus::communication_error);
|
||||
SetConnectState(sdv::ipc::EConnectState::communication_error);
|
||||
SDV_LOG_ERROR("No valid shared memory for receiving.");
|
||||
lock.unlock();
|
||||
m_cvStartConnect.notify_all();
|
||||
@@ -590,7 +590,7 @@ void CConnection::ReceiveMessages()
|
||||
std::chrono::high_resolution_clock::time_point tpLastReceiveLoop = std::chrono::high_resolution_clock::now();
|
||||
#endif
|
||||
SDataContext sDataCtxt;
|
||||
while (m_eStatus != sdv::ipc::EConnectStatus::terminating)
|
||||
while (m_eConnectState != sdv::ipc::EConnectState::terminating)
|
||||
{
|
||||
#ifdef TIME_TRACKING
|
||||
std::chrono::high_resolution_clock::time_point tpTrackNow = std::chrono::high_resolution_clock::now();
|
||||
@@ -604,7 +604,7 @@ void CConnection::ReceiveMessages()
|
||||
{
|
||||
// Start communication, but only if connection is client based. Server based should not start the communication. If
|
||||
// there is no client, the server would otherwise fill its send-buffer. Repeat sending every 500ms.
|
||||
if (!m_bServer && (/*m_eStatus == sdv::ipc::EConnectStatus::disconnected ||*/ m_eStatus == sdv::ipc::EConnectStatus::initialized))
|
||||
if (!m_bServer && (/*m_eConnectState == sdv::ipc::EConnectState::disconnected ||*/ m_eConnectState == sdv::ipc::EConnectState::initialized))
|
||||
{
|
||||
// Send request
|
||||
auto tpNow = std::chrono::high_resolution_clock::now();
|
||||
@@ -629,7 +629,7 @@ void CConnection::ReceiveMessages()
|
||||
if (!message.IsValid())
|
||||
{
|
||||
auto ssError = m_receiver.GetError();
|
||||
SetStatus(sdv::ipc::EConnectStatus::communication_error);
|
||||
SetConnectState(sdv::ipc::EConnectState::communication_error);
|
||||
SDV_LOG_ERROR("The message is invalid (invalid size or invalid type).");
|
||||
continue;
|
||||
}
|
||||
@@ -638,7 +638,7 @@ void CConnection::ReceiveMessages()
|
||||
message.PrintHeader(*this);
|
||||
|
||||
// Extra check to prevent race condition.
|
||||
if (m_eStatus == sdv::ipc::EConnectStatus::terminating) break;
|
||||
if (m_eConnectState == sdv::ipc::EConnectState::terminating) break;
|
||||
|
||||
#if ENABLE_REPORTING >= 1
|
||||
switch (message.GetMsgHdr().eType)
|
||||
@@ -846,7 +846,7 @@ bool CConnection::ReadDataChunk(CMessage& rMessage, uint32_t uiOffset, SDataCont
|
||||
std::unique_lock<std::mutex> lockReceive(m_mtxReceive);
|
||||
while (m_queueReceive.size() >= 16)
|
||||
{
|
||||
if (m_eStatus == sdv::ipc::EConnectStatus::terminating) return false;
|
||||
if (m_eConnectState == sdv::ipc::EConnectState::terminating) return false;
|
||||
m_cvReceiveProcessed.wait_for(lockReceive, std::chrono::milliseconds(100));
|
||||
}
|
||||
|
||||
@@ -868,7 +868,7 @@ bool CConnection::ReadDataChunk(CMessage& rMessage, uint32_t uiOffset, SDataCont
|
||||
#if ENABLE_DECOUPLING > 0
|
||||
void CConnection::DecoupleReceive()
|
||||
{
|
||||
while (m_eStatus != sdv::ipc::EConnectStatus::terminating)
|
||||
while (m_eConnectState != sdv::ipc::EConnectState::terminating)
|
||||
{
|
||||
// Wait for data
|
||||
std::unique_lock<std::mutex> lock(m_mtxReceive);
|
||||
@@ -887,7 +887,7 @@ void CConnection::DecoupleReceive()
|
||||
size_t nSize = 0;
|
||||
for (const sdv::pointer<uint8_t>& ptrData : seqData)
|
||||
nSize += ptrData.size();
|
||||
TRACE(m_bServer ? "SERVER" : "CLIENT", " DECOUPLED REVEICE DATA ", nSize, " bytes (", ConnectState(m_eStatus), ")");
|
||||
TRACE(m_bServer ? "SERVER" : "CLIENT", " DECOUPLED REVEICE DATA ", nSize, " bytes (", ConnectState2String(m_eConnectState), ")");
|
||||
#endif
|
||||
|
||||
// Process the data
|
||||
@@ -903,28 +903,28 @@ void CConnection::ReceiveSyncRequest(const CMessage& rMessage)
|
||||
{
|
||||
if (rMessage.GetSize() != sizeof(SMsgHdr))
|
||||
{
|
||||
SetStatus(sdv::ipc::EConnectStatus::connection_error);
|
||||
SetConnectState(sdv::ipc::EConnectState::connection_error);
|
||||
SDV_LOG_ERROR("Sync request received but with incorrect structure size ", rMessage.GetSize(), " in the request, but ",
|
||||
sizeof(SMsgHdr), " needed!");
|
||||
SetStatus(sdv::ipc::EConnectStatus::disconnected);
|
||||
SetConnectState(sdv::ipc::EConnectState::disconnected);
|
||||
return;
|
||||
}
|
||||
|
||||
// Check for compatibility
|
||||
if (rMessage.GetMsgHdr().uiVersion != SDVFrameworkInterfaceVersion)
|
||||
{
|
||||
SetStatus(sdv::ipc::EConnectStatus::connection_error);
|
||||
SetConnectState(sdv::ipc::EConnectState::connection_error);
|
||||
SDV_LOG_ERROR("Sync request received for an incompatible communication; interface version ", rMessage.GetMsgHdr().uiVersion,
|
||||
" requested, but ", SDVFrameworkInterfaceVersion, " needed!");
|
||||
SetStatus(sdv::ipc::EConnectStatus::disconnected);
|
||||
SetConnectState(sdv::ipc::EConnectState::disconnected);
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Start connecting
|
||||
if (m_eStatus == sdv::ipc::EConnectStatus::disconnected || m_eStatus == sdv::ipc::EConnectStatus::initialized)
|
||||
if (m_eConnectState == sdv::ipc::EConnectState::disconnected || m_eConnectState == sdv::ipc::EConnectState::initialized)
|
||||
{
|
||||
SetStatus(sdv::ipc::EConnectStatus::connecting);
|
||||
SetConnectState(sdv::ipc::EConnectState::connecting);
|
||||
|
||||
// Send an answer
|
||||
Send(SMsgHdr{ SDVFrameworkInterfaceVersion, EMsgType::sync_answer });
|
||||
@@ -935,18 +935,18 @@ void CConnection::ReceiveSyncRequest(const CMessage& rMessage)
|
||||
void CConnection::ReceiveConnectRequest(const CMessage& rMessage)
|
||||
{
|
||||
// Start negotiating
|
||||
if (m_eStatus == sdv::ipc::EConnectStatus::connecting)
|
||||
if (m_eConnectState == sdv::ipc::EConnectState::connecting)
|
||||
{
|
||||
// The connect message contains the process ID to monitor.
|
||||
m_rWatchDog.AddMonitor(rMessage.GetConnectHdr().tProcessID, this);
|
||||
|
||||
// Replay to the request
|
||||
SetStatus(sdv::ipc::EConnectStatus::negotiating);
|
||||
SetConnectState(sdv::ipc::EConnectState::negotiating);
|
||||
Send(SConnectMsg{ {SDVFrameworkInterfaceVersion, EMsgType::connect_answer},
|
||||
static_cast<sdv::process::TProcessID>(GetProcessID()) });
|
||||
|
||||
// Connected
|
||||
SetStatus(sdv::ipc::EConnectStatus::connected);
|
||||
SetConnectState(sdv::ipc::EConnectState::connected);
|
||||
#if ENABLE_REPORTING >= 1
|
||||
TRACE("Trigger connected");
|
||||
#endif
|
||||
@@ -956,34 +956,34 @@ void CConnection::ReceiveConnectRequest(const CMessage& rMessage)
|
||||
|
||||
void CConnection::ReceiveSyncAnswer(const CMessage& rMessage)
|
||||
{
|
||||
if (m_eStatus != sdv::ipc::EConnectStatus::disconnected && m_eStatus != sdv::ipc::EConnectStatus::initialized)
|
||||
if (m_eConnectState != sdv::ipc::EConnectState::disconnected && m_eConnectState != sdv::ipc::EConnectState::initialized)
|
||||
return;
|
||||
|
||||
// Check for compatibility
|
||||
if (rMessage.GetMsgHdr().uiVersion != SDVFrameworkInterfaceVersion)
|
||||
{
|
||||
SetStatus(sdv::ipc::EConnectStatus::communication_error);
|
||||
SetConnectState(sdv::ipc::EConnectState::communication_error);
|
||||
SDV_LOG_ERROR("Sync answer received for an incompatible communication; interface version ",
|
||||
rMessage.GetMsgHdr().uiVersion, " requested, but ", SDVFrameworkInterfaceVersion, " needed!");
|
||||
SetStatus(sdv::ipc::EConnectStatus::disconnected);
|
||||
SetConnectState(sdv::ipc::EConnectState::disconnected);
|
||||
return;
|
||||
}
|
||||
|
||||
// Start negotiating
|
||||
SetStatus(sdv::ipc::EConnectStatus::negotiating);
|
||||
SetConnectState(sdv::ipc::EConnectState::negotiating);
|
||||
Send(SConnectMsg{ {SDVFrameworkInterfaceVersion, EMsgType::connect_request}, GetProcessID() });
|
||||
}
|
||||
|
||||
void CConnection::ReceiveConnectAnswer(const CMessage& rMessage)
|
||||
{
|
||||
// Connection established
|
||||
if (m_eStatus == sdv::ipc::EConnectStatus::negotiating)
|
||||
if (m_eConnectState == sdv::ipc::EConnectState::negotiating)
|
||||
{
|
||||
// The connect message contains the process ID to monitor.
|
||||
m_rWatchDog.AddMonitor(rMessage.GetConnectHdr().tProcessID, this);
|
||||
|
||||
// Connected
|
||||
SetStatus(sdv::ipc::EConnectStatus::connected);
|
||||
SetConnectState(sdv::ipc::EConnectState::connected);
|
||||
#if ENABLE_REPORTING >= 1
|
||||
TRACE("Trigger connected...");
|
||||
#endif
|
||||
@@ -993,7 +993,7 @@ void CConnection::ReceiveConnectAnswer(const CMessage& rMessage)
|
||||
|
||||
void CConnection::ReceiveConnectTerm(CMessage& /*rMessage*/)
|
||||
{
|
||||
SetStatus(sdv::ipc::EConnectStatus::disconnected);
|
||||
SetConnectState(sdv::ipc::EConnectState::disconnected);
|
||||
m_rWatchDog.RemoveMonitor(this);
|
||||
|
||||
// Cancel any outstanding write... and reset the read position of the sender (otherwise any outstanding data will have
|
||||
@@ -1016,7 +1016,7 @@ void CConnection::ReceiveDataMessage(CMessage& rMessage, SDataContext& rsDataCtx
|
||||
uint32_t uiOffset = ReadDataTable(rMessage, rsDataCtxt);
|
||||
if (!uiOffset)
|
||||
{
|
||||
SetStatus(sdv::ipc::EConnectStatus::communication_error);
|
||||
SetConnectState(sdv::ipc::EConnectState::communication_error);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1033,7 +1033,7 @@ void CConnection::ReceiveDataMessage(CMessage& rMessage, SDataContext& rsDataCtx
|
||||
// Read data
|
||||
if (!ReadDataChunk(rMessage, uiOffset, rsDataCtxt))
|
||||
{
|
||||
SetStatus(sdv::ipc::EConnectStatus::communication_error);
|
||||
SetConnectState(sdv::ipc::EConnectState::communication_error);
|
||||
return;
|
||||
}
|
||||
#if ENABLE_REPORTING >= 1
|
||||
@@ -1057,7 +1057,7 @@ void CConnection::ReceiveDataFragementMessage(CMessage& rMessage, SDataContext&
|
||||
uiOffset = ReadDataTable(rMessage, rsDataCtxt);
|
||||
if (!uiOffset)
|
||||
{
|
||||
SetStatus(sdv::ipc::EConnectStatus::communication_error);
|
||||
SetConnectState(sdv::ipc::EConnectState::communication_error);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1075,7 +1075,7 @@ void CConnection::ReceiveDataFragementMessage(CMessage& rMessage, SDataContext&
|
||||
// Read data chunk
|
||||
if (!ReadDataChunk(rMessage, uiOffset, rsDataCtxt))
|
||||
{
|
||||
SetStatus(sdv::ipc::EConnectStatus::communication_error);
|
||||
SetConnectState(sdv::ipc::EConnectState::communication_error);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -1147,19 +1147,19 @@ void CConnection::CMessage::PrintHeader([[maybe_unused]] const CConnection& rCon
|
||||
#if ENABLE_REPORTING >= 2
|
||||
switch (GetMsgHdr().eType)
|
||||
{
|
||||
case EMsgType::sync_request: rConnection.TRACE(rConnection.IsServer() ? "SERVER" : "CLIENT", " RECEIVE SYNC_REQUEST (", ConnectState(rConnection.GetStatus()), ")"); break;
|
||||
case EMsgType::sync_answer: rConnection.TRACE(rConnection.IsServer() ? "SERVER" : "CLIENT", " RECEIVE SYNC_ANSWER (", ConnectState(rConnection.GetStatus()), ")"); break;
|
||||
case EMsgType::connect_request: rConnection.TRACE(rConnection.IsServer() ? "SERVER" : "CLIENT", " RECEIVE CONNECT_REQUEST (", ConnectState(rConnection.GetStatus()), ")"); break;
|
||||
case EMsgType::connect_answer: rConnection.TRACE(rConnection.IsServer() ? "SERVER" : "CLIENT", " RECEIVE CONNECT_ANSWER (", ConnectState(rConnection.GetStatus()), ")"); break;
|
||||
case EMsgType::connect_term: rConnection.TRACE(rConnection.IsServer() ? "SERVER" : "CLIENT", " RECEIVE CONNECT_TERM (", ConnectState(rConnection.GetStatus()), ")"); break;
|
||||
case EMsgType::sync_request: rConnection.TRACE(rConnection.IsServer() ? "SERVER" : "CLIENT", " RECEIVE SYNC_REQUEST (", ConnectState2String(rConnection.GetConnectState()), ")"); break;
|
||||
case EMsgType::sync_answer: rConnection.TRACE(rConnection.IsServer() ? "SERVER" : "CLIENT", " RECEIVE SYNC_ANSWER (", ConnectState2String(rConnection.GetConnectState()), ")"); break;
|
||||
case EMsgType::connect_request: rConnection.TRACE(rConnection.IsServer() ? "SERVER" : "CLIENT", " RECEIVE CONNECT_REQUEST (", ConnectState2String(rConnection.GetConnectState()), ")"); break;
|
||||
case EMsgType::connect_answer: rConnection.TRACE(rConnection.IsServer() ? "SERVER" : "CLIENT", " RECEIVE CONNECT_ANSWER (", ConnectState2String(rConnection.GetConnectState()), ")"); break;
|
||||
case EMsgType::connect_term: rConnection.TRACE(rConnection.IsServer() ? "SERVER" : "CLIENT", " RECEIVE CONNECT_TERM (", ConnectState2String(rConnection.GetConnectState()), ")"); break;
|
||||
#if ENABLE_REPORTING >= 3
|
||||
case EMsgType::data: rConnection.TRACE(rConnection.IsServer() ? "SERVER" : "CLIENT", " RECEIVE DATA ", GetSize() - sizeof(SMsgHdr), " bytes (", ConnectState(rConnection.GetStatus()), ")"); break;
|
||||
case EMsgType::data_fragment: rConnection.TRACE(rConnection.IsServer() ? "SERVER" : "CLIENT", " RECEIVE DATA FRAGMENT ", GetSize() - sizeof(SFragmentedMsgHdr), " bytes (", ConnectState(rConnection.GetStatus()), ")"); break;
|
||||
case EMsgType::data: rConnection.TRACE(rConnection.IsServer() ? "SERVER" : "CLIENT", " RECEIVE DATA ", GetSize() - sizeof(SMsgHdr), " bytes (", ConnectState2String(rConnection.GetConnectState()), ")"); break;
|
||||
case EMsgType::data_fragment: rConnection.TRACE(rConnection.IsServer() ? "SERVER" : "CLIENT", " RECEIVE DATA FRAGMENT ", GetSize() - sizeof(SFragmentedMsgHdr), " bytes (", ConnectState2String(rConnection.GetConnectState()), ")"); break;
|
||||
#else
|
||||
case EMsgType::data: break;
|
||||
case EMsgType::data_fragment: break;
|
||||
#endif
|
||||
default: rConnection.TRACE(rConnection.IsServer() ? "SERVER" : "CLIENT", " RECEIVE UNKNOWN version=", static_cast<uint32_t>(GetMsgHdr().uiVersion), " type=", static_cast<uint32_t>(GetMsgHdr().eType), "(", ConnectState(rConnection.GetStatus()), ")"); break;
|
||||
default: rConnection.TRACE(rConnection.IsServer() ? "SERVER" : "CLIENT", " RECEIVE UNKNOWN version=", static_cast<uint32_t>(GetMsgHdr().uiVersion), " type=", static_cast<uint32_t>(GetMsgHdr().eType), "(", ConnectState2String(rConnection.GetConnectState()), ")"); break;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -106,7 +106,7 @@ public:
|
||||
* @brief Establish a connection and start sending/receiving messages. Overload of
|
||||
* sdv::ipc::IConnect::AsyncConnect.
|
||||
* @param[in] pReceiver The message has to be forwarded.
|
||||
* @return Returns 'true' when a connection could be established. Use IConnectStatus or IConnectEventCallback to check the
|
||||
* @return Returns 'true' when a connection could be established. Use IConnect or IConnectEventCallback to check the
|
||||
* connection state.
|
||||
*/
|
||||
virtual bool AsyncConnect(/*in*/ sdv::IInterfaceAccess* pReceiver) override;
|
||||
@@ -127,33 +127,33 @@ public:
|
||||
// Suppress cppcheck warning. The destructor calls Disconnect without dynamic binding. This is correct so.
|
||||
// cppcheck-suppress virtualCallInConstructor
|
||||
/**
|
||||
* @brief Disconnect from a connection. This will set the connect status to disconnected. Overload of
|
||||
* @brief Disconnect from a connection. This will set the connect state to disconnected. Overload of
|
||||
* sdv::ipc::IConnect::Disconnect.
|
||||
*/
|
||||
virtual void Disconnect() override;
|
||||
|
||||
/**
|
||||
* @brief Register event callback interface. Overload of sdv::ipc::IConnect::RegisterStatusEventCallback.
|
||||
* @details Register a connection status event callback interface. The exposed interface must be of type
|
||||
* @brief Register event callback interface. Overload of sdv::ipc::IConnect::RegisterStateEventCallback.
|
||||
* @details Register a connection state event callback interface. The exposed interface must be of type
|
||||
* IConnectEventCallback. The registration will exist until a call to the unregister function with the returned cookie
|
||||
* or until the connection is terminated.
|
||||
* @param[in] pEventCallback Pointer to the object exposing the IConnectEventCallback interface.
|
||||
* @return The cookie assigned to the registration.
|
||||
*/
|
||||
virtual uint64_t RegisterStatusEventCallback(/*in*/ sdv::IInterfaceAccess* pEventCallback) override;
|
||||
virtual uint64_t RegisterStateEventCallback(/*in*/ sdv::IInterfaceAccess* pEventCallback) override;
|
||||
|
||||
/**
|
||||
* @brief Unregister the status event callback with the returned cookie from the registration. Overload of
|
||||
* sdv::ipc::IConnect::UnregisterStatusEventCallback.
|
||||
* @brief Unregister the state event callback with the returned cookie from the registration. Overload of
|
||||
* sdv::ipc::IConnect::UnregisterStateEventCallback.
|
||||
* @param[in] uiCookie The cookie returned by a previous call to the registration function.
|
||||
*/
|
||||
virtual void UnregisterStatusEventCallback(/*in*/ uint64_t uiCookie) override;
|
||||
virtual void UnregisterStateEventCallback(/*in*/ uint64_t uiCookie) override;
|
||||
|
||||
/**
|
||||
* @brief Get status of the connection. Overload of sdv::ipc::IConnect::GetStatus.
|
||||
* @return Returns the ipc::EConnectStatus struct
|
||||
* @brief Get the current state of the IPC conection. Overload of sdv::ipc::IConnect::GetConnectState.
|
||||
* @return Returns connection state.
|
||||
*/
|
||||
virtual sdv::ipc::EConnectStatus GetStatus() const override;
|
||||
virtual sdv::ipc::EConnectState GetConnectState() const override;
|
||||
|
||||
/**
|
||||
* @brief Destroy the object. Overload of IObjectDestroy::DestroyObject.
|
||||
@@ -162,10 +162,10 @@ public:
|
||||
virtual void DestroyObject() override;
|
||||
|
||||
/**
|
||||
* @brief Set the connection status and if needed call the event callback.
|
||||
* @param[in] eStatus The new status.
|
||||
* @brief Set the connection state and if needed call the event callback.
|
||||
* @param[in] eConnectState The new state.
|
||||
*/
|
||||
void SetStatus(sdv::ipc::EConnectStatus eStatus);
|
||||
void SetConnectState(sdv::ipc::EConnectState eConnectState);
|
||||
|
||||
/**
|
||||
* @brief Returns whether this is a server connection or a client connection.
|
||||
@@ -258,14 +258,14 @@ private:
|
||||
CSharedMemBufferTx m_sender; ///< Shared buffer for sending.
|
||||
CSharedMemBufferRx m_receiver; ///< Shared buffer for receiving.
|
||||
std::thread m_threadReceive; ///< Thread which receives data from the socket.
|
||||
std::atomic<sdv::ipc::EConnectStatus> m_eStatus = sdv::ipc::EConnectStatus::uninitialized; ///< the status of the connection
|
||||
std::atomic<sdv::ipc::EConnectState> m_eConnectState = sdv::ipc::EConnectState::uninitialized; ///< the state of the connection
|
||||
sdv::ipc::IDataReceiveCallback* m_pReceiver = nullptr; ///< Receiver to pass the messages to if available
|
||||
std::shared_mutex m_mtxEventCallbacks; ///< Protect access to callback list. Only locking when
|
||||
///< inserting.
|
||||
std::list<SEventCallback> m_lstEventCallbacks; ///< List containing event callbacks. New callbacks will
|
||||
///< be inserted in front (called first). Removed
|
||||
///< callbacks are NULL; the entry stays to allow
|
||||
///< removal during a SetStatus call.
|
||||
///< removal during a SetConnectState call.
|
||||
mutable std::mutex m_mtxSend; ///< Synchronize all packages to be sent.
|
||||
std::mutex m_mtxConnect; ///< Connection mutex.
|
||||
std::condition_variable m_cvConnect; ///< Connection variable for connecting.
|
||||
|
||||
@@ -196,7 +196,7 @@ void CWatchDog::ProcessTerminated(/*in*/ sdv::process::TProcessID tProcessID, /*
|
||||
// Inform the connection about the removed process.
|
||||
for (auto& rptrConnection : vecDisconnectedConnections)
|
||||
{
|
||||
rptrConnection->SetStatus(sdv::ipc::EConnectStatus::disconnected_forced);
|
||||
rptrConnection->SetConnectState(sdv::ipc::EConnectState::disconnected_forced);
|
||||
|
||||
#if ENABLE_REPORTING > 0
|
||||
TRACE("Forced disconnection for PID#", tProcessID);
|
||||
|
||||
@@ -13,16 +13,13 @@ if(UNIX)
|
||||
project(uds_unix_sockets VERSION 1.0 LANGUAGES CXX)
|
||||
|
||||
# Define target
|
||||
add_library(uds_unix_sockets SHARED
|
||||
"channel_mgnt.h"
|
||||
"channel_mgnt.cpp"
|
||||
"connection.h"
|
||||
"connection.cpp"
|
||||
add_library(uds_unix_sockets STATIC
|
||||
channel_mgnt.cpp
|
||||
connection.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(uds_unix_sockets rt ${CMAKE_DL_LIBS} ${CMAKE_THREAD_LIBS_INIT})
|
||||
|
||||
target_link_options(uds_unix_sockets PRIVATE)
|
||||
target_include_directories(uds_unix_sockets PRIVATE ./include/)
|
||||
set_target_properties(uds_unix_sockets PROPERTIES PREFIX "")
|
||||
set_target_properties(uds_unix_sockets PROPERTIES SUFFIX ".sdv")
|
||||
|
||||
@@ -105,7 +105,7 @@ CUnixSocketConnection::CUnixSocketConnection(int preconfiguredFd,
|
||||
, m_UdsPath(udsPath)
|
||||
, m_StopReceiveThread(false)
|
||||
, m_StopConnectThread(false)
|
||||
, m_eStatus(sdv::ipc::EConnectStatus::uninitialized)
|
||||
, m_eConnectState(sdv::ipc::EConnectState::uninitialized)
|
||||
, m_pReceiver(nullptr)
|
||||
, m_pEvent(nullptr)
|
||||
{
|
||||
@@ -170,10 +170,10 @@ bool CUnixSocketConnection::SendData(sdv::sequence<sdv::pointer<uint8_t>>& seqDa
|
||||
#endif
|
||||
|
||||
// Only send when connected and FD valid
|
||||
if (m_eStatus != sdv::ipc::EConnectStatus::connected || m_Fd < 0)
|
||||
if (m_eConnectState != sdv::ipc::EConnectState::connected || m_Fd < 0)
|
||||
{
|
||||
SetStatus(sdv::ipc::EConnectStatus::communication_error);
|
||||
SDV_LOG_WARNING("[UDS][TX] Send requested while not connected or FD invalid (status=", static_cast<int>(m_eStatus.load()), ")");
|
||||
SetConnectState(sdv::ipc::EConnectState::communication_error);
|
||||
SDV_LOG_WARNING("[UDS][TX] Send requested while not connected or FD invalid (state=", static_cast<int>(m_eConnectState.load()), ")");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -328,7 +328,7 @@ bool CUnixSocketConnection::SendData(sdv::sequence<sdv::pointer<uint8_t>>& seqDa
|
||||
return true;
|
||||
}
|
||||
|
||||
uint64_t CUnixSocketConnection::RegisterStatusEventCallback(sdv::IInterfaceAccess* pEventCallback)
|
||||
uint64_t CUnixSocketConnection::RegisterStateEventCallback(sdv::IInterfaceAccess* pEventCallback)
|
||||
{
|
||||
if (!pEventCallback) return 0;
|
||||
|
||||
@@ -351,7 +351,7 @@ uint64_t CUnixSocketConnection::RegisterStatusEventCallback(sdv::IInterfaceAcces
|
||||
return cookie;
|
||||
}
|
||||
|
||||
void CUnixSocketConnection::UnregisterStatusEventCallback(uint64_t uiCookie)
|
||||
void CUnixSocketConnection::UnregisterStateEventCallback(uint64_t uiCookie)
|
||||
{
|
||||
if (!uiCookie) return;
|
||||
|
||||
@@ -386,7 +386,7 @@ bool CUnixSocketConnection::AsyncConnect(sdv::IInterfaceAccess* pReceiver)
|
||||
std::lock_guard<std::mutex> lk(m_StateMtx);
|
||||
m_pReceiver = sdv::TInterfaceAccessPtr(pReceiver).GetInterface<sdv::ipc::IDataReceiveCallback>();
|
||||
m_pEvent = sdv::TInterfaceAccessPtr(pReceiver).GetInterface<sdv::ipc::IConnectEventCallback>();
|
||||
m_eStatus = sdv::ipc::EConnectStatus::initializing;
|
||||
m_eConnectState = sdv::ipc::EConnectState::initializing;
|
||||
|
||||
// Reset stop flags
|
||||
m_StopReceiveThread.store(false);
|
||||
@@ -450,22 +450,22 @@ int CUnixSocketConnection::AcceptConnection() // deprecated
|
||||
|
||||
bool CUnixSocketConnection::WaitForConnection(uint32_t uiWaitMs)
|
||||
{
|
||||
if (m_eStatus.load(std::memory_order_acquire) == sdv::ipc::EConnectStatus::connected) return true;
|
||||
if (m_eConnectState.load(std::memory_order_acquire) == sdv::ipc::EConnectState::connected) return true;
|
||||
|
||||
std::unique_lock<std::mutex> lk(m_MtxConnect);
|
||||
|
||||
if (uiWaitMs == 0xFFFFFFFFu)
|
||||
{
|
||||
m_CvConnect.wait(lk, [this]{
|
||||
return m_eStatus.load(std::memory_order_acquire) == sdv::ipc::EConnectStatus::connected;
|
||||
return m_eConnectState.load(std::memory_order_acquire) == sdv::ipc::EConnectState::connected;
|
||||
});
|
||||
return true;
|
||||
}
|
||||
if (uiWaitMs == 0u)
|
||||
return (m_eStatus.load(std::memory_order_acquire) == sdv::ipc::EConnectStatus::connected);
|
||||
return (m_eConnectState.load(std::memory_order_acquire) == sdv::ipc::EConnectState::connected);
|
||||
|
||||
return m_CvConnect.wait_for(lk, std::chrono::milliseconds(uiWaitMs),
|
||||
[this]{ return m_eStatus.load(std::memory_order_acquire) == sdv::ipc::EConnectStatus::connected; });
|
||||
[this]{ return m_eConnectState.load(std::memory_order_acquire) == sdv::ipc::EConnectState::connected; });
|
||||
}
|
||||
|
||||
void CUnixSocketConnection::CancelWait()
|
||||
@@ -476,33 +476,33 @@ void CUnixSocketConnection::CancelWait()
|
||||
void CUnixSocketConnection::Disconnect()
|
||||
{
|
||||
StopThreadsAndCloseSockets(/*unlinkPath*/ false);
|
||||
SetStatus(sdv::ipc::EConnectStatus::disconnected);
|
||||
SetConnectState(sdv::ipc::EConnectState::disconnected);
|
||||
}
|
||||
|
||||
sdv::ipc::EConnectStatus CUnixSocketConnection::GetStatus() const
|
||||
sdv::ipc::EConnectState CUnixSocketConnection::GetConnectState() const
|
||||
{
|
||||
return m_eStatus;
|
||||
return m_eConnectState;
|
||||
}
|
||||
|
||||
void CUnixSocketConnection::DestroyObject()
|
||||
{
|
||||
m_StopReceiveThread.store(true);
|
||||
m_eStatus = sdv::ipc::EConnectStatus::disconnected;
|
||||
m_eConnectState = sdv::ipc::EConnectState::disconnected;
|
||||
}
|
||||
|
||||
void CUnixSocketConnection::SetStatus(sdv::ipc::EConnectStatus eStatus)
|
||||
void CUnixSocketConnection::SetConnectState(sdv::ipc::EConnectState eConnectState)
|
||||
{
|
||||
// Update internal state atomically and wake up waiters.
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(m_MtxConnect);
|
||||
m_eStatus.store(eStatus, std::memory_order_release);
|
||||
m_eConnectState.store(eConnectState, std::memory_order_release);
|
||||
}
|
||||
m_CvConnect.notify_all();
|
||||
|
||||
// Notify the legacy single-listener (kept for backward compatibility).
|
||||
try
|
||||
{
|
||||
m_pEvent->SetStatus(eStatus);
|
||||
m_pEvent->SetConnectState(eConnectState);
|
||||
}
|
||||
catch (...) { /* swallow: user callback must not crash transport */ }
|
||||
|
||||
@@ -515,7 +515,7 @@ void CUnixSocketConnection::SetStatus(sdv::ipc::EConnectStatus eStatus)
|
||||
if (!entry.pCallback) { needCompact = true; continue; }
|
||||
try
|
||||
{
|
||||
entry.pCallback->SetStatus(eStatus);
|
||||
entry.pCallback->SetConnectState(eConnectState);
|
||||
}
|
||||
catch (...) { /* swallow per-listener */ }
|
||||
}
|
||||
@@ -588,7 +588,7 @@ void CUnixSocketConnection::ConnectWorker()
|
||||
if (!EnsureDir(dir))
|
||||
{
|
||||
SDV_LOG_ERROR("[UDS][Server] ensure_dir('", dir, "') failed: ", std::strerror(errno));
|
||||
SetStatus(sdv::ipc::EConnectStatus::connection_error);
|
||||
SetConnectState(sdv::ipc::EConnectState::connection_error);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -596,7 +596,7 @@ void CUnixSocketConnection::ConnectWorker()
|
||||
if (m_ListenFd < 0)
|
||||
{
|
||||
SDV_LOG_ERROR("[UDS][Server] socket() failed: ", std::strerror(errno));
|
||||
SetStatus(sdv::ipc::EConnectStatus::connection_error);
|
||||
SetConnectState(sdv::ipc::EConnectState::connection_error);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -608,7 +608,7 @@ void CUnixSocketConnection::ConnectWorker()
|
||||
{
|
||||
SDV_LOG_ERROR("[UDS][Server] bind('", m_UdsPath, "') failed: ", std::strerror(errno));
|
||||
::close(m_ListenFd); m_ListenFd = -1;
|
||||
SetStatus(sdv::ipc::EConnectStatus::connection_error);
|
||||
SetConnectState(sdv::ipc::EConnectState::connection_error);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -618,7 +618,7 @@ void CUnixSocketConnection::ConnectWorker()
|
||||
{
|
||||
SDV_LOG_ERROR("[UDS][Server] listen() failed: ", std::strerror(errno));
|
||||
::close(m_ListenFd); m_ListenFd = -1;
|
||||
SetStatus(sdv::ipc::EConnectStatus::connection_error);
|
||||
SetConnectState(sdv::ipc::EConnectState::connection_error);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -670,12 +670,12 @@ void CUnixSocketConnection::ConnectWorker()
|
||||
}
|
||||
if (clientFd < 0)
|
||||
{
|
||||
SetStatus(sdv::ipc::EConnectStatus::connection_error);
|
||||
SetConnectState(sdv::ipc::EConnectState::connection_error);
|
||||
return;
|
||||
}
|
||||
|
||||
m_Fd = clientFd;
|
||||
SetStatus(sdv::ipc::EConnectStatus::connected);
|
||||
SetConnectState(sdv::ipc::EConnectState::connected);
|
||||
StartReceiveThread_Unsafe();
|
||||
return;
|
||||
}
|
||||
@@ -686,7 +686,7 @@ void CUnixSocketConnection::ConnectWorker()
|
||||
if (m_Fd < 0)
|
||||
{
|
||||
SDV_LOG_ERROR("[UDS][Client] socket() failed: ", std::strerror(errno));
|
||||
SetStatus(sdv::ipc::EConnectStatus::connection_error);
|
||||
SetConnectState(sdv::ipc::EConnectState::connection_error);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -702,7 +702,7 @@ void CUnixSocketConnection::ConnectWorker()
|
||||
{
|
||||
if (::connect(m_Fd, reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr)) == 0)
|
||||
{
|
||||
SetStatus(sdv::ipc::EConnectStatus::connected);
|
||||
SetConnectState(sdv::ipc::EConnectState::connected);
|
||||
#if ENABLE_REPORTING >= 1
|
||||
TRACE("[UDS][Client] Connected");
|
||||
#endif
|
||||
@@ -719,19 +719,19 @@ void CUnixSocketConnection::ConnectWorker()
|
||||
}
|
||||
|
||||
::close(m_Fd); m_Fd = -1;
|
||||
SetStatus(sdv::ipc::EConnectStatus::connection_error);
|
||||
SetConnectState(sdv::ipc::EConnectState::connection_error);
|
||||
return;
|
||||
}
|
||||
}
|
||||
catch (const std::exception& ex)
|
||||
{
|
||||
SDV_LOG_ERROR("[UDS][ConnectWorker] exception: ", ex.what());
|
||||
SetStatus(sdv::ipc::EConnectStatus::connection_error);
|
||||
SetConnectState(sdv::ipc::EConnectState::connection_error);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
SDV_LOG_ERROR("[UDS][ConnectWorker] unknown exception");
|
||||
SetStatus(sdv::ipc::EConnectStatus::connection_error);
|
||||
SetConnectState(sdv::ipc::EConnectState::connection_error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -741,7 +741,7 @@ void CUnixSocketConnection::ReceiveSyncAnswer(const CMessage& message)
|
||||
const auto hdr = message.GetMsgHdr();
|
||||
if (hdr.uiVersion != SDVFrameworkInterfaceVersion)
|
||||
{
|
||||
SetStatus(sdv::ipc::EConnectStatus::communication_error);
|
||||
SetConnectState(sdv::ipc::EConnectState::communication_error);
|
||||
SDV_LOG_WARNING("[UDS][RX] sync_answer with invalid version");
|
||||
return;
|
||||
}
|
||||
@@ -772,13 +772,13 @@ void CUnixSocketConnection::ReceiveMessages()
|
||||
|
||||
while (!m_StopReceiveThread.load())
|
||||
{
|
||||
if (m_eStatus == sdv::ipc::EConnectStatus::terminating) break;
|
||||
if (m_eConnectState == sdv::ipc::EConnectState::terminating) break;
|
||||
|
||||
// Snapshot FD
|
||||
const int fd = m_Fd;
|
||||
if (fd < 0)
|
||||
{
|
||||
SetStatus(sdv::ipc::EConnectStatus::disconnected);
|
||||
SetConnectState(sdv::ipc::EConnectState::disconnected);
|
||||
SDV_LOG_WARNING("[UDS][RX] FD invalidated -> disconnected");
|
||||
break;
|
||||
}
|
||||
@@ -789,7 +789,7 @@ void CUnixSocketConnection::ReceiveMessages()
|
||||
|
||||
if (pr == 0)
|
||||
{
|
||||
if (!m_AcceptConnectionRequired && (m_eStatus == sdv::ipc::EConnectStatus::initialized))
|
||||
if (!m_AcceptConnectionRequired && (m_eConnectState == sdv::ipc::EConnectState::initialized))
|
||||
{
|
||||
auto now = std::chrono::high_resolution_clock::now();
|
||||
if (std::chrono::duration<double>(now - tpStart).count() > 0.5)
|
||||
@@ -805,7 +805,7 @@ void CUnixSocketConnection::ReceiveMessages()
|
||||
|
||||
if (pr < 0 || (pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) != 0)
|
||||
{
|
||||
SetStatus(sdv::ipc::EConnectStatus::disconnected);
|
||||
SetConnectState(sdv::ipc::EConnectState::disconnected);
|
||||
SDV_LOG_WARNING("[UDS][RX] poll() hangup/error -> disconnected");
|
||||
break;
|
||||
}
|
||||
@@ -815,7 +815,7 @@ void CUnixSocketConnection::ReceiveMessages()
|
||||
uint32_t packetSize = 0;
|
||||
if (!ReadTransportHeader(packetSize))
|
||||
{
|
||||
SetStatus(sdv::ipc::EConnectStatus::disconnected);
|
||||
SetConnectState(sdv::ipc::EConnectState::disconnected);
|
||||
SDV_LOG_WARNING("[UDS][RX] Invalid/missing transport header -> disconnected");
|
||||
break;
|
||||
}
|
||||
@@ -824,7 +824,7 @@ void CUnixSocketConnection::ReceiveMessages()
|
||||
std::vector<uint8_t> payload(packetSize);
|
||||
if (!ReadNumberOfBytes(reinterpret_cast<char*>(payload.data()), packetSize))
|
||||
{
|
||||
SetStatus(sdv::ipc::EConnectStatus::disconnected);
|
||||
SetConnectState(sdv::ipc::EConnectState::disconnected);
|
||||
SDV_LOG_WARNING("[UDS][RX] Incomplete payload read -> disconnected");
|
||||
break;
|
||||
}
|
||||
@@ -832,12 +832,12 @@ void CUnixSocketConnection::ReceiveMessages()
|
||||
CMessage msg(std::move(payload));
|
||||
if (!msg.IsValid())
|
||||
{
|
||||
SetStatus(sdv::ipc::EConnectStatus::communication_error);
|
||||
SetConnectState(sdv::ipc::EConnectState::communication_error);
|
||||
SDV_LOG_WARNING("[UDS][RX] Invalid SDV message (envelope)");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (m_eStatus == sdv::ipc::EConnectStatus::terminating) break;
|
||||
if (m_eConnectState == sdv::ipc::EConnectState::terminating) break;
|
||||
|
||||
#if ENABLE_REPORTING >= 1
|
||||
switch (msg.GetMsgHdr().eType)
|
||||
@@ -874,12 +874,12 @@ void CUnixSocketConnection::ReceiveMessages()
|
||||
catch (const std::exception& ex)
|
||||
{
|
||||
SDV_LOG_ERROR("[UDS][RX] exception: ", ex.what());
|
||||
SetStatus(sdv::ipc::EConnectStatus::disconnected);
|
||||
SetConnectState(sdv::ipc::EConnectState::disconnected);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
SDV_LOG_ERROR("[UDS][RX] unknown exception");
|
||||
SetStatus(sdv::ipc::EConnectStatus::disconnected);
|
||||
SetConnectState(sdv::ipc::EConnectState::disconnected);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -888,7 +888,7 @@ void CUnixSocketConnection::ReceiveSyncRequest(const CMessage& message)
|
||||
const auto hdr = message.GetMsgHdr();
|
||||
if (hdr.uiVersion != SDVFrameworkInterfaceVersion)
|
||||
{
|
||||
SetStatus(sdv::ipc::EConnectStatus::communication_error);
|
||||
SetConnectState(sdv::ipc::EConnectState::communication_error);
|
||||
SDV_LOG_WARNING("[UDS][RX] sync_request with invalid version");
|
||||
return;
|
||||
}
|
||||
@@ -907,7 +907,7 @@ void CUnixSocketConnection::ReceiveConnectRequest(const CMessage& message)
|
||||
const auto hdr = message.GetConnectHdr();
|
||||
if (hdr.uiVersion != SDVFrameworkInterfaceVersion)
|
||||
{
|
||||
SetStatus(sdv::ipc::EConnectStatus::communication_error);
|
||||
SetConnectState(sdv::ipc::EConnectState::communication_error);
|
||||
SDV_LOG_WARNING("[UDS][RX] connect_request with invalid version");
|
||||
return;
|
||||
}
|
||||
@@ -927,19 +927,19 @@ void CUnixSocketConnection::ReceiveConnectAnswer(const CMessage& message)
|
||||
const auto hdr = message.GetConnectHdr();
|
||||
if (hdr.uiVersion != SDVFrameworkInterfaceVersion)
|
||||
{
|
||||
SetStatus(sdv::ipc::EConnectStatus::communication_error);
|
||||
SetConnectState(sdv::ipc::EConnectState::communication_error);
|
||||
SDV_LOG_WARNING("[UDS][RX] connect_answer with invalid version");
|
||||
return;
|
||||
}
|
||||
|
||||
// Fully established
|
||||
SetStatus(sdv::ipc::EConnectStatus::connected);
|
||||
SetConnectState(sdv::ipc::EConnectState::connected);
|
||||
}
|
||||
|
||||
void CUnixSocketConnection::ReceiveConnectTerm(const CMessage& /*message*/)
|
||||
{
|
||||
// Peer requested termination
|
||||
SetStatus(sdv::ipc::EConnectStatus::disconnected);
|
||||
SetConnectState(sdv::ipc::EConnectState::disconnected);
|
||||
m_StopReceiveThread.store(true);
|
||||
}
|
||||
|
||||
@@ -999,7 +999,7 @@ void CUnixSocketConnection::ReceiveDataMessage(const CMessage& rMessage, SDataCo
|
||||
uint32_t uiOffset = ReadDataTable(rMessage, rsDataCtxt);
|
||||
if (!uiOffset)
|
||||
{
|
||||
SetStatus(sdv::ipc::EConnectStatus::communication_error);
|
||||
SetConnectState(sdv::ipc::EConnectState::communication_error);
|
||||
SDV_LOG_WARNING("[UDS][RX] Invalid data table");
|
||||
return;
|
||||
}
|
||||
@@ -1012,7 +1012,7 @@ void CUnixSocketConnection::ReceiveDataMessage(const CMessage& rMessage, SDataCo
|
||||
|
||||
if (!ReadDataChunk(rMessage, uiOffset, rsDataCtxt))
|
||||
{
|
||||
SetStatus(sdv::ipc::EConnectStatus::communication_error);
|
||||
SetConnectState(sdv::ipc::EConnectState::communication_error);
|
||||
SDV_LOG_WARNING("[UDS][RX] Failed to read data chunk");
|
||||
return;
|
||||
}
|
||||
@@ -1034,7 +1034,7 @@ void CUnixSocketConnection::ReceiveDataFragmentMessage(const CMessage& rMessage,
|
||||
uiOffset = ReadDataTable(rMessage, rsDataCtxt);
|
||||
if (!uiOffset)
|
||||
{
|
||||
SetStatus(sdv::ipc::EConnectStatus::communication_error);
|
||||
SetConnectState(sdv::ipc::EConnectState::communication_error);
|
||||
SDV_LOG_WARNING("[UDS][RX] Invalid fragmented data table");
|
||||
return;
|
||||
}
|
||||
@@ -1048,7 +1048,7 @@ void CUnixSocketConnection::ReceiveDataFragmentMessage(const CMessage& rMessage,
|
||||
|
||||
if (!ReadDataChunk(rMessage, uiOffset, rsDataCtxt))
|
||||
{
|
||||
SetStatus(sdv::ipc::EConnectStatus::communication_error);
|
||||
SetConnectState(sdv::ipc::EConnectState::communication_error);
|
||||
SDV_LOG_WARNING("[UDS][RX] Failed to read fragmented chunk");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -82,24 +82,24 @@ public:
|
||||
/** @brief Optionally cancel WaitForConnection (no-op in current implementation). */
|
||||
void CancelWait() override;
|
||||
|
||||
/** @brief Disconnect and teardown threads/FDs; sets status to 'disconnected'. */
|
||||
/** @brief Disconnect and teardown threads/FDs; sets state to 'disconnected'. */
|
||||
void Disconnect() override;
|
||||
|
||||
// ---------- IConnect: event callbacks ----------
|
||||
/** @brief Register a status event callback (no-op storage in UDS). */
|
||||
uint64_t RegisterStatusEventCallback(/*in*/ sdv::IInterfaceAccess* pEventCallback) override;
|
||||
/** @brief Register a state event callback (no-op storage in UDS). */
|
||||
uint64_t RegisterStateEventCallback(/*in*/ sdv::IInterfaceAccess* pEventCallback) override;
|
||||
|
||||
/** @brief Unregister a previously registered callback (no-op storage in UDS). */
|
||||
void UnregisterStatusEventCallback(/*in*/ uint64_t uiCookie) override;
|
||||
void UnregisterStateEventCallback(/*in*/ uint64_t uiCookie) override;
|
||||
|
||||
/** @brief Get current connection status. */
|
||||
sdv::ipc::EConnectStatus GetStatus() const override;
|
||||
/** @brief Get current connection state. */
|
||||
sdv::ipc::EConnectState GetConnectState() const override;
|
||||
|
||||
/** @brief Destroy object (IObjectDestroy). */
|
||||
void DestroyObject() override;
|
||||
|
||||
/** @brief Set status and notify listeners (callback-safe). */
|
||||
void SetStatus(sdv::ipc::EConnectStatus eStatus);
|
||||
/** @brief Set state and notify listeners (callback-safe). */
|
||||
void SetConnectState(sdv::ipc::EConnectState eConnectState);
|
||||
|
||||
/** @brief @return true if this side is server (needs accept()), false otherwise. */
|
||||
bool IsServer() const;
|
||||
@@ -297,7 +297,7 @@ public:
|
||||
* @brief Handle an incoming connect_term message.
|
||||
*
|
||||
* Indicates that the peer requests immediate termination of the connection.
|
||||
* Sets status to disconnected and stops the RX loop.
|
||||
* Sets state to disconnected and stops the RX loop.
|
||||
*
|
||||
* @param message SDV envelope containing the connect_term header.
|
||||
*/
|
||||
@@ -352,7 +352,7 @@ public:
|
||||
// ---------- Internal threading ----------
|
||||
/** @brief Connect worker (server accept loop or client connect retry). */
|
||||
void ConnectWorker();
|
||||
/** @brief Start RX thread (precondition: status=connected, FD valid). */
|
||||
/** @brief Start RX thread (precondition: state=connected, FD valid). */
|
||||
void StartReceiveThread_Unsafe();
|
||||
/**
|
||||
* @brief Stop workers and close sockets, then optionally unlink path.
|
||||
@@ -373,9 +373,9 @@ private:
|
||||
std::thread m_ReceiveThread;
|
||||
std::thread m_ConnectThread;
|
||||
|
||||
//Status & synchronization
|
||||
//State & synchronization
|
||||
std::condition_variable m_StateCv;
|
||||
std::atomic<sdv::ipc::EConnectStatus> m_eStatus { sdv::ipc::EConnectStatus::uninitialized };
|
||||
std::atomic<sdv::ipc::EConnectState> m_eConnectState { sdv::ipc::EConnectState::uninitialized };
|
||||
sdv::ipc::IDataReceiveCallback* m_pReceiver { nullptr };
|
||||
sdv::ipc::IConnectEventCallback* m_pEvent { nullptr };
|
||||
std::mutex m_MtxConnect;
|
||||
|
||||
33
sdv_services/uds_unix_tunnel/CMakeLists.txt
Normal file
33
sdv_services/uds_unix_tunnel/CMakeLists.txt
Normal file
@@ -0,0 +1,33 @@
|
||||
#*******************************************************************************
|
||||
# Copyright (c) 2025-2026 ZF Friedrichshafen AG
|
||||
#
|
||||
# This program and the accompanying materials are made available under the
|
||||
# terms of the Apache License Version 2.0 which is available at
|
||||
# https://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
#*******************************************************************************
|
||||
|
||||
if(UNIX)
|
||||
# Define project
|
||||
project(uds_unix_tunnel VERSION 1.0 LANGUAGES CXX)
|
||||
|
||||
# Define target
|
||||
add_library(uds_unix_tunnel STATIC
|
||||
channel_mgnt.cpp
|
||||
connection.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(uds_unix_tunnel rt ${CMAKE_DL_LIBS} ${CMAKE_THREAD_LIBS_INIT})
|
||||
|
||||
target_include_directories(uds_unix_tunnel PRIVATE ./include/)
|
||||
set_target_properties(uds_unix_tunnel PROPERTIES PREFIX "")
|
||||
set_target_properties(uds_unix_tunnel PROPERTIES SUFFIX ".sdv")
|
||||
|
||||
# Build dependencies
|
||||
add_dependencies(uds_unix_tunnel CompileCoreIDL)
|
||||
|
||||
# Appending the service in the service list
|
||||
set(SDV_Service_List ${SDV_Service_List} uds_unix_tunnel PARENT_SCOPE)
|
||||
|
||||
endif()
|
||||
178
sdv_services/uds_unix_tunnel/channel_mgnt.cpp
Normal file
178
sdv_services/uds_unix_tunnel/channel_mgnt.cpp
Normal file
@@ -0,0 +1,178 @@
|
||||
#if defined(__unix__)
|
||||
|
||||
#include "channel_mgnt.h"
|
||||
#include "connection.h" // CUnixTunnelConnection
|
||||
#include "../sdv_services/uds_unix_sockets/connection.h" // CUnixSocketConnection
|
||||
|
||||
#include <support/toml.h>
|
||||
|
||||
#include <sstream>
|
||||
#include <map>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/un.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
/**
|
||||
* @brief Parses a semicolon-separated list of key=value pairs into a map.
|
||||
*
|
||||
* Example input: "proto=tunnel;role=server;path=/tmp/tunnel.sock;"
|
||||
* Example output: { {"proto","tunnel"}, {"role","server"}, {"path","/tmp/tunnel.sock"} }
|
||||
*
|
||||
* @param s The input string to parse.
|
||||
* @return Map of key-value pairs.
|
||||
*/
|
||||
static std::map<std::string, std::string> ParseKV(const std::string& s)
|
||||
{
|
||||
std::map<std::string, std::string> kv;
|
||||
std::stringstream ss(s);
|
||||
std::string item;
|
||||
while (std::getline(ss, item, ';'))
|
||||
{
|
||||
auto pos = item.find('=');
|
||||
if (pos != std::string::npos)
|
||||
kv[item.substr(0, pos)] = item.substr(pos + 1);
|
||||
}
|
||||
return kv;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Clamps an AF_UNIX pathname to the maximum allowed size for sockaddr_un::sun_path.
|
||||
*
|
||||
* @param p The path to clamp.
|
||||
* @return The clamped path string.
|
||||
*/
|
||||
static std::string ClampSunPath(const std::string& p)
|
||||
{
|
||||
constexpr size_t MaxLen = sizeof(sockaddr_un::sun_path);
|
||||
return (p.size() < MaxLen) ? p : p.substr(0, MaxLen - 1);
|
||||
}
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
std::string CUnixTunnelChannelMgnt::MakeUserRuntimeDir()
|
||||
{
|
||||
std::ostringstream oss;
|
||||
oss << "/run/user/" << ::getuid();
|
||||
|
||||
struct stat st{};
|
||||
if (::stat(oss.str().c_str(), &st) == 0)
|
||||
{
|
||||
std::string path = oss.str() + "/sdv";
|
||||
::mkdir(path.c_str(), 0770);
|
||||
return path;
|
||||
}
|
||||
|
||||
::mkdir("/tmp/sdv", 0770);
|
||||
return "/tmp/sdv";
|
||||
}
|
||||
|
||||
bool CUnixTunnelChannelMgnt::OnInitialize()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
void CUnixTunnelChannelMgnt::OnShutdown()
|
||||
{
|
||||
// Actual cleanup is handled by destructors of CUnixTunnelConnection
|
||||
// and CUnixSocketConnection (shared_ptr).
|
||||
}
|
||||
|
||||
sdv::ipc::SChannelEndpoint CUnixTunnelChannelMgnt::CreateEndpoint(
|
||||
const sdv::u8string& ssChannelConfig)
|
||||
{
|
||||
sdv::ipc::SChannelEndpoint endpoint{};
|
||||
|
||||
const std::string baseDir = MakeUserRuntimeDir();
|
||||
std::string name = "TUNNEL_" + std::to_string(::getpid());
|
||||
std::string path = baseDir + "/" + name + ".sock";
|
||||
|
||||
// Parse optional TOML config for custom name/path
|
||||
if (!ssChannelConfig.empty())
|
||||
{
|
||||
sdv::toml::CTOMLParser cfg(ssChannelConfig.c_str());
|
||||
auto nameNode = cfg.GetDirect("IpcChannel.Name");
|
||||
if (nameNode.GetType() == sdv::toml::ENodeType::node_string)
|
||||
name = static_cast<std::string>(nameNode.GetValue());
|
||||
|
||||
auto pathNode = cfg.GetDirect("IpcChannel.Path");
|
||||
if (pathNode.GetType() == sdv::toml::ENodeType::node_string)
|
||||
path = static_cast<std::string>(pathNode.GetValue());
|
||||
else
|
||||
path = baseDir + "/" + name + ".sock";
|
||||
}
|
||||
|
||||
path = ClampSunPath(path);
|
||||
|
||||
// Create underlying UDS server transport
|
||||
auto udsServer = std::make_shared<CUnixSocketConnection>(
|
||||
-1,
|
||||
/*acceptConnectionRequired*/ true,
|
||||
path);
|
||||
|
||||
// Create tunnel wrapper on top of UDS
|
||||
auto tunnelServer = std::make_shared<CUnixTunnelConnection>(
|
||||
udsServer,
|
||||
/*channelId*/ 0u);
|
||||
|
||||
m_ServerTunnels.push_back(tunnelServer);
|
||||
|
||||
endpoint.pConnection = static_cast<sdv::IInterfaceAccess*>(tunnelServer.get());
|
||||
endpoint.ssConnectString = "proto=tunnel;role=server;path=" + path + ";";
|
||||
|
||||
return endpoint;
|
||||
}
|
||||
|
||||
sdv::IInterfaceAccess* CUnixTunnelChannelMgnt::Access(
|
||||
const sdv::u8string& ssConnectString)
|
||||
{
|
||||
const auto kv = ParseKV(static_cast<std::string>(ssConnectString));
|
||||
|
||||
// Only handle proto=tunnel
|
||||
if (!kv.count("proto") || kv.at("proto") != "tunnel")
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const bool isServer =
|
||||
(kv.count("role") && kv.at("role") == "server");
|
||||
|
||||
const std::string path =
|
||||
kv.count("path")
|
||||
? kv.at("path")
|
||||
: (MakeUserRuntimeDir() + "/TUNNEL_auto.sock");
|
||||
|
||||
if (isServer)
|
||||
{
|
||||
// For simplicity, create a new server tunnel instance for each Access().
|
||||
// The SDV framework is expected to call Access(serverCS) only once in normal cases.
|
||||
auto udsServer = std::make_shared<CUnixSocketConnection>(
|
||||
-1,
|
||||
/*acceptConnectionRequired*/ true,
|
||||
path);
|
||||
|
||||
auto tunnelServer = std::make_shared<CUnixTunnelConnection>(
|
||||
udsServer,
|
||||
/*channelId*/ 0u);
|
||||
|
||||
m_ServerTunnels.push_back(tunnelServer);
|
||||
return static_cast<sdv::IInterfaceAccess*>(tunnelServer.get());
|
||||
}
|
||||
|
||||
// Client: allocate raw pointer (expected to be managed by SDV framework via IObjectDestroy)
|
||||
auto udsClient = std::make_shared<CUnixSocketConnection>(
|
||||
-1,
|
||||
/*acceptConnectionRequired*/ false,
|
||||
path);
|
||||
|
||||
auto* tunnelClient =
|
||||
new CUnixTunnelConnection(udsClient, /*channelId*/ 0u);
|
||||
|
||||
return static_cast<sdv::IInterfaceAccess*>(tunnelClient);
|
||||
}
|
||||
|
||||
#endif // defined(__unix__)
|
||||
140
sdv_services/uds_unix_tunnel/channel_mgnt.h
Normal file
140
sdv_services/uds_unix_tunnel/channel_mgnt.h
Normal file
@@ -0,0 +1,140 @@
|
||||
/********************************************************************************
|
||||
* Copyright (c) 2025-2026 ZF Friedrichshafen AG
|
||||
*
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Apache License Version 2.0 which is available at
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Contributors:
|
||||
* Denisa Ros - initial API and implementation
|
||||
********************************************************************************/
|
||||
#if defined(__unix__)
|
||||
#ifndef UNIX_TUNNEL_CHANNEL_MGNT_H
|
||||
#define UNIX_TUNNEL_CHANNEL_MGNT_H
|
||||
|
||||
#include <support/component_impl.h>
|
||||
#include <interfaces/ipc.h>
|
||||
#include "../sdv_services/uds_unix_sockets/channel_mgnt.h" // existing UDS transport
|
||||
|
||||
class CUnixTunnelConnection;
|
||||
/**
|
||||
* @brief Initialize WinSock on Windows (idempotent).
|
||||
*
|
||||
* This helper ensures WSAStartup() is called only once in the process.
|
||||
* On non-Windows platforms, this is a no-op and always returns success.
|
||||
*
|
||||
* @return 0 on success, otherwise a WinSock error code (Windows only).
|
||||
*/
|
||||
inline int StartUpWinSock()
|
||||
{
|
||||
#ifdef _WIN32
|
||||
static bool isInitialized = false;
|
||||
if (isInitialized)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
WSADATA wsaData {};
|
||||
const int error = WSAStartup(MAKEWORD(2, 2), &wsaData);
|
||||
if (error != 0)
|
||||
{
|
||||
SDV_LOG_ERROR("WSAStartup failed with error: ", std::to_string(error));
|
||||
}
|
||||
else
|
||||
{
|
||||
SDV_LOG_INFO("WSAStartup initialized");
|
||||
isInitialized = true;
|
||||
}
|
||||
return error;
|
||||
#else
|
||||
// Non-Windows: nothing to do. Return success for symmetry
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
/**
|
||||
* @class CUnixTunnelChannelMgnt
|
||||
* @brief IPC channel management class for Unix Domain Socket tunnel communication.
|
||||
*
|
||||
* This manager exposes the "tunnel" IPC type, similar to UDS, with channel multiplexing planned.
|
||||
* It provides creation and access to tunnel endpoints, manages server-side tunnel lifetimes,
|
||||
* and integrates with the SDV object/component framework.
|
||||
*/
|
||||
class CUnixTunnelChannelMgnt :
|
||||
public sdv::CSdvObject,
|
||||
public sdv::ipc::ICreateEndpoint,
|
||||
public sdv::ipc::IChannelAccess
|
||||
{
|
||||
public:
|
||||
// Interface map
|
||||
BEGIN_SDV_INTERFACE_MAP()
|
||||
SDV_INTERFACE_ENTRY(sdv::ipc::IChannelAccess)
|
||||
SDV_INTERFACE_ENTRY(sdv::ipc::ICreateEndpoint)
|
||||
END_SDV_INTERFACE_MAP()
|
||||
|
||||
// Object declarations
|
||||
DECLARE_OBJECT_CLASS_TYPE(sdv::EObjectType::system_object)
|
||||
DECLARE_OBJECT_CLASS_NAME("UnixTunnelChannelControl")
|
||||
DECLARE_OBJECT_CLASS_ALIAS("TunnelChannelControl")
|
||||
DECLARE_DEFAULT_OBJECT_NAME("TunnelChannelControl")
|
||||
DECLARE_OBJECT_SINGLETON()
|
||||
|
||||
/**
|
||||
* @brief Destructor for CUnixTunnelChannelMgnt.
|
||||
*/
|
||||
virtual ~CUnixTunnelChannelMgnt() = default;
|
||||
|
||||
/**
|
||||
* @brief Initialization event, called after object configuration was loaded. Overload of sdv::CSdvObject::OnInitialize.
|
||||
* @return Returns 'true' when the initialization was successful, 'false' when not.
|
||||
*/
|
||||
virtual bool OnInitialize() override;
|
||||
|
||||
/**
|
||||
* @brief Shutdown the object. Overload of sdv::CSdvObject::OnShutdown.
|
||||
*/
|
||||
virtual void OnShutdown() override;
|
||||
|
||||
/**
|
||||
* @brief Creates a tunnel endpoint (server side) and returns endpoint info.
|
||||
*
|
||||
* Optionally uses a TOML config string for custom endpoint parameters.
|
||||
*
|
||||
* @param ssChannelConfig Optional config string (TOML).
|
||||
* @return The channel endpoint structure.
|
||||
*/
|
||||
sdv::ipc::SChannelEndpoint CreateEndpoint(const sdv::u8string& ssChannelConfig) override;
|
||||
|
||||
/**
|
||||
* @brief Creates or accesses a connection object from the channel connect string.
|
||||
*
|
||||
* Parses the connect string and returns a pointer to the appropriate connection access interface.
|
||||
*
|
||||
* @param ssConnectString The channel connect string.
|
||||
* @return Pointer to connection access interface.
|
||||
*/
|
||||
sdv::IInterfaceAccess* Access(const sdv::u8string& ssConnectString) override;
|
||||
|
||||
private:
|
||||
/**
|
||||
* @brief Helper: chooses runtime dir (/run/user/<uid>/sdv) or fallback (/tmp/sdv).
|
||||
*
|
||||
* Used for determining the directory path for runtime sockets.
|
||||
*
|
||||
* @return Directory path for runtime sockets.
|
||||
*/
|
||||
static std::string MakeUserRuntimeDir();
|
||||
|
||||
/**
|
||||
* @brief Keeps server-side tunnel connections alive for the lifetime of the manager.
|
||||
*
|
||||
* This ensures that server tunnel objects are not destroyed while the manager is active.
|
||||
*/
|
||||
std::vector<std::shared_ptr<CUnixTunnelConnection>> m_ServerTunnels;
|
||||
};
|
||||
|
||||
DEFINE_SDV_OBJECT(CUnixTunnelChannelMgnt)
|
||||
|
||||
#endif // UNIX_TUNNEL_CHANNEL_MGNT_H
|
||||
#endif // defined(__unix__)
|
||||
230
sdv_services/uds_unix_tunnel/connection.cpp
Normal file
230
sdv_services/uds_unix_tunnel/connection.cpp
Normal file
@@ -0,0 +1,230 @@
|
||||
/********************************************************************************
|
||||
* Copyright (c) 2025-2026 ZF Friedrichshafen AG
|
||||
*
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Apache License Version 2.0 which is available at
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Contributors:
|
||||
* Denisa Ros - initial API and implementation
|
||||
********************************************************************************/
|
||||
#if defined(__unix__)
|
||||
#include "connection.h"
|
||||
|
||||
|
||||
/**
|
||||
* @brief Constructs a tunnel connection wrapper on top of an existing UDS transport.
|
||||
* @param transport Shared pointer to the underlying UDS transport.
|
||||
* @param channelId Logical channel ID for this tunnel instance.
|
||||
*/
|
||||
CUnixTunnelConnection::CUnixTunnelConnection(
|
||||
std::shared_ptr<CUnixSocketConnection> transport,
|
||||
uint16_t channelId)
|
||||
: m_Transport(std::move(transport))
|
||||
, m_ChannelId(channelId)
|
||||
{
|
||||
// No additional initialization required; acts as a thin wrapper.
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Prepends a tunnel header and forwards the data to the underlying transport.
|
||||
* @param seqData Sequence of message buffers to send (may be modified).
|
||||
* @return true if data was sent successfully, false otherwise.
|
||||
*/
|
||||
bool CUnixTunnelConnection::SendData(/*inout*/ sdv::sequence<sdv::pointer<uint8_t>>& seqData)
|
||||
{
|
||||
if (!m_Transport)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Build tunnel header buffer
|
||||
sdv::pointer<uint8_t> hdrBuf;
|
||||
hdrBuf.resize(sizeof(STunnelHeader));
|
||||
|
||||
STunnelHeader hdr{};
|
||||
hdr.uiChannelId = m_ChannelId; // Logical channel for this connection
|
||||
hdr.uiFlags = 0; // Reserved for future use
|
||||
|
||||
// Copy header structure into the first buffer (little-endian host layout)
|
||||
std::memcpy(hdrBuf.get(), &hdr, sizeof(STunnelHeader));
|
||||
|
||||
// Compose new sequence: [header] + original payload chunks
|
||||
sdv::sequence<sdv::pointer<uint8_t>> seqWithHdr;
|
||||
seqWithHdr.push_back(hdrBuf);
|
||||
for (auto& chunk : seqData)
|
||||
{
|
||||
seqWithHdr.push_back(chunk);
|
||||
}
|
||||
|
||||
return m_Transport->SendData(seqWithHdr);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Starts asynchronous connect and registers upper-layer callbacks.
|
||||
* @param pReceiver Pointer to callback interface for data and state notifications.
|
||||
* @return true if connect started, false otherwise.
|
||||
*/
|
||||
bool CUnixTunnelConnection::AsyncConnect(/*in*/ sdv::IInterfaceAccess* pReceiver)
|
||||
{
|
||||
if (!m_Transport)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Store upper-layer callbacks (safe for null)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_CallbackMtx);
|
||||
sdv::TInterfaceAccessPtr acc(pReceiver);
|
||||
m_pUpperReceiver = acc.GetInterface<sdv::ipc::IDataReceiveCallback>();
|
||||
m_pUpperEvent = acc.GetInterface<sdv::ipc::IConnectEventCallback>();
|
||||
}
|
||||
|
||||
// Register this tunnel as the data/event receiver in the UDS transport.
|
||||
// Do NOT pass pReceiver to UDS, only to our upper fields!
|
||||
return m_Transport->AsyncConnect(this);
|
||||
}
|
||||
|
||||
bool CUnixTunnelConnection::WaitForConnection(/*in*/ uint32_t uiWaitMs)
|
||||
{
|
||||
if (!m_Transport)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return m_Transport->WaitForConnection(uiWaitMs);
|
||||
}
|
||||
|
||||
void CUnixTunnelConnection::CancelWait()
|
||||
{
|
||||
if (!m_Transport)
|
||||
{
|
||||
return;
|
||||
}
|
||||
m_Transport->CancelWait();
|
||||
}
|
||||
|
||||
void CUnixTunnelConnection::Disconnect()
|
||||
{
|
||||
if (!m_Transport)
|
||||
{
|
||||
return;
|
||||
}
|
||||
m_Transport->Disconnect();
|
||||
|
||||
// Clear upper-layer callbacks (thread-safe)
|
||||
std::lock_guard<std::mutex> lock(m_CallbackMtx);
|
||||
m_pUpperReceiver = nullptr;
|
||||
m_pUpperEvent = nullptr;
|
||||
}
|
||||
|
||||
uint64_t CUnixTunnelConnection::RegisterStateEventCallback(/*in*/ sdv::IInterfaceAccess* pEventCallback)
|
||||
{
|
||||
if (!m_Transport)
|
||||
{
|
||||
return 0ULL;
|
||||
}
|
||||
|
||||
// Directly forward to the underlying transport. This allows external
|
||||
// components to receive connect-state changes without the tunnel
|
||||
// having to implement IConnectEventCallback itself
|
||||
return m_Transport->RegisterStateEventCallback(pEventCallback);
|
||||
}
|
||||
|
||||
void CUnixTunnelConnection::UnregisterStateEventCallback(/*in*/ uint64_t uiCookie)
|
||||
{
|
||||
if (!m_Transport || uiCookie == 0ULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
m_Transport->UnregisterStateEventCallback(uiCookie);
|
||||
}
|
||||
|
||||
sdv::ipc::EConnectState CUnixTunnelConnection::GetConnectState() const
|
||||
{
|
||||
if (!m_Transport)
|
||||
{
|
||||
// Reasonable default if transport is missing
|
||||
return sdv::ipc::EConnectState::uninitialized;
|
||||
}
|
||||
return m_Transport->GetConnectState();
|
||||
}
|
||||
|
||||
void CUnixTunnelConnection::DestroyObject()
|
||||
{
|
||||
// Disconnect underlying transport and clear callbacks.
|
||||
Disconnect();
|
||||
|
||||
std::lock_guard<std::mutex> lock(m_CallbackMtx);
|
||||
m_Transport.reset();
|
||||
}
|
||||
|
||||
void CUnixTunnelConnection::ReceiveData(/*inout*/ sdv::sequence<sdv::pointer<uint8_t>>& seqData)
|
||||
{
|
||||
#ifdef DEBUG_TUNNEL_RECEIVE
|
||||
// Optional debug: count every call and print buffer size.
|
||||
static std::atomic<uint64_t> s_counter{0};
|
||||
auto id = ++s_counter;
|
||||
std::cerr << "[Tunnel] ReceiveData call #" << id
|
||||
<< ", seqData.size=" << seqData.size() << std::endl;
|
||||
#endif
|
||||
|
||||
if (seqData.empty())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Extract and validate tunnel header, then remove it
|
||||
const auto& hdrChunk = seqData[0];
|
||||
if (hdrChunk.size() < sizeof(STunnelHeader))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
STunnelHeader hdr{};
|
||||
std::memcpy(&hdr, hdrChunk.get(), sizeof(STunnelHeader));
|
||||
|
||||
seqData.erase(seqData.begin()); // remove header chunk
|
||||
|
||||
// Forward rest of data to upper-layer receiver (set by AsyncConnect)
|
||||
sdv::ipc::IDataReceiveCallback* upper = nullptr;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_CallbackMtx);
|
||||
upper = m_pUpperReceiver;
|
||||
}
|
||||
|
||||
if (upper)
|
||||
{
|
||||
upper->ReceiveData(seqData);
|
||||
}
|
||||
}
|
||||
|
||||
void CUnixTunnelConnection::SetChannelId(uint16_t channelId)
|
||||
{
|
||||
m_ChannelId = channelId;
|
||||
}
|
||||
|
||||
void CUnixTunnelConnection::SetConnectState(sdv::ipc::EConnectState state)
|
||||
{
|
||||
sdv::ipc::IConnectEventCallback* upper = nullptr;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_CallbackMtx);
|
||||
upper = m_pUpperEvent;
|
||||
}
|
||||
|
||||
if (upper)
|
||||
{
|
||||
try
|
||||
{
|
||||
upper->SetConnectState(state);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
// Never let user callback crash the transport.
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif // defined(__unix__)
|
||||
191
sdv_services/uds_unix_tunnel/connection.h
Normal file
191
sdv_services/uds_unix_tunnel/connection.h
Normal file
@@ -0,0 +1,191 @@
|
||||
/********************************************************************************
|
||||
* Copyright (c) 2025-2026 ZF Friedrichshafen AG
|
||||
*
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Apache License Version 2.0 which is available at
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Contributors:
|
||||
* Denisa Ros - initial API and implementation
|
||||
********************************************************************************/
|
||||
#if defined(__unix__)
|
||||
#ifndef UNIX_SOCKET_TUNNEL_CONNECTION_H
|
||||
#define UNIX_SOCKET_TUNNEL_CONNECTION_H
|
||||
|
||||
#include <interfaces/ipc.h>
|
||||
#include <support/component_impl.h>
|
||||
#include <support/interface_ptr.h>
|
||||
|
||||
#include <atomic>
|
||||
#include <cstdint>
|
||||
#include <mutex>
|
||||
#include <memory>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "../sdv_services/uds_unix_sockets/connection.h" // existing UDS transport
|
||||
|
||||
/**
|
||||
* @class CUnixTunnelConnection
|
||||
* @brief Logical tunnel connection on top of a shared Unix Domain Socket (UDS) transport.
|
||||
*
|
||||
* This class wraps an existing CUnixSocketConnection (physical tunnel port) and provides
|
||||
* optional tunneling capabilities such as a prepended tunnel header (channelId, flags).
|
||||
* It is designed to enable demultiplexing of incoming payloads per logical channel in future versions.
|
||||
*
|
||||
* Currently, this acts as a simple pass-through wrapper. Tunnel header parsing and multi-channel
|
||||
* routing are planned as future improvements (see TODOs in implementation).
|
||||
*/
|
||||
class CUnixTunnelConnection :
|
||||
public sdv::IInterfaceAccess,
|
||||
public sdv::IObjectDestroy,
|
||||
public sdv::ipc::IDataSend,
|
||||
public sdv::ipc::IConnect,
|
||||
public sdv::ipc::IDataReceiveCallback,
|
||||
public sdv::ipc::IConnectEventCallback
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @struct STunnelHeader
|
||||
* @brief Small header prepended to each tunneled SDV message.
|
||||
*
|
||||
* Used for logical channel identification and control flags. This enables future support for
|
||||
* multiplexing and advanced features (QoS, direction, etc.).
|
||||
*/
|
||||
struct STunnelHeader
|
||||
{
|
||||
uint16_t uiChannelId; ///< Logical channel ID (e.g., IPC_x / REMOTE_IPC_x)
|
||||
uint16_t uiFlags; ///< Reserved for future use (QoS, direction, etc.)
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Constructs a tunnel connection wrapper.
|
||||
* @param transport Shared pointer to the underlying UDS transport (physical tunnel port).
|
||||
* @param channelId Default logical channel ID for this object view (may be ignored if full demux is implemented later).
|
||||
*/
|
||||
explicit CUnixTunnelConnection(
|
||||
std::shared_ptr<CUnixSocketConnection> transport,
|
||||
uint16_t channelId);
|
||||
|
||||
/**
|
||||
* @brief Destructor. Cleans up resources if needed.
|
||||
*/
|
||||
virtual ~CUnixTunnelConnection() = default;
|
||||
|
||||
BEGIN_SDV_INTERFACE_MAP()
|
||||
SDV_INTERFACE_ENTRY(sdv::ipc::IDataSend)
|
||||
SDV_INTERFACE_ENTRY(sdv::ipc::IConnect)
|
||||
SDV_INTERFACE_ENTRY(sdv::ipc::IDataReceiveCallback)
|
||||
SDV_INTERFACE_ENTRY(sdv::ipc::IConnectEventCallback)
|
||||
SDV_INTERFACE_ENTRY(sdv::IObjectDestroy)
|
||||
END_SDV_INTERFACE_MAP()
|
||||
|
||||
// ---------- IDataSend ----------
|
||||
/**
|
||||
* @brief Sends a sequence of buffers via the tunnel.
|
||||
*
|
||||
* Prepends a STunnelHeader to the outgoing message buffers and forwards them to the underlying UDS transport.
|
||||
* In the current implementation, the channel header is always present but not yet used for multiplexing.
|
||||
*
|
||||
* @param seqData Sequence of message buffers (may be modified by callee).
|
||||
* @return true on successful send, false otherwise.
|
||||
*/
|
||||
bool SendData(/*inout*/ sdv::sequence<sdv::pointer<uint8_t>>& seqData) override;
|
||||
|
||||
// ---------- IConnect ----------
|
||||
/**
|
||||
* @brief Starts asynchronous connect on the underlying transport and registers this object as receiver.
|
||||
* @param pReceiver Pointer to callback interface for data and state notifications.
|
||||
* @return true if connect started, false otherwise.
|
||||
*/
|
||||
bool AsyncConnect(/*in*/ sdv::IInterfaceAccess* pReceiver) override;
|
||||
|
||||
/**
|
||||
* @brief Waits until the underlying transport becomes 'connected'.
|
||||
*
|
||||
* Forwards to CUnixSocketConnection::WaitForConnection.
|
||||
* @param uiWaitMs Timeout in milliseconds to wait.
|
||||
* @return true if connection established, false on timeout or error.
|
||||
*/
|
||||
bool WaitForConnection(/*in*/ uint32_t uiWaitMs) override;
|
||||
|
||||
/**
|
||||
* @brief Cancels any pending connect or wait operation.
|
||||
* Delegated to the underlying transport, if needed.
|
||||
*/
|
||||
void CancelWait() override;
|
||||
|
||||
/**
|
||||
* @brief Disconnects the tunnel and underlying transport.
|
||||
*/
|
||||
void Disconnect() override;
|
||||
|
||||
/**
|
||||
* @brief Registers a state event callback (forwards to transport).
|
||||
* @param pEventCallback Pointer to event callback interface.
|
||||
* @return Registration cookie (nonzero) or 0 on failure.
|
||||
*/
|
||||
uint64_t RegisterStateEventCallback(/*in*/ sdv::IInterfaceAccess* pEventCallback) override;
|
||||
|
||||
/**
|
||||
* @brief Unregisters a previously registered state event callback.
|
||||
* @param uiCookie Registration cookie returned by RegisterStateEventCallback.
|
||||
*/
|
||||
void UnregisterStateEventCallback(/*in*/ uint64_t uiCookie) override;
|
||||
|
||||
/**
|
||||
* @brief Gets the current state from the underlying transport.
|
||||
* @return The current connection state.
|
||||
*/
|
||||
sdv::ipc::EConnectState GetConnectState() const override;
|
||||
|
||||
// ---------- IObjectDestroy ----------
|
||||
/**
|
||||
* @brief Releases and cleans up all resources associated with this object.
|
||||
*/
|
||||
void DestroyObject() override;
|
||||
|
||||
// ---------- IDataReceiveCallback ----------
|
||||
/**
|
||||
* @brief Receives data from the underlying UDS transport.
|
||||
*
|
||||
* Expects the first chunk of seqData to be a STunnelHeader, strips and processes it,
|
||||
* and delivers the remaining payload to the upper-layer receiver registered via AsyncConnect.
|
||||
*
|
||||
* @param seqData Sequence of received message buffers (header chunk is removed by this call).
|
||||
*/
|
||||
void ReceiveData(/*inout*/ sdv::sequence<sdv::pointer<uint8_t>>& seqData) override;
|
||||
|
||||
// ---------- IConnectEventCallback ----------
|
||||
/**
|
||||
* @brief Forwards state changes from the underlying UDS transport to the upper layer.
|
||||
* @param state New connection state.
|
||||
*/
|
||||
void SetConnectState(sdv::ipc::EConnectState state) override;
|
||||
|
||||
/**
|
||||
* @brief Assigns a logical channel ID for this connection.
|
||||
* Optional helper; you may extend with more routing metadata for advanced tunnel use-cases.
|
||||
* @param channelId The logical channel ID to set.
|
||||
*/
|
||||
void SetChannelId(uint16_t channelId);
|
||||
|
||||
/**
|
||||
* @brief Get the logical channel ID for this connection
|
||||
* @return The channel ID
|
||||
*/
|
||||
uint16_t GetChannelId() const noexcept { return m_ChannelId; }
|
||||
|
||||
private:
|
||||
std::shared_ptr<CUnixSocketConnection> m_Transport; ///< shared physical tunnel port
|
||||
uint16_t m_ChannelId {0}; ///< default logical channel id
|
||||
sdv::ipc::IDataReceiveCallback* m_pUpperReceiver {nullptr}; ///< Callback to upper layer (data receive)
|
||||
sdv::ipc::IConnectEventCallback* m_pUpperEvent {nullptr}; ///< Callback to upper layer (state event)
|
||||
mutable std::mutex m_CallbackMtx; ///< Mutex to guard callback access
|
||||
};
|
||||
|
||||
#endif // UNIX_SOCKET_TUNNEL_CONNECTION_H
|
||||
#endif // defined(__unix__)
|
||||
@@ -13,16 +13,22 @@ if(WIN32)
|
||||
project(uds_win_sockets VERSION 1.0 LANGUAGES CXX)
|
||||
|
||||
# Define target
|
||||
add_library(uds_win_sockets SHARED
|
||||
"channel_mgnt.h"
|
||||
"channel_mgnt.cpp"
|
||||
"connection.h"
|
||||
"connection.cpp")
|
||||
add_library(uds_win_sockets STATIC
|
||||
channel_mgnt.cpp
|
||||
connection.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(uds_win_sockets ${CMAKE_THREAD_LIBS_INIT} Ws2_32.lib)
|
||||
target_include_directories(uds_win_sockets
|
||||
PUBLIC
|
||||
${CMAKE_CURRENT_SOURCE_DIR}
|
||||
./include/
|
||||
)
|
||||
|
||||
target_link_options(uds_win_sockets PRIVATE)
|
||||
target_include_directories(uds_win_sockets PRIVATE ./include/)
|
||||
target_link_libraries(uds_win_sockets
|
||||
PUBLIC
|
||||
${CMAKE_THREAD_LIBS_INIT}
|
||||
Ws2_32.lib
|
||||
)
|
||||
|
||||
set_target_properties(uds_win_sockets PROPERTIES PREFIX "")
|
||||
set_target_properties(uds_win_sockets PROPERTIES SUFFIX ".sdv")
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
* Contributors:
|
||||
* Denisa Ros - initial API and implementation
|
||||
********************************************************************************/
|
||||
#ifdef _WIN32
|
||||
|
||||
#include "channel_mgnt.h"
|
||||
#include "connection.h"
|
||||
@@ -323,15 +324,12 @@ static SOCKET ConnectUnixSocket(
|
||||
const auto deadline = std::chrono::steady_clock::now() +
|
||||
std::chrono::milliseconds(totalTimeoutMs);
|
||||
|
||||
int lastError = 0;
|
||||
|
||||
while (true)
|
||||
{
|
||||
SOCKET s = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
if (s == INVALID_SOCKET)
|
||||
{
|
||||
lastError = WSAGetLastError();
|
||||
SDV_LOG_ERROR("[AF_UNIX] socket() FAIL (client), WSA=", lastError);
|
||||
SDV_LOG_ERROR("[AF_UNIX] socket() FAIL (client), WSA=", WSAGetLastError());
|
||||
return INVALID_SOCKET;
|
||||
}
|
||||
|
||||
@@ -341,7 +339,7 @@ static SOCKET ConnectUnixSocket(
|
||||
return s;
|
||||
}
|
||||
|
||||
lastError = WSAGetLastError();
|
||||
int lastError = WSAGetLastError();
|
||||
closesocket(s);
|
||||
|
||||
if (std::chrono::steady_clock::now() >= deadline)
|
||||
@@ -364,7 +362,7 @@ bool CSocketsChannelMgnt::OnInitialize()
|
||||
return true;
|
||||
}
|
||||
|
||||
void CSocketsChannelMgnt::OnServerClosed(const std::string& udsPath, CConnection* ptr)
|
||||
void CSocketsChannelMgnt::OnServerClosed(const std::string& udsPath, CWinsockConnection* ptr)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_udsMtx);
|
||||
|
||||
@@ -411,8 +409,8 @@ sdv::ipc::SChannelEndpoint CSocketsChannelMgnt::CreateEndpoint(const sdv::u8stri
|
||||
return {};
|
||||
}
|
||||
|
||||
// Server-side CConnection, it will accept() a client on first use
|
||||
auto server = std::make_shared<CConnection>(listenSocket, /*acceptRequired*/ true);
|
||||
// Server-side CWinsockConnection, it will accept() a client on first use
|
||||
auto server = std::make_shared<CWinsockConnection>(listenSocket, /*acceptRequired*/ true);
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_udsMtx);
|
||||
@@ -481,5 +479,7 @@ sdv::IInterfaceAccess* CSocketsChannelMgnt::Access(const sdv::u8string& cs)
|
||||
|
||||
// Client-side connection object (acceptRequired=false)
|
||||
// Ownership is transferred to the caller (VAPI runtime)
|
||||
return new CConnection(s, /*acceptRequired*/ false);
|
||||
}
|
||||
return new CWinsockConnection(s, /*acceptRequired*/ false);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -10,9 +10,10 @@
|
||||
* Contributors:
|
||||
* Denisa Ros - initial API and implementation
|
||||
********************************************************************************/
|
||||
#ifdef _WIN32
|
||||
|
||||
#ifndef CHANNEL_MGNT_H
|
||||
#define CHANNEL_MGNT_H
|
||||
#ifndef WIN_SOCKETS_CHANNEL_MGNT_H
|
||||
#define WIN_SOCKETS_CHANNEL_MGNT_H
|
||||
|
||||
#include <support/component_impl.h>
|
||||
#include <interfaces/ipc.h>
|
||||
@@ -150,6 +151,8 @@ public:
|
||||
DECLARE_DEFAULT_OBJECT_NAME("LocalChannelControl")
|
||||
DECLARE_OBJECT_SINGLETON()
|
||||
|
||||
virtual ~CSocketsChannelMgnt() = default;
|
||||
|
||||
/**
|
||||
* @brief Initialization event, called after object configuration was loaded. Overload of sdv::CSdvObject::OnInitialize.
|
||||
* @return Returns 'true' when the initialization was successful, 'false' when not.
|
||||
@@ -169,7 +172,7 @@ public:
|
||||
* The endpoint is implemented as a local AF_UNIX server socket
|
||||
* (listen socket) on Windows. The connect string has the format:
|
||||
*
|
||||
* proto=uds;path=<udsPath>
|
||||
* proto=uds;path=<udsPath>;
|
||||
*
|
||||
* If no configuration is provided or no path is specified, a default
|
||||
* path is used (under %LOCALAPPDATA%/sdv)
|
||||
@@ -193,7 +196,7 @@ public:
|
||||
* Overload of sdv::ipc::IChannelAccess::Access
|
||||
*
|
||||
* The connect string is expected to contain:
|
||||
* proto=uds;path=<udsPath>
|
||||
* proto=uds;path=<udsPath>;
|
||||
*
|
||||
* For the first Access() call with a given path, the server-side
|
||||
* connection object created by CreateEndpoint() can be returned.
|
||||
@@ -206,18 +209,17 @@ public:
|
||||
sdv::IInterfaceAccess* Access(const sdv::u8string& ssConnectString) override;
|
||||
|
||||
/**
|
||||
* @brief Called by a CConnection instance when the server side is closed
|
||||
* @brief Called by a CWinsockConnection instance when the server side is closed
|
||||
*
|
||||
* Used to clean up internal registries for a given UDS path
|
||||
*
|
||||
* @param udsPath Path to the UDS endpoint
|
||||
* @param ptr Pointer to the CConnection instance that was closed
|
||||
* @param ptr Pointer to the CWinsockConnection instance that was closed
|
||||
*/
|
||||
void OnServerClosed(const std::string& udsPath, CConnection* ptr);
|
||||
void OnServerClosed(const std::string& udsPath, CWinsockConnection* ptr);
|
||||
|
||||
private:
|
||||
/// @brief Registry of AF_UNIX server connections keyed by normalized UDS path
|
||||
std::map<std::string, std::shared_ptr<CConnection>> m_udsServers;
|
||||
std::map<std::string, std::shared_ptr<CWinsockConnection>> m_udsServers;
|
||||
|
||||
/**
|
||||
* @brief Set of UDS paths that already returned their server-side
|
||||
@@ -234,4 +236,5 @@ private:
|
||||
// SDV object factory macro
|
||||
DEFINE_SDV_OBJECT(CSocketsChannelMgnt)
|
||||
|
||||
#endif // ! defined CHANNEL_MGNT_H
|
||||
#endif // ! defined WIN_SOCKETS_CHANNEL_MGNT_H
|
||||
#endif
|
||||
@@ -10,7 +10,7 @@
|
||||
* Contributors:
|
||||
* Denisa Ros - initial API and implementation
|
||||
********************************************************************************/
|
||||
|
||||
#ifdef _WIN32
|
||||
#include "connection.h"
|
||||
|
||||
#include <numeric>
|
||||
@@ -18,8 +18,8 @@
|
||||
#include <cstring>
|
||||
#include <chrono>
|
||||
|
||||
CConnection::CConnection(SOCKET preconfiguredSocket, bool acceptConnectionRequired)
|
||||
: m_ConnectionStatus(sdv::ipc::EConnectStatus::uninitialized)
|
||||
CWinsockConnection::CWinsockConnection(unsigned long long preconfiguredSocket, bool acceptConnectionRequired)
|
||||
: m_ConnectionState(sdv::ipc::EConnectState::uninitialized)
|
||||
, m_AcceptConnectionRequired(acceptConnectionRequired)
|
||||
, m_CancelWait(false)
|
||||
{
|
||||
@@ -30,23 +30,34 @@ CConnection::CConnection(SOCKET preconfiguredSocket, bool acceptConnectionRequir
|
||||
if (m_AcceptConnectionRequired)
|
||||
{
|
||||
// Server side: we own a listening socket, active socket is not yet accepted
|
||||
m_ListenSocket = preconfiguredSocket;
|
||||
m_ListenSocket = static_cast<SOCKET>(preconfiguredSocket);
|
||||
m_ConnectionSocket = INVALID_SOCKET;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Client side: we already have a connected socket
|
||||
m_ListenSocket = INVALID_SOCKET;
|
||||
m_ConnectionSocket = preconfiguredSocket;
|
||||
m_ConnectionSocket = static_cast<SOCKET>(preconfiguredSocket);
|
||||
}
|
||||
}
|
||||
|
||||
CConnection::~CConnection()
|
||||
/*CWinsockConnection::CWinsockConnection()
|
||||
: m_ConnectionState(sdv::ipc::EConnectState::uninitialized)
|
||||
, m_AcceptConnectionRequired(false)
|
||||
, m_CancelWait(false)
|
||||
{
|
||||
m_ListenSocket = INVALID_SOCKET;
|
||||
m_ConnectionSocket = INVALID_SOCKET;
|
||||
std::fill(std::begin(m_SendBuffer), std::end(m_SendBuffer), '\0');
|
||||
std::fill(std::begin(m_ReceiveBuffer), std::end(m_ReceiveBuffer), '\0');
|
||||
}*/
|
||||
|
||||
CWinsockConnection::~CWinsockConnection()
|
||||
{
|
||||
try
|
||||
{
|
||||
StopThreadsAndCloseSockets();
|
||||
m_ConnectionStatus = sdv::ipc::EConnectStatus::disconnected;
|
||||
m_ConnectionState = sdv::ipc::EConnectState::disconnected;
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
@@ -54,11 +65,11 @@ CConnection::~CConnection()
|
||||
}
|
||||
}
|
||||
|
||||
void CConnection::SetStatus(sdv::ipc::EConnectStatus status)
|
||||
void CWinsockConnection::SetConnectState(sdv::ipc::EConnectState state)
|
||||
{
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(m_MtxConnect);
|
||||
m_ConnectionStatus.store(status, std::memory_order_release);
|
||||
m_ConnectionState.store(state, std::memory_order_release);
|
||||
}
|
||||
|
||||
// Wake up any waiter
|
||||
@@ -69,7 +80,7 @@ void CConnection::SetStatus(sdv::ipc::EConnectStatus status)
|
||||
{
|
||||
try
|
||||
{
|
||||
m_pEvent->SetStatus(status);
|
||||
m_pEvent->SetConnectState(state);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
@@ -78,7 +89,7 @@ void CConnection::SetStatus(sdv::ipc::EConnectStatus status)
|
||||
}
|
||||
}
|
||||
|
||||
int32_t CConnection::Send(const char* data, int32_t dataLength)
|
||||
int32_t CWinsockConnection::Send(const char* data, int32_t dataLength)
|
||||
{
|
||||
int32_t total = 0;
|
||||
|
||||
@@ -88,7 +99,7 @@ int32_t CConnection::Send(const char* data, int32_t dataLength)
|
||||
if (n == SOCKET_ERROR)
|
||||
{
|
||||
SDV_LOG_ERROR("send failed with error: ", std::to_string(WSAGetLastError()));
|
||||
m_ConnectionStatus = sdv::ipc::EConnectStatus::communication_error;
|
||||
m_ConnectionState = sdv::ipc::EConnectState::communication_error;
|
||||
m_ConnectionSocket = INVALID_SOCKET;
|
||||
return (total > 0) ? total : SOCKET_ERROR;
|
||||
}
|
||||
@@ -98,7 +109,7 @@ int32_t CConnection::Send(const char* data, int32_t dataLength)
|
||||
return total;
|
||||
}
|
||||
|
||||
int CConnection::SendExact(const char* data, int len)
|
||||
int CWinsockConnection::SendExact(const char* data, int len)
|
||||
{
|
||||
int total = 0;
|
||||
|
||||
@@ -115,13 +126,13 @@ int CConnection::SendExact(const char* data, int len)
|
||||
return total;
|
||||
}
|
||||
|
||||
bool CConnection::SendData(/*inout*/ sdv::sequence<sdv::pointer<uint8_t>>& seqData)
|
||||
bool CWinsockConnection::SendData(/*inout*/ sdv::sequence<sdv::pointer<uint8_t>>& seqData)
|
||||
{
|
||||
// Must be connected and have a valid socket
|
||||
if (m_ConnectionStatus != sdv::ipc::EConnectStatus::connected ||
|
||||
if (m_ConnectionState != sdv::ipc::EConnectState::connected ||
|
||||
m_ConnectionSocket == INVALID_SOCKET)
|
||||
{
|
||||
m_ConnectionStatus = sdv::ipc::EConnectStatus::communication_error;
|
||||
m_ConnectionState = sdv::ipc::EConnectState::communication_error;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -288,11 +299,11 @@ bool CConnection::SendData(/*inout*/ sdv::sequence<sdv::pointer<uint8_t>>& seqDa
|
||||
return (sentOffset == required);
|
||||
}
|
||||
|
||||
SOCKET CConnection::AcceptConnection()
|
||||
SOCKET CWinsockConnection::AcceptConnection()
|
||||
{
|
||||
if (m_ListenSocket == INVALID_SOCKET)
|
||||
{
|
||||
SetStatus(sdv::ipc::EConnectStatus::connection_error);
|
||||
SetConnectState(sdv::ipc::EConnectState::connection_error);
|
||||
return INVALID_SOCKET;
|
||||
}
|
||||
|
||||
@@ -310,7 +321,7 @@ SOCKET CConnection::AcceptConnection()
|
||||
if (sr == SOCKET_ERROR)
|
||||
{
|
||||
SDV_LOG_ERROR("[AF_UNIX] select(listen) FAIL, WSA=", WSAGetLastError());
|
||||
SetStatus(sdv::ipc::EConnectStatus::connection_error);
|
||||
SetConnectState(sdv::ipc::EConnectState::connection_error);
|
||||
return INVALID_SOCKET;
|
||||
}
|
||||
|
||||
@@ -329,7 +340,7 @@ SOCKET CConnection::AcceptConnection()
|
||||
}
|
||||
|
||||
SDV_LOG_ERROR("[AF_UNIX] accept FAIL, WSA=", err);
|
||||
SetStatus(sdv::ipc::EConnectStatus::connection_error);
|
||||
SetConnectState(sdv::ipc::EConnectState::connection_error);
|
||||
return INVALID_SOCKET;
|
||||
}
|
||||
|
||||
@@ -338,11 +349,11 @@ SOCKET CConnection::AcceptConnection()
|
||||
}
|
||||
|
||||
SDV_LOG_ERROR("[AF_UNIX] accept canceled (stop flag)");
|
||||
SetStatus(sdv::ipc::EConnectStatus::connection_error);
|
||||
SetConnectState(sdv::ipc::EConnectState::connection_error);
|
||||
return INVALID_SOCKET;
|
||||
}
|
||||
|
||||
bool CConnection::AsyncConnect(sdv::IInterfaceAccess* pReceiver)
|
||||
bool CWinsockConnection::AsyncConnect(sdv::IInterfaceAccess* pReceiver)
|
||||
{
|
||||
// Store callbacks
|
||||
m_pReceiver = sdv::TInterfaceAccessPtr(pReceiver).GetInterface<sdv::ipc::IDataReceiveCallback>();
|
||||
@@ -360,14 +371,14 @@ bool CConnection::AsyncConnect(sdv::IInterfaceAccess* pReceiver)
|
||||
m_ConnectThread.join();
|
||||
|
||||
// Start the connect worker
|
||||
m_ConnectThread = std::thread(&CConnection::ConnectWorker, this);
|
||||
m_ConnectThread = std::thread(&CWinsockConnection::ConnectWorker, this);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CConnection::WaitForConnection(uint32_t uiWaitMs)
|
||||
bool CWinsockConnection::WaitForConnection(uint32_t uiWaitMs)
|
||||
{
|
||||
if (m_ConnectionStatus.load(std::memory_order_acquire) ==
|
||||
sdv::ipc::EConnectStatus::connected)
|
||||
if (m_ConnectionState.load(std::memory_order_acquire) ==
|
||||
sdv::ipc::EConnectState::connected)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@@ -380,16 +391,16 @@ bool CConnection::WaitForConnection(uint32_t uiWaitMs)
|
||||
lk,
|
||||
[this]
|
||||
{
|
||||
return m_ConnectionStatus.load(std::memory_order_acquire) ==
|
||||
sdv::ipc::EConnectStatus::connected;
|
||||
return m_ConnectionState.load(std::memory_order_acquire) ==
|
||||
sdv::ipc::EConnectState::connected;
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
if (uiWaitMs == 0u) // zero wait
|
||||
{
|
||||
return (m_ConnectionStatus.load(std::memory_order_acquire) ==
|
||||
sdv::ipc::EConnectStatus::connected);
|
||||
return (m_ConnectionState.load(std::memory_order_acquire) ==
|
||||
sdv::ipc::EConnectState::connected);
|
||||
}
|
||||
|
||||
// finite wait
|
||||
@@ -398,24 +409,24 @@ bool CConnection::WaitForConnection(uint32_t uiWaitMs)
|
||||
std::chrono::milliseconds(uiWaitMs),
|
||||
[this]
|
||||
{
|
||||
return m_ConnectionStatus.load(std::memory_order_acquire) ==
|
||||
sdv::ipc::EConnectStatus::connected;
|
||||
return m_ConnectionState.load(std::memory_order_acquire) ==
|
||||
sdv::ipc::EConnectState::connected;
|
||||
});
|
||||
}
|
||||
|
||||
void CConnection::CancelWait()
|
||||
void CWinsockConnection::CancelWait()
|
||||
{
|
||||
m_CancelWait.store(true);
|
||||
}
|
||||
|
||||
void CConnection::Disconnect()
|
||||
void CWinsockConnection::Disconnect()
|
||||
{
|
||||
m_CancelWait.store(true);
|
||||
StopThreadsAndCloseSockets();
|
||||
SetStatus(sdv::ipc::EConnectStatus::disconnected);
|
||||
SetConnectState(sdv::ipc::EConnectState::disconnected);
|
||||
}
|
||||
|
||||
uint64_t CConnection::RegisterStatusEventCallback(/*in*/ sdv::IInterfaceAccess* pEventCallback)
|
||||
uint64_t CWinsockConnection::RegisterStateEventCallback(/*in*/ sdv::IInterfaceAccess* pEventCallback)
|
||||
{
|
||||
// Extract IConnectEventCallback interface
|
||||
m_pEvent = sdv::TInterfaceAccessPtr(pEventCallback).GetInterface<sdv::ipc::IConnectEventCallback>();
|
||||
@@ -424,7 +435,7 @@ uint64_t CConnection::RegisterStatusEventCallback(/*in*/ sdv::IInterfaceAccess*
|
||||
return (m_pEvent != nullptr) ? 1ULL : 0ULL;
|
||||
}
|
||||
|
||||
void CConnection::UnregisterStatusEventCallback(/*in*/ uint64_t uiCookie)
|
||||
void CWinsockConnection::UnregisterStateEventCallback(/*in*/ uint64_t uiCookie)
|
||||
{
|
||||
// Only one callback supported -> cookie value is 1
|
||||
if (uiCookie == 1ULL)
|
||||
@@ -433,21 +444,21 @@ void CConnection::UnregisterStatusEventCallback(/*in*/ uint64_t uiCookie)
|
||||
}
|
||||
}
|
||||
|
||||
sdv::ipc::EConnectStatus CConnection::GetStatus() const
|
||||
sdv::ipc::EConnectState CWinsockConnection::GetConnectState() const
|
||||
{
|
||||
return m_ConnectionStatus;
|
||||
return m_ConnectionState;
|
||||
}
|
||||
|
||||
void CConnection::DestroyObject()
|
||||
void CWinsockConnection::DestroyObject()
|
||||
{
|
||||
m_StopReceiveThread = true;
|
||||
m_StopConnectThread = true;
|
||||
|
||||
StopThreadsAndCloseSockets();
|
||||
m_ConnectionStatus = sdv::ipc::EConnectStatus::disconnected;
|
||||
m_ConnectionState = sdv::ipc::EConnectState::disconnected;
|
||||
}
|
||||
|
||||
void CConnection::ConnectWorker()
|
||||
void CWinsockConnection::ConnectWorker()
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -456,18 +467,18 @@ void CConnection::ConnectWorker()
|
||||
// SERVER SIDE
|
||||
if (m_ListenSocket == INVALID_SOCKET)
|
||||
{
|
||||
SetStatus(sdv::ipc::EConnectStatus::connection_error);
|
||||
SetConnectState(sdv::ipc::EConnectState::connection_error);
|
||||
return;
|
||||
}
|
||||
|
||||
SetStatus(sdv::ipc::EConnectStatus::initializing);
|
||||
SetConnectState(sdv::ipc::EConnectState::initializing);
|
||||
SDV_LOG_INFO("[AF_UNIX] Srv ConnectWorker start: listen=%llu",
|
||||
static_cast<uint64_t>(m_ListenSocket));
|
||||
|
||||
SOCKET c = AcceptConnection();
|
||||
SDV_LOG_INFO("[AF_UNIX] Srv AcceptConnection returned: sock=%llu status=%d",
|
||||
SDV_LOG_INFO("[AF_UNIX] Srv AcceptConnection returned: sock=%llu state=%d",
|
||||
static_cast<uint64_t>(c),
|
||||
static_cast<int>(m_ConnectionStatus.load()));
|
||||
static_cast<int>(m_ConnectionState.load()));
|
||||
|
||||
if (c == INVALID_SOCKET)
|
||||
{
|
||||
@@ -475,7 +486,7 @@ void CConnection::ConnectWorker()
|
||||
{
|
||||
try
|
||||
{
|
||||
m_pEvent->SetStatus(m_ConnectionStatus);
|
||||
m_pEvent->SetConnectState(m_ConnectionState);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
@@ -485,7 +496,7 @@ void CConnection::ConnectWorker()
|
||||
}
|
||||
|
||||
m_ConnectionSocket = c;
|
||||
SetStatus(sdv::ipc::EConnectStatus::connected);
|
||||
SetConnectState(sdv::ipc::EConnectState::connected);
|
||||
StartReceiveThread_Unsafe();
|
||||
return;
|
||||
}
|
||||
@@ -494,22 +505,22 @@ void CConnection::ConnectWorker()
|
||||
// CLIENT SIDE
|
||||
if (m_ConnectionSocket == INVALID_SOCKET)
|
||||
{
|
||||
SetStatus(sdv::ipc::EConnectStatus::connection_error);
|
||||
SetConnectState(sdv::ipc::EConnectState::connection_error);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Client side: socket is already connected
|
||||
SetStatus(sdv::ipc::EConnectStatus::connected);
|
||||
SetConnectState(sdv::ipc::EConnectState::connected);
|
||||
StartReceiveThread_Unsafe();
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
SetStatus(sdv::ipc::EConnectStatus::connection_error);
|
||||
SetConnectState(sdv::ipc::EConnectState::connection_error);
|
||||
}
|
||||
}
|
||||
|
||||
void CConnection::StartReceiveThread_Unsafe()
|
||||
void CWinsockConnection::StartReceiveThread_Unsafe()
|
||||
{
|
||||
if (m_ReceiveThread.joinable())
|
||||
{
|
||||
@@ -517,10 +528,10 @@ void CConnection::StartReceiveThread_Unsafe()
|
||||
}
|
||||
|
||||
m_StopReceiveThread.store(false);
|
||||
m_ReceiveThread = std::thread(&CConnection::ReceiveMessages, this);
|
||||
m_ReceiveThread = std::thread(&CWinsockConnection::ReceiveMessages, this);
|
||||
}
|
||||
|
||||
void CConnection::StopThreadsAndCloseSockets()
|
||||
void CWinsockConnection::StopThreadsAndCloseSockets()
|
||||
{
|
||||
// Signal stop
|
||||
m_StopReceiveThread.store(true);
|
||||
@@ -574,7 +585,7 @@ void CConnection::StopThreadsAndCloseSockets()
|
||||
static_cast<uint64_t>(s));
|
||||
}
|
||||
|
||||
bool CConnection::ReadNumberOfBytes(char* buffer, uint32_t length)
|
||||
bool CWinsockConnection::ReadNumberOfBytes(char* buffer, uint32_t length)
|
||||
{
|
||||
uint32_t received = 0;
|
||||
|
||||
@@ -607,7 +618,7 @@ bool CConnection::ReadNumberOfBytes(char* buffer, uint32_t length)
|
||||
return (received == length);
|
||||
}
|
||||
|
||||
bool CConnection::ValidateHeader(const SMsgHeader& msgHeader)
|
||||
bool CWinsockConnection::ValidateHeader(const SMsgHeader& msgHeader)
|
||||
{
|
||||
// Kept only for compatibility with any legacy users (not used in SDV path)
|
||||
if ((msgHeader.msgStart == m_MsgStart) && (msgHeader.msgEnd == m_MsgEnd))
|
||||
@@ -618,7 +629,7 @@ bool CConnection::ValidateHeader(const SMsgHeader& msgHeader)
|
||||
}
|
||||
|
||||
|
||||
uint32_t CConnection::ReadDataTable(const CMessage& message, SDataContext& dataCtx)
|
||||
uint32_t CWinsockConnection::ReadDataTable(const CMessage& message, SDataContext& dataCtx)
|
||||
{
|
||||
uint32_t offset = 0;
|
||||
|
||||
@@ -691,7 +702,7 @@ uint32_t CConnection::ReadDataTable(const CMessage& message, SDataContext& dataC
|
||||
return offset;
|
||||
}
|
||||
|
||||
bool CConnection::ReadDataChunk(const CMessage& message, uint32_t offset, SDataContext& dataCtx)
|
||||
bool CWinsockConnection::ReadDataChunk(const CMessage& message, uint32_t offset, SDataContext& dataCtx)
|
||||
{
|
||||
if (offset < sizeof(SMsgHdr))
|
||||
return false;
|
||||
@@ -743,12 +754,12 @@ bool CConnection::ReadDataChunk(const CMessage& message, uint32_t offset, SDataC
|
||||
return true;
|
||||
}
|
||||
|
||||
void CConnection::ReceiveSyncRequest(const CMessage& message)
|
||||
void CWinsockConnection::ReceiveSyncRequest(const CMessage& message)
|
||||
{
|
||||
const auto hdr = message.GetMsgHdr();
|
||||
if (hdr.uiVersion != SDVFrameworkInterfaceVersion)
|
||||
{
|
||||
SetStatus(sdv::ipc::EConnectStatus::communication_error);
|
||||
SetConnectState(sdv::ipc::EConnectState::communication_error);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -764,12 +775,12 @@ void CConnection::ReceiveSyncRequest(const CMessage& message)
|
||||
return;
|
||||
}
|
||||
|
||||
void CConnection::ReceiveSyncAnswer(const CMessage& message)
|
||||
void CWinsockConnection::ReceiveSyncAnswer(const CMessage& message)
|
||||
{
|
||||
const auto hdr = message.GetMsgHdr();
|
||||
if (hdr.uiVersion != SDVFrameworkInterfaceVersion)
|
||||
{
|
||||
SetStatus(sdv::ipc::EConnectStatus::communication_error);
|
||||
SetConnectState(sdv::ipc::EConnectState::communication_error);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -786,12 +797,12 @@ void CConnection::ReceiveSyncAnswer(const CMessage& message)
|
||||
return;
|
||||
}
|
||||
|
||||
void CConnection::ReceiveConnectRequest(const CMessage& message)
|
||||
void CWinsockConnection::ReceiveConnectRequest(const CMessage& message)
|
||||
{
|
||||
const auto hdr = message.GetConnectHdr();
|
||||
if (hdr.uiVersion != SDVFrameworkInterfaceVersion)
|
||||
{
|
||||
SetStatus(sdv::ipc::EConnectStatus::communication_error);
|
||||
SetConnectState(sdv::ipc::EConnectState::communication_error);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -808,42 +819,42 @@ void CConnection::ReceiveConnectRequest(const CMessage& message)
|
||||
return;
|
||||
}
|
||||
|
||||
void CConnection::ReceiveConnectAnswer(const CMessage& message)
|
||||
void CWinsockConnection::ReceiveConnectAnswer(const CMessage& message)
|
||||
{
|
||||
const auto hdr = message.GetConnectHdr();
|
||||
if (hdr.uiVersion != SDVFrameworkInterfaceVersion)
|
||||
{
|
||||
SetStatus(sdv::ipc::EConnectStatus::communication_error);
|
||||
SetConnectState(sdv::ipc::EConnectState::communication_error);
|
||||
return;
|
||||
}
|
||||
|
||||
// Handshake complete (client side)
|
||||
SetStatus(sdv::ipc::EConnectStatus::connected);
|
||||
SetConnectState(sdv::ipc::EConnectState::connected);
|
||||
}
|
||||
|
||||
void CConnection::ReceiveConnectTerm(const CMessage& /*message*/)
|
||||
void CWinsockConnection::ReceiveConnectTerm(const CMessage& /*message*/)
|
||||
{
|
||||
SetStatus(sdv::ipc::EConnectStatus::disconnected);
|
||||
SetConnectState(sdv::ipc::EConnectState::disconnected);
|
||||
m_StopReceiveThread.store(true);
|
||||
}
|
||||
|
||||
void CConnection::ReceiveDataMessage(const CMessage& message, SDataContext& dataCtx)
|
||||
void CWinsockConnection::ReceiveDataMessage(const CMessage& message, SDataContext& dataCtx)
|
||||
{
|
||||
const uint32_t payloadOffset = ReadDataTable(message, dataCtx);
|
||||
if (payloadOffset == 0)
|
||||
{
|
||||
SetStatus(sdv::ipc::EConnectStatus::communication_error);
|
||||
SetConnectState(sdv::ipc::EConnectState::communication_error);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!ReadDataChunk(message, payloadOffset, dataCtx))
|
||||
{
|
||||
SetStatus(sdv::ipc::EConnectStatus::communication_error);
|
||||
SetConnectState(sdv::ipc::EConnectState::communication_error);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void CConnection::ReceiveDataFragmentMessage(const CMessage& message, SDataContext& dataCtx)
|
||||
void CWinsockConnection::ReceiveDataFragmentMessage(const CMessage& message, SDataContext& dataCtx)
|
||||
{
|
||||
uint32_t offset = static_cast<uint32_t>(sizeof(SFragmentedMsgHdr));
|
||||
|
||||
@@ -852,19 +863,19 @@ void CConnection::ReceiveDataFragmentMessage(const CMessage& message, SDataConte
|
||||
offset = ReadDataTable(message, dataCtx);
|
||||
if (offset == 0)
|
||||
{
|
||||
SetStatus(sdv::ipc::EConnectStatus::communication_error);
|
||||
SetConnectState(sdv::ipc::EConnectState::communication_error);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (!ReadDataChunk(message, offset, dataCtx))
|
||||
{
|
||||
SetStatus(sdv::ipc::EConnectStatus::communication_error);
|
||||
SetConnectState(sdv::ipc::EConnectState::communication_error);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void CConnection::ReceiveMessages()
|
||||
void CWinsockConnection::ReceiveMessages()
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -898,14 +909,14 @@ void CConnection::ReceiveMessages()
|
||||
if (!ReadNumberOfBytes(reinterpret_cast<char*>(&packetSize),
|
||||
sizeof(packetSize)))
|
||||
{
|
||||
SetStatus(sdv::ipc::EConnectStatus::disconnected);
|
||||
SetConnectState(sdv::ipc::EConnectState::disconnected);
|
||||
SDV_LOG_WARNING("[UDS][RX] Incomplete payload read -> disconnected");
|
||||
break;
|
||||
}
|
||||
|
||||
if (packetSize == 0 || packetSize > kMaxUdsPacketSize)
|
||||
{
|
||||
SetStatus(sdv::ipc::EConnectStatus::communication_error);
|
||||
SetConnectState(sdv::ipc::EConnectState::communication_error);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -914,7 +925,7 @@ void CConnection::ReceiveMessages()
|
||||
if (!ReadNumberOfBytes(reinterpret_cast<char*>(payload.data()),
|
||||
packetSize))
|
||||
{
|
||||
SetStatus(sdv::ipc::EConnectStatus::communication_error);
|
||||
SetConnectState(sdv::ipc::EConnectState::communication_error);
|
||||
SDV_LOG_WARNING("[UDS][RX] Invalid SDV message (envelope)");
|
||||
break;
|
||||
}
|
||||
@@ -923,7 +934,7 @@ void CConnection::ReceiveMessages()
|
||||
CMessage msg(std::move(payload));
|
||||
if (!msg.IsValid())
|
||||
{
|
||||
SetStatus(sdv::ipc::EConnectStatus::communication_error);
|
||||
SetConnectState(sdv::ipc::EConnectState::communication_error);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -941,12 +952,13 @@ void CConnection::ReceiveMessages()
|
||||
break;
|
||||
}
|
||||
|
||||
if (m_ConnectionStatus == sdv::ipc::EConnectStatus::terminating)
|
||||
if (m_ConnectionState == sdv::ipc::EConnectState::terminating)
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
SetStatus(sdv::ipc::EConnectStatus::disconnected);
|
||||
SetConnectState(sdv::ipc::EConnectState::disconnected);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -10,9 +10,9 @@
|
||||
* Contributors:
|
||||
* Denisa Ros - initial API and implementation
|
||||
********************************************************************************/
|
||||
|
||||
#ifndef CONNECTION_H
|
||||
#define CONNECTION_H
|
||||
#ifdef _WIN32
|
||||
#ifndef WIN_SOCKETS_CONNECTION_H
|
||||
#define WIN_SOCKETS_CONNECTION_H
|
||||
|
||||
#include <interfaces/ipc.h>
|
||||
#include <support/interface_ptr.h>
|
||||
@@ -35,6 +35,7 @@
|
||||
# endif
|
||||
#endif
|
||||
|
||||
|
||||
/// @brief Legacy framing markers for the old message header (not used by SDV envelope)
|
||||
constexpr uint32_t m_MsgStart = 0x01020304; ///< Value to mark the start of the legacy message header
|
||||
constexpr uint32_t m_MsgEnd = 0x05060708; ///< Value to mark the end of the legacy message header
|
||||
@@ -67,10 +68,10 @@ struct SMsgHeader
|
||||
*
|
||||
* Exposes:
|
||||
* - sdv::ipc::IDataSend : sending SDV-framed messages
|
||||
* - sdv::ipc::IConnect : async connect / wait / status / events
|
||||
* - sdv::ipc::IConnect : async connect / wait / state / events
|
||||
* - sdv::IObjectDestroy : explicit destruction hook for SDV runtime
|
||||
*/
|
||||
class CConnection
|
||||
class CWinsockConnection
|
||||
: public sdv::IInterfaceAccess
|
||||
, public sdv::ipc::IDataSend
|
||||
, public sdv::ipc::IConnect
|
||||
@@ -80,19 +81,19 @@ public:
|
||||
/**
|
||||
* @brief default constructor used by create endpoint - allocates new buffers m_Sender and m_Receiver
|
||||
*/
|
||||
CConnection();
|
||||
CWinsockConnection();
|
||||
|
||||
/**
|
||||
* @brief access existing connection
|
||||
* @param[in] preconfiguredSocket Prepared socket for the connection
|
||||
* @param[in] acceptConnectionRequired If true connection has to be accepted before receive thread can be started
|
||||
*/
|
||||
CConnection(SOCKET preconfiguredSocket, bool acceptConnectionRequired);
|
||||
CWinsockConnection(unsigned long long preconfiguredSocket, bool acceptConnectionRequired);
|
||||
|
||||
/**
|
||||
* @brief Virtual destructor needed for "delete this;"
|
||||
*/
|
||||
virtual ~CConnection();
|
||||
virtual ~CWinsockConnection();
|
||||
|
||||
BEGIN_SDV_INTERFACE_MAP()
|
||||
SDV_INTERFACE_ENTRY(sdv::ipc::IDataSend)
|
||||
@@ -122,7 +123,7 @@ public:
|
||||
* @param[in] pReceiver Object that exposes IDataReceiveCallback and
|
||||
* optionally IConnectEventCallback
|
||||
* @return true when the connect worker was started successfully
|
||||
* Use GetStatus / WaitForConnection / callbacks for status
|
||||
* Use GetConnectState / WaitForConnection / callbacks for state
|
||||
*/
|
||||
bool AsyncConnect(/*in*/ sdv::IInterfaceAccess* pReceiver) override;
|
||||
|
||||
@@ -132,7 +133,7 @@ public:
|
||||
* Overload of sdv::ipc::IConnect::WaitForConnection
|
||||
*
|
||||
* @param[in] uiWaitMs
|
||||
* - 0 : do not wait, just check current status
|
||||
* - 0 : do not wait, just check current state
|
||||
* - 0xFFFFFFFF: wait indefinitely
|
||||
* - otherwise : wait up to uiWaitMs milliseconds
|
||||
* @return true if the connection is in connected state when returning
|
||||
@@ -149,36 +150,36 @@ public:
|
||||
/**
|
||||
* @brief Disconnect from the peer and stop I/O threads
|
||||
*
|
||||
* This sets the status to disconnected and releases the event interface
|
||||
* This sets the state to disconnected and releases the event interface
|
||||
*/
|
||||
void Disconnect() override;
|
||||
|
||||
/**
|
||||
* @brief Register event callback interface for connection status updates
|
||||
* @brief Register event callback interface for connection state updates
|
||||
*
|
||||
* Overload of sdv::ipc::IConnect::RegisterStatusEventCallback
|
||||
* Overload of sdv::ipc::IConnect::RegisterStateEventCallback
|
||||
*
|
||||
* @param[in] pEventCallback Pointer to an object exposing
|
||||
* sdv::ipc::IConnectEventCallback.
|
||||
* @return A registration cookie (1 = valid, 0 = failure)
|
||||
*/
|
||||
uint64_t RegisterStatusEventCallback(/*in*/ sdv::IInterfaceAccess* pEventCallback) override;
|
||||
uint64_t RegisterStateEventCallback(/*in*/ sdv::IInterfaceAccess* pEventCallback) override;
|
||||
|
||||
/**
|
||||
* @brief Unregister the status event callback using its cookie
|
||||
* @brief Unregister the state event callback using its cookie
|
||||
*
|
||||
* Overload of sdv::ipc::IConnect::UnregisterStatusEventCallback
|
||||
* Overload of sdv::ipc::IConnect::UnregisterStateEventCallback
|
||||
*
|
||||
* @param[in] uiCookie Cookie returned by RegisterStatusEventCallback
|
||||
* @param[in] uiCookie Cookie returned by RegisterStateEventCallback
|
||||
*/
|
||||
void UnregisterStatusEventCallback(/*in*/ uint64_t uiCookie) override;
|
||||
void UnregisterStateEventCallback(/*in*/ uint64_t uiCookie) override;
|
||||
|
||||
/**
|
||||
* @brief Get current status of the connection
|
||||
* @brief Get current state of the connection
|
||||
*
|
||||
* @return Current sdv::ipc::EConnectStatus
|
||||
* @return Current sdv::ipc::EConnectState
|
||||
*/
|
||||
sdv::ipc::EConnectStatus GetStatus() const override;
|
||||
sdv::ipc::EConnectState GetConnectState() const override;
|
||||
|
||||
/**
|
||||
* @brief Destroy the object
|
||||
@@ -196,7 +197,7 @@ private:
|
||||
std::thread m_ConnectThread;
|
||||
std::atomic<bool> m_StopReceiveThread{false}; ///< bool variable to stop thread
|
||||
std::atomic<bool> m_StopConnectThread{false};
|
||||
std::atomic<sdv::ipc::EConnectStatus> m_ConnectionStatus; ///< the status of the connection
|
||||
std::atomic<sdv::ipc::EConnectState> m_ConnectionState; ///< the state of the connection
|
||||
|
||||
sdv::ipc::IDataReceiveCallback* m_pReceiver = nullptr; ///< Receiver to pass the messages if available
|
||||
sdv::ipc::IConnectEventCallback* m_pEvent = nullptr; ///< Event receiver
|
||||
@@ -217,7 +218,7 @@ private:
|
||||
/// @brief Server accept loop / client connect confirmation
|
||||
void ConnectWorker();
|
||||
|
||||
/// @brief Start the RX thread (pre: status=connected, socket valid)
|
||||
/// @brief Start the RX thread (pre: state=connected, socket valid)
|
||||
void StartReceiveThread_Unsafe();
|
||||
|
||||
/// @brief Stop worker threads and close all sockets cleanly
|
||||
@@ -451,7 +452,7 @@ private:
|
||||
* @brief Handle an incoming connect_term message
|
||||
*
|
||||
* Indicates that the peer requests immediate termination of the connection
|
||||
* Sets status to disconnected and stops the RX loop
|
||||
* Sets state to disconnected and stops the RX loop
|
||||
*
|
||||
* @param message SDV envelope containing the connect_term header
|
||||
*/
|
||||
@@ -503,8 +504,9 @@ private:
|
||||
*/
|
||||
bool ReadDataChunk(const CMessage& rMessage, uint32_t uiOffset, SDataContext& rsDataCtxt);
|
||||
|
||||
/// @brief Centralized status update (notifies waiters and callbacks)
|
||||
void SetStatus(sdv::ipc::EConnectStatus status);
|
||||
/// @brief Centralized state update (notifies waiters and callbacks)
|
||||
void SetConnectState(sdv::ipc::EConnectState state);
|
||||
};
|
||||
|
||||
#endif // CONNECTION_H
|
||||
#endif // WIN_SOCKETS_CONNECTION_H
|
||||
#endif
|
||||
40
sdv_services/uds_win_tunnel/CMakeLists.txt
Normal file
40
sdv_services/uds_win_tunnel/CMakeLists.txt
Normal file
@@ -0,0 +1,40 @@
|
||||
#*******************************************************************************
|
||||
# Copyright (c) 2025-2026 ZF Friedrichshafen AG
|
||||
#
|
||||
# This program and the accompanying materials are made available under the
|
||||
# terms of the Apache License Version 2.0 which is available at
|
||||
# https://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
#*******************************************************************************
|
||||
|
||||
# Define project
|
||||
project(uds_win_tunnel VERSION 1.0 LANGUAGES CXX)
|
||||
|
||||
# Define target
|
||||
add_library(uds_win_tunnel STATIC
|
||||
channel_mgnt.cpp
|
||||
connection.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(uds_win_tunnel
|
||||
PRIVATE
|
||||
uds_win_sockets
|
||||
Ws2_32.lib
|
||||
${CMAKE_THREAD_LIBS_INIT}
|
||||
)
|
||||
|
||||
target_include_directories(uds_win_tunnel
|
||||
PRIVATE
|
||||
./include/
|
||||
../uds_win_sockets/
|
||||
)
|
||||
|
||||
set_target_properties(uds_win_tunnel PROPERTIES PREFIX "")
|
||||
set_target_properties(uds_win_tunnel PROPERTIES SUFFIX ".sdv")
|
||||
|
||||
# Build dependencies
|
||||
add_dependencies(uds_win_tunnel CompileCoreIDL)
|
||||
|
||||
# Appending the service in the service list
|
||||
set(SDV_Service_List ${SDV_Service_List} uds_win_tunnel PARENT_SCOPE)
|
||||
414
sdv_services/uds_win_tunnel/channel_mgnt.cpp
Normal file
414
sdv_services/uds_win_tunnel/channel_mgnt.cpp
Normal file
@@ -0,0 +1,414 @@
|
||||
/********************************************************************************
|
||||
* Copyright (c) 2025-2026 ZF Friedrichshafen AG
|
||||
*
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Apache License Version 2.0 which is available at
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Contributors:
|
||||
* Denisa Ros - initial API and implementation
|
||||
********************************************************************************/
|
||||
#ifdef _WIN32
|
||||
#include "channel_mgnt.h"
|
||||
|
||||
#include "../../global/base64.h"
|
||||
#include <support/toml.h>
|
||||
#include <interfaces/process.h>
|
||||
|
||||
#include <future>
|
||||
|
||||
#pragma push_macro("interface")
|
||||
#undef interface
|
||||
|
||||
#pragma push_macro("GetObject")
|
||||
#undef GetObject
|
||||
|
||||
#ifndef NOMINMAX
|
||||
#define NOMINMAX
|
||||
#endif
|
||||
|
||||
#include <WinSock2.h>
|
||||
#include <Windows.h>
|
||||
#include <ws2tcpip.h>
|
||||
#include <afunix.h>
|
||||
|
||||
#include <array>
|
||||
|
||||
#pragma pop_macro("GetObject")
|
||||
#pragma pop_macro("interface")
|
||||
|
||||
extern int StartUpWinSock();
|
||||
|
||||
namespace
|
||||
{
|
||||
/**
|
||||
* @brief Parse a tunnel connect/config string and extract the path.
|
||||
*
|
||||
* Expected format:
|
||||
* "proto=tunnel;path=<something>;"
|
||||
*
|
||||
* Behavior:
|
||||
* - If "proto=tunnel" missing -> false
|
||||
* - If "path=" missing -> true and outPath.clear()
|
||||
*
|
||||
* @param[in] cs The connect/config string to parse.
|
||||
* @param[out] outPath The extracted path, or empty if not found.
|
||||
* @return true if parsing succeeded, false otherwise.
|
||||
*/
|
||||
static bool ParseTunnelPath(const std::string& cs, std::string& outPath)
|
||||
{
|
||||
constexpr const char* protoKey = "proto=tunnel";
|
||||
constexpr const char* pathKey = "path=";
|
||||
|
||||
if (cs.find(protoKey) == std::string::npos)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto p = cs.find(pathKey);
|
||||
if (p == std::string::npos)
|
||||
{
|
||||
outPath.clear();
|
||||
return true;
|
||||
}
|
||||
|
||||
const auto start = p + std::strlen(pathKey);
|
||||
const auto end = cs.find(';', start);
|
||||
if (end == std::string::npos)
|
||||
{
|
||||
outPath = cs.substr(start);
|
||||
}
|
||||
else
|
||||
{
|
||||
outPath = cs.substr(start, end - start);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Expands Windows environment variables in a string (e.g., %TEMP%).
|
||||
* @param[in] in Input string possibly containing environment variables.
|
||||
* @return String with environment variables expanded, or original if expansion fails.
|
||||
*/
|
||||
static std::string ExpandEnvVars(const std::string& in)
|
||||
{
|
||||
if (in.find('%') == std::string::npos)
|
||||
{
|
||||
return in;
|
||||
}
|
||||
char buf[4096] = {};
|
||||
DWORD n = ExpandEnvironmentStringsA(in.c_str(), buf, static_cast<DWORD>(sizeof(buf)));
|
||||
if (n > 0 && n < sizeof(buf))
|
||||
{
|
||||
return std::string(buf);
|
||||
}
|
||||
return in;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Clamps a UDS path to the maximum allowed by SOCKADDR_UN.
|
||||
* @param[in] p The input path.
|
||||
* @return The clamped path.
|
||||
*/
|
||||
static std::string ClampUdsPath(const std::string& p)
|
||||
{
|
||||
SOCKADDR_UN tmp{};
|
||||
constexpr auto kMax = sizeof(tmp.sun_path) - 1;
|
||||
if (p.size() <= kMax)
|
||||
{
|
||||
return p;
|
||||
}
|
||||
return p.substr(0, kMax);
|
||||
}
|
||||
|
||||
// Only for logging – basename
|
||||
/**
|
||||
* @brief Normalizes a raw UDS path for Windows, extracting the basename and ensuring a default if empty.
|
||||
* @param[in] raw The raw path string.
|
||||
* @return The normalized basename, clamped to max length.
|
||||
*/
|
||||
static std::string NormalizeUdsPathForWindows(const std::string& raw)
|
||||
{
|
||||
std::string p = ExpandEnvVars(raw);
|
||||
const size_t pos = p.find_last_of("/\\");
|
||||
std::string base = (pos == std::string::npos) ? p : p.substr(pos + 1);
|
||||
if (base.empty())
|
||||
{
|
||||
base = "sdv_tunnel.sock";
|
||||
}
|
||||
SDV_LOG_INFO("[AF_UNIX][Tunnel] Normalize raw='", raw, "' -> base='", base, "'");
|
||||
return ClampUdsPath(base);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Creates a short, safe UDS path in the Windows temp directory.
|
||||
* @param[in] raw The raw path string.
|
||||
* @return The full path in %TEMP%\sdv\, clamped to max length.
|
||||
*/
|
||||
static std::string MakeShortWinUdsPath(const std::string& raw)
|
||||
{
|
||||
std::string p = ExpandEnvVars(raw);
|
||||
const size_t pos = p.find_last_of("/\\");
|
||||
std::string base = (pos == std::string::npos) ? p : p.substr(pos + 1);
|
||||
if (base.empty())
|
||||
{
|
||||
base = "sdv_tunnel.sock";
|
||||
}
|
||||
|
||||
std::string dir = ExpandEnvVars("%TEMP%\\sdv\\");
|
||||
CreateDirectoryA(dir.c_str(), nullptr);
|
||||
const std::string full = dir + base;
|
||||
|
||||
return ClampUdsPath(full);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Creates an AF_UNIX listen socket at the specified path.
|
||||
* @param[in] rawPath The raw path for the socket.
|
||||
* @return The created socket handle, or INVALID_SOCKET on failure.
|
||||
*/
|
||||
static SOCKET CreateUnixListenSocket(const std::string& rawPath)
|
||||
{
|
||||
SOCKET s = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
if (s == INVALID_SOCKET)
|
||||
{
|
||||
SDV_LOG_ERROR("[AF_UNIX][Tunnel] socket() FAIL (listen), WSA=", WSAGetLastError());
|
||||
return INVALID_SOCKET;
|
||||
}
|
||||
|
||||
std::string udsPath = MakeShortWinUdsPath(rawPath);
|
||||
SOCKADDR_UN addr{};
|
||||
addr.sun_family = AF_UNIX;
|
||||
strcpy_s(addr.sun_path, sizeof(addr.sun_path), udsPath.c_str());
|
||||
|
||||
const int addrlen = static_cast<int>(
|
||||
offsetof(SOCKADDR_UN, sun_path) + std::strlen(addr.sun_path) + 1);
|
||||
|
||||
::remove(udsPath.c_str());
|
||||
if (bind(s, reinterpret_cast<sockaddr*>(&addr), addrlen) == SOCKET_ERROR)
|
||||
{
|
||||
SDV_LOG_ERROR("[AF_UNIX][Tunnel] bind FAIL, WSA=",
|
||||
WSAGetLastError(), ", path='", udsPath, "'");
|
||||
closesocket(s);
|
||||
return INVALID_SOCKET;
|
||||
}
|
||||
if (listen(s, SOMAXCONN) == SOCKET_ERROR)
|
||||
{
|
||||
SDV_LOG_ERROR("[AF_UNIX][Tunnel] listen FAIL, WSA=",
|
||||
WSAGetLastError(), ", path='", udsPath, "'");
|
||||
closesocket(s);
|
||||
return INVALID_SOCKET;
|
||||
}
|
||||
|
||||
SDV_LOG_INFO("[AF_UNIX][Tunnel] bind+listen OK, path='", udsPath, "'");
|
||||
return s;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Connects to an AF_UNIX socket at the specified path, retrying until timeout.
|
||||
* @param[in] rawPath The raw path to connect to.
|
||||
* @param[in] totalTimeoutMs Total timeout in milliseconds.
|
||||
* @param[in] retryDelayMs Delay between retries in milliseconds.
|
||||
* @return The connected socket handle, or INVALID_SOCKET on failure.
|
||||
*/
|
||||
static SOCKET ConnectUnixSocket(
|
||||
const std::string& rawPath,
|
||||
uint32_t totalTimeoutMs,
|
||||
uint32_t retryDelayMs)
|
||||
{
|
||||
const std::string udsPath = MakeShortWinUdsPath(rawPath);
|
||||
SOCKADDR_UN addr{};
|
||||
addr.sun_family = AF_UNIX;
|
||||
strcpy_s(addr.sun_path, sizeof(addr.sun_path), udsPath.c_str());
|
||||
|
||||
const int addrlen = static_cast<int>(
|
||||
offsetof(SOCKADDR_UN, sun_path) + std::strlen(addr.sun_path) + 1);
|
||||
|
||||
const auto deadline = std::chrono::steady_clock::now() +
|
||||
std::chrono::milliseconds(totalTimeoutMs);
|
||||
|
||||
int lastError = 0;
|
||||
while (true)
|
||||
{
|
||||
SOCKET s = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||
if (s == INVALID_SOCKET)
|
||||
{
|
||||
lastError = WSAGetLastError();
|
||||
SDV_LOG_ERROR("[AF_UNIX][Tunnel] socket() FAIL (client), WSA=", lastError);
|
||||
return INVALID_SOCKET;
|
||||
}
|
||||
|
||||
if (connect(s, reinterpret_cast<const sockaddr*>(&addr), addrlen) == 0)
|
||||
{
|
||||
SDV_LOG_INFO("[AF_UNIX][Tunnel] connect OK, path='", udsPath, "'");
|
||||
return s;
|
||||
}
|
||||
|
||||
lastError = WSAGetLastError();
|
||||
closesocket(s);
|
||||
|
||||
if (std::chrono::steady_clock::now() >= deadline)
|
||||
{
|
||||
SDV_LOG_ERROR("[AF_UNIX][Tunnel] connect TIMEOUT, last WSA=",
|
||||
lastError, ", path='", udsPath, "'");
|
||||
return INVALID_SOCKET;
|
||||
}
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(retryDelayMs));
|
||||
}
|
||||
}
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
bool CSocketsTunnelChannelMgnt::OnInitialize()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
void CSocketsTunnelChannelMgnt::OnShutdown()
|
||||
{}
|
||||
|
||||
// -------- Server bookkeeping (optional) --------
|
||||
void CSocketsTunnelChannelMgnt::OnServerClosed(const std::string& udsPath, CWinTunnelConnection* ptr)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_udsMtx);
|
||||
auto it = m_udsServers.find(udsPath);
|
||||
if (it != m_udsServers.end() && it->second.get() == ptr)
|
||||
{
|
||||
m_udsServers.erase(it);
|
||||
}
|
||||
m_udsServerClaimed.erase(udsPath);
|
||||
}
|
||||
|
||||
// -------- ICreateEndpoint --------
|
||||
sdv::ipc::SChannelEndpoint CSocketsTunnelChannelMgnt::CreateEndpoint(const sdv::u8string& cfgStr)
|
||||
{
|
||||
sdv::ipc::SChannelEndpoint ep{};
|
||||
|
||||
if (StartUpWinSock() != 0)
|
||||
{
|
||||
SDV_LOG_ERROR("[AF_UNIX][Tunnel] WinSock startup failed in CreateEndpoint");
|
||||
return ep;
|
||||
}
|
||||
|
||||
// Optional TOML config: [IpcChannel] Path = "..."
|
||||
std::string udsRaw;
|
||||
if (!cfgStr.empty())
|
||||
{
|
||||
//for toml file
|
||||
bool isTOML = cfgStr.find('=') == std::string::npos;
|
||||
if(isTOML)
|
||||
{
|
||||
sdv::toml::CTOMLParser cfg(cfgStr.c_str());
|
||||
auto pathNode = cfg.GetDirect("IpcChannel.Path");
|
||||
if (pathNode.GetType() == sdv::toml::ENodeType::node_string)
|
||||
{
|
||||
udsRaw = static_cast<std::string>(pathNode.GetValue());
|
||||
}
|
||||
}
|
||||
|
||||
//for connect string
|
||||
if (udsRaw.empty())
|
||||
{
|
||||
const std::string s(cfgStr);
|
||||
const std::string key = "path=";
|
||||
auto pos = s.find(key);
|
||||
if (pos != std::string::npos)
|
||||
{
|
||||
auto end = s.find(';', pos + key.size());
|
||||
if (end == std::string::npos)
|
||||
udsRaw = s.substr(pos + key.size());
|
||||
else
|
||||
udsRaw = s.substr(pos + key.size(), end - pos - key.size());
|
||||
}
|
||||
}
|
||||
}
|
||||
if (udsRaw.empty())
|
||||
{
|
||||
udsRaw = "%LOCALAPPDATA%/sdv/tunnel.sock";
|
||||
}
|
||||
|
||||
std::string udsPathBase = NormalizeUdsPathForWindows(udsRaw);
|
||||
SDV_LOG_INFO("[AF_UNIX][Tunnel] endpoint udsPath=", udsPathBase);
|
||||
|
||||
SOCKET listenSocket = CreateUnixListenSocket(udsPathBase);
|
||||
if (listenSocket == INVALID_SOCKET)
|
||||
{
|
||||
SDV_LOG_ERROR("[AF_UNIX][Tunnel] Failed to create listen socket for endpoint: ", udsPathBase);
|
||||
return ep;
|
||||
}
|
||||
|
||||
auto serverTransport = std::make_shared<CWinsockConnection>( static_cast<unsigned long long>(listenSocket), true);
|
||||
auto serverTunnel = std::make_shared<CWinTunnelConnection>(serverTransport, /*channelId*/ static_cast<uint16_t>(0u));
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_udsMtx);
|
||||
m_udsServers[udsPathBase] = serverTunnel;
|
||||
m_udsServerClaimed.erase(udsPathBase);
|
||||
}
|
||||
|
||||
ep.pConnection = static_cast<sdv::IInterfaceAccess*>(serverTunnel.get());
|
||||
ep.ssConnectString = "proto=tunnel;role=server;path=" + udsPathBase + ";";
|
||||
|
||||
return ep;
|
||||
}
|
||||
|
||||
sdv::IInterfaceAccess* CSocketsTunnelChannelMgnt::Access(const sdv::u8string& cs)
|
||||
{
|
||||
if (StartUpWinSock() != 0)
|
||||
{
|
||||
SDV_LOG_ERROR("[AF_UNIX][Tunnel] WinSock startup failed in Access()" );
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
std::string connectStr = static_cast<std::string>(cs);
|
||||
std::string udsRaw;
|
||||
if (!ParseTunnelPath(connectStr, udsRaw))
|
||||
{
|
||||
SDV_LOG_ERROR("[AF_UNIX][Tunnel] Invalid tunnel connect string: ", connectStr);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (udsRaw.empty())
|
||||
{
|
||||
udsRaw = "%LOCALAPPDATA%/sdv/tunnel.sock";
|
||||
}
|
||||
|
||||
std::string udsPathBase = NormalizeUdsPathForWindows(udsRaw);
|
||||
SDV_LOG_INFO("[AF_UNIX][Tunnel] Access udsPath=", udsPathBase);
|
||||
|
||||
const bool isServer =
|
||||
(connectStr.find("role=server") != std::string::npos);
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_udsMtx);
|
||||
auto it = m_udsServers.find(udsPathBase);
|
||||
if (isServer && it != m_udsServers.end() && it->second != nullptr)
|
||||
{
|
||||
if (!m_udsServerClaimed.count(udsPathBase))
|
||||
{
|
||||
m_udsServerClaimed.insert(udsPathBase);
|
||||
SDV_LOG_INFO("[AF_UNIX][Tunnel] Access -> RETURN SERVER for ", udsPathBase);
|
||||
return it->second.get(); // Ownership: managed by m_udsServers (do not delete)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// CLIENT: create AF_UNIX client socket and wrap it in a tunnel
|
||||
SOCKET s = ConnectUnixSocket(udsPathBase, /*totalTimeoutMs*/ 5000, /*retryDelayMs*/ 50);
|
||||
if (s == INVALID_SOCKET)
|
||||
{
|
||||
SDV_LOG_ERROR("[AF_UNIX][Tunnel] Failed to connect client socket for ", udsPathBase);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
SDV_LOG_INFO("[AF_UNIX][Tunnel] Access -> CREATE CLIENT for ", udsPathBase);
|
||||
auto clientTransport = std::make_shared<CWinsockConnection>(s, /*acceptRequired*/ false);
|
||||
// Ownership: The returned pointer must be managed and deleted by the SDV framework via IObjectDestroy
|
||||
auto* tunnelClient = new CWinTunnelConnection(clientTransport, /*channelId*/ 0u);
|
||||
|
||||
return static_cast<sdv::IInterfaceAccess*>(tunnelClient);
|
||||
}
|
||||
#endif
|
||||
106
sdv_services/uds_win_tunnel/channel_mgnt.h
Normal file
106
sdv_services/uds_win_tunnel/channel_mgnt.h
Normal file
@@ -0,0 +1,106 @@
|
||||
/********************************************************************************
|
||||
* Copyright (c) 2025-2026 ZF Friedrichshafen AG
|
||||
*
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Apache License Version 2.0 which is available at
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Contributors:
|
||||
* Denisa Ros - initial API and implementation
|
||||
********************************************************************************/
|
||||
#ifdef _WIN32
|
||||
#ifndef WIN_TUNNEL_CHANNEL_MGNT_H
|
||||
#define WIN_TUNNEL_CHANNEL_MGNT_H
|
||||
|
||||
#include <support/component_impl.h>
|
||||
#include <interfaces/ipc.h>
|
||||
#include "../sdv_services/uds_win_sockets/channel_mgnt.h"
|
||||
#include "../sdv_services/uds_win_sockets/connection.h"
|
||||
#include "connection.h"
|
||||
|
||||
#include <mutex>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <set>
|
||||
#include <string>
|
||||
|
||||
// Winsock headers are required for SOCKET / AF_UNIX / WSAStartup
|
||||
// NOTE: The actual initialization is done via StartUpWinSock()
|
||||
#include <ws2tcpip.h>
|
||||
|
||||
class CWinTunnelConnection;
|
||||
|
||||
/**
|
||||
* @class CSocketsTunnelChannelMgnt
|
||||
* @brief IPC channel management class for Windows AF_UNIX tunnel communication.
|
||||
*
|
||||
* Similar to CSocketsChannelMgnt (proto=uds), but:
|
||||
* - uses CWinTunnelConnection (tunnel wrapper) on top of CWinsockConnection
|
||||
* - uses proto=tunnel in connect strings
|
||||
*
|
||||
* Provides creation and access to tunnel endpoints, manages server-side tunnel lifetimes,
|
||||
* and integrates with the SDV object/component framework.
|
||||
*/
|
||||
class CSocketsTunnelChannelMgnt :
|
||||
public sdv::CSdvObject,
|
||||
public sdv::ipc::ICreateEndpoint,
|
||||
public sdv::ipc::IChannelAccess
|
||||
{
|
||||
public:
|
||||
BEGIN_SDV_INTERFACE_MAP()
|
||||
SDV_INTERFACE_ENTRY(sdv::ipc::IChannelAccess)
|
||||
SDV_INTERFACE_ENTRY(sdv::ipc::ICreateEndpoint)
|
||||
END_SDV_INTERFACE_MAP()
|
||||
|
||||
DECLARE_OBJECT_CLASS_TYPE(sdv::EObjectType::system_object)
|
||||
DECLARE_OBJECT_CLASS_NAME("WinTunnelChannelControl")
|
||||
DECLARE_OBJECT_CLASS_ALIAS("LocalChannelControl")
|
||||
DECLARE_DEFAULT_OBJECT_NAME("LocalChannelControl")
|
||||
DECLARE_OBJECT_SINGLETON()
|
||||
|
||||
virtual ~CSocketsTunnelChannelMgnt() = default;
|
||||
|
||||
/**
|
||||
* @brief Initialization event, called after object configuration was loaded. Overload of sdv::CSdvObject::OnInitialize.
|
||||
* @return Returns 'true' when the initialization was successful, 'false' when not.
|
||||
*/
|
||||
virtual bool OnInitialize() override;
|
||||
|
||||
/**
|
||||
* @brief Shutdown the object. Overload of sdv::CSdvObject::OnShutdown.
|
||||
*/
|
||||
virtual void OnShutdown() override;
|
||||
|
||||
/**
|
||||
* @brief Creates a tunnel endpoint (server side) and returns endpoint info.
|
||||
* @param[in] cfgStr Optional config string (TOML or connect string).
|
||||
* @return The channel endpoint structure.
|
||||
*/
|
||||
sdv::ipc::SChannelEndpoint CreateEndpoint(const sdv::u8string& cfgStr) override;
|
||||
|
||||
/**
|
||||
* @brief Creates or accesses a connection object from the channel connect string.
|
||||
* @param[in] cs The channel connect string.
|
||||
* @return Pointer to connection access interface.
|
||||
*/
|
||||
sdv::IInterfaceAccess* Access(const sdv::u8string& cs) override;
|
||||
|
||||
/**
|
||||
* @brief Called by server tunnel when closing (bookkeeping).
|
||||
* @param[in] udsPath The UDS path for the server.
|
||||
* @param[in] ptr Pointer to the tunnel connection being closed.
|
||||
*/
|
||||
void OnServerClosed(const std::string& udsPath, CWinTunnelConnection* ptr);
|
||||
|
||||
private:
|
||||
std::mutex m_udsMtx;
|
||||
std::map<std::string, std::shared_ptr<CWinTunnelConnection>> m_udsServers;
|
||||
std::set<std::string> m_udsServerClaimed;
|
||||
};
|
||||
|
||||
DEFINE_SDV_OBJECT(CSocketsTunnelChannelMgnt)
|
||||
|
||||
#endif // ! defined WIN_TUNNEL_CHANNEL_MGNT_H
|
||||
#endif
|
||||
226
sdv_services/uds_win_tunnel/connection.cpp
Normal file
226
sdv_services/uds_win_tunnel/connection.cpp
Normal file
@@ -0,0 +1,226 @@
|
||||
/************************************************************
|
||||
* Copyright (c) 2025-2026 ZF Friedrichshafen AG
|
||||
*
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Apache License Version 2.0 which is available at
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Contributors:
|
||||
* Denisa Ros - initial API and implementation
|
||||
************************************************************/
|
||||
|
||||
#ifdef _WIN32
|
||||
#include "connection.h"
|
||||
|
||||
|
||||
CWinTunnelConnection::CWinTunnelConnection(
|
||||
std::shared_ptr<CWinsockConnection> transport,
|
||||
uint16_t channelId)
|
||||
: m_Transport(std::move(transport))
|
||||
, m_ChannelId(channelId)
|
||||
{
|
||||
// No additional initialization required; acts as a thin wrapper.
|
||||
}
|
||||
|
||||
bool CWinTunnelConnection::SendData(
|
||||
/*inout*/ sdv::sequence<sdv::pointer<uint8_t>>& seqData)
|
||||
{
|
||||
if (!m_Transport)
|
||||
{
|
||||
SDV_LOG_ERROR("[WinTunnel] SendData failed: transport is null");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Build tunnel header buffer
|
||||
sdv::pointer<uint8_t> hdrBuf;
|
||||
hdrBuf.resize(sizeof(STunnelHeader));
|
||||
|
||||
STunnelHeader hdr{};
|
||||
hdr.uiChannelId = m_ChannelId; // Logical channel for this connection
|
||||
hdr.uiFlags = 0; // Reserved for future use
|
||||
|
||||
std::memcpy(hdrBuf.get(), &hdr, sizeof(STunnelHeader));
|
||||
|
||||
// Compose new sequence: [header] + original payload chunks
|
||||
sdv::sequence<sdv::pointer<uint8_t>> seqWithHdr;
|
||||
seqWithHdr.push_back(hdrBuf);
|
||||
for (auto& chunk : seqData)
|
||||
{
|
||||
seqWithHdr.push_back(chunk);
|
||||
}
|
||||
|
||||
bool result = m_Transport->SendData(seqWithHdr);
|
||||
if (!result) {
|
||||
SDV_LOG_ERROR("[WinTunnel] SendData failed in underlying transport");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
bool CWinTunnelConnection::AsyncConnect(/*in*/ sdv::IInterfaceAccess* pReceiver)
|
||||
{
|
||||
if (!m_Transport)
|
||||
{
|
||||
SDV_LOG_ERROR("[WinTunnel] AsyncConnect failed: transport is null");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Store upper-layer callbacks (safe for null)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_CallbackMtx);
|
||||
sdv::TInterfaceAccessPtr acc(pReceiver);
|
||||
m_pUpperReceiver = acc.GetInterface<sdv::ipc::IDataReceiveCallback>();
|
||||
m_pUpperEvent = acc.GetInterface<sdv::ipc::IConnectEventCallback>();
|
||||
}
|
||||
|
||||
// Register this tunnel as the data/event receiver in the AF_UNIX transport.
|
||||
bool result = m_Transport->AsyncConnect(this);
|
||||
if (!result) {
|
||||
SDV_LOG_ERROR("[WinTunnel] AsyncConnect failed in underlying transport");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
bool CWinTunnelConnection::WaitForConnection(/*in*/ uint32_t uiWaitMs)
|
||||
{
|
||||
if (!m_Transport)
|
||||
{
|
||||
SDV_LOG_ERROR("[WinTunnel] WaitForConnection failed: transport is null");
|
||||
return false;
|
||||
}
|
||||
return m_Transport->WaitForConnection(uiWaitMs);
|
||||
}
|
||||
|
||||
void CWinTunnelConnection::CancelWait()
|
||||
{
|
||||
if (!m_Transport)
|
||||
{
|
||||
SDV_LOG_ERROR("[WinTunnel] CancelWait failed: transport is null");
|
||||
return;
|
||||
}
|
||||
m_Transport->CancelWait();
|
||||
}
|
||||
|
||||
void CWinTunnelConnection::Disconnect()
|
||||
{
|
||||
if (!m_Transport)
|
||||
{
|
||||
SDV_LOG_ERROR("[WinTunnel] Disconnect failed: transport is null");
|
||||
return;
|
||||
}
|
||||
|
||||
m_Transport->Disconnect();
|
||||
|
||||
// Clear upper-layer callbacks (thread-safe)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_CallbackMtx);
|
||||
m_pUpperReceiver = nullptr;
|
||||
m_pUpperEvent = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t CWinTunnelConnection::RegisterStateEventCallback(
|
||||
/*in*/ sdv::IInterfaceAccess* pEventCallback)
|
||||
{
|
||||
if (!m_Transport)
|
||||
{
|
||||
SDV_LOG_ERROR("[WinTunnel] RegisterStateEventCallback failed: transport is null");
|
||||
return 0ULL;
|
||||
}
|
||||
|
||||
// Forward directly to underlying CWinsockConnection
|
||||
return m_Transport->RegisterStateEventCallback(pEventCallback);
|
||||
}
|
||||
|
||||
void CWinTunnelConnection::UnregisterStateEventCallback(
|
||||
/*in*/ uint64_t uiCookie)
|
||||
{
|
||||
if (!m_Transport || uiCookie == 0ULL)
|
||||
{
|
||||
SDV_LOG_ERROR("[WinTunnel] UnregisterStateEventCallback failed: transport is null or cookie is 0");
|
||||
return;
|
||||
}
|
||||
|
||||
m_Transport->UnregisterStateEventCallback(uiCookie);
|
||||
}
|
||||
|
||||
sdv::ipc::EConnectState CWinTunnelConnection::GetConnectState() const
|
||||
{
|
||||
if (!m_Transport)
|
||||
{
|
||||
return sdv::ipc::EConnectState::uninitialized;
|
||||
}
|
||||
return m_Transport->GetConnectState();
|
||||
}
|
||||
|
||||
void CWinTunnelConnection::DestroyObject()
|
||||
{
|
||||
Disconnect();
|
||||
std::lock_guard<std::mutex> lock(m_CallbackMtx);
|
||||
m_Transport.reset();
|
||||
}
|
||||
|
||||
void CWinTunnelConnection::ReceiveData(
|
||||
/*inout*/ sdv::sequence<sdv::pointer<uint8_t>>& seqData)
|
||||
{
|
||||
// Expect at least one chunk (the tunnel header)
|
||||
if (seqData.empty())
|
||||
{
|
||||
SDV_LOG_ERROR("[WinTunnel] ReceiveData: empty sequence");
|
||||
return; // nothing to do
|
||||
}
|
||||
|
||||
const auto& hdrChunk = seqData[0];
|
||||
if (hdrChunk.size() < sizeof(STunnelHeader))
|
||||
{
|
||||
SDV_LOG_ERROR("[WinTunnel] ReceiveData: invalid tunnel header size");
|
||||
// Invalid tunnel frame; drop it for now (could set communication_error)
|
||||
return;
|
||||
}
|
||||
|
||||
STunnelHeader hdr{};
|
||||
std::memcpy(&hdr, hdrChunk.get(), sizeof(STunnelHeader));
|
||||
|
||||
|
||||
// TODO: use channelId for multiplexing later
|
||||
|
||||
// Build payload-only sequence: drop header chunk, keep others
|
||||
sdv::sequence<sdv::pointer<uint8_t>> payload;
|
||||
for (size_t i = 1; i < seqData.size(); ++i)
|
||||
{
|
||||
payload.push_back(seqData[i]);
|
||||
}
|
||||
|
||||
if (m_pUpperReceiver)
|
||||
{
|
||||
try {
|
||||
m_pUpperReceiver->ReceiveData(payload); // header stripped
|
||||
} catch (...) {
|
||||
SDV_LOG_ERROR("[WinTunnel] Exception in upper receiver's ReceiveData");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void CWinTunnelConnection::SetConnectState(sdv::ipc::EConnectState state)
|
||||
{
|
||||
sdv::ipc::IConnectEventCallback* upper = nullptr;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_CallbackMtx);
|
||||
upper = m_pUpperEvent;
|
||||
}
|
||||
|
||||
if (upper)
|
||||
{
|
||||
try
|
||||
{
|
||||
upper->SetConnectState(state);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
SDV_LOG_ERROR("[WinTunnel] Exception in upper event callback's SetConnectState");
|
||||
// Never let user callback crash the transport.
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
168
sdv_services/uds_win_tunnel/connection.h
Normal file
168
sdv_services/uds_win_tunnel/connection.h
Normal file
@@ -0,0 +1,168 @@
|
||||
/************************************************************
|
||||
* Copyright (c) 2025-2026 ZF Friedrichshafen AG
|
||||
*
|
||||
* This program and the accompanying materials are made available under the
|
||||
* terms of the Apache License Version 2.0 which is available at
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Contributors:
|
||||
* Denisa Ros - initial API and implementation
|
||||
************************************************************/
|
||||
#ifdef _WIN32
|
||||
#ifndef UDS_WIN_TUNNEL_CONNECTION_H
|
||||
#define UDS_WIN_TUNNEL_CONNECTION_H
|
||||
|
||||
#include <interfaces/ipc.h>
|
||||
#include <support/component_impl.h>
|
||||
#include <support/interface_ptr.h>
|
||||
|
||||
#include <WinSock2.h>
|
||||
#include <atomic>
|
||||
#include <cstdint>
|
||||
#include <mutex>
|
||||
#include <memory>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include "../sdv_services/uds_win_sockets/connection.h" // existing AF_UNIX transport: CWinsockConnection
|
||||
|
||||
|
||||
/**
|
||||
* @brief Logical tunnel connection on top of a shared Windows AF_UNIX transport.
|
||||
*
|
||||
* This class does NOT create sockets by itself. It wraps an existing
|
||||
* CWinsockConnection (Winsock AF_UNIX) and adds:
|
||||
* - tunnel header (channelId, flags)
|
||||
* - (later) demultiplexing of incoming payloads per logical channel.
|
||||
*/
|
||||
class CWinTunnelConnection :
|
||||
public sdv::IInterfaceAccess,
|
||||
public sdv::IObjectDestroy,
|
||||
public sdv::ipc::IDataSend,
|
||||
public sdv::ipc::IConnect,
|
||||
public sdv::ipc::IDataReceiveCallback,
|
||||
public sdv::ipc::IConnectEventCallback
|
||||
{
|
||||
public:
|
||||
|
||||
/**
|
||||
* @struct STunnelHeader
|
||||
* @brief Header prepended to each tunneled SDV message for logical channel identification and flags.
|
||||
*/
|
||||
struct STunnelHeader
|
||||
{
|
||||
uint16_t uiChannelId; ///< Logical channel ID (IPC_x / REMOTE_IPC_x)
|
||||
uint16_t uiFlags; ///< Reserved for future use (QoS, direction, etc.)
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Construct a tunnel wrapper over an existing AF_UNIX transport.
|
||||
* @param[in] transport Shared pointer to the underlying AF_UNIX transport.
|
||||
* @param[in] channelId Logical channel ID for this tunnel instance.
|
||||
*/
|
||||
explicit CWinTunnelConnection(
|
||||
std::shared_ptr<CWinsockConnection> transport,
|
||||
uint16_t channelId);
|
||||
|
||||
/**
|
||||
* @brief Destructor.
|
||||
*/
|
||||
virtual ~CWinTunnelConnection() = default;
|
||||
|
||||
BEGIN_SDV_INTERFACE_MAP()
|
||||
SDV_INTERFACE_ENTRY(sdv::ipc::IDataSend)
|
||||
SDV_INTERFACE_ENTRY(sdv::ipc::IConnect)
|
||||
SDV_INTERFACE_ENTRY(sdv::ipc::IDataReceiveCallback)
|
||||
SDV_INTERFACE_ENTRY(sdv::ipc::IConnectEventCallback)
|
||||
SDV_INTERFACE_ENTRY(sdv::IObjectDestroy)
|
||||
END_SDV_INTERFACE_MAP()
|
||||
|
||||
// ---------- IDataSend ----------
|
||||
/**
|
||||
* @brief Send a sequence of buffers via the tunnel.
|
||||
* @param[in,out] seqData Sequence of message buffers (may be modified by callee).
|
||||
* @return true on successful send, false otherwise.
|
||||
*/
|
||||
bool SendData(/*inout*/ sdv::sequence<sdv::pointer<uint8_t>>& seqData) override;
|
||||
|
||||
// ---------- IConnect ----------
|
||||
/**
|
||||
* @brief Start asynchronous connect and register this object as receiver.
|
||||
* @param[in] pReceiver Pointer to callback interface for data and state notifications.
|
||||
* @return true if connect started, false otherwise.
|
||||
*/
|
||||
bool AsyncConnect(/*in*/ sdv::IInterfaceAccess* pReceiver) override;
|
||||
|
||||
/**
|
||||
* @brief Wait until the underlying transport becomes 'connected'.
|
||||
* @param[in] uiWaitMs Timeout in milliseconds to wait.
|
||||
* @return true if connection established, false on timeout or error.
|
||||
*/
|
||||
bool WaitForConnection(/*in*/ uint32_t uiWaitMs) override;
|
||||
|
||||
/**
|
||||
* @brief Cancel any pending connect or wait operation.
|
||||
*/
|
||||
void CancelWait() override;
|
||||
|
||||
/**
|
||||
* @brief Disconnect the tunnel and underlying transport.
|
||||
*/
|
||||
void Disconnect() override;
|
||||
|
||||
/**
|
||||
* @brief Register a state event callback (forwards to transport).
|
||||
* @param[in] pEventCallback Pointer to event callback interface.
|
||||
* @return Registration cookie (nonzero) or 0 on failure.
|
||||
*/
|
||||
uint64_t RegisterStateEventCallback(/*in*/ sdv::IInterfaceAccess* pEventCallback) override;
|
||||
|
||||
/**
|
||||
* @brief Unregister a previously registered state event callback.
|
||||
* @param[in] uiCookie Registration cookie returned by RegisterStateEventCallback.
|
||||
*/
|
||||
void UnregisterStateEventCallback(/*in*/ uint64_t uiCookie) override;
|
||||
/**
|
||||
* @brief Get the current state from the underlying transport.
|
||||
* @return The current connection state.
|
||||
*/
|
||||
sdv::ipc::EConnectState GetConnectState() const override;
|
||||
|
||||
// ---------- IObjectDestroy ----------
|
||||
/**
|
||||
* @brief Release and clean up all resources associated with this object.
|
||||
*/
|
||||
void DestroyObject() override;
|
||||
|
||||
// ---------- IDataReceiveCallback ----------
|
||||
/**
|
||||
* @brief Receive data from the underlying AF_UNIX transport.
|
||||
* @param[in,out] seqData Sequence of received message buffers (header chunk is removed by this call).
|
||||
*/
|
||||
void ReceiveData(/*inout*/ sdv::sequence<sdv::pointer<uint8_t>>& seqData) override;
|
||||
|
||||
// ---------- IConnectEventCallback ----------
|
||||
/**
|
||||
* @brief Forward state changes from the underlying transport to the upper layer.
|
||||
* @param[in] state New connection state.
|
||||
*/
|
||||
void SetConnectState(sdv::ipc::EConnectState state) override;
|
||||
|
||||
// Helpers
|
||||
void SetChannelId(uint16_t channelId) { m_ChannelId = channelId; }
|
||||
uint16_t GetChannelId() const noexcept { return m_ChannelId; }
|
||||
|
||||
private:
|
||||
std::shared_ptr<CWinsockConnection> m_Transport; ///< shared physical tunnel port
|
||||
uint16_t m_ChannelId{0}; ///< default logical channel id
|
||||
|
||||
// Upper layer callbacks (original VAPI receiver)
|
||||
sdv::ipc::IDataReceiveCallback* m_pUpperReceiver{nullptr};
|
||||
sdv::ipc::IConnectEventCallback* m_pUpperEvent{nullptr};
|
||||
mutable std::mutex m_CallbackMtx;
|
||||
};
|
||||
|
||||
#endif // UDS_WIN_TUNNEL_CONNECTION_H
|
||||
#endif
|
||||
Reference in New Issue
Block a user