X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Fsocket.cpp;h=9e289baa2c5f6c2412e63fb422388a01e3d7deec;hb=36747794ab981e5d1cc085075979ef981e783b55;hp=499ee504e0d7e33c3c793577a5f040b2971a8486;hpb=c32da52104cc6bbe8ed0bf1ba1a7874f015cb738;p=minetest.git diff --git a/src/socket.cpp b/src/socket.cpp index 499ee504e..9e289baa2 100644 --- a/src/socket.cpp +++ b/src/socket.cpp @@ -1,32 +1,61 @@ /* -Minetest-c55 -Copyright (C) 2010 celeron55, Perttu Ahola +Minetest +Copyright (C) 2013 celeron55, Perttu Ahola This program is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2 of the License, or +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. +GNU Lesser General Public License for more details. -You should have received a copy of the GNU General Public License along +You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include "socket.h" + +#ifdef _WIN32 + #define WIN32_LEAN_AND_MEAN + // Without this some of the network functions are not found on mingw + #ifndef _WIN32_WINNT + #define _WIN32_WINNT 0x0501 + #endif + #include + #include + #include + #ifdef _MSC_VER + #pragma comment(lib, "ws2_32.lib") + #endif +typedef SOCKET socket_t; +typedef int socklen_t; +#else + #include + #include + #include + #include + #include + #include + #include +typedef int socket_t; +#endif + +#include "constants.h" #include "debug.h" #include #include #include #include -#include "utility.h" +#include "util/string.h" +#include "util/numeric.h" -// Debug printing options -#define DP 0 +bool socket_enable_debug_output = false; +#define DP socket_enable_debug_output +// This is prepended to everything printed here #define DPS "" bool g_sockets_initialized = false; @@ -51,6 +80,8 @@ void sockets_cleanup() Address::Address() { + m_address = 0; + m_port = 0; } Address::Address(unsigned int address, unsigned short port) @@ -93,6 +124,16 @@ void Address::Resolve(const char *name) freeaddrinfo(resolved); } +std::string Address::serializeString() const +{ + unsigned int a, b, c, d; + a = (m_address & 0xFF000000)>>24; + b = (m_address & 0x00FF0000)>>16; + c = (m_address & 0x0000FF00)>>8; + d = (m_address & 0x000000FF); + return itos(a)+"."+itos(b)+"."+itos(c)+"."+itos(d); +} + unsigned int Address::getAddress() const { return m_address; @@ -108,6 +149,12 @@ void Address::setAddress(unsigned int address) m_address = address; } +void Address::setAddress(unsigned int a, unsigned int b, + unsigned int c, unsigned int d) +{ + m_address = (a<<24) | (b<<16) | ( c<<8) | d; +} + void Address::setPort(unsigned short port) { m_port = port; @@ -207,7 +254,8 @@ void UDPSocket::Send(const Address & destination, const void * data, int size) dstream<<", size="<20) dstream<<"..."; @@ -269,7 +317,8 @@ int UDPSocket::Receive(Address & sender, void * data, int size) dstream<<", size="<20) dstream<<"..."; @@ -311,6 +360,9 @@ bool UDPSocket::WaitData(int timeout_ms) <