Back to home page

EIC code displayed by LXR

 
 

    


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

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 <sys/socket.h>
0012 #include <unistd.h>
0013 #include <mutex>
0014 
0015 #include "gloo/transport/address.h"
0016 
0017 namespace gloo {
0018 namespace transport {
0019 namespace tcp {
0020 
0021 using sequence_number_t = ssize_t;
0022 
0023 class Address : public ::gloo::transport::Address {
0024  public:
0025   static constexpr sequence_number_t kSequenceNumberUnset = -1;
0026 
0027   Address() {}
0028 
0029   explicit Address(struct sockaddr_storage ss, sequence_number_t seq = -1);
0030 
0031   explicit Address(const struct sockaddr* addr, size_t addrlen);
0032 
0033   explicit Address(const std::vector<char>&);
0034 
0035   Address(const Address& other);
0036 
0037   Address& operator=(Address&& other);
0038   Address& operator=(const Address& other);
0039 
0040   virtual std::vector<char> bytes() const override;
0041 
0042   virtual std::string str() const override;
0043 
0044   const struct sockaddr_storage& getSockaddr() const {
0045     return impl_.ss;
0046   }
0047 
0048   sequence_number_t getSeq() const {
0049     return impl_.seq;
0050   }
0051 
0052   static Address fromSockName(int fd);
0053 
0054   static Address fromPeerName(int fd);
0055 
0056  protected:
0057   // Encapsulate fields such that it is trivially copyable. This class
0058   // is not trivially copyable itself.
0059   struct Impl {
0060     // IP address of the listening socket.
0061     struct sockaddr_storage ss;
0062 
0063     // Sequence number of this address.
0064     // If this is equal to -1, the address is assumed to
0065     // represent the listening socket of a device. The sequence number
0066     // must be set before it can be used by a pair.
0067     sequence_number_t seq{kSequenceNumberUnset};
0068   };
0069 
0070   static_assert(std::is_trivially_copyable<Impl>::value, "!");
0071   static_assert(sizeof(Impl) <= kMaxByteSize, "!");
0072 
0073   Impl impl_;
0074   mutable std::mutex m_;
0075 };
0076 
0077 } // namespace tcp
0078 } // namespace transport
0079 } // namespace gloo