14 m_tcpSocket(tcpSocket) {
20 void DoIPConnection::closeSocket() {
37 if (!optHeader.has_value()) {
45 auto plType = optHeader->first;
46 auto payloadLength = optHeader->second;
50 if (payloadLength > 0) {
51 LOG_DOIP_DEBUG(
"Waiting for {} bytes of payload...", payloadLength);
53 if (receivedPayloadBytes < payloadLength) {
65 DoIPMessage message(plType, m_receiveBuf.data(), payloadLength);
86 size_t payloadPos = 0;
87 size_t remainingPayload = payloadLength;
89 while (remainingPayload > 0) {
90 ssize_t result = recv(m_tcpSocket, &receivedData[payloadPos], remainingPayload, 0);
94 size_t readBytes =
static_cast<size_t>(result);
95 payloadPos += readBytes;
96 remainingPayload -= readBytes;
103 LOG_DOIP_INFO(
"Application requested to disconnect Client from Server");
114 ssize_t DoIPConnection::sendMessage(
const uint8_t *message,
size_t messageLength) {
115 ssize_t result = write(m_tcpSocket, message, messageLength);
121 ssize_t sentBytes = sendMessage(msg.
data(), msg.
size());
133 LOG_DOIP_DEBUG(
"Connection already closing - ignoring recursive call");
152 return m_logicalAddress;
156 m_logicalAddress = address;
#define LOG_DOIP_ERROR(...)
#define LOG_DOIP_INFO(...)
#define LOG_DOIP_DEBUG(...)
bool hasDownstreamHandler() const override
Check if downstream forwarding is available.
DoIPAddress getClientAddress() const override
Get the currently client (active source) address.
DoIPDiagnosticAck notifyDiagnosticMessage(const DoIPMessage &msg) override
Handle an incoming diagnostic message (application callback)
void notifyDiagnosticAckSent(DoIPDiagnosticAck ack) override
Notify application that diagnostic ACK/NACK was sent.
ssize_t sendProtocolMessage(const DoIPMessage &msg) override
Send a DoIP protocol message to the client.
void setClientAddress(const DoIPAddress &address) override
Set the client (active source) address after routing activation.
void closeConnection(DoIPCloseReason reason) override
Close the TCP connection.
size_t receiveFixedNumberOfBytesFromTCP(uint8_t *receivedData, size_t payloadLength)
Receive exactly payloadLength bytes from the TCP stream and put them into receivedData.
void triggerDisconnection()
DoIPConnection(int tcpSocket, UniqueServerModelPtr model)
void notifyConnectionClosed(DoIPCloseReason reason) override
Notify application that connection is closing.
DoIPAddress getServerAddress() const override
Get the server's logical address.
Default implementation of IConnectionContext.
void closeConnection(DoIPCloseReason reason) override
Closes the connection.
void handleMessage2(const DoIPMessage &message)
Handles a message (internal helper)
UniqueServerModelPtr m_serverModel
Represents a complete DoIP message with internal ByteArray representation.
static std::optional< std::pair< DoIPPayloadType, uint32_t > > tryParseHeader(const uint8_t *data, size_t length)
Gets the payload type from raw data.
size_t size() const
Gets the size for use with legacy APIs.
const uint8_t * data() const
Gets direct pointer to the message data (for legacy APIs).
uint16_t DoIPAddress
Represents a 16-bit DoIP address consisting of high and low significant bytes.
std::optional< DoIPNegativeDiagnosticAck > DoIPDiagnosticAck
Alias for diagnostic acknowledgment type.
constexpr size_t DOIP_HEADER_SIZE
Size of the DoIP header.
constexpr DoIPAddress ZERO_ADDRESS
DoIPCloseReason
Reason for connection closure.
std::unique_ptr< DoIPServerModel > UniqueServerModelPtr
streamed_t< T > streamed(const T &v)