blob: c6b6324d919a1149e4243453bb2fd5a688653fce [file] [log] [blame]
Austin Schuhe84c3ed2019-12-14 15:29:48 -08001#ifndef AOS_NETWORK_SCTP_CLIENT_H_
2#define AOS_NETWORK_SCTP_CLIENT_H_
3
Tyler Chatowbf0609c2021-07-31 16:13:27 -07004#include <cstdio>
5#include <cstdlib>
Austin Schuhe84c3ed2019-12-14 15:29:48 -08006#include <string_view>
7
Philipp Schrader790cb542023-07-05 21:06:52 -07008#include "glog/logging.h"
9
Austin Schuhe84c3ed2019-12-14 15:29:48 -080010#include "aos/network/sctp_lib.h"
11#include "aos/unique_malloc_ptr.h"
Austin Schuhe84c3ed2019-12-14 15:29:48 -080012
13namespace aos {
14namespace message_bridge {
15
16// Class to encapsulate everything needed to be a SCTP client.
17class SctpClient {
18 public:
19 SctpClient(std::string_view remote_host, int remote_port, int streams,
Adam Snaider96a0f4b2023-05-18 20:41:19 -070020 std::string_view local_host = "0.0.0.0", int local_port = 9971,
21 std::vector<uint8_t> sctp_auth_key = {});
Austin Schuhe84c3ed2019-12-14 15:29:48 -080022
Austin Schuh507f7582021-07-31 20:39:55 -070023 ~SctpClient() {}
Austin Schuhe84c3ed2019-12-14 15:29:48 -080024
25 // Receives the next packet from the remote.
Austin Schuh507f7582021-07-31 20:39:55 -070026 aos::unique_c_ptr<Message> Read() { return sctp_.ReadMessage(); }
Austin Schuhe84c3ed2019-12-14 15:29:48 -080027
28 // Sends a block of data on a stream with a TTL.
Austin Schuh507f7582021-07-31 20:39:55 -070029 // TODO(austin): time_to_live should be a chrono::duration
30 bool Send(int stream, std::string_view data, int time_to_live) {
Sarah Newman80e955e2022-04-13 11:19:36 -070031 return sctp_.SendMessage(stream, data, time_to_live, sockaddr_remote_,
32 sac_assoc_id_);
Austin Schuh507f7582021-07-31 20:39:55 -070033 }
Austin Schuhe84c3ed2019-12-14 15:29:48 -080034
Sarah Newman80e955e2022-04-13 11:19:36 -070035 // Aborts a connection. Returns true on success.
36 bool Abort() { return sctp_.Abort(sac_assoc_id_); }
37
Austin Schuh507f7582021-07-31 20:39:55 -070038 int fd() { return sctp_.fd(); }
Austin Schuhe84c3ed2019-12-14 15:29:48 -080039
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 // Remote to send to.
46 struct sockaddr_storage sockaddr_remote() const {
47 return sockaddr_remote_;
48 }
49
50 void LogSctpStatus(sctp_assoc_t assoc_id);
51
Austin Schuh89e1e9c2023-05-15 14:38:44 -070052 void SetMaxReadSize(size_t max_size) { sctp_.SetMaxReadSize(max_size); }
53 void SetMaxWriteSize(size_t max_size) { sctp_.SetMaxWriteSize(max_size); }
Austin Schuh4f558e52023-05-08 21:51:31 -070054 void SetPoolSize(size_t pool_size) { sctp_.SetPoolSize(pool_size); }
Austin Schuh7bc59052020-02-16 23:48:33 -080055
Sarah Newman80e955e2022-04-13 11:19:36 -070056 void SetAssociationId(sctp_assoc_t sac_assoc_id) {
57 sac_assoc_id_ = sac_assoc_id;
58 }
59
Austin Schuh4f558e52023-05-08 21:51:31 -070060 void FreeMessage(aos::unique_c_ptr<Message> &&message) {
61 sctp_.FreeMessage(std::move(message));
62 }
63
Austin Schuhe84c3ed2019-12-14 15:29:48 -080064 private:
65 struct sockaddr_storage sockaddr_remote_;
66 struct sockaddr_storage sockaddr_local_;
Austin Schuh507f7582021-07-31 20:39:55 -070067 SctpReadWrite sctp_;
Sarah Newman80e955e2022-04-13 11:19:36 -070068
69 // Valid if != 0.
70 sctp_assoc_t sac_assoc_id_ = 0;
Austin Schuhe84c3ed2019-12-14 15:29:48 -080071};
72
73} // namespace message_bridge
74} // namespace aos
75
76#endif // AOS_NETWORK_SCTP_CLIENT_H_