blob: 8fa3d15e08141b1031236e98d708ee4414fed31e [file] [log] [blame]
Austin Schuhe84c3ed2019-12-14 15:29:48 -08001#ifndef AOS_NETWORK_SCTP_SERVER_H_
2#define AOS_NETWORK_SCTP_SERVER_H_
3
4#include <arpa/inet.h>
5#include <net/if.h>
6#include <netdb.h>
7#include <netinet/in.h>
8#include <netinet/sctp.h>
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12#include <memory>
13#include <sys/socket.h>
14
15#include "aos/network/sctp_lib.h"
16#include "aos/unique_malloc_ptr.h"
17#include "glog/logging.h"
18
19namespace aos {
20namespace message_bridge {
21
22class SctpServer {
23 public:
24 SctpServer(std::string_view local_host = "0.0.0.0", int local_port = 9971);
25
26 ~SctpServer() {
27 LOG(INFO) << "close(" << fd_ << ")";
28 PCHECK(close(fd_) == 0);
29 }
30
31 // Receives the next packet from the remote.
32 aos::unique_c_ptr<Message> Read();
33
Austin Schuh83afb7a2020-03-15 23:09:22 -070034 // Sends a block of data to a client on a stream with a TTL. Returns true on
35 // success.
36 bool Send(std::string_view data, sctp_assoc_t snd_assoc_id, int stream,
Austin Schuhe84c3ed2019-12-14 15:29:48 -080037 int timetolive);
38
39 int fd() { return fd_; }
40
41 // Enables the priority scheduler. This is a SCTP feature which lets us
42 // configure the priority per stream so that higher priority packets don't get
43 // backed up behind lower priority packets in the networking queues.
44 void SetPriorityScheduler(sctp_assoc_t assoc_id);
45
46 // Sets the priority of a specific stream.
47 void SetStreamPriority(sctp_assoc_t assoc_id, int stream_id,
48 uint16_t priority);
49
Austin Schuh7bc59052020-02-16 23:48:33 -080050 void SetMaxSize(size_t max_size) {
51 max_size_ = max_size;
52 // Have the kernel give us a factor of 10 more. This lets us have more than
53 // one full sized packet in flight.
54 max_size = max_size * 10;
Austin Schuh2fe4b712020-03-15 14:21:45 -070055
56 CHECK_GE(ReadRMemMax(), max_size);
57 CHECK_GE(ReadWMemMax(), max_size);
Austin Schuh7bc59052020-02-16 23:48:33 -080058 PCHECK(setsockopt(fd_, SOL_SOCKET, SO_RCVBUF, &max_size,
59 sizeof(max_size)) == 0);
Austin Schuh2fe4b712020-03-15 14:21:45 -070060 PCHECK(setsockopt(fd_, SOL_SOCKET, SO_SNDBUF, &max_size,
61 sizeof(max_size)) == 0);
Austin Schuh7bc59052020-02-16 23:48:33 -080062 }
63
Austin Schuhe84c3ed2019-12-14 15:29:48 -080064 private:
65 struct sockaddr_storage sockaddr_local_;
66 int fd_;
67
Austin Schuhe84c3ed2019-12-14 15:29:48 -080068 size_t max_size_ = 1000;
69
70 int ppid_ = 1;
71};
72
73
74} // namespace message_bridge
75} // namespace aos
76
77#endif // AOS_NETWORK_SCTP_SERVER_H_