Deprecate dynamic::dynamic(std::initializer_list<dynamic>)
[folly.git] / folly / SocketAddress.h
index a17a46238b1790a9772b5a5a2b4c6ab53ee6e37d..4ff1fd18f6b9511192352ca9184683446b452feb 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2015 Facebook, Inc.
+ * Copyright 2016 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -32,7 +32,7 @@ namespace folly {
 
 class SocketAddress {
  public:
-  SocketAddress() {}
+  SocketAddress() = default;
 
   /**
    * Construct a SocketAddress from a hostname and port.
@@ -73,6 +73,10 @@ class SocketAddress {
     }
   }
 
+  SocketAddress(const IPAddress& ipAddr, uint16_t port) {
+    setFromIpAddrPort(ipAddr, port);
+  }
+
   SocketAddress(const SocketAddress& addr) {
     port_ = addr.port_;
     if (addr.getFamily() == AF_UNIX) {
@@ -190,6 +194,14 @@ class SocketAddress {
     setFromIpPort(ip.c_str(), port);
   }
 
+  /**
+   * Initialize this SocketAddress from an IPAddress struct and port.
+   *
+   * @param ip The IP address in IPAddress format
+   * @param port The port (in host byte order)
+   */
+  void setFromIpAddrPort(const IPAddress& ip, uint16_t port);
+
   /**
    * Initialize this SocketAddress from a local port number.
    *
@@ -276,22 +288,25 @@ class SocketAddress {
 
   void setFromPath(const char* path, size_t length);
 
+  // a typedef that allow us to compile against both winsock & POSIX sockets:
+  using SocketDesc = decltype(socket(0,0,0)); // POSIX: int, winsock: unsigned
+
   /**
    * Initialize this SocketAddress from a socket's peer address.
    *
    * Raises std::system_error on error.
    */
-  void setFromPeerAddress(int socket);
+  void setFromPeerAddress(SocketDesc socket);
 
   /**
    * Initialize this SocketAddress from a socket's local address.
    *
    * Raises std::system_error on error.
    */
-  void setFromLocalAddress(int socket);
+  void setFromLocalAddress(SocketDesc socket);
 
   /**
-   * Initialize this TSocketAddress from a struct sockaddr.
+   * Initialize this folly::SocketAddress from a struct sockaddr.
    *
    * Raises std::system_error on error.
    *
@@ -544,11 +559,19 @@ class SocketAddress {
     }
   };
 
+  // a typedef that allow us to compile against both winsock & POSIX sockets:
+  // (both arg types and calling conventions differ for both)
+  // POSIX: void setFromSocket(int socket,
+  //                  int(*fn)(int, struct sockaddr*, socklen_t*));
+  // mingw: void setFromSocket(unsigned socket,
+  //                  int(*fn)(unsigned, struct sockaddr*, socklen_t*));
+  using GetPeerNameFunc = decltype(getpeername);
+
   struct addrinfo* getAddrInfo(const char* host, uint16_t port, int flags);
   struct addrinfo* getAddrInfo(const char* host, const char* port, int flags);
   void setFromAddrInfo(const struct addrinfo* results);
   void setFromLocalAddr(const struct addrinfo* results);
-  void setFromSocket(int socket, int (*fn)(int, struct sockaddr*, socklen_t*));
+  void setFromSocket(SocketDesc socket, GetPeerNameFunc fn);
   std::string getIpString(int flags) const;
   void getIpString(char *buf, size_t buflen, int flags) const;