]> git.lizzy.rs Git - dragonfireclient.git/blob - src/socket.h
local game connects to 127.0.0.1 instead of localhost (windows returns an ipv6 addres...
[dragonfireclient.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 #ifdef _WIN32
24         #define WIN32_LEAN_AND_MEAN
25         #include <windows.h>
26         #include <winsock2.h>
27         #include <ws2tcpip.h>
28         #pragma comment(lib, "wsock32.lib")
29 typedef SOCKET socket_t;
30 typedef int socklen_t;
31 #else
32         #include <sys/socket.h>
33         #include <netinet/in.h>
34         #include <fcntl.h>
35         #include <netdb.h>
36         #include <unistd.h>
37 typedef int socket_t;
38 #endif
39
40 #include <ostream>
41 #include "exceptions.h"
42 #include "constants.h"
43
44 class SocketException : public BaseException
45 {
46 public:
47         SocketException(const char *s):
48                 BaseException(s)
49         {
50         }
51 };
52
53 class ResolveError : public BaseException
54 {
55 public:
56         ResolveError(const char *s):
57                 BaseException(s)
58         {
59         }
60 };
61
62 class SendFailedException : public BaseException
63 {
64 public:
65         SendFailedException(const char *s):
66                 BaseException(s)
67         {
68         }
69 };
70
71 void sockets_init();
72 void sockets_cleanup();
73
74 class Address
75 {
76 public:
77         Address();
78         Address(unsigned int address, unsigned short port);
79         Address(unsigned int a, unsigned int b,
80                         unsigned int c, unsigned int d,
81                         unsigned short port);
82         bool operator==(Address &address);
83         bool operator!=(Address &address);
84         void Resolve(const char *name);
85         unsigned int getAddress() const;
86         unsigned short getPort() const;
87         void setAddress(unsigned int address);
88         void setAddress(unsigned int a, unsigned int b,
89                         unsigned int c, unsigned int d);
90         void setPort(unsigned short port);
91         void print(std::ostream *s) const;
92         void print() const;
93 private:
94         unsigned int m_address;
95         unsigned short m_port;
96 };
97
98 class UDPSocket
99 {
100 public:
101         UDPSocket();
102         ~UDPSocket();
103         void Bind(unsigned short port);
104         //void Close();
105         //bool IsOpen();
106         void Send(const Address & destination, const void * data, int size);
107         // Returns -1 if there is no data
108         int Receive(Address & sender, void * data, int size);
109         int GetHandle(); // For debugging purposes only
110         void setTimeoutMs(int timeout_ms);
111         // Returns true if there is data, false if timeout occurred
112         bool WaitData(int timeout_ms);
113 private:
114         int m_handle;
115         int m_timeout_ms;
116 };
117
118 #endif
119