summaryrefslogtreecommitdiffstats
path: root/src/OSSupport/Socket.h
blob: 34f09cc740f63937fda5a676840c9777a42eeb45 (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

#pragma once





class cSocket
{
public:
	enum eFamily
	{
		IPv4 = AF_INET,
		IPv6 = AF_INET6,
	} ;
	
#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);
	~cSocket();

	bool IsValid(void) const { return IsValidSocket(m_Socket); }
	void CloseSocket(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);
	
	static int WSAStartup(void);

	static AString GetErrorString(int a_ErrNo);
	static int     GetLastError();
	static AString GetLastErrorString(void)
	{
		return GetErrorString(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, unsigned int a_Length, unsigned int a_Flags);
	int Send   (const char * a_Buffer, unsigned int a_Length);
	
	unsigned short GetPort(void) const;  // Returns 0 on failure

	const AString & GetIPString(void) const { return m_IPString; }

private:
	xSocket m_Socket;
	AString m_IPString;
};