| 1 | #ifndef SWRAPSOCK_H
 | 
|---|
| 2 | #define SWRAPSOCK_H
 | 
|---|
| 3 | 
 | 
|---|
| 4 | #include "machdefs.h"
 | 
|---|
| 5 | #include "pexceptions.h"
 | 
|---|
| 6 | 
 | 
|---|
| 7 | #include <unistd.h>
 | 
|---|
| 8 | #include <iostream>
 | 
|---|
| 9 | 
 | 
|---|
| 10 | #include <sys/types.h>
 | 
|---|
| 11 | #include <sys/socket.h>
 | 
|---|
| 12 | #include <netinet/in.h>
 | 
|---|
| 13 | 
 | 
|---|
| 14 | 
 | 
|---|
| 15 | namespace SOPHYA {
 | 
|---|
| 16 | 
 | 
|---|
| 17 | /*! 
 | 
|---|
| 18 |   \class SocketException
 | 
|---|
| 19 |   \ingroup SysTools
 | 
|---|
| 20 |   \brief Exception class used by IP socket wrapper classes in SysTools module
 | 
|---|
| 21 | */
 | 
|---|
| 22 | 
 | 
|---|
| 23 | class SocketException : public PException {
 | 
|---|
| 24 |   public:
 | 
|---|
| 25 |   explicit SocketException(const char * m) : PException(m) {}
 | 
|---|
| 26 |   explicit SocketException(const string& m) : PException(m) {}
 | 
|---|
| 27 | };
 | 
|---|
| 28 | 
 | 
|---|
| 29 | 
 | 
|---|
| 30 | //  --------------------------  
 | 
|---|
| 31 | // Classe de base pour enrober l'initialisation et l'utilisation 
 | 
|---|
| 32 | // des sockets pour les connections IP.
 | 
|---|
| 33 | //  --------------------------   
 | 
|---|
| 34 | 
 | 
|---|
| 35 | class Socket {
 | 
|---|
| 36 | public:
 | 
|---|
| 37 |   Socket(int s=-1);
 | 
|---|
| 38 |   Socket(Socket const & a);
 | 
|---|
| 39 |   ~Socket();
 | 
|---|
| 40 | 
 | 
|---|
| 41 |   size_t Send(const char * buff, size_t len, int flag=0);
 | 
|---|
| 42 |   size_t Receive(char * buff, size_t len, int flag=0);
 | 
|---|
| 43 |   int Close();
 | 
|---|
| 44 |   
 | 
|---|
| 45 |   inline Socket& operator=(Socket const & a)
 | 
|---|
| 46 |   { Set(a);  return (*this); }
 | 
|---|
| 47 | 
 | 
|---|
| 48 |   inline int SetPrtLevel(int lev=0) 
 | 
|---|
| 49 |     { if (lev > 0) prtlev = lev; return prtlev; }
 | 
|---|
| 50 |   inline long NBytesSent() { return totsnd; }
 | 
|---|
| 51 |   inline long NBytesRecv() { return totrcv; }
 | 
|---|
| 52 |   
 | 
|---|
| 53 | protected:
 | 
|---|
| 54 |   void Set(Socket const & a);
 | 
|---|
| 55 | 
 | 
|---|
| 56 |   int skt;
 | 
|---|
| 57 |   long tstart;    /*  Temps de demarrage en sec  */
 | 
|---|
| 58 |   long tlast;     /*  Temps de derniere acces en sec */
 | 
|---|
| 59 |   long totrcv;    /*  Nb total d'octets recus  */
 | 
|---|
| 60 |   long totsnd;    /*  Nb total d'octets envoyes */
 | 
|---|
| 61 |   long dts;       /*  Dt en ms send (File or Data) */
 | 
|---|
| 62 |   long nbbs;      /*  Nb Octets send (File or Data) */
 | 
|---|
| 63 |   long dtr;       /*  Dt en ms recv (File or Data) */
 | 
|---|
| 64 |   long nbbr;      /*  Nb Octets recv (File or Data) */
 | 
|---|
| 65 |   long lstdt;     /*  Dt en ms last send or receive */
 | 
|---|
| 66 |   long lstnbb;    /*  Nb bytes last send or receive */
 | 
|---|
| 67 |   long errcnt;    /*  Total error count */
 | 
|---|
| 68 |   int prtlev;
 | 
|---|
| 69 | };
 | 
|---|
| 70 | 
 | 
|---|
| 71 | 
 | 
|---|
| 72 | //  --------------------------  
 | 
|---|
| 73 | //  Classe ServerSocket - A instancier cote serveur
 | 
|---|
| 74 | //  --------------------------   
 | 
|---|
| 75 | 
 | 
|---|
| 76 | class ServerSocket : public Socket
 | 
|---|
| 77 | {
 | 
|---|
| 78 | public :
 | 
|---|
| 79 |   ServerSocket(int port, int nconmax);
 | 
|---|
| 80 |   Socket WaitClientConnection();
 | 
|---|
| 81 | 
 | 
|---|
| 82 | protected:
 | 
|---|
| 83 |   int portid;
 | 
|---|
| 84 |   struct sockaddr_in  ipskt;
 | 
|---|
| 85 |   int NConMax;
 | 
|---|
| 86 |   int nclitot;  
 | 
|---|
| 87 | };
 | 
|---|
| 88 | 
 | 
|---|
| 89 | //  --------------------------  
 | 
|---|
| 90 | //  Classe ClientSocket - A instancier cote client
 | 
|---|
| 91 | //  --------------------------   
 | 
|---|
| 92 | 
 | 
|---|
| 93 | class ClientSocket : public Socket
 | 
|---|
| 94 | {
 | 
|---|
| 95 | public :
 | 
|---|
| 96 |   ClientSocket(string const& srvname, int port);
 | 
|---|
| 97 |   ClientSocket(const char* srvname, int port);
 | 
|---|
| 98 |   ClientSocket(ClientSocket const& a);
 | 
|---|
| 99 |   inline ClientSocket& operator=(ClientSocket const& a)
 | 
|---|
| 100 |   { SetC(a);  return (*this); }
 | 
|---|
| 101 | protected:
 | 
|---|
| 102 |   void SetC(ClientSocket const& a);
 | 
|---|
| 103 |   void InitConnection(const char* srvname, int port);
 | 
|---|
| 104 | 
 | 
|---|
| 105 |   int portid;
 | 
|---|
| 106 |   struct sockaddr_in  ipskt;
 | 
|---|
| 107 | 
 | 
|---|
| 108 | };
 | 
|---|
| 109 | 
 | 
|---|
| 110 | } // Fin du namespace
 | 
|---|
| 111 | 
 | 
|---|
| 112 | #endif
 | 
|---|