blob: 9356612646fefd1567551fcde75f05700f9d9e27 [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) {
29 return sctp_.SendMessage(stream, data, time_to_live, sockaddr_remote_, 0);
30 }
Austin Schuhe84c3ed2019-12-14 15:29:48 -080031
Austin Schuh507f7582021-07-31 20:39:55 -070032 int fd() { return sctp_.fd(); }
Austin Schuhe84c3ed2019-12-14 15:29:48 -080033
34 // Enables the priority scheduler. This is a SCTP feature which lets us
35 // configure the priority per stream so that higher priority packets don't get
36 // backed up behind lower priority packets in the networking queues.
37 void SetPriorityScheduler(sctp_assoc_t assoc_id);
38
39 // Remote to send to.
40 struct sockaddr_storage sockaddr_remote() const {
41 return sockaddr_remote_;
42 }
43
44 void LogSctpStatus(sctp_assoc_t assoc_id);
45
Austin Schuh507f7582021-07-31 20:39:55 -070046 void SetMaxSize(size_t max_size) { sctp_.SetMaxSize(max_size); }
Austin Schuh7bc59052020-02-16 23:48:33 -080047
Austin Schuhe84c3ed2019-12-14 15:29:48 -080048 private:
49 struct sockaddr_storage sockaddr_remote_;
50 struct sockaddr_storage sockaddr_local_;
Austin Schuh507f7582021-07-31 20:39:55 -070051 SctpReadWrite sctp_;
Austin Schuhe84c3ed2019-12-14 15:29:48 -080052};
53
54} // namespace message_bridge
55} // namespace aos
56
57#endif // AOS_NETWORK_SCTP_CLIENT_H_