forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTransportMgrBase.cpp
157 lines (137 loc) · 4.87 KB
/
TransportMgrBase.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
*
* Copyright (c) 2020-2021 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
*/
#include <transport/TransportMgrBase.h>
#include <lib/support/CodeUtils.h>
#include <platform/LockTracker.h>
#include <transport/TransportMgr.h>
#include <transport/raw/Base.h>
namespace chip {
CHIP_ERROR TransportMgrBase::SendMessage(const Transport::PeerAddress & address, System::PacketBufferHandle && msgBuf)
{
return mTransport->SendMessage(address, std::move(msgBuf));
}
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
CHIP_ERROR TransportMgrBase::TCPConnect(const Transport::PeerAddress & address, Transport::AppTCPConnectionCallbackCtxt * appState,
Transport::ActiveTCPConnectionState ** peerConnState)
{
return mTransport->TCPConnect(address, appState, peerConnState);
}
void TransportMgrBase::TCPDisconnect(const Transport::PeerAddress & address)
{
mTransport->TCPDisconnect(address);
}
void TransportMgrBase::TCPDisconnect(Transport::ActiveTCPConnectionState * conn, bool shouldAbort)
{
mTransport->TCPDisconnect(conn, shouldAbort);
}
#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT
CHIP_ERROR TransportMgrBase::Init(Transport::Base * transport)
{
if (mTransport != nullptr)
{
return CHIP_ERROR_INCORRECT_STATE;
}
mTransport = transport;
mTransport->SetDelegate(this);
ChipLogDetail(Inet, "TransportMgr initialized");
return CHIP_NO_ERROR;
}
void TransportMgrBase::Close()
{
mSessionManager = nullptr;
mTransport = nullptr;
}
CHIP_ERROR TransportMgrBase::MulticastGroupJoinLeave(const Transport::PeerAddress & address, bool join)
{
return mTransport->MulticastGroupJoinLeave(address, join);
}
void TransportMgrBase::HandleMessageReceived(const Transport::PeerAddress & peerAddress, System::PacketBufferHandle && msg,
Transport::MessageTransportContext * ctxt)
{
// This is the first point all incoming messages funnel through. Ensure
// that our message receipts are all synchronized correctly.
assertChipStackLockedByCurrentThread();
if (msg->HasChainedBuffer())
{
// Something in the lower levels messed up.
char addrBuffer[Transport::PeerAddress::kMaxToStringSize];
peerAddress.ToString(addrBuffer);
ChipLogError(Inet, "message from %s dropped due to lower layers not ensuring a single packet buffer.", addrBuffer);
return;
}
if (mSessionManager != nullptr)
{
mSessionManager->OnMessageReceived(peerAddress, std::move(msg), ctxt);
}
else
{
char addrBuffer[Transport::PeerAddress::kMaxToStringSize];
peerAddress.ToString(addrBuffer);
ChipLogError(Inet, "message from %s is dropped since no corresponding handler is set in TransportMgr.", addrBuffer);
}
}
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
void TransportMgrBase::HandleConnectionReceived(Transport::ActiveTCPConnectionState * conn)
{
if (mSessionManager != nullptr)
{
mSessionManager->HandleConnectionReceived(conn);
}
else
{
Transport::TCPBase * tcp = reinterpret_cast<Transport::TCPBase *>(conn->mEndPoint->mAppState);
// Close connection here since no upper layer is interested in the
// connection.
if (tcp)
{
tcp->TCPDisconnect(conn, /* shouldAbort = */ true);
}
}
}
void TransportMgrBase::HandleConnectionAttemptComplete(Transport::ActiveTCPConnectionState * conn, CHIP_ERROR conErr)
{
if (mSessionManager != nullptr)
{
mSessionManager->HandleConnectionAttemptComplete(conn, conErr);
}
else
{
Transport::TCPBase * tcp = reinterpret_cast<Transport::TCPBase *>(conn->mEndPoint->mAppState);
// Close connection here since no upper layer is interested in the
// connection.
if (tcp)
{
tcp->TCPDisconnect(conn, /* shouldAbort = */ true);
}
}
}
void TransportMgrBase::HandleConnectionClosed(Transport::ActiveTCPConnectionState * conn, CHIP_ERROR conErr)
{
if (mSessionManager != nullptr)
{
mSessionManager->HandleConnectionClosed(conn, conErr);
}
else
{
Transport::TCPBase * tcp = reinterpret_cast<Transport::TCPBase *>(conn->mEndPoint->mAppState);
if (tcp)
{
tcp->TCPDisconnect(conn, /* shouldAbort = */ true);
}
}
}
#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT
} // namespace chip