]> git.lizzy.rs Git - minetest.git/blob - src/socket.h
Add output levels --info and --trace (--verbose is now more verbose)
[minetest.git] / src / socket.h
1 /*
2 Minetest-c55
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 #include <ostream>
24 #include "exceptions.h"
25
26 extern bool socket_enable_debug_output;
27
28 class SocketException : public BaseException
29 {
30 public:
31         SocketException(const char *s):
32                 BaseException(s)
33         {
34         }
35 };
36
37 class ResolveError : public BaseException
38 {
39 public:
40         ResolveError(const char *s):
41                 BaseException(s)
42         {
43         }
44 };
45
46 class SendFailedException : public BaseException
47 {
48 public:
49         SendFailedException(const char *s):
50                 BaseException(s)
51         {
52         }
53 };
54
55 void sockets_init();
56 void sockets_cleanup();
57
58 class Address
59 {
60 public:
61         Address();
62         Address(unsigned int address, unsigned short port);
63         Address(unsigned int a, unsigned int b,
64                         unsigned int c, unsigned int d,
65                         unsigned short port);
66         bool operator==(Address &address);
67         bool operator!=(Address &address);
68         void Resolve(const char *name);
69         unsigned int getAddress() const;
70         unsigned short getPort() const;
71         void setAddress(unsigned int address);
72         void setAddress(unsigned int a, unsigned int b,
73                         unsigned int c, unsigned int d);
74         void setPort(unsigned short port);
75         void print(std::ostream *s) const;
76         void print() const;
77         std::string serializeString() const;
78 private:
79         unsigned int m_address;
80         unsigned short m_port;
81 };
82
83 class UDPSocket
84 {
85 public:
86         UDPSocket();
87         ~UDPSocket();
88         void Bind(unsigned short port);
89         //void Close();
90         //bool IsOpen();
91         void Send(const Address & destination, const void * data, int size);
92         // Returns -1 if there is no data
93         int Receive(Address & sender, void * data, int size);
94         int GetHandle(); // For debugging purposes only
95         void setTimeoutMs(int timeout_ms);
96         // Returns true if there is data, false if timeout occurred
97         bool WaitData(int timeout_ms);
98 private:
99         int m_handle;
100         int m_timeout_ms;
101 };
102
103 #endif
104