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