Line data Source code
1 : /*
2 : *
3 : * Copyright (c) 2020-2021 Project CHIP Authors
4 : *
5 : * Licensed under the Apache License, Version 2.0 (the "License");
6 : * you may not use this file except in compliance with the License.
7 : * You may obtain a copy of the License at
8 : *
9 : * http://www.apache.org/licenses/LICENSE-2.0
10 : *
11 : * Unless required by applicable law or agreed to in writing, software
12 : * distributed under the License is distributed on an "AS IS" BASIS,
13 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 : * See the License for the specific language governing permissions and
15 : */
16 :
17 : /**
18 : * @file
19 : * This file implements a stateless TransportMgr, it will took a raw message
20 : * buffer from transports, and then extract the message header without decode it.
21 : * For secure messages, it will pass it to the SessionManager, and for unsecure
22 : * messages (rendezvous messages), it will pass it to RendezvousSession.
23 : * When sending messages, it will encode the packet header, and pass it to the
24 : * transports.
25 : * The whole process is fully stateless.
26 : */
27 :
28 : #pragma once
29 :
30 : #include <lib/support/CodeUtils.h>
31 : #include <lib/support/logging/CHIPLogging.h>
32 : #include <transport/TransportMgrBase.h>
33 : #include <transport/raw/Base.h>
34 : #include <transport/raw/MessageHeader.h>
35 : #include <transport/raw/PeerAddress.h>
36 : #include <transport/raw/Tuple.h>
37 : #if INET_CONFIG_ENABLE_TCP_ENDPOINT
38 : #include <transport/raw/TCP.h>
39 : #endif // INET_CONFIG_ENABLE_TCP_ENDPOINT
40 :
41 : namespace chip {
42 :
43 : class TransportMgrBase;
44 :
45 : class TransportMgrDelegate
46 : {
47 : public:
48 419 : virtual ~TransportMgrDelegate() = default;
49 : /**
50 : * @brief
51 : * Handle received secure message.
52 : *
53 : * @param source the source address of the package
54 : * @param msgBuf the buffer containing a full CHIP message (except for the optional length field).
55 : * @param ctxt the pointer to additional context on the underlying transport. For TCP, it is a pointer
56 : * to the underlying connection object.
57 : */
58 : virtual void OnMessageReceived(const Transport::PeerAddress & source, System::PacketBufferHandle && msgBuf,
59 : Transport::MessageTransportContext * ctxt = nullptr) = 0;
60 :
61 : #if INET_CONFIG_ENABLE_TCP_ENDPOINT
62 : /**
63 : * @brief
64 : * Handle connection attempt completion.
65 : *
66 : * @param conn the connection object
67 : * @param conErr the connection error on the attempt, or CHIP_NO_ERROR.
68 : */
69 0 : virtual void HandleConnectionAttemptComplete(Transport::ActiveTCPConnectionState * conn, CHIP_ERROR conErr){};
70 :
71 0 : virtual void HandleConnectionClosed(Transport::ActiveTCPConnectionState * conn, CHIP_ERROR conErr){};
72 :
73 0 : virtual void HandleConnectionReceived(Transport::ActiveTCPConnectionState * conn){};
74 : #endif // INET_CONFIG_ENABLE_TCP_ENDPOINT
75 : };
76 :
77 : template <typename... TransportTypes>
78 : class TransportMgr : public TransportMgrBase
79 : {
80 : public:
81 : template <typename... Args>
82 1 : CHIP_ERROR Init(Args &&... transportInitArgs)
83 : {
84 1 : ReturnErrorOnFailure(mTransport.Init(this, std::forward<Args>(transportInitArgs)...));
85 1 : return TransportMgrBase::Init(&mTransport);
86 : }
87 :
88 : template <typename... Args>
89 : CHIP_ERROR ResetTransport(Args &&... transportInitArgs)
90 : {
91 : return mTransport.Init(this, std::forward<Args>(transportInitArgs)...);
92 : }
93 :
94 1 : void Close()
95 : {
96 1 : TransportMgrBase::Close();
97 1 : mTransport.Close();
98 1 : };
99 :
100 : private:
101 : Transport::Tuple<TransportTypes...> mTransport;
102 :
103 : public:
104 1 : auto & GetTransport() { return mTransport; }
105 : };
106 :
107 : } // namespace chip
|