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> |
| 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <string.h> |
| 12 | #include <memory> |
| 13 | #include <sys/socket.h> |
| 14 | |
| 15 | #include "aos/network/sctp_lib.h" |
| 16 | #include "aos/unique_malloc_ptr.h" |
| 17 | #include "glog/logging.h" |
| 18 | |
| 19 | namespace aos { |
| 20 | namespace message_bridge { |
| 21 | |
| 22 | class SctpServer { |
| 23 | public: |
| 24 | SctpServer(std::string_view local_host = "0.0.0.0", int local_port = 9971); |
| 25 | |
| 26 | ~SctpServer() { |
| 27 | LOG(INFO) << "close(" << fd_ << ")"; |
| 28 | PCHECK(close(fd_) == 0); |
| 29 | } |
| 30 | |
| 31 | // Receives the next packet from the remote. |
| 32 | aos::unique_c_ptr<Message> Read(); |
| 33 | |
| 34 | // Sends a block of data to a client on a stream with a TTL. |
| 35 | void Send(std::string_view data, sctp_assoc_t snd_assoc_id, int stream, |
| 36 | int timetolive); |
| 37 | |
| 38 | int fd() { return fd_; } |
| 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 | // Sets the priority of a specific stream. |
| 46 | void SetStreamPriority(sctp_assoc_t assoc_id, int stream_id, |
| 47 | uint16_t priority); |
| 48 | |
Austin Schuh | 7bc5905 | 2020-02-16 23:48:33 -0800 | [diff] [blame] | 49 | void SetMaxSize(size_t max_size) { |
| 50 | max_size_ = max_size; |
| 51 | // Have the kernel give us a factor of 10 more. This lets us have more than |
| 52 | // one full sized packet in flight. |
| 53 | max_size = max_size * 10; |
| 54 | PCHECK(setsockopt(fd_, SOL_SOCKET, SO_RCVBUF, &max_size, |
| 55 | sizeof(max_size)) == 0); |
| 56 | } |
| 57 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 58 | private: |
| 59 | struct sockaddr_storage sockaddr_local_; |
| 60 | int fd_; |
| 61 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 62 | size_t max_size_ = 1000; |
| 63 | |
| 64 | int ppid_ = 1; |
| 65 | }; |
| 66 | |
| 67 | |
| 68 | } // namespace message_bridge |
| 69 | } // namespace aos |
| 70 | |
| 71 | #endif // AOS_NETWORK_SCTP_SERVER_H_ |