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 #include <string>
00038 
00039 /*************************************************************************/
00040 
00042 
00043 namespace sockets
00044 {
00045 
00046 /*************************************************************************/
00047 
00048 class Monitor;
00049 
00050 /*************************************************************************/
00051 
00053 
00061 class Socket
00062 {
00063     public:
00064         /* Constants */
00065         
00067         enum
00068         {
00070             ssCreated = 0x00000001,
00072             ssLocalInfo = 0x00000002,
00074             ssRemoteInfo = 0x00000004,
00076             ssBound = 0x00000008,
00078             ssConnecting = 0x00000010,
00080             ssConnected = 0x00000020,
00082             ssListening = 0x00000040,
00084             ssMonitored = 0x00000080,
00086             ssReadable = 0x00000100,
00088             ssWritable = 0x00000200,
00089             
00091             ssCloseFlags = (ssCreated | ssBound | ssConnecting | ssConnected |
00092                     ssListening | ssMonitored | ssReadable | ssWritable),
00094             ssCopyFlags = (ssLocalInfo | ssRemoteInfo)
00095         };
00096         
00097         /* Static functions */
00098         
00100         static char const *hostname();
00101         
00102         /* Functions */
00103         
00105 
00117         explicit Socket(int family = PF_UNSPEC, int type = 0,
00118                 int protocol = 0);
00120 
00124         Socket(Socket const &s) : m_fd(-1), m_family(s.m_family),
00125                 m_type(s.m_type), m_protocol(s.m_protocol),
00126                 m_status(s.m_status & ssCopyFlags), m_monitor(s.m_monitor) { }
00128 
00132         virtual Socket &operator=(Socket const &right);
00134 
00135         virtual ~Socket();
00136         
00138 
00152         virtual bool assign(int fd, uint32_t status);
00153         
00155 
00160         virtual bool read_event() { return true; }
00161         
00163 
00168         virtual bool write_event() { return true; }
00169         
00171         int family() const { return m_family; }
00173         int type() const { return m_type; }
00175         int protocol() const { return m_protocol; }
00176         
00178 
00182         uint32_t status() const { return m_status; }
00183         
00185         std::string const &monitor() const;
00187 
00195         bool monitor(std::string const &name);
00196     protected:
00198 
00210         virtual bool create();
00211         
00213 
00222         virtual bool close();
00223         
00225 
00234         virtual bool watch();
00235         
00237 
00241         virtual void unwatch();
00242         
00244 
00248         int fd() const { return m_fd; }
00249         
00251 
00256         int family(int family);
00257         
00259 
00264         int type(int type);
00265         
00267 
00272         int protocol(int protocol);
00273         
00275 
00280         uint32_t status_add(uint32_t flags) { return (m_status |= flags); }
00281         
00283 
00288         uint32_t status_remove(uint32_t flags) { return (m_status &= ~flags); }
00289     private:
00291         int m_fd;
00292         
00294         int m_family;
00296         int m_type;
00298         int m_protocol;
00299         
00301         uint32_t m_status;
00302         
00304         Monitor *m_monitor;
00305 };
00306 
00307 /*************************************************************************/
00308 
00309 } /* namespace sockets */
00310 
00311 /*************************************************************************/
00312 
00313 #endif /* SOCKETS_SOCKET_H */

Generated on Wed Aug 15 00:37:22 2007 for Epona API by  doxygen 1.5.2