blob: 1ba32ff79d6de825864fcea16313c016c6ec60e5 [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 Schuh507f7582021-07-31 20:39:55 -070050 void SetMaxSize(size_t max_size) { sctp_.SetMaxSize(max_size); }
Austin Schuh7bc59052020-02-16 23:48:33 -080051
Sarah Newman80e955e2022-04-13 11:19:36 -070052 void SetAssociationId(sctp_assoc_t sac_assoc_id) {
53 sac_assoc_id_ = sac_assoc_id;
54 }
55
Austin Schuhe84c3ed2019-12-14 15:29:48 -080056 private:
57 struct sockaddr_storage sockaddr_remote_;
58 struct sockaddr_storage sockaddr_local_;
Austin Schuh507f7582021-07-31 20:39:55 -070059 SctpReadWrite sctp_;
Sarah Newman80e955e2022-04-13 11:19:36 -070060
61 // Valid if != 0.
62 sctp_assoc_t sac_assoc_id_ = 0;
Austin Schuhe84c3ed2019-12-14 15:29:48 -080063};
64
65} // namespace message_bridge
66} // namespace aos
67
68#endif // AOS_NETWORK_SCTP_CLIENT_H_