blob: 25d81fc5df7bad82346f6377b2098f0c5c4c032e [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
34 // Sends a block of data to a client on a stream with a TTL.
35 void Send(std::string_view data, sctp_assoc_t snd_assoc_id, int stream,
36 int timetolive);
37
38 int fd() { return fd_; }
39
40 // Enables the priority scheduler. This is a SCTP feature which lets us
41 // configure the priority per stream so that higher priority packets don't get
42 // backed up behind lower priority packets in the networking queues.
43 void SetPriorityScheduler(sctp_assoc_t assoc_id);
44
45 // Sets the priority of a specific stream.
46 void SetStreamPriority(sctp_assoc_t assoc_id, int stream_id,
47 uint16_t priority);
48
Austin Schuh7bc59052020-02-16 23:48:33 -080049 void SetMaxSize(size_t max_size) {
50 max_size_ = max_size;
51 // Have the kernel give us a factor of 10 more. This lets us have more than
52 // one full sized packet in flight.
53 max_size = max_size * 10;
Austin Schuh2fe4b712020-03-15 14:21:45 -070054
55 CHECK_GE(ReadRMemMax(), max_size);
56 CHECK_GE(ReadWMemMax(), max_size);
Austin Schuh7bc59052020-02-16 23:48:33 -080057 PCHECK(setsockopt(fd_, SOL_SOCKET, SO_RCVBUF, &max_size,
58 sizeof(max_size)) == 0);
Austin Schuh2fe4b712020-03-15 14:21:45 -070059 PCHECK(setsockopt(fd_, SOL_SOCKET, SO_SNDBUF, &max_size,
60 sizeof(max_size)) == 0);
Austin Schuh7bc59052020-02-16 23:48:33 -080061 }
62
Austin Schuhe84c3ed2019-12-14 15:29:48 -080063 private:
64 struct sockaddr_storage sockaddr_local_;
65 int fd_;
66
Austin Schuhe84c3ed2019-12-14 15:29:48 -080067 size_t max_size_ = 1000;
68
69 int ppid_ = 1;
70};
71
72
73} // namespace message_bridge
74} // namespace aos
75
76#endif // AOS_NETWORK_SCTP_SERVER_H_