blob: 5affecc284f91011108d6f448a2126d0709196c3 [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,
20 std::string_view local_host = "0.0.0.0", int local_port = 9971);
21
Austin Schuh507f7582021-07-31 20:39:55 -070022 ~SctpClient() {}
Austin Schuhe84c3ed2019-12-14 15:29:48 -080023
24 // Receives the next packet from the remote.
Austin Schuh507f7582021-07-31 20:39:55 -070025 aos::unique_c_ptr<Message> Read() { return sctp_.ReadMessage(); }
Austin Schuhe84c3ed2019-12-14 15:29:48 -080026
27 // Sends a block of data on a stream with a TTL.
Austin Schuh507f7582021-07-31 20:39:55 -070028 // TODO(austin): time_to_live should be a chrono::duration
29 bool Send(int stream, std::string_view data, int time_to_live) {
Sarah Newman80e955e2022-04-13 11:19:36 -070030 return sctp_.SendMessage(stream, data, time_to_live, sockaddr_remote_,
31 sac_assoc_id_);
Austin Schuh507f7582021-07-31 20:39:55 -070032 }
Austin Schuhe84c3ed2019-12-14 15:29:48 -080033
Sarah Newman80e955e2022-04-13 11:19:36 -070034 // Aborts a connection. Returns true on success.
35 bool Abort() { return sctp_.Abort(sac_assoc_id_); }
36
Austin Schuh507f7582021-07-31 20:39:55 -070037 int fd() { return sctp_.fd(); }
Austin Schuhe84c3ed2019-12-14 15:29:48 -080038
39 // Enables the priority scheduler. This is a SCTP feature which lets us
40 // configure the priority per stream so that higher priority packets don't get
41 // backed up behind lower priority packets in the networking queues.
42 void SetPriorityScheduler(sctp_assoc_t assoc_id);
43
44 // Remote to send to.
45 struct sockaddr_storage sockaddr_remote() const {
46 return sockaddr_remote_;
47 }
48
49 void LogSctpStatus(sctp_assoc_t assoc_id);
50
Austin Schuh89e1e9c2023-05-15 14:38:44 -070051 void SetMaxReadSize(size_t max_size) { sctp_.SetMaxReadSize(max_size); }
52 void SetMaxWriteSize(size_t max_size) { sctp_.SetMaxWriteSize(max_size); }
Austin Schuh4f558e52023-05-08 21:51:31 -070053 void SetPoolSize(size_t pool_size) { sctp_.SetPoolSize(pool_size); }
Austin Schuh7bc59052020-02-16 23:48:33 -080054
Sarah Newman80e955e2022-04-13 11:19:36 -070055 void SetAssociationId(sctp_assoc_t sac_assoc_id) {
56 sac_assoc_id_ = sac_assoc_id;
57 }
58
Austin Schuh4f558e52023-05-08 21:51:31 -070059 void FreeMessage(aos::unique_c_ptr<Message> &&message) {
60 sctp_.FreeMessage(std::move(message));
61 }
62
Austin Schuhe84c3ed2019-12-14 15:29:48 -080063 private:
64 struct sockaddr_storage sockaddr_remote_;
65 struct sockaddr_storage sockaddr_local_;
Austin Schuh507f7582021-07-31 20:39:55 -070066 SctpReadWrite sctp_;
Sarah Newman80e955e2022-04-13 11:19:36 -070067
68 // Valid if != 0.
69 sctp_assoc_t sac_assoc_id_ = 0;
Austin Schuhe84c3ed2019-12-14 15:29:48 -080070};
71
72} // namespace message_bridge
73} // namespace aos
74
75#endif // AOS_NETWORK_SCTP_CLIENT_H_