Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 1 | #ifndef AOS_NETWORK_SCTP_SERVER_H_ |
| 2 | #define AOS_NETWORK_SCTP_SERVER_H_ |
| 3 | |
| 4 | #include <arpa/inet.h> |
| 5 | #include <net/if.h> |
| 6 | #include <netdb.h> |
| 7 | #include <netinet/in.h> |
| 8 | #include <netinet/sctp.h> |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 9 | #include <sys/socket.h> |
| 10 | |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame^] | 11 | #include <cstdio> |
| 12 | #include <cstdlib> |
| 13 | #include <cstring> |
| 14 | #include <memory> |
| 15 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 16 | #include "aos/network/sctp_lib.h" |
| 17 | #include "aos/unique_malloc_ptr.h" |
| 18 | #include "glog/logging.h" |
| 19 | |
| 20 | namespace aos { |
| 21 | namespace message_bridge { |
| 22 | |
| 23 | class SctpServer { |
| 24 | public: |
| 25 | SctpServer(std::string_view local_host = "0.0.0.0", int local_port = 9971); |
| 26 | |
| 27 | ~SctpServer() { |
| 28 | LOG(INFO) << "close(" << fd_ << ")"; |
| 29 | PCHECK(close(fd_) == 0); |
| 30 | } |
| 31 | |
| 32 | // Receives the next packet from the remote. |
| 33 | aos::unique_c_ptr<Message> Read(); |
| 34 | |
Austin Schuh | 83afb7a | 2020-03-15 23:09:22 -0700 | [diff] [blame] | 35 | // Sends a block of data to a client on a stream with a TTL. Returns true on |
| 36 | // success. |
| 37 | bool Send(std::string_view data, sctp_assoc_t snd_assoc_id, int stream, |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 38 | int timetolive); |
| 39 | |
Austin Schuh | 4889b18 | 2020-11-18 19:11:56 -0800 | [diff] [blame] | 40 | // Aborts a connection. Returns true on success. |
| 41 | bool Abort(sctp_assoc_t snd_assoc_id); |
| 42 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 43 | int fd() { return fd_; } |
| 44 | |
| 45 | // Enables the priority scheduler. This is a SCTP feature which lets us |
| 46 | // configure the priority per stream so that higher priority packets don't get |
| 47 | // backed up behind lower priority packets in the networking queues. |
| 48 | void SetPriorityScheduler(sctp_assoc_t assoc_id); |
| 49 | |
| 50 | // Sets the priority of a specific stream. |
| 51 | void SetStreamPriority(sctp_assoc_t assoc_id, int stream_id, |
| 52 | uint16_t priority); |
| 53 | |
Austin Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 54 | void SetMaxSize(size_t max_size) { |
| 55 | max_size_ = max_size; |
| 56 | // Have the kernel give us a factor of 10 more. This lets us have more than |
| 57 | // one full sized packet in flight. |
| 58 | max_size = max_size * 10; |
Austin Schuh | 2fe4b71 | 2020-03-15 14:21:45 -0700 | [diff] [blame] | 59 | |
| 60 | CHECK_GE(ReadRMemMax(), max_size); |
| 61 | CHECK_GE(ReadWMemMax(), max_size); |
Austin Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 62 | PCHECK(setsockopt(fd_, SOL_SOCKET, SO_RCVBUF, &max_size, |
| 63 | sizeof(max_size)) == 0); |
Austin Schuh | 2fe4b71 | 2020-03-15 14:21:45 -0700 | [diff] [blame] | 64 | PCHECK(setsockopt(fd_, SOL_SOCKET, SO_SNDBUF, &max_size, |
| 65 | sizeof(max_size)) == 0); |
Austin Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 68 | private: |
| 69 | struct sockaddr_storage sockaddr_local_; |
| 70 | int fd_; |
| 71 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 72 | size_t max_size_ = 1000; |
| 73 | |
| 74 | int ppid_ = 1; |
| 75 | }; |
| 76 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 77 | } // namespace message_bridge |
| 78 | } // namespace aos |
| 79 | |
| 80 | #endif // AOS_NETWORK_SCTP_SERVER_H_ |