blob: 2831a681f64c32299b29beeafcd4b9115f4d3dc1 [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>
Adam Snaiderbe263512023-05-18 20:40:23 -07005#include <linux/sctp.h>
Austin Schuhe84c3ed2019-12-14 15:29:48 -08006#include <net/if.h>
7#include <netdb.h>
8#include <netinet/in.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
Philipp Schrader790cb542023-07-05 21:06:52 -070016#include "glog/logging.h"
17
Austin Schuhe84c3ed2019-12-14 15:29:48 -080018#include "aos/network/sctp_lib.h"
19#include "aos/unique_malloc_ptr.h"
Austin Schuhe84c3ed2019-12-14 15:29:48 -080020
21namespace aos {
22namespace message_bridge {
23
24class SctpServer {
25 public:
Brian Silverman833dfb62021-11-03 10:47:55 -070026 SctpServer(int streams, std::string_view local_host = "0.0.0.0",
27 int local_port = 9971);
Austin Schuhe84c3ed2019-12-14 15:29:48 -080028
Austin Schuh507f7582021-07-31 20:39:55 -070029 ~SctpServer() {}
Austin Schuhe84c3ed2019-12-14 15:29:48 -080030
31 // Receives the next packet from the remote.
Austin Schuh507f7582021-07-31 20:39:55 -070032 aos::unique_c_ptr<Message> Read() { return sctp_.ReadMessage(); }
Austin Schuhe84c3ed2019-12-14 15:29:48 -080033
Austin Schuhf95a6ab2023-05-15 14:34:57 -070034 // Frees the message returned by Read();
35 void FreeMessage(aos::unique_c_ptr<Message> &&message) {
36 sctp_.FreeMessage(std::move(message));
37 }
38
Austin Schuh83afb7a2020-03-15 23:09:22 -070039 // Sends a block of data to a client on a stream with a TTL. Returns true on
40 // success.
41 bool Send(std::string_view data, sctp_assoc_t snd_assoc_id, int stream,
Austin Schuh507f7582021-07-31 20:39:55 -070042 int time_to_live) {
43 return sctp_.SendMessage(stream, data, time_to_live, std::nullopt,
44 snd_assoc_id);
45 }
Austin Schuhe84c3ed2019-12-14 15:29:48 -080046
Austin Schuh4889b182020-11-18 19:11:56 -080047 // Aborts a connection. Returns true on success.
Sarah Newman80e955e2022-04-13 11:19:36 -070048 bool Abort(sctp_assoc_t snd_assoc_id) { return sctp_.Abort(snd_assoc_id); }
Austin Schuh4889b182020-11-18 19:11:56 -080049
Austin Schuh507f7582021-07-31 20:39:55 -070050 int fd() { return sctp_.fd(); }
Austin Schuhe84c3ed2019-12-14 15:29:48 -080051
52 // Enables the priority scheduler. This is a SCTP feature which lets us
53 // configure the priority per stream so that higher priority packets don't get
54 // backed up behind lower priority packets in the networking queues.
55 void SetPriorityScheduler(sctp_assoc_t assoc_id);
56
57 // Sets the priority of a specific stream.
58 void SetStreamPriority(sctp_assoc_t assoc_id, int stream_id,
59 uint16_t priority);
60
Austin Schuh89e1e9c2023-05-15 14:38:44 -070061 void SetMaxReadSize(size_t max_size) { sctp_.SetMaxReadSize(max_size); }
62 void SetMaxWriteSize(size_t max_size) { sctp_.SetMaxWriteSize(max_size); }
Austin Schuh7bc59052020-02-16 23:48:33 -080063
Austin Schuhf95a6ab2023-05-15 14:34:57 -070064 void SetPoolSize(size_t pool_size) { sctp_.SetPoolSize(pool_size); }
65
Austin Schuhe84c3ed2019-12-14 15:29:48 -080066 private:
67 struct sockaddr_storage sockaddr_local_;
Austin Schuh507f7582021-07-31 20:39:55 -070068 SctpReadWrite sctp_;
Austin Schuhe84c3ed2019-12-14 15:29:48 -080069};
70
Austin Schuhe84c3ed2019-12-14 15:29:48 -080071} // namespace message_bridge
72} // namespace aos
73
74#endif // AOS_NETWORK_SCTP_SERVER_H_