3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
24 #define WIN32_LEAN_AND_MEAN
25 // Without this some of the network functions are not found on mingw
27 #define _WIN32_WINNT 0x0501
33 #pragma comment(lib, "wsock32.lib")
35 typedef SOCKET socket_t;
36 typedef int socklen_t;
38 #include <sys/socket.h>
39 #include <netinet/in.h>
47 #include "exceptions.h"
48 #include "constants.h"
50 class SocketException : public BaseException
53 SocketException(const char *s):
59 class ResolveError : public BaseException
62 ResolveError(const char *s):
68 class SendFailedException : public BaseException
71 SendFailedException(const char *s):
78 void sockets_cleanup();
84 Address(unsigned int address, unsigned short port);
85 Address(unsigned int a, unsigned int b,
86 unsigned int c, unsigned int d,
88 bool operator==(Address &address);
89 bool operator!=(Address &address);
90 void Resolve(const char *name);
91 unsigned int getAddress() const;
92 unsigned short getPort() const;
93 void setAddress(unsigned int address);
94 void setAddress(unsigned int a, unsigned int b,
95 unsigned int c, unsigned int d);
96 void setPort(unsigned short port);
97 void print(std::ostream *s) const;
100 unsigned int m_address;
101 unsigned short m_port;
109 void Bind(unsigned short port);
112 void Send(const Address & destination, const void * data, int size);
113 // Returns -1 if there is no data
114 int Receive(Address & sender, void * data, int size);
115 int GetHandle(); // For debugging purposes only
116 void setTimeoutMs(int timeout_ms);
117 // Returns true if there is data, false if timeout occurred
118 bool WaitData(int timeout_ms);