00001 /* --------------------------------------------------------------------------- 00002 commonc++ - A C++ Common Class Library 00003 Copyright (C) 2005-2012 Mark A Lindner 00004 00005 This file is part of commonc++. 00006 00007 This library is free software; you can redistribute it and/or 00008 modify it under the terms of the GNU Library General Public 00009 License as published by the Free Software Foundation; either 00010 version 2 of the License, or (at your option) any later version. 00011 00012 This library is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 Library General Public License for more details. 00016 00017 You should have received a copy of the GNU Library General Public 00018 License along with this library; if not, write to the Free 00019 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00020 --------------------------------------------------------------------------- 00021 */ 00022 00023 #ifndef __ccxx_SocketAddress_hxx 00024 #define __ccxx_SocketAddress_hxx 00025 00026 #include <commonc++/Common.h++> 00027 #include <commonc++/ByteOrder.h++> 00028 #include <commonc++/InetAddress.h++> 00029 #include <commonc++/IOException.h++> 00030 #include <commonc++/String.h++> 00031 00032 #ifdef CCXX_OS_POSIX 00033 #include <sys/types.h> 00034 #include <sys/socket.h> 00035 #include <netinet/in.h> 00036 #endif 00037 00038 namespace ccxx { 00039 00046 class COMMONCPP_API SocketAddress 00047 { 00048 public: 00049 00051 SocketAddress() throw(); 00052 00058 SocketAddress(const InetAddress &address, uint16_t port); 00059 00061 explicit SocketAddress(const struct sockaddr& other) throw(); 00062 00064 explicit SocketAddress(const struct sockaddr_in& other) throw(); 00065 00067 SocketAddress(const SocketAddress& other) throw(); 00068 00070 ~SocketAddress() throw(); 00071 00073 SocketAddress& operator=(const struct sockaddr& other) throw(); 00074 00076 SocketAddress& operator=(const struct sockaddr_in& other) throw(); 00077 00079 SocketAddress& operator=(const SocketAddress& other) throw(); 00080 00082 InetAddress getAddress() const throw(); 00083 00085 void setAddress(const InetAddress &address) throw(); 00086 00092 void setHost(const String &host) throw(HostNotFoundException); 00093 00095 inline uint16_t getPort() const throw() 00096 { return(ByteOrder::networkToHost(_insaddr.sin_port)); } 00097 00099 inline void setPort(uint16_t port) throw() 00100 { _insaddr.sin_port = ByteOrder::hostToNetwork(port); } 00101 00103 inline void setSocketAddress(const struct sockaddr& addr) throw() 00104 { _saddr = addr; } 00105 00107 inline sockaddr getSocketAddress() const throw() 00108 { return(_saddr); } 00109 00111 void reset() throw(); 00112 00114 inline operator sockaddr *() throw() 00115 { return(&_saddr); } 00116 00118 inline operator sockaddr_in *() throw() 00119 { return(&_insaddr); } 00120 00122 String toString() const; 00123 00125 bool operator==(const SocketAddress& other) const throw(); 00126 00128 inline bool operator!=(const SocketAddress& other) const throw() 00129 { return(! operator==(other)); } 00130 00131 private: 00132 00133 union 00134 { 00135 struct sockaddr _saddr; 00136 struct sockaddr_in _insaddr; 00137 }; 00138 }; 00139 00140 inline std::ostream& operator<<(std::ostream& stream, const SocketAddress& a) 00141 { return(stream << a.toString().c_str()); } 00142 00143 }; // namespace ccxx 00144 00145 #endif // __ccxx_SocketAddress_hxx 00146 00147 /* end of header file */
1.6.3