diff options
author | Eliot Horowitz <eliot@10gen.com> | 2011-01-04 00:40:41 -0500 |
---|---|---|
committer | Eliot Horowitz <eliot@10gen.com> | 2011-01-04 00:40:41 -0500 |
commit | b828d21630d8715fff5a30c682a51ab79880093d (patch) | |
tree | cbbc46069dcfc08ab1525ec06a5dff5967dde148 /util/sock.h | |
parent | 4315a900ae604e11f2d9d68d1e6f87b8aa01dddc (diff) | |
download | mongo-b828d21630d8715fff5a30c682a51ab79880093d.tar.gz |
ran astyle SERVER-2304
Diffstat (limited to 'util/sock.h')
-rw-r--r-- | util/sock.h | 84 |
1 files changed, 42 insertions, 42 deletions
diff --git a/util/sock.h b/util/sock.h index 7f63966102c..98f307375b8 100644 --- a/util/sock.h +++ b/util/sock.h @@ -106,7 +106,7 @@ namespace mongo { #endif - inline string makeUnixSockPath(int port){ + inline string makeUnixSockPath(int port) { return "/tmp/mongodb-" + BSONObjBuilder::numStr(port) + ".sock"; } @@ -143,7 +143,7 @@ namespace mongo { template <typename T> const T& as() const { return *(const T*)(&sa); } - string toString(bool includePort=true) const{ + string toString(bool includePort=true) const { string out = getAddr(); if (includePort && getType() != AF_UNIX && getType() != AF_UNSPEC) out += ':' + BSONObjBuilder::numStr(getPort()); @@ -156,34 +156,34 @@ namespace mongo { } unsigned getPort() const { - switch (getType()){ - case AF_INET: return ntohs(as<sockaddr_in>().sin_port); - case AF_INET6: return ntohs(as<sockaddr_in6>().sin6_port); - case AF_UNIX: return 0; - case AF_UNSPEC: return 0; - default: massert(SOCK_FAMILY_UNKNOWN_ERROR, "unsupported address family", false); return 0; + switch (getType()) { + case AF_INET: return ntohs(as<sockaddr_in>().sin_port); + case AF_INET6: return ntohs(as<sockaddr_in6>().sin6_port); + case AF_UNIX: return 0; + case AF_UNSPEC: return 0; + default: massert(SOCK_FAMILY_UNKNOWN_ERROR, "unsupported address family", false); return 0; } } string getAddr() const { - switch (getType()){ - case AF_INET: - case AF_INET6: { - const int buflen=128; - char buffer[buflen]; - int ret = getnameinfo(raw(), addressSize, buffer, buflen, NULL, 0, NI_NUMERICHOST); - massert(13082, getAddrInfoStrError(ret), ret == 0); - return buffer; - } - - case AF_UNIX: return (addressSize > 2 ? as<sockaddr_un>().sun_path : "anonymous unix socket"); - case AF_UNSPEC: return "(NONE)"; - default: massert(SOCK_FAMILY_UNKNOWN_ERROR, "unsupported address family", false); return ""; + switch (getType()) { + case AF_INET: + case AF_INET6: { + const int buflen=128; + char buffer[buflen]; + int ret = getnameinfo(raw(), addressSize, buffer, buflen, NULL, 0, NI_NUMERICHOST); + massert(13082, getAddrInfoStrError(ret), ret == 0); + return buffer; + } + + case AF_UNIX: return (addressSize > 2 ? as<sockaddr_un>().sun_path : "anonymous unix socket"); + case AF_UNSPEC: return "(NONE)"; + default: massert(SOCK_FAMILY_UNKNOWN_ERROR, "unsupported address family", false); return ""; } } bool isLocalHost() const; - + bool operator==(const SockAddr& r) const { if (getType() != r.getType()) return false; @@ -191,12 +191,12 @@ namespace mongo { if (getPort() != r.getPort()) return false; - switch (getType()){ - case AF_INET: return as<sockaddr_in>().sin_addr.s_addr == r.as<sockaddr_in>().sin_addr.s_addr; - case AF_INET6: return memcmp(as<sockaddr_in6>().sin6_addr.s6_addr, r.as<sockaddr_in6>().sin6_addr.s6_addr, sizeof(in6_addr)) == 0; - case AF_UNIX: return strcmp(as<sockaddr_un>().sun_path, r.as<sockaddr_un>().sun_path) == 0; - case AF_UNSPEC: return true; // assume all unspecified addresses are the same - default: massert(SOCK_FAMILY_UNKNOWN_ERROR, "unsupported address family", false); + switch (getType()) { + case AF_INET: return as<sockaddr_in>().sin_addr.s_addr == r.as<sockaddr_in>().sin_addr.s_addr; + case AF_INET6: return memcmp(as<sockaddr_in6>().sin6_addr.s6_addr, r.as<sockaddr_in6>().sin6_addr.s6_addr, sizeof(in6_addr)) == 0; + case AF_UNIX: return strcmp(as<sockaddr_un>().sun_path, r.as<sockaddr_un>().sun_path) == 0; + case AF_UNSPEC: return true; // assume all unspecified addresses are the same + default: massert(SOCK_FAMILY_UNKNOWN_ERROR, "unsupported address family", false); } } bool operator!=(const SockAddr& r) const { @@ -213,12 +213,12 @@ namespace mongo { else if (getPort() > r.getPort()) return false; - switch (getType()){ - case AF_INET: return as<sockaddr_in>().sin_addr.s_addr < r.as<sockaddr_in>().sin_addr.s_addr; - case AF_INET6: return memcmp(as<sockaddr_in6>().sin6_addr.s6_addr, r.as<sockaddr_in6>().sin6_addr.s6_addr, sizeof(in6_addr)) < 0; - case AF_UNIX: return strcmp(as<sockaddr_un>().sun_path, r.as<sockaddr_un>().sun_path) < 0; - case AF_UNSPEC: return false; - default: massert(SOCK_FAMILY_UNKNOWN_ERROR, "unsupported address family", false); + switch (getType()) { + case AF_INET: return as<sockaddr_in>().sin_addr.s_addr < r.as<sockaddr_in>().sin_addr.s_addr; + case AF_INET6: return memcmp(as<sockaddr_in6>().sin6_addr.s6_addr, r.as<sockaddr_in6>().sin6_addr.s6_addr, sizeof(in6_addr)) < 0; + case AF_UNIX: return strcmp(as<sockaddr_un>().sun_path, r.as<sockaddr_un>().sun_path) < 0; + case AF_UNSPEC: return false; + default: massert(SOCK_FAMILY_UNKNOWN_ERROR, "unsupported address family", false); } } @@ -226,7 +226,7 @@ namespace mongo { sockaddr* raw() {return (sockaddr*)&sa;} socklen_t addressSize; - private: + private: struct sockaddr_storage sa; }; @@ -248,24 +248,24 @@ namespace mongo { class ListeningSockets { public: - ListeningSockets() + ListeningSockets() : _mutex("ListeningSockets") , _sockets( new set<int>() ) , _socketPaths( new set<string>() ) { } - void add( int sock ){ + void add( int sock ) { scoped_lock lk( _mutex ); _sockets->insert( sock ); } - void addPath( string path ){ + void addPath( string path ) { scoped_lock lk( _mutex ); _socketPaths->insert( path ); } - void remove( int sock ){ + void remove( int sock ) { scoped_lock lk( _mutex ); _sockets->erase( sock ); } - void closeAll(){ + void closeAll() { set<int>* sockets; set<string>* paths; @@ -281,13 +281,13 @@ namespace mongo { int sock = *i; log() << "closing listening socket: " << sock << endl; closesocket( sock ); - } + } for ( set<string>::iterator i=paths->begin(); i!=paths->end(); i++ ) { string path = *i; log() << "removing socket file: " << path << endl; ::remove( path.c_str() ); - } + } } static ListeningSockets* get(); private: |