Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:10:13

0001 /**
0002  * Copyright (c) 2017-present, Facebook, Inc.
0003  * All rights reserved.
0004  *
0005  * This source code is licensed under the BSD-style license found in the
0006  * LICENSE file in the root directory of this source tree.
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   // Return underlying file descriptor.
0032   int fd() const {
0033     return fd_;
0034   }
0035 
0036   // Release underlying file descriptor.
0037   int release() {
0038     auto fd = fd_;
0039     fd_ = -1;
0040     return fd;
0041   }
0042 
0043   // Enable or disable SO_REUSEADDR socket option.
0044   void reuseAddr(bool on);
0045 
0046   // Enable or disable TCP_NODELAY socket option.
0047   void noDelay(bool on);
0048 
0049   // Configure if the socket is blocking or not.
0050   void block(bool on);
0051 
0052   // Configure recv timeout.
0053   void recvTimeout(std::chrono::milliseconds timeout);
0054 
0055   // Configure send timeout.
0056   void sendTimeout(std::chrono::milliseconds timeout);
0057 
0058   // Bind socket to address.
0059   void bind(const sockaddr_storage& ss);
0060 
0061   // Bind socket to address.
0062   void bind(const struct sockaddr* addr, socklen_t addrlen);
0063 
0064   // Listen on socket.
0065   void listen(int backlog);
0066 
0067   // Accept new socket connecting to listening socket.
0068   std::shared_ptr<Socket> accept();
0069 
0070   // Connect to address.
0071   void connect(const sockaddr_storage& ss);
0072 
0073   // Connect to address.
0074   void connect(const struct sockaddr* addr, socklen_t addrlen);
0075 
0076   // Proxy to read(2) with EINTR retry.
0077   ssize_t read(void* buf, size_t count);
0078 
0079   // Proxy to write(2) with EINTR retry.
0080   ssize_t write(const void* buf, size_t count);
0081 
0082   // Return address for getsockname(2).
0083   Address sockName() const;
0084 
0085   // Return address for getpeername(2).
0086   Address peerName() const;
0087 
0088  private:
0089   int fd_;
0090 
0091   // Configure send or recv timeout.
0092   void configureTimeout(int opt, std::chrono::milliseconds timeout);
0093 };
0094 
0095 } // namespace tcp
0096 } // namespace transport
0097 } // namespace gloo