Connection.h

Go to the documentation of this file.
00001 #ifndef __CONNECTION_H__
00002 #define __CONNECTION_H__
00003 
00004 /*
00005 
00006 $Header$
00007 
00008 */
00009 
00010 #include <netinet/in.h>
00011 #include <stdarg.h>
00012 
00013 #include "Object.h"
00014 #include "Error.h"
00015 #include "Poller.h"
00016 #include "Signal.h"
00017 #include "String.h"
00018 
00019 static const char *const CONNECTION_DEFAULT_INT_NAME = "eth0";
00020 
00021 // Socket operations timeout in microseconds.
00022 static const unsigned int CONNECTION_SLEEP_TIME        = 100000;
00023 static const unsigned int CONNECTION_TRIES_NUM_DEFAULT = 3;
00024 
00025 // Messages.
00026 static const char *const MSG_SOCKET_CANNOT_CREATE      = "Cannot create network connection (%s).";
00027 static const char *const MSG_SOCKET_CANNOT_CONNECT     = "Cannot open network connection (%s).";
00028 static const char *const MSG_SOCKET_CANNOT_READ        = "Cannot read data from network (%s).";
00029 static const char *const MSG_SOCKET_CANNOT_WRITE       = "Cannot write data to network (%s).";
00030 static const char *const MSG_SOCKET_CANNOT_CLOSE       = "Cannot close network connection (%s).";
00031 static const char *const MSG_SOCKET_CANNOT_SHUTDOWN    = "Cannot shutdown network connection (%s).";
00032 static const char *const MSG_SOCKET_UNKNOWN_HOST       = "Unknown host name '%s'.";
00033 static const char *const MSG_SOCKET_UNKNOWN_SERVICE    = "Unknown service name '%s'.";
00034 static const char *const MSG_SOCKET_CANNOT_SET_OPTION  = "Cannot set socket option (%s).";
00035 static const char *const MSG_SOCKET_CORRUPTED          = "Socket corrupted.";
00036 static const char *const MSG_SOCKET_CANNOT_GET_INFO    = "Cannot get socket information (%s).";
00037 static const char *const MSG_SOCKET_CANNOT_BIND        = "Cannot bind socket (%s).";
00038 
00039 using namespace std;
00040 
00041 class Connection : public Socket
00042 {
00043 
00044   PYLON_OBJECT
00045 
00046 friend class Poller;
00047 
00048 public:
00049 
00050   Connection(const int socket, struct sockaddr_in *addr);
00051   Connection();
00052   virtual ~Connection();
00053 
00054   int shutdown(const int how);
00055   int close();
00056 
00057   String localIp() const;
00058   String peerIp() const;
00059   unsigned int localPort() const;
00060   unsigned int peerPort() const;
00061 
00062   int available(int &size);
00063   int read(char *buffer, const unsigned int size, const unsigned int triesNum = 0);
00064   int readAvailable(char *buffer, const unsigned int maxSize, int &actuallyRead);
00065   int readLn(String &buffer, const unsigned int maxSize = LIMIT_CSTRING_SIZE,
00066              const unsigned int triesNum = CONNECTION_TRIES_NUM_DEFAULT);
00067   int write(const char *data, const unsigned int size, int &actuallyWritten);
00068   int writeStr(const String &str);
00069   int writeStr(char *str);
00070   int writeLn(char *data, const char *eol = String::eol());
00071   int vprintf(const char *format, va_list ap);
00072   int printf(const char *format, ...) __attribute__ ((format (printf, 2, 3)));
00073 
00074   int isAlive(bool &yes);
00075   int clear();
00076 
00077   static int setAddress(const char *host, const char *service, const char *protocol, struct sockaddr_in *sap);
00078 
00079   static int localIp(String &ip);
00080 
00081   // Signals.
00082   SIGNAL(dataReady);
00083   SIGNAL(closed);
00084   SIGNAL(dead);
00085 
00086 protected:
00087 
00088   int setup(const int socket, struct sockaddr_in *addr);
00089 
00090   int setDataReady();
00091   int setDead();
00092 
00093   String _host;
00094   String _service;
00095   struct sockaddr_in _local;
00096   struct sockaddr_in _peer;
00097   String _localIp, _peerIp;
00098   int _localPort, _peerPort;
00099 };
00100 
00101 #endif

Generated on Thu Sep 6 20:11:25 2007 for Pylon Application Platform by  doxygen 1.5.1