blob: d7a2b43da42002c2fd09d56a1aa7cf983603305e [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
8#include "aos/network/sctp_lib.h"
9#include "aos/unique_malloc_ptr.h"
10#include "glog/logging.h"
11
12namespace aos {
13namespace message_bridge {
14
15// Class to encapsulate everything needed to be a SCTP client.
16class SctpClient {
17 public:
18 SctpClient(std::string_view remote_host, int remote_port, int streams,
19 std::string_view local_host = "0.0.0.0", int local_port = 9971);
20
Austin Schuh507f7582021-07-31 20:39:55 -070021 ~SctpClient() {}
Austin Schuhe84c3ed2019-12-14 15:29:48 -080022
23 // Receives the next packet from the remote.
Austin Schuh507f7582021-07-31 20:39:55 -070024 aos::unique_c_ptr<Message> Read() { return sctp_.ReadMessage(); }
Austin Schuhe84c3ed2019-12-14 15:29:48 -080025
26 // Sends a block of data on a stream with a TTL.
Austin Schuh507f7582021-07-31 20:39:55 -070027 // TODO(austin): time_to_live should be a chrono::duration
28 bool Send(int stream, std::string_view data, int time_to_live) {
Sarah Newman80e955e2022-04-13 11:19:36 -070029 return sctp_.SendMessage(stream, data, time_to_live, sockaddr_remote_,
30 sac_assoc_id_);
Austin Schuh507f7582021-07-31 20:39:55 -070031 }
Austin Schuhe84c3ed2019-12-14 15:29:48 -080032
Sarah Newman80e955e2022-04-13 11:19:36 -070033 // Aborts a connection. Returns true on success.
34 bool Abort() { return sctp_.Abort(sac_assoc_id_); }
35
Austin Schuh507f7582021-07-31 20:39:55 -070036 int fd() { return sctp_.fd(); }
Austin Schuhe84c3ed2019-12-14 15:29:48 -080037
38 // Enables the priority scheduler. This is a SCTP feature which lets us
39 // configure the priority per stream so that higher priority packets don't get
40 // backed up behind lower priority packets in the networking queues.
41 void SetPriorityScheduler(sctp_assoc_t assoc_id);
42
43 // Remote to send to.
44 struct sockaddr_storage sockaddr_remote() const {
45 return sockaddr_remote_;
46 }
47
48 void LogSctpStatus(sctp_assoc_t assoc_id);
49
Austin Schuh89e1e9c2023-05-15 14:38:44 -070050 void SetMaxReadSize(size_t max_size) { sctp_.SetMaxReadSize(max_size); }
51 void SetMaxWriteSize(size_t max_size) { sctp_.SetMaxWriteSize(max_size); }
Austin Schuh4f558e52023-05-08 21:51:31 -070052 void SetPoolSize(size_t pool_size) { sctp_.SetPoolSize(pool_size); }
Austin Schuh7bc59052020-02-16 23:48:33 -080053
Sarah Newman80e955e2022-04-13 11:19:36 -070054 void SetAssociationId(sctp_assoc_t sac_assoc_id) {
55 sac_assoc_id_ = sac_assoc_id;
56 }
57
Austin Schuh4f558e52023-05-08 21:51:31 -070058 void FreeMessage(aos::unique_c_ptr<Message> &&message) {
59 sctp_.FreeMessage(std::move(message));
60 }
61
Austin Schuhe84c3ed2019-12-14 15:29:48 -080062 private:
63 struct sockaddr_storage sockaddr_remote_;
64 struct sockaddr_storage sockaddr_local_;
Austin Schuh507f7582021-07-31 20:39:55 -070065 SctpReadWrite sctp_;
Sarah Newman80e955e2022-04-13 11:19:36 -070066
67 // Valid if != 0.
68 sctp_assoc_t sac_assoc_id_ = 0;
Austin Schuhe84c3ed2019-12-14 15:29:48 -080069};
70
71} // namespace message_bridge
72} // namespace aos
73
74#endif // AOS_NETWORK_SCTP_CLIENT_H_