Socket.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #ifndef SOCKET_H
  2. #define SOCKET_H
  3. #ifdef WIN32 /* si vous êtes sous Windows */
  4. #include <winsock2.h>
  5. #elif defined (linux) /* si vous êtes sous Linux */
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <netinet/in.h>
  9. #include <arpa/inet.h>
  10. #include <unistd.h> /* close */
  11. #include <netdb.h> /* gethostbyname */
  12. #define INVALID_SOCKET -1
  13. #define SOCKET_ERROR -1
  14. #define closesocket(s) close(s)
  15. typedef int SOCKET;
  16. typedef struct sockaddr_in SOCKADDR_IN;
  17. typedef struct sockaddr SOCKADDR;
  18. typedef struct in_addr IN_ADDR;
  19. #include <sys/un.h>
  20. #else /* sinon vous êtes sur une plateforme non supportée */
  21. #error not defined for this platform
  22. #endif
  23. #include <string>
  24. #include <istream>
  25. #include <ostream>
  26. class SocketError
  27. {
  28. public:
  29. SocketError(const std::string& s, const std::string& t) : error(s), type(t){}
  30. virtual ~SocketError(){}
  31. std::string error;
  32. std::string type;
  33. };
  34. #define DefineSocketError(__Err__) class __Err__ : public SocketError{ public: __Err__(const std::string& e, const std::string& t) : SocketError(e, t){} ~__Err__(){} };
  35. DefineSocketError(ConnectSocketError)
  36. DefineSocketError(FileSocketNotFoundError)
  37. DefineSocketError(SendSocketError)
  38. DefineSocketError(RecieveSocketError)
  39. DefineSocketError(AcceptSocketError)
  40. DefineSocketError(BindSocketError)
  41. DefineSocketError(ListenSocketError)
  42. DefineSocketError(HostSocketError)
  43. DefineSocketError(OptSocketError)
  44. #ifndef THROW
  45. #define THROW(__ERR__, __MSG__) {std::ostringstream os; os << #__ERR__<< " : " << __MSG__ <<"\n"; std::cerr << os.str(); throw __ERR__(os.str(), #__ERR__);}
  46. #endif
  47. class _AbsSocket
  48. {
  49. public:
  50. enum SocketType { INET, UNIX, STDIO } ;
  51. _AbsSocket(SocketType);
  52. virtual ~_AbsSocket();
  53. virtual void close();
  54. virtual bool is_open();
  55. protected:
  56. SocketType m_type;
  57. SOCKET m_sock;
  58. bool m_is_open;
  59. };
  60. class ISocket : public std::istream, public std::ostream
  61. {
  62. public:
  63. virtual ~ISocket(){}
  64. virtual void write(const void* data, int size)=0;
  65. virtual int read(void* data, int n, bool blocking)=0;
  66. virtual ISocket& putc(int c) {write(&c,1); return *(ISocket*)this;}
  67. virtual void write(const std::string& data){write(data.c_str(), data.size());}
  68. virtual int read(void* data, int n){return read(data,n,false);};
  69. virtual int readc(){int c; return (read(&c,1)==1)?c:-1;}
  70. virtual std::string& readline(std::string&);
  71. virtual _AbsSocket::SocketType get_type() const =0;
  72. virtual void close()=0;
  73. virtual bool is_open()=0;
  74. };
  75. class Socket : public _AbsSocket, public ISocket
  76. {
  77. public:
  78. Socket(SocketType t) : _AbsSocket(t){}
  79. Socket(SocketType type, SOCKET client) : Socket(type){m_sock=client; m_is_open=true;}
  80. virtual ~Socket(){}
  81. virtual void write(const void* data, int size);
  82. virtual int read(void* data, int n, bool blocking);
  83. virtual SocketType get_type() const {return m_type;}
  84. virtual void close(){_AbsSocket::close();};
  85. virtual bool is_open(){return _AbsSocket::is_open();}
  86. };
  87. class InetSocket : public Socket
  88. {
  89. public:
  90. InetSocket() : Socket(INET){}
  91. InetSocket(SOCKET sock) : Socket(INET, sock){}
  92. virtual ~InetSocket(){}
  93. virtual bool connect(const std::string& addr, int port);
  94. virtual Socket::SocketType get_type() const {return Socket::INET;}
  95. protected:
  96. std::string m_hostname;
  97. int m_port;
  98. };
  99. class UnixSocket : public Socket
  100. {
  101. public:
  102. UnixSocket() : Socket(UNIX){}
  103. virtual ~UnixSocket(){}
  104. virtual bool connect(const std::string& addr);
  105. virtual Socket::SocketType get_type() const {return Socket::UNIX;}
  106. protected:
  107. std::string m_hostname;
  108. int m_port;
  109. };
  110. class IAbsServer
  111. {
  112. public:
  113. virtual ~IAbsServer(){}
  114. virtual ISocket* accept()=0;
  115. virtual Socket::SocketType get_type() const=0;
  116. };
  117. class InetServer : public _AbsSocket, public IAbsServer
  118. {
  119. public:
  120. InetServer();
  121. virtual ~InetServer(){}
  122. virtual void listen(int port);
  123. virtual ISocket* accept();
  124. virtual Socket::SocketType get_type() const {return Socket::INET;}
  125. };
  126. class UnixServer : public _AbsSocket, public IAbsServer
  127. {
  128. public:
  129. UnixServer();
  130. virtual ~UnixServer();
  131. virtual void listen(const std::string& path){return listen(path, true);}
  132. virtual void listen(const std::string& path, bool);
  133. virtual ISocket* accept();
  134. virtual Socket::SocketType get_type() const {return Socket::UNIX;}
  135. protected:
  136. std::string m_path;
  137. };
  138. class StdioSocket : public ISocket
  139. {
  140. public:
  141. StdioSocket(int first){m_first=first;}
  142. virtual ~StdioSocket(){}
  143. virtual void write(const void* data, int size);
  144. virtual int read(void* data, int n, bool blocking);
  145. virtual Socket::SocketType get_type() const {return Socket::STDIO;}
  146. virtual void close(){};
  147. virtual bool is_open(){return false;}
  148. public:
  149. int m_first;
  150. };
  151. class StdioServer : public IAbsServer
  152. {
  153. public:
  154. StdioServer() {}
  155. virtual ~StdioServer(){}
  156. virtual ISocket* accept();
  157. virtual Socket::SocketType get_type() const {return Socket::STDIO;}
  158. };
  159. #endif // SOCKET_H