]> git.lizzy.rs Git - minetest-m13.git/blob - src/socket.h
Add Minetest-M13 source
[minetest-m13.git] / src / socket.h
1 /*
2 Minetest-m13
3 Copyright (C) 2010 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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
14
15 You should have received a copy of the GNU 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         #define WIN32_LEAN_AND_MEAN
25         // Without this some of the network functions are not found on mingw
26         #ifndef _WIN32_WINNT
27                 #define _WIN32_WINNT 0x0501
28         #endif
29         #include <windows.h>
30         #include <winsock2.h>
31         #include <ws2tcpip.h>
32         #ifdef _MSC_VER
33                 #pragma comment(lib, "ws2_32.lib")
34         #endif
35 typedef SOCKET socket_t;
36 typedef int socklen_t;
37 #else
38         #include <sys/types.h>
39         #include <sys/socket.h>
40         #include <netinet/in.h>
41         #include <fcntl.h>
42         #include <netdb.h>
43         #include <unistd.h>
44 typedef int socket_t;
45 #endif
46
47 #include <ostream>
48 #include "exceptions.h"
49 #include "constants.h"
50
51 class SocketException : public BaseException
52 {
53 public:
54         SocketException(const char *s):
55                 BaseException(s)
56         {
57         }
58 };
59
60 class ResolveError : public BaseException
61 {
62 public:
63         ResolveError(const char *s):
64                 BaseException(s)
65         {
66         }
67 };
68
69 class SendFailedException : public BaseException
70 {
71 public:
72         SendFailedException(const char *s):
73                 BaseException(s)
74         {
75         }
76 };
77
78 void sockets_init();
79 void sockets_cleanup();
80
81 class Address
82 {
83 public:
84         Address();
85         Address(unsigned int address, unsigned short port);
86         Address(unsigned int a, unsigned int b,
87                         unsigned int c, unsigned int d,
88                         unsigned short port);
89         bool operator==(Address &address);
90         bool operator!=(Address &address);
91         void Resolve(const char *name);
92         unsigned int getAddress() const;
93         unsigned short getPort() const;
94         void setAddress(unsigned int address);
95         void setAddress(unsigned int a, unsigned int b,
96                         unsigned int c, unsigned int d);
97         void setPort(unsigned short port);
98         void print(std::ostream *s) const;
99         void print() const;
100         std::string serializeString() const;
101 private:
102         unsigned int m_address;
103         unsigned short m_port;
104 };
105
106 class UDPSocket
107 {
108 public:
109         UDPSocket();
110         ~UDPSocket();
111         void Bind(unsigned short port);
112         //void Close();
113         //bool IsOpen();
114         void Send(const Address & destination, const void * data, int size);
115         // Returns -1 if there is no data
116         int Receive(Address & sender, void * data, int size);
117         int GetHandle(); // For debugging purposes only
118         void setTimeoutMs(int timeout_ms);
119         // Returns true if there is data, false if timeout occurred
120         bool WaitData(int timeout_ms);
121 private:
122         int m_handle;
123         int m_timeout_ms;
124 };
125
126 #endif
127