PageRenderTime 82ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/addons/pvr.nextpvr/src/Socket.h

https://github.com/tzickel/xbmc-pvr-addons
C Header | 308 lines | 143 code | 45 blank | 120 comment | 4 complexity | 2de65e75c777d63f53dea6bd6c69157c MD5 | raw file
Possible License(s): GPL-3.0, AGPL-1.0, LGPL-2.1, MIT
  1. /*
  2. * Copyright (C) 2005-2011 Team XBMC
  3. * http://www.xbmc.org
  4. *
  5. * This Program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2, or (at your option)
  8. * any later version.
  9. *
  10. * This Program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. #pragma once
  20. namespace NextPVR
  21. {
  22. //Include platform specific datatypes, header files, defines and constants:
  23. #if defined TARGET_WINDOWS
  24. #define WIN32_LEAN_AND_MEAN // Enable LEAN_AND_MEAN support
  25. #pragma warning(disable:4005) // Disable "warning C4005: '_WINSOCKAPI_' : macro redefinition"
  26. #include <winsock2.h>
  27. #pragma warning(default:4005)
  28. #include <windows.h>
  29. #ifndef NI_MAXHOST
  30. #define NI_MAXHOST 1025
  31. #endif
  32. #ifndef socklen_t
  33. typedef int socklen_t;
  34. #endif
  35. #ifndef ipaddr_t
  36. typedef unsigned long ipaddr_t;
  37. #endif
  38. #ifndef port_t
  39. typedef unsigned short port_t;
  40. #endif
  41. #elif defined TARGET_LINUX || defined TARGET_DARWIN || defined TARGET_FREEBSD
  42. #ifdef SOCKADDR_IN
  43. #undef SOCKADDR_IN
  44. #endif
  45. #include <sys/types.h> /* for socket,connect */
  46. #include <sys/socket.h> /* for socket,connect */
  47. #include <sys/un.h> /* for Unix socket */
  48. #include <arpa/inet.h> /* for inet_pton */
  49. #include <netdb.h> /* for gethostbyname */
  50. #include <netinet/in.h> /* for htons */
  51. #include <unistd.h> /* for read, write, close */
  52. #include <errno.h>
  53. #include <fcntl.h>
  54. typedef int SOCKET;
  55. typedef sockaddr SOCKADDR;
  56. typedef sockaddr_in SOCKADDR_IN;
  57. #ifndef INVALID_SOCKET
  58. #define INVALID_SOCKET (-1)
  59. #endif
  60. #define SOCKET_ERROR (-1)
  61. #else
  62. #error Platform specific socket support is not yet available on this platform!
  63. #endif
  64. using namespace std;
  65. #include <vector>
  66. #define MAXCONNECTIONS 1 ///< Maximum number of pending connections before "Connection refused"
  67. #define MAXRECV 1500 ///< Maximum packet size
  68. enum SocketFamily
  69. {
  70. #ifdef CONFIG_SOCKET_IPV6
  71. af_inet6 = AF_INET6,
  72. af_unspec = AF_UNSPEC, ///< Either INET or INET6
  73. #endif
  74. af_inet = AF_INET
  75. };
  76. enum SocketDomain
  77. {
  78. #if defined TARGET_LINUX || defined TARGET_DARWIN || defined TARGET_FREEBSD
  79. pf_unix = PF_UNIX,
  80. pf_local = PF_LOCAL,
  81. #endif
  82. #ifdef CONFIG_SOCKET_IPV6
  83. pf_inet6 = PF_INET6,
  84. pf_unspec = PF_UNSPEC, //< Either INET or INET6
  85. #endif
  86. pf_inet = PF_INET
  87. };
  88. enum SocketType
  89. {
  90. sock_stream = SOCK_STREAM,
  91. sock_dgram = SOCK_DGRAM
  92. };
  93. enum SocketProtocol
  94. {
  95. tcp = IPPROTO_TCP,
  96. udp = IPPROTO_UDP
  97. #ifdef CONFIG_SOCKET_IPV6
  98. , ipv6 = IPPROTO_IPV6
  99. #endif
  100. };
  101. class Socket
  102. {
  103. public:
  104. /*!
  105. * An unconnected socket may be created directly on the local
  106. * machine. The socket type (SOCK_STREAM, SOCK_DGRAM) and
  107. * protocol may also be specified.
  108. * If the socket cannot be created, an exception is thrown.
  109. *
  110. * \param family Socket family (IPv4 or IPv6)
  111. * \param domain The domain parameter specifies a communications domain within which communication will take place;
  112. * this selects the protocol family which should be used.
  113. * \param type base type and protocol family of the socket.
  114. * \param protocol specific protocol to apply.
  115. */
  116. Socket(const enum SocketFamily family, const enum SocketDomain domain, const enum SocketType type, const enum SocketProtocol protocol = tcp);
  117. Socket(void);
  118. virtual ~Socket();
  119. //Socket settings
  120. /*!
  121. * Socket setFamily
  122. * \param family Can be af_inet or af_inet6. Default: af_inet
  123. */
  124. void setFamily(const enum SocketFamily family)
  125. {
  126. _family = family;
  127. };
  128. /*!
  129. * Socket setDomain
  130. * \param domain Can be pf_unix, pf_local, pf_inet or pf_inet6. Default: pf_inet
  131. */
  132. void setDomain(const enum SocketDomain domain)
  133. {
  134. _domain = domain;
  135. };
  136. /*!
  137. * Socket setType
  138. * \param type Can be sock_stream or sock_dgram. Default: sock_stream.
  139. */
  140. void setType(const enum SocketType type)
  141. {
  142. _type = type;
  143. };
  144. /*!
  145. * Socket setProtocol
  146. * \param protocol Can be tcp or udp. Default: tcp.
  147. */
  148. void setProtocol(const enum SocketProtocol protocol)
  149. {
  150. _protocol = protocol;
  151. };
  152. /*!
  153. * Socket setPort
  154. * \param port port number for socket communication
  155. */
  156. void setPort (const unsigned short port)
  157. {
  158. _sockaddr.sin_port = htons ( port );
  159. };
  160. bool setHostname ( const std::string& host );
  161. // Server initialization
  162. /*!
  163. * Socket create
  164. * Create a new socket
  165. * \return True if succesful
  166. */
  167. bool create();
  168. /*!
  169. * Socket close
  170. * Close the socket
  171. * \return True if succesful
  172. */
  173. bool close();
  174. /*!
  175. * Socket bind
  176. */
  177. bool bind ( const unsigned short port );
  178. bool listen() const;
  179. bool accept ( Socket& socket ) const;
  180. // Client initialization
  181. bool connect ( const std::string& host, const unsigned short port );
  182. bool reconnect();
  183. // Data Transmission
  184. /*!
  185. * Socket send function
  186. *
  187. * \param data Reference to a std::string with the data to transmit
  188. * \return Number of bytes send or -1 in case of an error
  189. */
  190. int send ( const std::string& data );
  191. /*!
  192. * Socket send function
  193. *
  194. * \param data Pointer to a character array of size 'size' with the data to transmit
  195. * \param size Length of the data to transmit
  196. * \return Number of bytes send or -1 in case of an error
  197. */
  198. int send ( const char* data, const unsigned int size );
  199. /*!
  200. * Socket sendto function
  201. *
  202. * \param data Reference to a std::string with the data to transmit
  203. * \param size Length of the data to transmit
  204. * \param sendcompletebuffer If 'true': do not return until the complete buffer is transmitted
  205. * \return Number of bytes send or -1 in case of an error
  206. */
  207. int sendto ( const char* data, unsigned int size, bool sendcompletebuffer = false);
  208. // Data Receive
  209. /*!
  210. * Socket receive function
  211. *
  212. * \param data Reference to a std::string for storage of the received data.
  213. * \param minpacketsize The minimum number of bytes that should be received before returning from this function
  214. * \return Number of bytes received or SOCKET_ERROR
  215. */
  216. int receive ( std::string& data, unsigned int minpacketsize ) const;
  217. /*!
  218. * Socket receive function
  219. *
  220. * \param data Reference to a std::string for storage of the received data.
  221. * \return Number of bytes received or SOCKET_ERROR
  222. */
  223. int receive ( std::string& data ) const;
  224. /*!
  225. * Socket receive function
  226. *
  227. * \param data Pointer to a character array of size buffersize. Used to store the received data.
  228. * \param buffersize Size of the 'data' buffer
  229. * \param minpacketsize Specifies the minimum number of bytes that need to be received before returning
  230. * \return Number of bytes received or SOCKET_ERROR
  231. */
  232. int receive ( char* data, const unsigned int buffersize, const unsigned int minpacketsize ) const;
  233. /*!
  234. * Socket recvfrom function
  235. *
  236. * \param data Pointer to a character array of size buffersize. Used to store the received data.
  237. * \param buffersize Size of the 'data' buffer
  238. * \param from Optional: pointer to a sockaddr struct that will get the address from which the data is received
  239. * \param fromlen Optional, only required if 'from' is given: length of from struct
  240. * \return Number of bytes received or SOCKET_ERROR
  241. */
  242. int recvfrom ( char* data, const int buffersize, struct sockaddr* from = NULL, socklen_t* fromlen = NULL) const;
  243. bool set_non_blocking ( const bool );
  244. bool ReadResponse (int &code, vector<string> &lines);
  245. bool is_valid() const;
  246. bool read_ready();
  247. private:
  248. SOCKET _sd; ///< Socket Descriptor
  249. SOCKADDR_IN _sockaddr; ///< Socket Address
  250. enum SocketFamily _family; ///< Socket Address Family
  251. enum SocketProtocol _protocol; ///< Socket Protocol
  252. enum SocketType _type; ///< Socket Type
  253. enum SocketDomain _domain; ///< Socket domain
  254. #ifdef TARGET_WINDOWS
  255. WSADATA _wsaData; ///< Windows Socket data
  256. static int win_usage_count; ///< Internal Windows usage counter used to prevent a global WSACleanup when more than one Socket object is used
  257. #endif
  258. void errormessage( int errornum, const char* functionname = NULL) const;
  259. int getLastError(void) const;
  260. bool osInit();
  261. void osCleanup();
  262. };
  263. } //namespace MPTV