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 | |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 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 | |
| 21 | ~SctpClient() { |
| 22 | LOG(INFO) << "close(" << fd_ << ")"; |
| 23 | PCHECK(close(fd_) == 0); |
| 24 | } |
| 25 | |
| 26 | // Receives the next packet from the remote. |
| 27 | aos::unique_c_ptr<Message> Read(); |
| 28 | |
| 29 | // Sends a block of data on a stream with a TTL. |
| 30 | bool Send(int stream, std::string_view data, int time_to_live); |
| 31 | |
| 32 | int fd() { return fd_; } |
| 33 | |
| 34 | // Enables the priority scheduler. This is a SCTP feature which lets us |
| 35 | // configure the priority per stream so that higher priority packets don't get |
| 36 | // backed up behind lower priority packets in the networking queues. |
| 37 | void SetPriorityScheduler(sctp_assoc_t assoc_id); |
| 38 | |
| 39 | // Remote to send to. |
| 40 | struct sockaddr_storage sockaddr_remote() const { |
| 41 | return sockaddr_remote_; |
| 42 | } |
| 43 | |
| 44 | void LogSctpStatus(sctp_assoc_t assoc_id); |
| 45 | |
| 46 | private: |
| 47 | struct sockaddr_storage sockaddr_remote_; |
| 48 | struct sockaddr_storage sockaddr_local_; |
| 49 | int fd_; |
| 50 | |
| 51 | size_t max_size_ = 1000; |
| 52 | }; |
| 53 | |
| 54 | } // namespace message_bridge |
| 55 | } // namespace aos |
| 56 | |
| 57 | #endif // AOS_NETWORK_SCTP_CLIENT_H_ |