Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 1 | #ifndef AOS_NETWORK_SCTP_CLIENT_H_ |
| 2 | #define AOS_NETWORK_SCTP_CLIENT_H_ |
| 3 | |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 4 | #include <cstdio> |
| 5 | #include <cstdlib> |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 6 | #include <string_view> |
| 7 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 8 | #include "glog/logging.h" |
| 9 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 10 | #include "aos/network/sctp_lib.h" |
| 11 | #include "aos/unique_malloc_ptr.h" |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 12 | |
| 13 | namespace aos { |
| 14 | namespace message_bridge { |
| 15 | |
| 16 | // Class to encapsulate everything needed to be a SCTP client. |
| 17 | class SctpClient { |
| 18 | public: |
| 19 | SctpClient(std::string_view remote_host, int remote_port, int streams, |
Adam Snaider | 96a0f4b | 2023-05-18 20:41:19 -0700 | [diff] [blame^] | 20 | std::string_view local_host = "0.0.0.0", int local_port = 9971, |
| 21 | std::vector<uint8_t> sctp_auth_key = {}); |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 22 | |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 23 | ~SctpClient() {} |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 24 | |
| 25 | // Receives the next packet from the remote. |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 26 | aos::unique_c_ptr<Message> Read() { return sctp_.ReadMessage(); } |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 27 | |
| 28 | // Sends a block of data on a stream with a TTL. |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 29 | // TODO(austin): time_to_live should be a chrono::duration |
| 30 | bool Send(int stream, std::string_view data, int time_to_live) { |
Sarah Newman | 80e955e | 2022-04-13 11:19:36 -0700 | [diff] [blame] | 31 | return sctp_.SendMessage(stream, data, time_to_live, sockaddr_remote_, |
| 32 | sac_assoc_id_); |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 33 | } |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 34 | |
Sarah Newman | 80e955e | 2022-04-13 11:19:36 -0700 | [diff] [blame] | 35 | // Aborts a connection. Returns true on success. |
| 36 | bool Abort() { return sctp_.Abort(sac_assoc_id_); } |
| 37 | |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 38 | int fd() { return sctp_.fd(); } |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 39 | |
| 40 | // Enables the priority scheduler. This is a SCTP feature which lets us |
| 41 | // configure the priority per stream so that higher priority packets don't get |
| 42 | // backed up behind lower priority packets in the networking queues. |
| 43 | void SetPriorityScheduler(sctp_assoc_t assoc_id); |
| 44 | |
| 45 | // Remote to send to. |
| 46 | struct sockaddr_storage sockaddr_remote() const { |
| 47 | return sockaddr_remote_; |
| 48 | } |
| 49 | |
| 50 | void LogSctpStatus(sctp_assoc_t assoc_id); |
| 51 | |
Austin Schuh | 89e1e9c | 2023-05-15 14:38:44 -0700 | [diff] [blame] | 52 | void SetMaxReadSize(size_t max_size) { sctp_.SetMaxReadSize(max_size); } |
| 53 | void SetMaxWriteSize(size_t max_size) { sctp_.SetMaxWriteSize(max_size); } |
Austin Schuh | 4f558e5 | 2023-05-08 21:51:31 -0700 | [diff] [blame] | 54 | void SetPoolSize(size_t pool_size) { sctp_.SetPoolSize(pool_size); } |
Austin Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 55 | |
Sarah Newman | 80e955e | 2022-04-13 11:19:36 -0700 | [diff] [blame] | 56 | void SetAssociationId(sctp_assoc_t sac_assoc_id) { |
| 57 | sac_assoc_id_ = sac_assoc_id; |
| 58 | } |
| 59 | |
Austin Schuh | 4f558e5 | 2023-05-08 21:51:31 -0700 | [diff] [blame] | 60 | void FreeMessage(aos::unique_c_ptr<Message> &&message) { |
| 61 | sctp_.FreeMessage(std::move(message)); |
| 62 | } |
| 63 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 64 | private: |
| 65 | struct sockaddr_storage sockaddr_remote_; |
| 66 | struct sockaddr_storage sockaddr_local_; |
Austin Schuh | 507f758 | 2021-07-31 20:39:55 -0700 | [diff] [blame] | 67 | SctpReadWrite sctp_; |
Sarah Newman | 80e955e | 2022-04-13 11:19:36 -0700 | [diff] [blame] | 68 | |
| 69 | // Valid if != 0. |
| 70 | sctp_assoc_t sac_assoc_id_ = 0; |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | } // namespace message_bridge |
| 74 | } // namespace aos |
| 75 | |
| 76 | #endif // AOS_NETWORK_SCTP_CLIENT_H_ |