Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

SocketDescriptor.h

Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 #ifndef MPS_SocketDescriptor_H
00003 #define MPS_SocketDescriptor_H
00004 
00005 #include <ref.h>
00006 #include <exception>
00007 #include <string>
00008 #include <map>
00009 
00010 #include <FileDescriptor.h>
00011 
00012 class SocketDescriptor;
00013 class ClientSocketDescriptor;
00014 class ServerSocketDescriptor;
00015 
00016 /**
00017  * Subclasses FileDescriptor to provide a general-purpose wrapper for
00018  * BSD TCP or UDP server- or client-sockets. */
00019 class SocketDescriptor: public FileDescriptor {
00020 private:
00021   string hostname_local;        ///< Name of the IP-address of the local end of the socket
00022   int port_local;               ///< Port number of the local end of the socket
00023 
00024   string hostname_remote;       ///< Name of the IP-address of the remote end of the socket
00025   int port_remote;              ///< Port number of the remote end of the socket
00026 
00027 protected:
00028   /// Override to use ::recv and ::send if on linux or win32.
00029   virtual int lowlevel_read(int fd, char *buf, int len);
00030   virtual int lowlevel_write(int fd, char const *buf, int len);
00031 
00032 #ifdef __WIN32__
00033   virtual int lowlevel_close(int fd);
00034 #endif
00035 
00036   friend class ServerSocketDescriptor;
00037   SocketDescriptor(ref<FileDescriptorManager> const &_fdMgr, int _fd)
00038     : FileDescriptor(_fdMgr, _fd),
00039       hostname_local("UNKNOWN"),
00040       port_local(0),
00041       hostname_remote("UNKNOWN"),
00042       port_remote(0)
00043   {}
00044 
00045   void setLocalAddr(string const &hostname, int port) {
00046     hostname_local = hostname;
00047     port_local = port;
00048   }
00049 
00050   void setRemoteAddr(string const &hostname, int port) {
00051     hostname_remote = hostname;
00052     port_remote = port;
00053   }
00054 
00055 public:
00056   virtual void close();         ///< Overridden to deal with shutdown() before close()
00057 
00058   /** @name AddressAccessors
00059    *
00060    * Accessors for local and remote address information for this
00061    * socket. */
00062   //@{
00063   string const &getLocalHostname() const { return hostname_local; }
00064   int getLocalPort() const { return port_local; }
00065   string const &getRemoteHostname() const { return hostname_remote; }
00066   int getRemotePort() const { return port_remote; }
00067   //@}
00068 };
00069 
00070 /**
00071  * Subclass of SocketDescriptor to provide TCP client-socket functionality. */
00072 class ClientSocketDescriptor: public SocketDescriptor {
00073 public:
00074   /// Constructs a TCP socket connected to the given hostname/port combination.
00075   ClientSocketDescriptor(ref<FileDescriptorManager> const &_fdMgr,
00076                          string const &_hostname, int _port);
00077 };
00078 
00079 /**
00080  * Subclass of SocketDescriptor to provide TCP server-socket functionality. */
00081 class ServerSocketDescriptor: public SocketDescriptor {
00082 private:
00083   string canonicalAddress;      /// Local end's xxx.xxx.xxx.xxx:yyyyy IP addr/port number
00084 
00085 public:
00086   /**
00087    * Construct a TCP server-socket listening on the given port, unless
00088    * 0 is specified, in which case an unused port is used, on the
00089    * given interface-name, unless the empty-string is specified, in
00090    * which case INADDR_ANY is used (ie. bind() to all interfaces if no
00091    * preference is supplied). */
00092   ServerSocketDescriptor(ref<FileDescriptorManager> const &_fdMgr,
00093                          int _port = 0, string const &_hostname = "");
00094 
00095   /**
00096    * Calls ::accept() and returns a SocketDescriptor for the resulting
00097    * new connection. Throws FileDescriptor::Exception on failure. */
00098   ref<SocketDescriptor> accept();
00099 
00100   /// Retrieves the IP address and port number that this server socket is bound to.
00101   string const &getCanonicalAddress() const { return canonicalAddress; }
00102 };
00103 
00104 ///////////////////////////////////////////////////////////////////////////
00105 // Utility classes
00106 
00107 template <class ConnectionHandler> class SocketAcceptorCallback: public FileDescriptor::Callback {
00108 private:
00109   void acceptHandler(ref<FileDescriptor> const &desc) {
00110     ref<ServerSocketDescriptor> server = (ServerSocketDescriptor *) desc.get();
00111     ref<Referenceable> dummyRef = new ConnectionHandler(server->accept());
00112   }
00113 
00114 public:
00115   SocketAcceptorCallback(ref<ServerSocketDescriptor> const &server) {
00116     server->setReadCallback(this,
00117                             (FileDescriptor::Callback::Method)
00118                               &SocketAcceptorCallback::acceptHandler);
00119   }
00120 };
00121 
00122 #endif

Generated at Wed Aug 15 01:05:16 2001 for mps-cpp by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001