blob: 6b8463812fc8d056bfb487aa57a5eca4e28247ab [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
4#include <stdio.h>
5#include <stdlib.h>
6#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
52 CHECK_GE(ReadRMemMax(), max_size);
53 CHECK_GE(ReadWMemMax(), max_size);
54 PCHECK(setsockopt(fd_, SOL_SOCKET, SO_RCVBUF, &max_size,
55 sizeof(max_size)) == 0);
56 PCHECK(setsockopt(fd_, SOL_SOCKET, SO_SNDBUF, &max_size,
57 sizeof(max_size)) == 0);
58 }
Austin Schuh7bc59052020-02-16 23:48:33 -080059
Austin Schuhe84c3ed2019-12-14 15:29:48 -080060 private:
61 struct sockaddr_storage sockaddr_remote_;
62 struct sockaddr_storage sockaddr_local_;
63 int fd_;
64
65 size_t max_size_ = 1000;
66};
67
68} // namespace message_bridge
69} // namespace aos
70
71#endif // AOS_NETWORK_SCTP_CLIENT_H_