3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #ifndef WIN32_LEAN_AND_MEAN
25 #define WIN32_LEAN_AND_MEAN
28 #define _WIN32_WINNT 0x0501
34 #include <sys/socket.h>
35 #include <netinet/in.h>
40 #include "irrlichttypes.h"
41 #include "exceptions.h"
43 extern bool socket_enable_debug_output;
45 class SocketException : public BaseException
48 SocketException(const std::string &s):
54 class ResolveError : public BaseException
57 ResolveError(const std::string &s):
63 class SendFailedException : public BaseException
66 SendFailedException(const std::string &s):
73 void sockets_cleanup();
75 class IPv6AddressBytes
79 IPv6AddressBytes() { memset(bytes, 0, 16); }
86 Address(u32 address, u16 port);
87 Address(u8 a, u8 b, u8 c, u8 d, u16 port);
88 Address(const IPv6AddressBytes *ipv6_bytes, u16 port);
89 bool operator==(const Address &address);
90 bool operator!=(const Address &address);
91 // Resolve() may throw ResolveError (address is unchanged in this case)
92 void Resolve(const char *name);
93 struct sockaddr_in getAddress() const;
94 unsigned short getPort() const;
95 void setAddress(u32 address);
96 void setAddress(u8 a, u8 b, u8 c, u8 d);
97 void setAddress(const IPv6AddressBytes *ipv6_bytes);
98 struct sockaddr_in6 getAddress6() const;
99 int getFamily() const;
102 void setPort(unsigned short port);
103 void print(std::ostream *s) const;
104 std::string serializeString() const;
106 unsigned int m_addr_family;
109 struct sockaddr_in ipv4;
110 struct sockaddr_in6 ipv6;
112 u16 m_port; // Port is separate from sockaddr structures
119 UDPSocket(bool ipv6);
121 void Bind(Address addr);
123 bool init(bool ipv6, bool noExceptions = false);
127 void Send(const Address & destination, const void * data, int size);
128 // Returns -1 if there is no data
129 int Receive(Address & sender, void * data, int size);
130 int GetHandle(); // For debugging purposes only
131 void setTimeoutMs(int timeout_ms);
132 // Returns true if there is data, false if timeout occurred
133 bool WaitData(int timeout_ms);