blob: 1f7e2eeed1afdc753af3b7f19d29ffdaad585d28 [file] [log] [blame]
Austin Schuhe84c3ed2019-12-14 15:29:48 -08001#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 Schuhe84c3ed2019-12-14 15:29:48 -08009#include <sys/socket.h>
10
Tyler Chatowbf0609c2021-07-31 16:13:27 -070011#include <cstdio>
12#include <cstdlib>
13#include <cstring>
14#include <memory>
15
Austin Schuhe84c3ed2019-12-14 15:29:48 -080016#include "aos/network/sctp_lib.h"
17#include "aos/unique_malloc_ptr.h"
18#include "glog/logging.h"
19
20namespace aos {
21namespace message_bridge {
22
23class SctpServer {
24 public:
Brian Silverman833dfb62021-11-03 10:47:55 -070025 SctpServer(int streams, std::string_view local_host = "0.0.0.0",
26 int local_port = 9971);
Austin Schuhe84c3ed2019-12-14 15:29:48 -080027
Austin Schuh507f7582021-07-31 20:39:55 -070028 ~SctpServer() {}
Austin Schuhe84c3ed2019-12-14 15:29:48 -080029
30 // Receives the next packet from the remote.
Austin Schuh507f7582021-07-31 20:39:55 -070031 aos::unique_c_ptr<Message> Read() { return sctp_.ReadMessage(); }
Austin Schuhe84c3ed2019-12-14 15:29:48 -080032
Austin Schuhf95a6ab2023-05-15 14:34:57 -070033 // Frees the message returned by Read();
34 void FreeMessage(aos::unique_c_ptr<Message> &&message) {
35 sctp_.FreeMessage(std::move(message));
36 }
37
Austin Schuh83afb7a2020-03-15 23:09:22 -070038 // Sends a block of data to a client on a stream with a TTL. Returns true on
39 // success.
40 bool Send(std::string_view data, sctp_assoc_t snd_assoc_id, int stream,
Austin Schuh507f7582021-07-31 20:39:55 -070041 int time_to_live) {
42 return sctp_.SendMessage(stream, data, time_to_live, std::nullopt,
43 snd_assoc_id);
44 }
Austin Schuhe84c3ed2019-12-14 15:29:48 -080045
Austin Schuh4889b182020-11-18 19:11:56 -080046 // Aborts a connection. Returns true on success.
Sarah Newman80e955e2022-04-13 11:19:36 -070047 bool Abort(sctp_assoc_t snd_assoc_id) { return sctp_.Abort(snd_assoc_id); }
Austin Schuh4889b182020-11-18 19:11:56 -080048
Austin Schuh507f7582021-07-31 20:39:55 -070049 int fd() { return sctp_.fd(); }
Austin Schuhe84c3ed2019-12-14 15:29:48 -080050
51 // Enables the priority scheduler. This is a SCTP feature which lets us
52 // configure the priority per stream so that higher priority packets don't get
53 // backed up behind lower priority packets in the networking queues.
54 void SetPriorityScheduler(sctp_assoc_t assoc_id);
55
56 // Sets the priority of a specific stream.
57 void SetStreamPriority(sctp_assoc_t assoc_id, int stream_id,
58 uint16_t priority);
59
Austin Schuh507f7582021-07-31 20:39:55 -070060 void SetMaxSize(size_t max_size) { sctp_.SetMaxSize(max_size); }
Austin Schuh7bc59052020-02-16 23:48:33 -080061
Austin Schuhf95a6ab2023-05-15 14:34:57 -070062 void SetPoolSize(size_t pool_size) { sctp_.SetPoolSize(pool_size); }
63
Austin Schuhe84c3ed2019-12-14 15:29:48 -080064 private:
65 struct sockaddr_storage sockaddr_local_;
Austin Schuh507f7582021-07-31 20:39:55 -070066 SctpReadWrite sctp_;
Austin Schuhe84c3ed2019-12-14 15:29:48 -080067};
68
Austin Schuhe84c3ed2019-12-14 15:29:48 -080069} // namespace message_bridge
70} // namespace aos
71
72#endif // AOS_NETWORK_SCTP_SERVER_H_