socket.h

Go to the documentation of this file.
00001 /* Sockets base class.
00002  *
00003  * PegSoft sockets library (c) 2007 PegSoft
00004  * Contact us at pegsoft@pegsoft.net
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program (see the file COPYING); if not, write to the
00018  * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  */
00020 
00024 #ifndef SOCKETS_SOCKET_H
00025 #define SOCKETS_SOCKET_H
00026 
00027 /*************************************************************************/
00028 
00029 #if HAVE_SYS_SOCKET_H
00030 # include <sys/socket.h>
00031 #endif
00032 
00033 #if HAVE_NETINET_IN_H
00034 # include <netinet/in.h>
00035 #endif
00036 
00037 #if HAVE_SYS_UN_H
00038 # include <sys/un.h>
00039 #endif
00040 
00041 #include <string>
00042 
00043 /*************************************************************************/
00044 
00046 
00047 namespace sockets
00048 {
00049 
00050 /*************************************************************************/
00051 
00052 class Monitor;
00053 
00054 /*************************************************************************/
00055 
00057 
00065 class Socket
00066 {
00067     public:
00068         /* Friend classes */
00069         
00071         friend class SSLInfo;
00072         
00073         /* Constants */
00074         
00076         enum
00077         {
00079             ssCreated = 0x00000001,
00081             ssLocalInfo = 0x00000002,
00083             ssRemoteInfo = 0x00000004,
00085             ssBound = 0x00000008,
00087             ssConnecting = 0x00000010,
00089             ssConnected = 0x00000020,
00091             ssListening = 0x00000040,
00093             ssMonitored = 0x00000080,
00095             ssReadable = 0x00000100,
00097             ssWritable = 0x00000200,
00099             ssSSL = 0x00000400,
00101             ssSSLHandshake = 0x00000800,
00102             
00104             ssCloseFlags = (ssCreated | ssBound | ssConnecting | ssConnected |
00105                     ssListening | ssMonitored | ssReadable | ssWritable |
00106                     ssSSLHandshake),
00108             ssCopyFlags = (ssLocalInfo | ssRemoteInfo | ssSSL)
00109         };
00110         
00111         /* Static functions */
00112         
00114 
00121         static void init(size_t hint);
00122         
00124         static char const *hostname();
00125         
00126         /* Functions */
00127         
00129 
00141         explicit Socket(int family = PF_UNSPEC, int type = 0,
00142                 int protocol = 0);
00144 
00148         Socket(Socket const &s) : m_fd(-1), m_family(s.m_family),
00149                 m_type(s.m_type), m_protocol(s.m_protocol),
00150                 m_status(s.m_status & ssCopyFlags), m_monitor(s.m_monitor) { }
00152 
00156         virtual Socket &operator=(Socket const &right);
00158 
00159         virtual ~Socket();
00160         
00162 
00176         virtual bool assign(int fd, uint32_t status);
00177         
00179 
00184         virtual bool read_event() { return true; }
00185         
00187 
00192         virtual bool write_event() { return true; }
00193         
00195         int family() const { return m_family; }
00197         int type() const { return m_type; }
00199         int protocol() const { return m_protocol; }
00200         
00202 
00206         uint32_t status() const { return m_status; }
00207         
00209         std::string const &monitor() const;
00211 
00219         bool monitor(std::string const &name);
00220     protected:
00222 
00234         virtual bool create();
00235         
00237 
00246         virtual bool close();
00247         
00249 
00258         virtual bool watch();
00259         
00261 
00265         virtual void unwatch();
00266         
00268 
00272         int fd() const { return m_fd; }
00273         
00275 
00280         int family(int family);
00281         
00283 
00288         int type(int type);
00289         
00291 
00296         int protocol(int protocol);
00297         
00299 
00304         uint32_t status_add(uint32_t flags) { return (m_status |= flags); }
00305         
00307 
00312         uint32_t status_remove(uint32_t flags) { return (m_status &= ~flags); }
00313     private:
00315         int m_fd;
00316         
00318         int m_family;
00320         int m_type;
00322         int m_protocol;
00323         
00325         uint32_t m_status;
00326         
00328         Monitor *m_monitor;
00329 };
00330 
00331 /*************************************************************************/
00332 
00333 
00334 
00335 /*************************************************************************/
00336 
00337 } /* namespace sockets */
00338 
00339 /*************************************************************************/
00340 
00341 #endif /* SOCKETS_SOCKET_H */

Generated on Fri Apr 18 22:03:27 2008 for Epona API by  doxygen 1.5.3