blob: 5b6df3beb63bda5f7235f41531e2632c7108ce5b [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 Schuh7bc59052020-02-16 23:48:33 -080046 void set_max_size(size_t max_size) { max_size_ = max_size; }
47
Austin Schuhe84c3ed2019-12-14 15:29:48 -080048 private:
49 struct sockaddr_storage sockaddr_remote_;
50 struct sockaddr_storage sockaddr_local_;
51 int fd_;
52
53 size_t max_size_ = 1000;
54};
55
56} // namespace message_bridge
57} // namespace aos
58
59#endif // AOS_NETWORK_SCTP_CLIENT_H_