File indexing completed on 2025-01-30 10:10:13
0001
0002
0003
0004
0005
0006
0007
0008
0009 #pragma once
0010
0011 #include <netinet/in.h>
0012 #include <sys/socket.h>
0013
0014 #include <chrono>
0015 #include <memory>
0016
0017 #include <gloo/transport/tcp/address.h>
0018
0019 namespace gloo {
0020 namespace transport {
0021 namespace tcp {
0022
0023 class Socket final : public std::enable_shared_from_this<Socket> {
0024 public:
0025 static std::shared_ptr<Socket> createForFamily(sa_family_t ai_family);
0026
0027 explicit Socket(int fd);
0028
0029 ~Socket();
0030
0031
0032 int fd() const {
0033 return fd_;
0034 }
0035
0036
0037 int release() {
0038 auto fd = fd_;
0039 fd_ = -1;
0040 return fd;
0041 }
0042
0043
0044 void reuseAddr(bool on);
0045
0046
0047 void noDelay(bool on);
0048
0049
0050 void block(bool on);
0051
0052
0053 void recvTimeout(std::chrono::milliseconds timeout);
0054
0055
0056 void sendTimeout(std::chrono::milliseconds timeout);
0057
0058
0059 void bind(const sockaddr_storage& ss);
0060
0061
0062 void bind(const struct sockaddr* addr, socklen_t addrlen);
0063
0064
0065 void listen(int backlog);
0066
0067
0068 std::shared_ptr<Socket> accept();
0069
0070
0071 void connect(const sockaddr_storage& ss);
0072
0073
0074 void connect(const struct sockaddr* addr, socklen_t addrlen);
0075
0076
0077 ssize_t read(void* buf, size_t count);
0078
0079
0080 ssize_t write(const void* buf, size_t count);
0081
0082
0083 Address sockName() const;
0084
0085
0086 Address peerName() const;
0087
0088 private:
0089 int fd_;
0090
0091
0092 void configureTimeout(int opt, std::chrono::milliseconds timeout);
0093 };
0094
0095 }
0096 }
0097 }