Line data Source code
1 : /*
2 : Minetest
3 : Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 :
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.
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 Lesser General Public License for more details.
14 :
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.
18 : */
19 :
20 : #ifndef SOCKET_HEADER
21 : #define SOCKET_HEADER
22 :
23 : #ifdef _WIN32
24 : #ifndef WIN32_LEAN_AND_MEAN
25 : #define WIN32_LEAN_AND_MEAN
26 : #endif
27 : #ifndef _WIN32_WINNT
28 : #define _WIN32_WINNT 0x0501
29 : #endif
30 : #include <windows.h>
31 : #include <winsock2.h>
32 : #include <ws2tcpip.h>
33 : #else
34 : #include <sys/socket.h>
35 : #include <netinet/in.h>
36 : #endif
37 :
38 : #include <ostream>
39 : #include <string.h>
40 : #include "irrlichttypes.h"
41 : #include "exceptions.h"
42 :
43 : extern bool socket_enable_debug_output;
44 :
45 0 : class SocketException : public BaseException
46 : {
47 : public:
48 0 : SocketException(const char *s):
49 0 : BaseException(s)
50 : {
51 0 : }
52 : };
53 :
54 0 : class ResolveError : public BaseException
55 : {
56 : public:
57 0 : ResolveError(const char *s):
58 0 : BaseException(s)
59 : {
60 0 : }
61 : };
62 :
63 0 : class SendFailedException : public BaseException
64 : {
65 : public:
66 0 : SendFailedException(const char *s):
67 0 : BaseException(s)
68 : {
69 0 : }
70 : };
71 :
72 : void sockets_init();
73 : void sockets_cleanup();
74 :
75 : class IPv6AddressBytes
76 : {
77 : public:
78 : u8 bytes[16];
79 0 : IPv6AddressBytes() { memset(bytes, 0, 16); }
80 : };
81 :
82 : class Address
83 : {
84 : public:
85 : Address();
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;
100 : bool isIPv6() const;
101 : bool isZero() const;
102 : void setPort(unsigned short port);
103 : void print(std::ostream *s) const;
104 : std::string serializeString() const;
105 : private:
106 : unsigned int m_addr_family;
107 : union
108 : {
109 : struct sockaddr_in ipv4;
110 : struct sockaddr_in6 ipv6;
111 : } m_address;
112 : u16 m_port; // Port is separate from sockaddr structures
113 : };
114 :
115 : class UDPSocket
116 : {
117 : public:
118 0 : UDPSocket() { }
119 : UDPSocket(bool ipv6);
120 : ~UDPSocket();
121 : void Bind(Address addr);
122 :
123 : bool init(bool ipv6, bool noExceptions = false);
124 :
125 : //void Close();
126 : //bool IsOpen();
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);
134 : private:
135 : int m_handle;
136 : int m_timeout_ms;
137 : int m_addr_family;
138 : };
139 :
140 : #endif
141 :
|