PageRenderTime 25ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/addons/pvr.wmc/src/Socket.h

https://github.com/smuehlmann/xbmc-pvr-addons
C Header | 275 lines | 149 code | 41 blank | 85 comment | 4 complexity | 409ccc246d72d03cd0da843f9a1cff71 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.1, MIT, GPL-3.0
  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. //Include platform specific datatypes, header files, defines and constants:
  21. #if defined TARGET_WINDOWS
  22. #define WIN32_LEAN_AND_MEAN // Enable LEAN_AND_MEAN support
  23. #pragma warning(disable:4005) // Disable "warning C4005: '_WINSOCKAPI_' : macro redefinition"
  24. #include <winsock2.h>
  25. #pragma warning(default:4005)
  26. #include <windows.h>
  27. #ifndef NI_MAXHOST
  28. #define NI_MAXHOST 1025
  29. #endif
  30. #ifndef socklen_t
  31. typedef int socklen_t;
  32. #endif
  33. #ifndef ipaddr_t
  34. typedef unsigned long ipaddr_t;
  35. #endif
  36. #ifndef port_t
  37. typedef unsigned short port_t;
  38. #endif
  39. #ifndef sa_family_t
  40. #define sa_family_t ADDRESS_FAMILY
  41. #endif
  42. #elif defined TARGET_LINUX || defined TARGET_DARWIN || defined TARGET_FREEBSD
  43. #ifdef SOCKADDR_IN
  44. #undef SOCKADDR_IN
  45. #endif
  46. #include <sys/types.h> /* for socket,connect */
  47. #include <sys/socket.h> /* for socket,connect */
  48. #include <sys/un.h> /* for Unix socket */
  49. #include <arpa/inet.h> /* for inet_pton */
  50. #include <netdb.h> /* for gethostbyname */
  51. #include <netinet/in.h> /* for htons */
  52. #include <unistd.h> /* for read, write, close */
  53. #include <errno.h>
  54. #include <fcntl.h>
  55. typedef int SOCKET;
  56. typedef sockaddr SOCKADDR;
  57. typedef sockaddr_in SOCKADDR_IN;
  58. #ifndef INVALID_SOCKET
  59. #define INVALID_SOCKET (-1)
  60. #endif
  61. #define SOCKET_ERROR (-1)
  62. #else
  63. #error Platform specific socket support is not yet available on this platform!
  64. #endif
  65. using namespace std;
  66. #include <vector>
  67. #define MAXCONNECTIONS 1 ///< Maximum number of pending connections before "Connection refused"
  68. #define MAXRECV 1500 ///< Maximum packet size
  69. enum SocketFamily
  70. {
  71. #ifdef CONFIG_SOCKET_IPV6
  72. af_inet6 = AF_INET6,
  73. af_unspec = AF_UNSPEC, ///< Either INET or INET6
  74. #endif
  75. af_inet = AF_INET
  76. };
  77. enum SocketDomain
  78. {
  79. #if defined TARGET_LINUX || defined TARGET_DARWIN || defined TARGET_FREEBSD
  80. pf_unix = PF_UNIX,
  81. pf_local = PF_LOCAL,
  82. #endif
  83. #ifdef CONFIG_SOCKET_IPV6
  84. pf_inet6 = PF_INET6,
  85. pf_unspec = PF_UNSPEC, //< Either INET or INET6
  86. #endif
  87. pf_inet = PF_INET
  88. };
  89. enum SocketType
  90. {
  91. sock_stream = SOCK_STREAM,
  92. sock_dgram = SOCK_DGRAM
  93. };
  94. enum SocketProtocol
  95. {
  96. tcp = IPPROTO_TCP,
  97. udp = IPPROTO_UDP
  98. #ifdef CONFIG_SOCKET_IPV6
  99. , ipv6 = IPPROTO_IPV6
  100. #endif
  101. };
  102. class Socket
  103. {
  104. public:
  105. /*!
  106. * An unconnected socket may be created directly on the local
  107. * machine. The socket type (SOCK_STREAM, SOCK_DGRAM) and
  108. * protocol may also be specified.
  109. * If the socket cannot be created, an exception is thrown.
  110. *
  111. * \param family Socket family (IPv4 or IPv6)
  112. * \param domain The domain parameter specifies a communications domain within which communication will take place;
  113. * this selects the protocol family which should be used.
  114. * \param type base type and protocol family of the socket.
  115. * \param protocol specific protocol to apply.
  116. */
  117. Socket(const enum SocketFamily family, const enum SocketDomain domain, const enum SocketType type, const enum SocketProtocol protocol = tcp);
  118. Socket(void);
  119. virtual ~Socket();
  120. //Socket settings
  121. /*!
  122. * Socket setFamily
  123. * \param family Can be af_inet or af_inet6. Default: af_inet
  124. */
  125. void setFamily(const enum SocketFamily family)
  126. {
  127. _family = family;
  128. };
  129. /*!
  130. * Socket setDomain
  131. * \param domain Can be pf_unix, pf_local, pf_inet or pf_inet6. Default: pf_inet
  132. */
  133. void setDomain(const enum SocketDomain domain)
  134. {
  135. _domain = domain;
  136. };
  137. /*!
  138. * Socket setType
  139. * \param type Can be sock_stream or sock_dgram. Default: sock_stream.
  140. */
  141. void setType(const enum SocketType type)
  142. {
  143. _type = type;
  144. };
  145. /*!
  146. * Socket setProtocol
  147. * \param protocol Can be tcp or udp. Default: tcp.
  148. */
  149. void setProtocol(const enum SocketProtocol protocol)
  150. {
  151. _protocol = protocol;
  152. };
  153. /*!
  154. * Socket setPort
  155. * \param port port number for socket communication
  156. */
  157. void setPort (const unsigned short port)
  158. {
  159. _sockaddr.sin_port = htons ( port );
  160. };
  161. bool setHostname ( const CStdString& host );
  162. // Server initialization
  163. /*!
  164. * Socket create
  165. * Create a new socket
  166. * \return True if succesful
  167. */
  168. bool create();
  169. /*!
  170. * Socket close
  171. * Close the socket
  172. * \return True if succesful
  173. */
  174. bool close();
  175. /*!
  176. * Socket bind
  177. */
  178. //bool bind ( const unsigned short port );
  179. //bool listen() const;
  180. //bool accept ( Socket& socket ) const;
  181. // Client initialization
  182. bool connect ( const CStdString& host, const unsigned short port );
  183. bool reconnect();
  184. // Data Transmission
  185. /*!
  186. * Socket send function
  187. *
  188. * \param data Reference to a std::string with the data to transmit
  189. * \return Number of bytes send or -1 in case of an error
  190. */
  191. int send ( const CStdString& data );
  192. /*!
  193. * Socket send function
  194. *
  195. * \param data Pointer to a character array of size 'size' with the data to transmit
  196. * \param size Length of the data to transmit
  197. * \return Number of bytes send or -1 in case of an error
  198. */
  199. int send ( const char* data, const unsigned int size );
  200. bool set_non_blocking ( const bool );
  201. bool ReadResponses(int &code, vector<CStdString> &lines);
  202. bool is_valid() const;
  203. private:
  204. SOCKET _sd; ///< Socket Descriptor
  205. SOCKADDR_IN _sockaddr; ///< Socket Address
  206. enum SocketFamily _family; ///< Socket Address Family
  207. enum SocketProtocol _protocol; ///< Socket Protocol
  208. enum SocketType _type; ///< Socket Type
  209. enum SocketDomain _domain; ///< Socket domain
  210. #ifdef TARGET_WINDOWS
  211. WSADATA _wsaData; ///< Windows Socket data
  212. static int win_usage_count; ///< Internal Windows usage counter used to prevent a global WSACleanup when more than one Socket object is used
  213. #endif
  214. void errormessage( int errornum, const char* functionname = NULL) const;
  215. int getLastError(void) const;
  216. bool osInit();
  217. void osCleanup();
  218. // client interface
  219. private:
  220. CStdString _serverName;
  221. CStdString _clientName;
  222. int _port;
  223. int SendRequest(CStdString requestStr);
  224. public:
  225. void SetServerName(CStdString strServerName);
  226. void SetClientName(CStdString strClientName);
  227. void SetServerPort(int port);
  228. std::vector<CStdString> GetVector(const CStdString &request, bool allowRetry);
  229. CStdString GetString(const CStdString &request, bool allowRetry);
  230. bool GetBool(const CStdString &request, bool allowRetry);
  231. int GetInt(const CStdString &request, bool allowRetry);
  232. long long GetLL(const CStdString &request, bool allowRetry);
  233. void SetTimeOut(int tSec);
  234. };