summaryrefslogtreecommitdiffstats
path: root/src/OSSupport/Socket.h
blob: 9ec8c1111a0af38a4cba99390cfdfce3d75cd8f8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125

#pragma once





// Windows and MacOSX don't have the MSG_NOSIGNAL flag
#if ( \
	defined(_WIN32) || \
	(defined(__APPLE__) && defined(__MACH__)) \
)
	#define MSG_NOSIGNAL (0)
#endif


#include "Errors.h"


class cSocket
{
public:
	enum eFamily
	{
		IPv4 = AF_INET,
		IPv6 = AF_INET6,
		
		#ifdef _WIN32
		ErrWouldBlock = WSAEWOULDBLOCK,
		#else
		ErrWouldBlock = EWOULDBLOCK,
		#endif
	} ;
	
#ifdef _WIN32
	typedef SOCKET xSocket;
#else
	typedef int xSocket;
	static const int INVALID_SOCKET = -1;
#endif

	cSocket(void) : m_Socket(INVALID_SOCKET) {}
	cSocket(xSocket a_Socket);

	bool IsValid(void) const { return IsValidSocket(m_Socket); }
	void CloseSocket(void);
	
	/** Notifies the socket that we don't expect any more reads nor writes on it.
	Most TCPIP implementations use this to send the FIN flag in a packet */
	void ShutdownReadWrite(void);
	
	operator xSocket(void) const;
	xSocket GetSocket(void) const;
	
	bool operator == (const cSocket & a_Other) {return m_Socket == a_Other.m_Socket; }
	
	void SetSocket(xSocket a_Socket);

	/// Sets the address-reuse socket flag; returns true on success
	bool SetReuseAddress(void);
	
	/// Initializes the network stack. Returns 0 on success, or another number as an error code.
	static int WSAStartup(void);

	static int     GetLastError();
	static AString GetLastErrorString(void)
	{
		return GetOSErrorString(GetLastError());
	}
	
	/// Creates a new socket of the specified address family
	static cSocket CreateSocket(eFamily a_Family);

	inline static bool IsSocketError(int a_ReturnedValue)
	{
		#ifdef _WIN32
			return ((a_ReturnedValue == SOCKET_ERROR) || (a_ReturnedValue == 0));
		#else
			return (a_ReturnedValue <= 0);
		#endif
	}
	
	static bool IsValidSocket(xSocket a_Socket);

	static const unsigned short ANY_PORT = 0;  // When given to Bind() functions, they will find a free port
	static const int DEFAULT_BACKLOG = 10;

	/// Binds to the specified port on "any" interface (0.0.0.0). Returns true if successful.
	bool BindToAnyIPv4(unsigned short a_Port);
	
	/// Binds to the specified port on "any" interface (::/128). Returns true if successful.
	bool BindToAnyIPv6(unsigned short a_Port);
	
	/// Binds to the specified port on localhost interface (127.0.0.1) through IPv4. Returns true if successful.
	bool BindToLocalhostIPv4(unsigned short a_Port);
	
	/// Sets the socket to listen for incoming connections. Returns true if successful.
	bool Listen(int a_Backlog = DEFAULT_BACKLOG);
	
	/// Accepts an IPv4 incoming connection. Blocks if none available.
	cSocket AcceptIPv4(void);
	
	/// Accepts an IPv6 incoming connection. Blocks if none available.
	cSocket AcceptIPv6(void);
	
	/// Connects to a localhost socket on the specified port using IPv4; returns true if successful.
	bool ConnectToLocalhostIPv4(unsigned short a_Port);
	
	/// Connects to the specified host or string IP address and port, using IPv4. Returns true if successful.
	bool ConnectIPv4(const AString & a_HostNameOrAddr, unsigned short a_Port);
	
	int Receive(char * a_Buffer, size_t a_Length, unsigned int a_Flags);
	int Send   (const char * a_Buffer, size_t a_Length);
	
	unsigned short GetPort(void) const;  // Returns 0 on failure

	const AString & GetIPString(void) const { return m_IPString; }
	
	/** Sets the socket into non-blocking mode */
	void SetNonBlocking(void);

private:
	xSocket m_Socket;
	AString m_IPString;
};