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