blob: bc3d1f6cb07f53062ccf0680af737e10850f4466 [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
21 ~SctpClient() {
22 LOG(INFO) << "close(" << fd_ << ")";
23 PCHECK(close(fd_) == 0);
24 }
25
26 // Receives the next packet from the remote.
27 aos::unique_c_ptr<Message> Read();
28
29 // Sends a block of data on a stream with a TTL.
30 bool Send(int stream, std::string_view data, int time_to_live);
31
32 int fd() { return fd_; }
33
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 Schuh2fe4b712020-03-15 14:21:45 -070046 void SetMaxSize(size_t max_size) {
47 max_size_ = max_size;
48 // Have the kernel give us a factor of 10 more. This lets us have more than
49 // one full sized packet in flight.
50 max_size = max_size * 10;
51
James Kuszmaula5b504e2021-08-01 20:18:41 -070052 CHECK_GE(ReadRMemMax(), max_size)
53 << "rmem_max is too low. To increase rmem_max temporarily, do sysctl "
54 "-w net.core.rmem_max=NEW_SIZE";
55 CHECK_GE(ReadWMemMax(), max_size)
56 << "wmem_max is too low. To increase wmem_max temporarily, do sysctl "
57 "-w net.core.wmem_max=NEW_SIZE";
Austin Schuh2fe4b712020-03-15 14:21:45 -070058 PCHECK(setsockopt(fd_, SOL_SOCKET, SO_RCVBUF, &max_size,
59 sizeof(max_size)) == 0);
60 PCHECK(setsockopt(fd_, SOL_SOCKET, SO_SNDBUF, &max_size,
61 sizeof(max_size)) == 0);
62 }
Austin Schuh7bc59052020-02-16 23:48:33 -080063
Austin Schuhe84c3ed2019-12-14 15:29:48 -080064 private:
65 struct sockaddr_storage sockaddr_remote_;
66 struct sockaddr_storage sockaddr_local_;
67 int fd_;
68
69 size_t max_size_ = 1000;
70};
71
72} // namespace message_bridge
73} // namespace aos
74
75#endif // AOS_NETWORK_SCTP_CLIENT_H_