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