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