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

/addons/pvr.mediaportal.tvserver/src/Socket.h

https://github.com/smuehlmann/xbmc-pvr-addons
C Header | 298 lines | 134 code | 44 blank | 120 comment | 4 complexity | 523ee515a63be1a61cb5e2ef71ad1454 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. namespace MPTV //Prevent name clash with Live555 Socket
  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. #ifndef sa_family_t
  42. #define sa_family_t ADDRESS_FAMILY
  43. #endif
  44. #elif defined TARGET_LINUX || defined TARGET_DARWIN || defined TARGET_FREEBSD
  45. #ifdef SOCKADDR_IN
  46. #undef SOCKADDR_IN
  47. #endif
  48. #include <sys/types.h> /* for socket,connect */
  49. #include <sys/socket.h> /* for socket,connect */
  50. #include <sys/un.h> /* for Unix socket */
  51. #include <arpa/inet.h> /* for inet_pton */
  52. #include <netdb.h> /* for gethostbyname */
  53. #include <netinet/in.h> /* for htons */
  54. #include <unistd.h> /* for read, write, close */
  55. #include <errno.h>
  56. #include <fcntl.h>
  57. typedef int SOCKET;
  58. typedef sockaddr SOCKADDR;
  59. typedef sockaddr_in SOCKADDR_IN;
  60. #ifndef INVALID_SOCKET
  61. #define INVALID_SOCKET (-1)
  62. #endif
  63. #define SOCKET_ERROR (-1)
  64. #else
  65. #error Platform specific socket support is not yet available on this platform!
  66. #endif
  67. using namespace std;
  68. #include <vector>
  69. #define MAXCONNECTIONS 1 ///< Maximum number of pending connections before "Connection refused"
  70. #define MAXRECV 1500 ///< Maximum packet size
  71. enum SocketFamily
  72. {
  73. af_inet = AF_INET
  74. };
  75. enum SocketDomain
  76. {
  77. #if defined TARGET_LINUX || defined TARGET_DARWIN || defined TARGET_FREEBSD
  78. pf_unix = PF_UNIX,
  79. pf_local = PF_LOCAL,
  80. #endif
  81. pf_inet = PF_INET
  82. };
  83. enum SocketType
  84. {
  85. sock_stream = SOCK_STREAM,
  86. sock_dgram = SOCK_DGRAM
  87. };
  88. enum SocketProtocol
  89. {
  90. tcp = IPPROTO_TCP,
  91. udp = IPPROTO_UDP
  92. };
  93. class Socket
  94. {
  95. public:
  96. /*!
  97. * An unconnected socket may be created directly on the local
  98. * machine. The socket type (SOCK_STREAM, SOCK_DGRAM) and
  99. * protocol may also be specified.
  100. * If the socket cannot be created, an exception is thrown.
  101. *
  102. * \param family Socket family (IPv4 or IPv6)
  103. * \param domain The domain parameter specifies a communications domain within which communication will take place;
  104. * this selects the protocol family which should be used.
  105. * \param type base type and protocol family of the socket.
  106. * \param protocol specific protocol to apply.
  107. */
  108. Socket(const enum SocketFamily family, const enum SocketDomain domain, const enum SocketType type, const enum SocketProtocol protocol = tcp);
  109. Socket(void);
  110. virtual ~Socket();
  111. //Socket settings
  112. /*!
  113. * Socket setFamily
  114. * \param family Can be af_inet or af_inet6. Default: af_inet
  115. */
  116. void setFamily(const enum SocketFamily family)
  117. {
  118. _family = family;
  119. };
  120. /*!
  121. * Socket setDomain
  122. * \param domain Can be pf_unix, pf_local, pf_inet or pf_inet6. Default: pf_inet
  123. */
  124. void setDomain(const enum SocketDomain domain)
  125. {
  126. _domain = domain;
  127. };
  128. /*!
  129. * Socket setType
  130. * \param type Can be sock_stream or sock_dgram. Default: sock_stream.
  131. */
  132. void setType(const enum SocketType type)
  133. {
  134. _type = type;
  135. };
  136. /*!
  137. * Socket setProtocol
  138. * \param protocol Can be tcp or udp. Default: tcp.
  139. */
  140. void setProtocol(const enum SocketProtocol protocol)
  141. {
  142. _protocol = protocol;
  143. };
  144. /*!
  145. * Socket setPort
  146. * \param port port number for socket communication
  147. */
  148. void setPort (const unsigned short port)
  149. {
  150. _sockaddr.sin_port = htons ( port );
  151. };
  152. bool setHostname ( const std::string& host );
  153. // Server initialization
  154. /*!
  155. * Socket create
  156. * Create a new socket
  157. * \return True if succesful
  158. */
  159. bool create();
  160. /*!
  161. * Socket close
  162. * Close the socket
  163. * \return True if succesful
  164. */
  165. bool close();
  166. /*!
  167. * Socket bind
  168. */
  169. bool bind ( const unsigned short port );
  170. bool listen() const;
  171. bool accept ( Socket& socket ) const;
  172. // Client initialization
  173. bool connect ( const std::string& host, const unsigned short port );
  174. bool reconnect();
  175. // Data Transmission
  176. /*!
  177. * Socket send function
  178. *
  179. * \param data Reference to a std::string with the data to transmit
  180. * \return Number of bytes send or -1 in case of an error
  181. */
  182. int send ( const std::string& data );
  183. /*!
  184. * Socket send function
  185. *
  186. * \param data Pointer to a character array of size 'size' with the data to transmit
  187. * \param size Length of the data to transmit
  188. * \return Number of bytes send or -1 in case of an error
  189. */
  190. int send ( const char* data, const unsigned int size );
  191. /*!
  192. * Socket sendto function
  193. *
  194. * \param data Reference to a std::string with the data to transmit
  195. * \param size Length of the data to transmit
  196. * \param sendcompletebuffer If 'true': do not return until the complete buffer is transmitted
  197. * \return Number of bytes send or -1 in case of an error
  198. */
  199. int sendto ( const char* data, unsigned int size, bool sendcompletebuffer = false);
  200. // Data Receive
  201. /*!
  202. * Socket receive function
  203. *
  204. * \param data Reference to a std::string for storage of the received data.
  205. * \param minpacketsize The minimum number of bytes that should be received before returning from this function
  206. * \return Number of bytes received or SOCKET_ERROR
  207. */
  208. int receive ( std::string& data, unsigned int minpacketsize ) const;
  209. /*!
  210. * Socket receive function
  211. *
  212. * \param data Reference to a std::string for storage of the received data.
  213. * \return Number of bytes received or SOCKET_ERROR
  214. */
  215. int receive ( std::string& data ) const;
  216. /*!
  217. * Socket receive function
  218. *
  219. * \param data Pointer to a character array of size buffersize. Used to store the received data.
  220. * \param buffersize Size of the 'data' buffer
  221. * \param minpacketsize Specifies the minimum number of bytes that need to be received before returning
  222. * \return Number of bytes received or SOCKET_ERROR
  223. */
  224. int receive ( char* data, const unsigned int buffersize, const unsigned int minpacketsize ) const;
  225. /*!
  226. * Socket recvfrom function
  227. *
  228. * \param data Pointer to a character array of size buffersize. Used to store the received data.
  229. * \param buffersize Size of the 'data' buffer
  230. * \param from Optional: pointer to a sockaddr struct that will get the address from which the data is received
  231. * \param fromlen Optional, only required if 'from' is given: length of from struct
  232. * \return Number of bytes received or SOCKET_ERROR
  233. */
  234. int recvfrom ( char* data, const int buffersize, struct sockaddr* from = NULL, socklen_t* fromlen = NULL) const;
  235. bool set_non_blocking ( const bool );
  236. bool ReadLine (string& line);
  237. bool is_valid() const;
  238. private:
  239. SOCKET _sd; ///< Socket Descriptor
  240. SOCKADDR_IN _sockaddr; ///< Socket Address
  241. enum SocketFamily _family; ///< Socket Address Family
  242. enum SocketProtocol _protocol; ///< Socket Protocol
  243. enum SocketType _type; ///< Socket Type
  244. enum SocketDomain _domain; ///< Socket domain
  245. #ifdef TARGET_WINDOWS
  246. WSADATA _wsaData; ///< Windows Socket data
  247. static int win_usage_count; ///< Internal Windows usage counter used to prevent a global WSACleanup when more than one Socket object is used
  248. #endif
  249. void errormessage( int errornum, const char* functionname = NULL) const;
  250. int getLastError(void) const;
  251. bool osInit();
  252. void osCleanup();
  253. };
  254. } //namespace MPTV