| 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 | virtual ~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 | size_t SendAll(const char * buff, size_t len); | 
|---|
| 44 | size_t ReceiveAll(char * buff, size_t len); | 
|---|
| 45 |  | 
|---|
| 46 | /* Configuration nombre maxi de retry et temps d'attente (microsecondes) | 
|---|
| 47 | pour SendAll et ReceiveAll | 
|---|
| 48 | inline void SetSendRecvAllMaxNTry(int maxnt=20, int slus=100) | 
|---|
| 49 | {  maxntry_=maxnt; slus_retry_=slus; } | 
|---|
| 50 | */ | 
|---|
| 51 |  | 
|---|
| 52 | virtual int Close(); | 
|---|
| 53 |  | 
|---|
| 54 | inline Socket& operator=(Socket const & a) | 
|---|
| 55 | { Set(a);  return (*this); } | 
|---|
| 56 |  | 
|---|
| 57 | inline int SetPrtLevel(int lev=0) | 
|---|
| 58 | { if (lev > 0) prtlev = lev; return prtlev; } | 
|---|
| 59 | inline long NBytesSent() { return totsnd; } | 
|---|
| 60 | inline long NBytesRecv() { return totrcv; } | 
|---|
| 61 |  | 
|---|
| 62 | protected: | 
|---|
| 63 | void Set(Socket const & a); | 
|---|
| 64 |  | 
|---|
| 65 | int skt; | 
|---|
| 66 | long tstart;    /*  Temps de demarrage en sec  */ | 
|---|
| 67 | long tlast;     /*  Temps de derniere acces en sec */ | 
|---|
| 68 | long totrcv;    /*  Nb total d'octets recus  */ | 
|---|
| 69 | long totsnd;    /*  Nb total d'octets envoyes */ | 
|---|
| 70 | long dts;       /*  Dt en ms send (File or Data) */ | 
|---|
| 71 | long nbbs;      /*  Nb Octets send (File or Data) */ | 
|---|
| 72 | long dtr;       /*  Dt en ms recv (File or Data) */ | 
|---|
| 73 | long nbbr;      /*  Nb Octets recv (File or Data) */ | 
|---|
| 74 | long lstdt;     /*  Dt en ms last send or receive */ | 
|---|
| 75 | long lstnbb;    /*  Nb bytes last send or receive */ | 
|---|
| 76 | long errcnt;    /*  Total error count */ | 
|---|
| 77 | //  int maxntry_;  int slus_retry_; | 
|---|
| 78 | int prtlev; | 
|---|
| 79 | }; | 
|---|
| 80 |  | 
|---|
| 81 |  | 
|---|
| 82 | //  -------------------------- | 
|---|
| 83 | //  Classe ServerSocket - A instancier cote serveur | 
|---|
| 84 | //  -------------------------- | 
|---|
| 85 |  | 
|---|
| 86 | class ServerSocket : public Socket | 
|---|
| 87 | { | 
|---|
| 88 | public : | 
|---|
| 89 | ServerSocket(int port, int nconmax); | 
|---|
| 90 | ServerSocket(const char* inadr, int port, int nconmax); | 
|---|
| 91 | ServerSocket(string const& inadr, int port, int nconmax); | 
|---|
| 92 | Socket WaitClientConnection(); | 
|---|
| 93 | virtual int Close();  // redefinition de la methode de Socket::Close() | 
|---|
| 94 |  | 
|---|
| 95 | protected: | 
|---|
| 96 | void Initialize(const char* inadr, int port, int nconmax); | 
|---|
| 97 |  | 
|---|
| 98 | int portid; | 
|---|
| 99 | struct sockaddr_in  ipskt; | 
|---|
| 100 | int NConMax; | 
|---|
| 101 | int nclitot; | 
|---|
| 102 | }; | 
|---|
| 103 |  | 
|---|
| 104 | //  -------------------------- | 
|---|
| 105 | //  Classe ClientSocket - A instancier cote client | 
|---|
| 106 | //  -------------------------- | 
|---|
| 107 |  | 
|---|
| 108 | class ClientSocket : public Socket | 
|---|
| 109 | { | 
|---|
| 110 | public : | 
|---|
| 111 | ClientSocket(string const& srvname, int port); | 
|---|
| 112 | ClientSocket(const char* srvname, int port); | 
|---|
| 113 | ClientSocket(ClientSocket const& a); | 
|---|
| 114 | inline ClientSocket& operator=(ClientSocket const& a) | 
|---|
| 115 | { SetC(a);  return (*this); } | 
|---|
| 116 | protected: | 
|---|
| 117 | void SetC(ClientSocket const& a); | 
|---|
| 118 | void InitConnection(const char* srvname, int port); | 
|---|
| 119 |  | 
|---|
| 120 | int portid; | 
|---|
| 121 | struct sockaddr_in  ipskt; | 
|---|
| 122 |  | 
|---|
| 123 | }; | 
|---|
| 124 |  | 
|---|
| 125 | } // Fin du namespace | 
|---|
| 126 |  | 
|---|
| 127 | #endif | 
|---|