serverlist.cpp
serverobject.cpp
settings.cpp
- socket.cpp
sound.cpp
staticobject.cpp
subgame.cpp
#include <IFileSystem.h>
#include "client.h"
#include "network/clientopcodes.h"
+#include "network/connection.h"
#include "network/networkpacket.h"
#include "threading/mutex_auto_lock.h"
#include "client/renderingengine.h"
tsrc, this
),
m_particle_manager(&m_env),
- m_con(PROTOCOL_ID, 512, CONNECTION_TIMEOUT, ipv6, this),
+ m_con(new con::Connection(PROTOCOL_ID, 512, CONNECTION_TIMEOUT, ipv6, this)),
m_address_name(address_name),
m_server_ser_ver(SER_FMT_VER_INVALID),
m_last_chat_message_sent(time(NULL)),
Client::~Client()
{
m_shutdown = true;
- m_con.Disconnect();
+ m_con->Disconnect();
m_mesh_update_thread.stop();
m_mesh_update_thread.wait();
initLocalMapSaving(address, m_address_name, is_local_server);
- m_con.SetTimeoutMs(0);
- m_con.Connect(address);
+ m_con->SetTimeoutMs(0);
+ m_con->Connect(address);
}
void Client::step(float dtime)
{
DSTACK(FUNCTION_NAME);
NetworkPacket pkt;
- m_con.Receive(&pkt);
+ m_con->Receive(&pkt);
ProcessData(&pkt);
}
void Client::Send(NetworkPacket* pkt)
{
- m_con.Send(PEER_ID_SERVER,
+ m_con->Send(PEER_ID_SERVER,
serverCommandFactoryTable[pkt->getCommand()].channel,
pkt,
serverCommandFactoryTable[pkt->getCommand()].reliable);
u16 our_peer_id;
{
//MutexAutoLock lock(m_con_mutex); //bulk comment-out
- our_peer_id = m_con.GetPeerID();
+ our_peer_id = m_con->GetPeerID();
}
// Set peer id if not set already
if(myplayer == NULL)
return;
- u16 our_peer_id = m_con.GetPeerID();
+ u16 our_peer_id = m_con->GetPeerID();
// Set peer id if not set already
if(myplayer->peer_id == PEER_ID_INEXISTENT)
return event;
}
+bool Client::connectedToServer()
+{
+ return m_con->Connected();
+}
+
+const Address Client::getServerAddress()
+{
+ return m_con->GetPeerAddress(PEER_ID_SERVER);
+}
+
float Client::mediaReceiveProgress()
{
if (m_media_downloader)
float Client::getRTT()
{
- return m_con.getPeerStat(PEER_ID_SERVER,con::AVG_RTT);
+ return m_con->getPeerStat(PEER_ID_SERVER,con::AVG_RTT);
}
float Client::getCurRate()
{
- return (m_con.getLocalStat(con::CUR_INC_RATE) +
- m_con.getLocalStat(con::CUR_DL_RATE));
+ return (m_con->getLocalStat(con::CUR_INC_RATE) +
+ m_con->getLocalStat(con::CUR_DL_RATE));
}
void Client::makeScreenshot()
#pragma once
-#include "network/connection.h"
#include "clientenvironment.h"
#include "irrlichttypes_extrabloated.h"
#include <ostream>
#include "mapnode.h"
#include "tileanimation.h"
#include "mesh_generator_thread.h"
+#include "network/address.h"
+#include "network/peerhandler.h"
#include <fstream>
#define CLIENT_CHAT_MESSAGE_LIMIT_PER_10S 10.0f
struct MinimapMapblock;
class Camera;
class NetworkPacket;
+namespace con {
+class Connection;
+}
enum LocalClientState {
LC_Created,
u8 getProtoVersion()
{ return m_proto_ver; }
- bool connectedToServer()
- { return m_con.Connected(); }
+ bool connectedToServer();
float mediaReceiveProgress();
void showGameFog(bool show = true);
void showGameDebug(bool show = true);
- const Address getServerAddress()
- {
- return m_con.GetPeerAddress(PEER_ID_SERVER);
- }
+ const Address getServerAddress();
const std::string &getAddressName() const
{
MeshUpdateThread m_mesh_update_thread;
ClientEnvironment m_env;
ParticleManager m_particle_manager;
- con::Connection m_con;
+ std::unique_ptr<con::Connection> m_con;
std::string m_address_name;
Camera *m_camera = nullptr;
Minimap *m_minimap = nullptr;
#include "clientlauncher.h"
#include "version.h"
#include "renderingengine.h"
+#include "network/networkexceptions.h"
/* mainmenumanager.h
*/
return porting::getTimeS() - m_connection_time;
}
-ClientInterface::ClientInterface(con::Connection* con)
+ClientInterface::ClientInterface(const std::shared_ptr<con::Connection> & con)
:
m_con(con),
m_env(NULL),
friend class Server;
- ClientInterface(con::Connection* con);
+ ClientInterface(const std::shared_ptr<con::Connection> &con);
~ClientInterface();
/* run sync step */
void UpdatePlayerList();
// Connection
- con::Connection* m_con;
+ std::shared_ptr<con::Connection> m_con;
std::mutex m_clients_mutex;
// Connected clients (behind the con mutex)
RemoteClientMap m_clients;
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
-#include "socket.h" // for select()
-#include "porting.h" // for sleep_ms(), get_sysinfo(), secure_rand_fill_buf()
#include "httpfetch.h"
+#include "porting.h" // for sleep_ms(), get_sysinfo(), secure_rand_fill_buf()
#include <iostream>
#include <sstream>
#include <list>
#include <map>
#include <cerrno>
#include <mutex>
+#include "network/socket.h" // for select()
#include "threading/event.h"
#include "config.h"
#include "exceptions.h"
#include "config.h"
#include "player.h"
#include "porting.h"
+#include "network/socket.h"
#if USE_CURSES
#include "terminal_chat_console.h"
#endif
set(common_network_SRCS
+ ${CMAKE_CURRENT_SOURCE_DIR}/address.cpp
${CMAKE_CURRENT_SOURCE_DIR}/connection.cpp
${CMAKE_CURRENT_SOURCE_DIR}/networkpacket.cpp
${CMAKE_CURRENT_SOURCE_DIR}/serverpackethandler.cpp
${CMAKE_CURRENT_SOURCE_DIR}/serveropcodes.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/socket.cpp
PARENT_SCOPE
)
--- /dev/null
+/*
+Minetest
+Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
+
+This program is free software; you can redistribute it and/or modify
+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 Lesser General Public License for more details.
+
+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 "address.h"
+
+#include <cstdio>
+#include <iostream>
+#include <cstdlib>
+#include <cstring>
+#include <cerrno>
+#include <sstream>
+#include <iomanip>
+#include "network/networkexceptions.h"
+#include "util/string.h"
+#include "util/numeric.h"
+#include "constants.h"
+#include "debug.h"
+#include "settings.h"
+#include "log.h"
+
+#ifdef _WIN32
+// Without this some of the network functions are not found on mingw
+#ifndef _WIN32_WINNT
+#define _WIN32_WINNT 0x0501
+#endif
+#include <windows.h>
+#include <winsock2.h>
+#include <ws2tcpip.h>
+#define LAST_SOCKET_ERR() WSAGetLastError()
+typedef SOCKET socket_t;
+typedef int socklen_t;
+#else
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <fcntl.h>
+#include <netdb.h>
+#include <unistd.h>
+#include <arpa/inet.h>
+#define LAST_SOCKET_ERR() (errno)
+typedef int socket_t;
+#endif
+
+/*
+ Address
+*/
+
+Address::Address()
+{
+ memset(&m_address, 0, sizeof(m_address));
+}
+
+Address::Address(u32 address, u16 port)
+{
+ memset(&m_address, 0, sizeof(m_address));
+ setAddress(address);
+ setPort(port);
+}
+
+Address::Address(u8 a, u8 b, u8 c, u8 d, u16 port)
+{
+ memset(&m_address, 0, sizeof(m_address));
+ setAddress(a, b, c, d);
+ setPort(port);
+}
+
+Address::Address(const IPv6AddressBytes *ipv6_bytes, u16 port)
+{
+ memset(&m_address, 0, sizeof(m_address));
+ setAddress(ipv6_bytes);
+ setPort(port);
+}
+
+// Equality (address family, address and port must be equal)
+bool Address::operator==(const Address &address)
+{
+ if (address.m_addr_family != m_addr_family || address.m_port != m_port)
+ return false;
+
+ if (m_addr_family == AF_INET) {
+ return m_address.ipv4.sin_addr.s_addr ==
+ address.m_address.ipv4.sin_addr.s_addr;
+ }
+
+ if (m_addr_family == AF_INET6) {
+ return memcmp(m_address.ipv6.sin6_addr.s6_addr,
+ address.m_address.ipv6.sin6_addr.s6_addr, 16) == 0;
+ }
+
+ return false;
+}
+
+bool Address::operator!=(const Address &address)
+{
+ return !(*this == address);
+}
+
+void Address::Resolve(const char *name)
+{
+ if (!name || name[0] == 0) {
+ if (m_addr_family == AF_INET) {
+ setAddress((u32)0);
+ } else if (m_addr_family == AF_INET6) {
+ setAddress((IPv6AddressBytes *)0);
+ }
+ return;
+ }
+
+ struct addrinfo *resolved, hints;
+ memset(&hints, 0, sizeof(hints));
+
+ // Setup hints
+ hints.ai_socktype = 0;
+ hints.ai_protocol = 0;
+ hints.ai_flags = 0;
+ if (g_settings->getBool("enable_ipv6")) {
+ // AF_UNSPEC allows both IPv6 and IPv4 addresses to be returned
+ hints.ai_family = AF_UNSPEC;
+ } else {
+ hints.ai_family = AF_INET;
+ }
+
+ // Do getaddrinfo()
+ int e = getaddrinfo(name, NULL, &hints, &resolved);
+ if (e != 0)
+ throw ResolveError(gai_strerror(e));
+
+ // Copy data
+ if (resolved->ai_family == AF_INET) {
+ struct sockaddr_in *t = (struct sockaddr_in *)resolved->ai_addr;
+ m_addr_family = AF_INET;
+ m_address.ipv4 = *t;
+ } else if (resolved->ai_family == AF_INET6) {
+ struct sockaddr_in6 *t = (struct sockaddr_in6 *)resolved->ai_addr;
+ m_addr_family = AF_INET6;
+ m_address.ipv6 = *t;
+ } else {
+ freeaddrinfo(resolved);
+ throw ResolveError("");
+ }
+ freeaddrinfo(resolved);
+}
+
+// IP address -> textual representation
+std::string Address::serializeString() const
+{
+// windows XP doesnt have inet_ntop, maybe use better func
+#ifdef _WIN32
+ if (m_addr_family == AF_INET) {
+ u8 a, b, c, d;
+ u32 addr;
+ addr = ntohl(m_address.ipv4.sin_addr.s_addr);
+ a = (addr & 0xFF000000) >> 24;
+ b = (addr & 0x00FF0000) >> 16;
+ c = (addr & 0x0000FF00) >> 8;
+ d = (addr & 0x000000FF);
+ return itos(a) + "." + itos(b) + "." + itos(c) + "." + itos(d);
+ } else if (m_addr_family == AF_INET6) {
+ std::ostringstream os;
+ for (int i = 0; i < 16; i += 2) {
+ u16 section = (m_address.ipv6.sin6_addr.s6_addr[i] << 8) |
+ (m_address.ipv6.sin6_addr.s6_addr[i + 1]);
+ os << std::hex << section;
+ if (i < 14)
+ os << ":";
+ }
+ return os.str();
+ } else
+ return std::string("");
+#else
+ char str[INET6_ADDRSTRLEN];
+ if (inet_ntop(m_addr_family,
+ (m_addr_family == AF_INET)
+ ? (void *)&(m_address.ipv4.sin_addr)
+ : (void *)&(m_address.ipv6.sin6_addr),
+ str, INET6_ADDRSTRLEN) == NULL) {
+ return std::string("");
+ }
+ return std::string(str);
+#endif
+}
+
+struct sockaddr_in Address::getAddress() const
+{
+ return m_address.ipv4; // NOTE: NO PORT INCLUDED, use getPort()
+}
+
+struct sockaddr_in6 Address::getAddress6() const
+{
+ return m_address.ipv6; // NOTE: NO PORT INCLUDED, use getPort()
+}
+
+u16 Address::getPort() const
+{
+ return m_port;
+}
+
+int Address::getFamily() const
+{
+ return m_addr_family;
+}
+
+bool Address::isIPv6() const
+{
+ return m_addr_family == AF_INET6;
+}
+
+bool Address::isZero() const
+{
+ if (m_addr_family == AF_INET) {
+ return m_address.ipv4.sin_addr.s_addr == 0;
+ }
+
+ if (m_addr_family == AF_INET6) {
+ static const char zero[16] = {0};
+ return memcmp(m_address.ipv6.sin6_addr.s6_addr, zero, 16) == 0;
+ }
+ return false;
+}
+
+void Address::setAddress(u32 address)
+{
+ m_addr_family = AF_INET;
+ m_address.ipv4.sin_family = AF_INET;
+ m_address.ipv4.sin_addr.s_addr = htonl(address);
+}
+
+void Address::setAddress(u8 a, u8 b, u8 c, u8 d)
+{
+ m_addr_family = AF_INET;
+ m_address.ipv4.sin_family = AF_INET;
+ u32 addr = htonl((a << 24) | (b << 16) | (c << 8) | d);
+ m_address.ipv4.sin_addr.s_addr = addr;
+}
+
+void Address::setAddress(const IPv6AddressBytes *ipv6_bytes)
+{
+ m_addr_family = AF_INET6;
+ m_address.ipv6.sin6_family = AF_INET6;
+ if (ipv6_bytes)
+ memcpy(m_address.ipv6.sin6_addr.s6_addr, ipv6_bytes->bytes, 16);
+ else
+ memset(m_address.ipv6.sin6_addr.s6_addr, 0, 16);
+}
+
+void Address::setPort(u16 port)
+{
+ m_port = port;
+}
+
+void Address::print(std::ostream *s) const
+{
+ if (m_addr_family == AF_INET6)
+ *s << "[" << serializeString() << "]:" << m_port;
+ else
+ *s << serializeString() << ":" << m_port;
+}
--- /dev/null
+/*
+Minetest
+Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
+
+This program is free software; you can redistribute it and/or modify
+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 Lesser General Public License for more details.
+
+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.
+*/
+
+#pragma once
+
+#ifdef _WIN32
+#ifndef _WIN32_WINNT
+#define _WIN32_WINNT 0x0501
+#endif
+#include <windows.h>
+#include <winsock2.h>
+#include <ws2tcpip.h>
+#else
+#include <netinet/in.h>
+#endif
+
+#include <ostream>
+#include <cstring>
+#include "irrlichttypes.h"
+#include "networkexceptions.h"
+
+class IPv6AddressBytes
+{
+public:
+ u8 bytes[16];
+ IPv6AddressBytes() { memset(bytes, 0, 16); }
+};
+
+class Address
+{
+public:
+ Address();
+ Address(u32 address, u16 port);
+ Address(u8 a, u8 b, u8 c, u8 d, u16 port);
+ Address(const IPv6AddressBytes *ipv6_bytes, u16 port);
+ bool operator==(const Address &address);
+ bool operator!=(const Address &address);
+ // Resolve() may throw ResolveError (address is unchanged in this case)
+ void Resolve(const char *name);
+ struct sockaddr_in getAddress() const;
+ unsigned short getPort() const;
+ void setAddress(u32 address);
+ void setAddress(u8 a, u8 b, u8 c, u8 d);
+ void setAddress(const IPv6AddressBytes *ipv6_bytes);
+ struct sockaddr_in6 getAddress6() const;
+ int getFamily() const;
+ bool isIPv6() const;
+ bool isZero() const;
+ void setPort(unsigned short port);
+ void print(std::ostream *s) const;
+ std::string serializeString() const;
+
+private:
+ unsigned int m_addr_family = 0;
+ union
+ {
+ struct sockaddr_in ipv4;
+ struct sockaddr_in6 ipv6;
+ } m_address;
+ u16 m_port = 0; // Port is separate from sockaddr structures
+};
#include "server.h"
#include "util/strfnd.h"
#include "network/clientopcodes.h"
+#include "network/connection.h"
#include "script/scripting_client.h"
#include "util/serialize.h"
#include "util/srp.h"
m_chosen_auth_mech = AUTH_MECHANISM_NONE;
m_access_denied = true;
m_access_denied_reason = "Unknown";
- m_con.Disconnect();
+ m_con->Disconnect();
}
}
#include "log.h"
#include "porting.h"
#include "network/networkpacket.h"
+#include "network/peerhandler.h"
#include "util/serialize.h"
#include "util/numeric.h"
#include "util/string.h"
#pragma once
#include "irrlichttypes_bloated.h"
+#include "peerhandler.h"
#include "socket.h"
-#include "exceptions.h"
#include "constants.h"
#include "util/pointer.h"
#include "util/container.h"
namespace con
{
-/*
- Exceptions
-*/
-class NotFoundException : public BaseException
-{
-public:
- NotFoundException(const char *s):
- BaseException(s)
- {}
-};
-
-class PeerNotFoundException : public BaseException
-{
-public:
- PeerNotFoundException(const char *s):
- BaseException(s)
- {}
-};
-
-class ConnectionException : public BaseException
-{
-public:
- ConnectionException(const char *s):
- BaseException(s)
- {}
-};
-
-class ConnectionBindFailed : public BaseException
-{
-public:
- ConnectionBindFailed(const char *s):
- BaseException(s)
- {}
-};
-
-class InvalidIncomingDataException : public BaseException
-{
-public:
- InvalidIncomingDataException(const char *s):
- BaseException(s)
- {}
-};
-
-class InvalidOutgoingDataException : public BaseException
-{
-public:
- InvalidOutgoingDataException(const char *s):
- BaseException(s)
- {}
-};
-
-class NoIncomingDataException : public BaseException
-{
-public:
- NoIncomingDataException(const char *s):
- BaseException(s)
- {}
-};
-
-class ProcessedSilentlyException : public BaseException
-{
-public:
- ProcessedSilentlyException(const char *s):
- BaseException(s)
- {}
-};
-
-class ProcessedQueued : public BaseException
-{
-public:
- ProcessedQueued(const char *s):
- BaseException(s)
- {}
-};
-
-class IncomingDataCorruption : public BaseException
-{
-public:
- IncomingDataCorruption(const char *s):
- BaseException(s)
- {}
-};
-
typedef enum MTProtocols {
MTP_PRIMARY,
MTP_UDP,
class Peer;
-enum PeerChangeType
-{
- PEER_ADDED,
- PEER_REMOVED
-};
-struct PeerChange
-{
- PeerChange(PeerChangeType t, u16 _peer_id, bool _timeout):
- type(t), peer_id(_peer_id), timeout(_timeout) {}
- PeerChange() = delete;
-
- PeerChangeType type;
- u16 peer_id;
- bool timeout;
-};
-
-class PeerHandler
-{
-public:
-
- PeerHandler() = default;
- virtual ~PeerHandler() = default;
-
- /*
- This is called after the Peer has been inserted into the
- Connection's peer container.
- */
- virtual void peerAdded(Peer *peer) = 0;
- /*
- This is called before the Peer has been removed from the
- Connection's peer container.
- */
- virtual void deletingPeer(Peer *peer, bool timeout) = 0;
-};
-
class PeerHelper
{
public:
class Connection;
-typedef enum {
- MIN_RTT,
- MAX_RTT,
- AVG_RTT,
- MIN_JITTER,
- MAX_JITTER,
- AVG_JITTER
-} rtt_stat_type;
-
typedef enum {
CUR_DL_RATE,
AVG_DL_RATE,
Connection *m_connection = nullptr;
};
+class PeerHandler;
+
class Connection
{
public:
--- /dev/null
+/*
+Minetest
+Copyright (C) 2013-2017 celeron55, Perttu Ahola <celeron55@gmail.com>
+
+This program is free software; you can redistribute it and/or modify
+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 Lesser General Public License for more details.
+
+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.
+*/
+
+#pragma once
+
+#include "exceptions.h"
+
+namespace con
+{
+/*
+ Exceptions
+*/
+class NotFoundException : public BaseException
+{
+public:
+ NotFoundException(const char *s) : BaseException(s) {}
+};
+
+class PeerNotFoundException : public BaseException
+{
+public:
+ PeerNotFoundException(const char *s) : BaseException(s) {}
+};
+
+class ConnectionException : public BaseException
+{
+public:
+ ConnectionException(const char *s) : BaseException(s) {}
+};
+
+class ConnectionBindFailed : public BaseException
+{
+public:
+ ConnectionBindFailed(const char *s) : BaseException(s) {}
+};
+
+class InvalidIncomingDataException : public BaseException
+{
+public:
+ InvalidIncomingDataException(const char *s) : BaseException(s) {}
+};
+
+class InvalidOutgoingDataException : public BaseException
+{
+public:
+ InvalidOutgoingDataException(const char *s) : BaseException(s) {}
+};
+
+class NoIncomingDataException : public BaseException
+{
+public:
+ NoIncomingDataException(const char *s) : BaseException(s) {}
+};
+
+class ProcessedSilentlyException : public BaseException
+{
+public:
+ ProcessedSilentlyException(const char *s) : BaseException(s) {}
+};
+
+class ProcessedQueued : public BaseException
+{
+public:
+ ProcessedQueued(const char *s) : BaseException(s) {}
+};
+
+class IncomingDataCorruption : public BaseException
+{
+public:
+ IncomingDataCorruption(const char *s) : BaseException(s) {}
+};
+}
+
+class SocketException : public BaseException
+{
+public:
+ SocketException(const std::string &s) : BaseException(s) {}
+};
+
+class ResolveError : public BaseException
+{
+public:
+ ResolveError(const std::string &s) : BaseException(s) {}
+};
+
+class SendFailedException : public BaseException
+{
+public:
+ SendFailedException(const std::string &s) : BaseException(s) {}
+};
\ No newline at end of file
#include "networkpacket.h"
#include <sstream>
-#include "exceptions.h"
+#include "networkexceptions.h"
#include "util/serialize.h"
NetworkPacket::NetworkPacket(u16 command, u32 datasize, u16 peer_id):
{
public:
- NetworkPacket(u16 command, u32 datasize, u16 peer_id);
- NetworkPacket(u16 command, u32 datasize);
- NetworkPacket() = default;
+ NetworkPacket(u16 command, u32 datasize, u16 peer_id);
+ NetworkPacket(u16 command, u32 datasize);
+ NetworkPacket() = default;
- ~NetworkPacket();
+ ~NetworkPacket();
- void putRawPacket(u8 *data, u32 datasize, u16 peer_id);
+ void putRawPacket(u8 *data, u32 datasize, u16 peer_id);
- // Getters
- u32 getSize() { return m_datasize; }
- u16 getPeerId() { return m_peer_id; }
- u16 getCommand() { return m_command; }
- const u32 getRemainingBytes() const { return m_datasize - m_read_offset; }
- const char* getRemainingString() { return getString(m_read_offset); }
+ // Getters
+ u32 getSize() { return m_datasize; }
+ u16 getPeerId() { return m_peer_id; }
+ u16 getCommand() { return m_command; }
+ const u32 getRemainingBytes() const { return m_datasize - m_read_offset; }
+ const char *getRemainingString() { return getString(m_read_offset); }
- // Returns a c-string without copying.
- // A better name for this would be getRawString()
- const char* getString(u32 from_offset);
- // major difference to putCString(): doesn't write len into the buffer
- void putRawString(const char* src, u32 len);
- void putRawString(const std::string &src)
- { putRawString(src.c_str(), src.size()); }
+ // Returns a c-string without copying.
+ // A better name for this would be getRawString()
+ const char *getString(u32 from_offset);
+ // major difference to putCString(): doesn't write len into the buffer
+ void putRawString(const char *src, u32 len);
+ void putRawString(const std::string &src)
+ {
+ putRawString(src.c_str(), src.size());
+ }
- NetworkPacket& operator>>(std::string& dst);
- NetworkPacket& operator<<(const std::string &src);
+ NetworkPacket &operator>>(std::string &dst);
+ NetworkPacket &operator<<(const std::string &src);
- void putLongString(const std::string &src);
+ void putLongString(const std::string &src);
- NetworkPacket& operator>>(std::wstring& dst);
- NetworkPacket& operator<<(const std::wstring &src);
+ NetworkPacket &operator>>(std::wstring &dst);
+ NetworkPacket &operator<<(const std::wstring &src);
- std::string readLongString();
+ std::string readLongString();
- char getChar(u32 offset);
- NetworkPacket& operator>>(char& dst);
- NetworkPacket& operator<<(char src);
+ char getChar(u32 offset);
+ NetworkPacket &operator>>(char &dst);
+ NetworkPacket &operator<<(char src);
- NetworkPacket& operator>>(bool& dst);
- NetworkPacket& operator<<(bool src);
+ NetworkPacket &operator>>(bool &dst);
+ NetworkPacket &operator<<(bool src);
- u8 getU8(u32 offset);
+ u8 getU8(u32 offset);
- NetworkPacket& operator>>(u8& dst);
- NetworkPacket& operator<<(u8 src);
+ NetworkPacket &operator>>(u8 &dst);
+ NetworkPacket &operator<<(u8 src);
- u8* getU8Ptr(u32 offset);
+ u8 *getU8Ptr(u32 offset);
- u16 getU16(u32 from_offset);
- NetworkPacket& operator>>(u16& dst);
- NetworkPacket& operator<<(u16 src);
+ u16 getU16(u32 from_offset);
+ NetworkPacket &operator>>(u16 &dst);
+ NetworkPacket &operator<<(u16 src);
- NetworkPacket& operator>>(u32& dst);
- NetworkPacket& operator<<(u32 src);
+ NetworkPacket &operator>>(u32 &dst);
+ NetworkPacket &operator<<(u32 src);
- NetworkPacket& operator>>(u64& dst);
- NetworkPacket& operator<<(u64 src);
+ NetworkPacket &operator>>(u64 &dst);
+ NetworkPacket &operator<<(u64 src);
- NetworkPacket& operator>>(std::time_t& dst);
- NetworkPacket& operator<<(std::time_t src);
+ NetworkPacket &operator>>(std::time_t &dst);
+ NetworkPacket &operator<<(std::time_t src);
- NetworkPacket& operator>>(float& dst);
- NetworkPacket& operator<<(float src);
+ NetworkPacket &operator>>(float &dst);
+ NetworkPacket &operator<<(float src);
- NetworkPacket& operator>>(v2f& dst);
- NetworkPacket& operator<<(v2f src);
+ NetworkPacket &operator>>(v2f &dst);
+ NetworkPacket &operator<<(v2f src);
- NetworkPacket& operator>>(v3f& dst);
- NetworkPacket& operator<<(v3f src);
+ NetworkPacket &operator>>(v3f &dst);
+ NetworkPacket &operator<<(v3f src);
- NetworkPacket& operator>>(s16& dst);
- NetworkPacket& operator<<(s16 src);
+ NetworkPacket &operator>>(s16 &dst);
+ NetworkPacket &operator<<(s16 src);
- NetworkPacket& operator>>(s32& dst);
- NetworkPacket& operator<<(s32 src);
+ NetworkPacket &operator>>(s32 &dst);
+ NetworkPacket &operator<<(s32 src);
- NetworkPacket& operator>>(v2s32& dst);
- NetworkPacket& operator<<(v2s32 src);
+ NetworkPacket &operator>>(v2s32 &dst);
+ NetworkPacket &operator<<(v2s32 src);
- NetworkPacket& operator>>(v3s16& dst);
- NetworkPacket& operator<<(v3s16 src);
+ NetworkPacket &operator>>(v3s16 &dst);
+ NetworkPacket &operator<<(v3s16 src);
- NetworkPacket& operator>>(v3s32& dst);
- NetworkPacket& operator<<(v3s32 src);
+ NetworkPacket &operator>>(v3s32 &dst);
+ NetworkPacket &operator<<(v3s32 src);
- NetworkPacket& operator>>(video::SColor& dst);
- NetworkPacket& operator<<(video::SColor src);
+ NetworkPacket &operator>>(video::SColor &dst);
+ NetworkPacket &operator<<(video::SColor src);
+
+ // Temp, we remove SharedBuffer when migration finished
+ Buffer<u8> oldForgePacket();
- // Temp, we remove SharedBuffer when migration finished
- Buffer<u8> oldForgePacket();
private:
- void checkReadOffset(u32 from_offset, u32 field_size);
-
- inline void checkDataSize(u32 field_size)
- {
- if (m_read_offset + field_size > m_datasize) {
- m_datasize = m_read_offset + field_size;
- m_data.resize(m_datasize);
- }
+ void checkReadOffset(u32 from_offset, u32 field_size);
+
+ inline void checkDataSize(u32 field_size)
+ {
+ if (m_read_offset + field_size > m_datasize) {
+ m_datasize = m_read_offset + field_size;
+ m_data.resize(m_datasize);
}
+ }
- std::vector<u8> m_data;
- u32 m_datasize = 0;
- u32 m_read_offset = 0;
- u16 m_command = 0;
- u16 m_peer_id = 0;
+ std::vector<u8> m_data;
+ u32 m_datasize = 0;
+ u32 m_read_offset = 0;
+ u16 m_command = 0;
+ u16 m_peer_id = 0;
};
--- /dev/null
+/*
+Minetest
+Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
+
+This program is free software; you can redistribute it and/or modify
+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 Lesser General Public License for more details.
+
+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.
+*/
+
+#pragma once
+
+namespace con
+{
+
+typedef enum {
+ MIN_RTT,
+ MAX_RTT,
+ AVG_RTT,
+ MIN_JITTER,
+ MAX_JITTER,
+ AVG_JITTER
+} rtt_stat_type;
+
+class Peer;
+
+class PeerHandler
+{
+public:
+ PeerHandler() = default;
+
+ virtual ~PeerHandler() = default;
+
+ /*
+ This is called after the Peer has been inserted into the
+ Connection's peer container.
+ */
+ virtual void peerAdded(Peer *peer) = 0;
+
+ /*
+ This is called before the Peer has been removed from the
+ Connection's peer container.
+ */
+ virtual void deletingPeer(Peer *peer, bool timeout) = 0;
+};
+
+enum PeerChangeType
+{
+ PEER_ADDED,
+ PEER_REMOVED
+};
+struct PeerChange
+{
+ PeerChange(PeerChangeType t, u16 _peer_id, bool _timeout)
+ : type(t), peer_id(_peer_id), timeout(_timeout)
+ {
+ }
+ PeerChange() = delete;
+
+ PeerChangeType type;
+ u16 peer_id;
+ bool timeout;
+};
+}
#include "settings.h"
#include "tool.h"
#include "version.h"
+#include "network/connection.h"
#include "network/networkprotocol.h"
#include "network/serveropcodes.h"
#include "util/auth.h"
actionstream
<< "TOSERVER_CLIENT_READY stage 2 client init failed for peer_id: "
<< peer_id << std::endl;
- m_con.DisconnectPeer(peer_id);
+ m_con->DisconnectPeer(peer_id);
return;
}
errorstream
<< "TOSERVER_CLIENT_READY client sent inconsistent data, disconnecting peer_id: "
<< peer_id << std::endl;
- m_con.DisconnectPeer(peer_id);
+ m_con->DisconnectPeer(peer_id);
return;
}
errorstream << "Server::ProcessData(): Canceling: "
"No player for peer_id=" << pkt->getPeerId()
<< " disconnecting peer!" << std::endl;
- m_con.DisconnectPeer(pkt->getPeerId());
+ m_con->DisconnectPeer(pkt->getPeerId());
return;
}
errorstream << "Server::ProcessData(): Canceling: "
"No player object for peer_id=" << pkt->getPeerId()
<< " disconnecting peer!" << std::endl;
- m_con.DisconnectPeer(pkt->getPeerId());
+ m_con->DisconnectPeer(pkt->getPeerId());
return;
}
errorstream << "Server::ProcessData(): Canceling: "
"No player for peer_id=" << pkt->getPeerId()
<< " disconnecting peer!" << std::endl;
- m_con.DisconnectPeer(pkt->getPeerId());
+ m_con->DisconnectPeer(pkt->getPeerId());
return;
}
errorstream << "Server::ProcessData(): Canceling: "
"No player object for peer_id=" << pkt->getPeerId()
<< " disconnecting peer!" << std::endl;
- m_con.DisconnectPeer(pkt->getPeerId());
+ m_con->DisconnectPeer(pkt->getPeerId());
return;
}
errorstream << "Server::ProcessData(): Canceling: "
"No player for peer_id=" << pkt->getPeerId()
<< " disconnecting peer!" << std::endl;
- m_con.DisconnectPeer(pkt->getPeerId());
+ m_con->DisconnectPeer(pkt->getPeerId());
return;
}
errorstream << "Server::ProcessData(): Canceling: "
"No player for peer_id=" << pkt->getPeerId()
<< " disconnecting peer!" << std::endl;
- m_con.DisconnectPeer(pkt->getPeerId());
+ m_con->DisconnectPeer(pkt->getPeerId());
return;
}
errorstream << "Server::ProcessData(): Canceling: "
"No player object for peer_id=" << pkt->getPeerId()
<< " disconnecting peer!" << std::endl;
- m_con.DisconnectPeer(pkt->getPeerId());
+ m_con->DisconnectPeer(pkt->getPeerId());
return;
}
errorstream << "Server::ProcessData(): Canceling: "
"No player for peer_id=" << pkt->getPeerId()
<< " disconnecting peer!" << std::endl;
- m_con.DisconnectPeer(pkt->getPeerId());
+ m_con->DisconnectPeer(pkt->getPeerId());
return;
}
errorstream << "Server::ProcessData(): Canceling: "
"No player for peer_id=" << pkt->getPeerId()
<< " disconnecting peer!" << std::endl;
- m_con.DisconnectPeer(pkt->getPeerId());
+ m_con->DisconnectPeer(pkt->getPeerId());
return;
}
errorstream << "Server::ProcessData(): Canceling: "
"No player object for peer_id=" << pkt->getPeerId()
<< " disconnecting peer!" << std::endl;
- m_con.DisconnectPeer(pkt->getPeerId());
+ m_con->DisconnectPeer(pkt->getPeerId());
return;
}
errorstream << "Server::ProcessData(): Canceling: "
"No player for peer_id=" << pkt->getPeerId()
<< " disconnecting peer!" << std::endl;
- m_con.DisconnectPeer(pkt->getPeerId());
+ m_con->DisconnectPeer(pkt->getPeerId());
return;
}
errorstream << "Server::ProcessData(): Canceling: "
"No player for peer_id=" << pkt->getPeerId()
<< " disconnecting peer!" << std::endl;
- m_con.DisconnectPeer(pkt->getPeerId());
+ m_con->DisconnectPeer(pkt->getPeerId());
return;
}
errorstream << "Server::ProcessData(): Canceling: "
"No player object for peer_id=" << pkt->getPeerId()
<< " disconnecting peer!" << std::endl;
- m_con.DisconnectPeer(pkt->getPeerId());
+ m_con->DisconnectPeer(pkt->getPeerId());
return;
}
errorstream << "Server::ProcessData(): Canceling: "
"No player for peer_id=" << pkt->getPeerId()
<< " disconnecting peer!" << std::endl;
- m_con.DisconnectPeer(pkt->getPeerId());
+ m_con->DisconnectPeer(pkt->getPeerId());
return;
}
errorstream << "Server::ProcessData(): Canceling: "
"No player object for peer_id=" << pkt->getPeerId()
<< " disconnecting peer!" << std::endl;
- m_con.DisconnectPeer(pkt->getPeerId());
+ m_con->DisconnectPeer(pkt->getPeerId());
return;
}
errorstream << "Server::ProcessData(): Canceling: "
"No player for peer_id=" << pkt->getPeerId()
<< " disconnecting peer!" << std::endl;
- m_con.DisconnectPeer(pkt->getPeerId());
+ m_con->DisconnectPeer(pkt->getPeerId());
return;
}
errorstream << "Server::ProcessData(): Canceling: "
"No player object for peer_id=" << pkt->getPeerId()
<< " disconnecting peer!" << std::endl;
- m_con.DisconnectPeer(pkt->getPeerId());
+ m_con->DisconnectPeer(pkt->getPeerId());
return;
}
--- /dev/null
+/*
+Minetest
+Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
+
+This program is free software; you can redistribute it and/or modify
+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 Lesser General Public License for more details.
+
+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"
+
+#include <cstdio>
+#include <iostream>
+#include <cstdlib>
+#include <cstring>
+#include <cerrno>
+#include <sstream>
+#include <iomanip>
+#include "util/string.h"
+#include "util/numeric.h"
+#include "constants.h"
+#include "debug.h"
+#include "settings.h"
+#include "log.h"
+
+#ifdef _WIN32
+// Without this some of the network functions are not found on mingw
+#ifndef _WIN32_WINNT
+#define _WIN32_WINNT 0x0501
+#endif
+#include <windows.h>
+#include <winsock2.h>
+#include <ws2tcpip.h>
+#define LAST_SOCKET_ERR() WSAGetLastError()
+typedef SOCKET socket_t;
+typedef int socklen_t;
+#else
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <fcntl.h>
+#include <netdb.h>
+#include <unistd.h>
+#include <arpa/inet.h>
+#define LAST_SOCKET_ERR() (errno)
+typedef int socket_t;
+#endif
+
+// Set to true to enable verbose debug output
+bool socket_enable_debug_output = false; // yuck
+
+static bool g_sockets_initialized = false;
+
+// Initialize sockets
+void sockets_init()
+{
+#ifdef _WIN32
+ // Windows needs sockets to be initialized before use
+ WSADATA WsaData;
+ if (WSAStartup(MAKEWORD(2, 2), &WsaData) != NO_ERROR)
+ throw SocketException("WSAStartup failed");
+#endif
+ g_sockets_initialized = true;
+}
+
+void sockets_cleanup()
+{
+#ifdef _WIN32
+ // On Windows, cleanup sockets after use
+ WSACleanup();
+#endif
+}
+
+/*
+ UDPSocket
+*/
+
+UDPSocket::UDPSocket(bool ipv6)
+{
+ init(ipv6, false);
+}
+
+bool UDPSocket::init(bool ipv6, bool noExceptions)
+{
+ if (!g_sockets_initialized) {
+ dstream << "Sockets not initialized" << std::endl;
+ return false;
+ }
+
+ // Use IPv6 if specified
+ m_addr_family = ipv6 ? AF_INET6 : AF_INET;
+ m_handle = socket(m_addr_family, SOCK_DGRAM, IPPROTO_UDP);
+
+ if (socket_enable_debug_output) {
+ dstream << "UDPSocket(" << (int)m_handle
+ << ")::UDPSocket(): ipv6 = " << (ipv6 ? "true" : "false")
+ << std::endl;
+ }
+
+ if (m_handle <= 0) {
+ if (noExceptions) {
+ return false;
+ }
+
+ throw SocketException(std::string("Failed to create socket: error ") +
+ itos(LAST_SOCKET_ERR()));
+ }
+
+ setTimeoutMs(0);
+
+ return true;
+}
+
+UDPSocket::~UDPSocket()
+{
+ if (socket_enable_debug_output) {
+ dstream << "UDPSocket( " << (int)m_handle << ")::~UDPSocket()"
+ << std::endl;
+ }
+
+#ifdef _WIN32
+ closesocket(m_handle);
+#else
+ close(m_handle);
+#endif
+}
+
+void UDPSocket::Bind(Address addr)
+{
+ if (socket_enable_debug_output) {
+ dstream << "UDPSocket(" << (int)m_handle
+ << ")::Bind(): " << addr.serializeString() << ":"
+ << addr.getPort() << std::endl;
+ }
+
+ if (addr.getFamily() != m_addr_family) {
+ static const char *errmsg =
+ "Socket and bind address families do not match";
+ errorstream << "Bind failed: " << errmsg << std::endl;
+ throw SocketException(errmsg);
+ }
+
+ if (m_addr_family == AF_INET6) {
+ struct sockaddr_in6 address;
+ memset(&address, 0, sizeof(address));
+
+ address = addr.getAddress6();
+ address.sin6_family = AF_INET6;
+ address.sin6_port = htons(addr.getPort());
+
+ if (bind(m_handle, (const struct sockaddr *)&address,
+ sizeof(struct sockaddr_in6)) < 0) {
+ dstream << (int)m_handle << ": Bind failed: " << strerror(errno)
+ << std::endl;
+ throw SocketException("Failed to bind socket");
+ }
+ } else {
+ struct sockaddr_in address;
+ memset(&address, 0, sizeof(address));
+
+ address = addr.getAddress();
+ address.sin_family = AF_INET;
+ address.sin_port = htons(addr.getPort());
+
+ if (bind(m_handle, (const struct sockaddr *)&address,
+ sizeof(struct sockaddr_in)) < 0) {
+ dstream << (int)m_handle << ": Bind failed: " << strerror(errno)
+ << std::endl;
+ throw SocketException("Failed to bind socket");
+ }
+ }
+}
+
+void UDPSocket::Send(const Address &destination, const void *data, int size)
+{
+ bool dumping_packet = false; // for INTERNET_SIMULATOR
+
+ if (INTERNET_SIMULATOR)
+ dumping_packet = myrand() % INTERNET_SIMULATOR_PACKET_LOSS == 0;
+
+ if (socket_enable_debug_output) {
+ // Print packet destination and size
+ dstream << (int)m_handle << " -> ";
+ destination.print(&dstream);
+ dstream << ", size=" << size;
+
+ // Print packet contents
+ dstream << ", data=";
+ for (int i = 0; i < size && i < 20; i++) {
+ if (i % 2 == 0)
+ dstream << " ";
+ unsigned int a = ((const unsigned char *)data)[i];
+ dstream << std::hex << std::setw(2) << std::setfill('0') << a;
+ }
+
+ if (size > 20)
+ dstream << "...";
+
+ if (dumping_packet)
+ dstream << " (DUMPED BY INTERNET_SIMULATOR)";
+
+ dstream << std::endl;
+ }
+
+ if (dumping_packet) {
+ // Lol let's forget it
+ dstream << "UDPSocket::Send(): INTERNET_SIMULATOR: dumping packet."
+ << std::endl;
+ return;
+ }
+
+ if (destination.getFamily() != m_addr_family)
+ throw SendFailedException("Address family mismatch");
+
+ int sent;
+ if (m_addr_family == AF_INET6) {
+ struct sockaddr_in6 address = destination.getAddress6();
+ address.sin6_port = htons(destination.getPort());
+ sent = sendto(m_handle, (const char *)data, size, 0,
+ (struct sockaddr *)&address, sizeof(struct sockaddr_in6));
+ } else {
+ struct sockaddr_in address = destination.getAddress();
+ address.sin_port = htons(destination.getPort());
+ sent = sendto(m_handle, (const char *)data, size, 0,
+ (struct sockaddr *)&address, sizeof(struct sockaddr_in));
+ }
+
+ if (sent != size)
+ throw SendFailedException("Failed to send packet");
+}
+
+int UDPSocket::Receive(Address &sender, void *data, int size)
+{
+ // Return on timeout
+ if (!WaitData(m_timeout_ms))
+ return -1;
+
+ int received;
+ if (m_addr_family == AF_INET6) {
+ struct sockaddr_in6 address;
+ memset(&address, 0, sizeof(address));
+ socklen_t address_len = sizeof(address);
+
+ received = recvfrom(m_handle, (char *)data, size, 0,
+ (struct sockaddr *)&address, &address_len);
+
+ if (received < 0)
+ return -1;
+
+ u16 address_port = ntohs(address.sin6_port);
+ IPv6AddressBytes bytes;
+ memcpy(bytes.bytes, address.sin6_addr.s6_addr, 16);
+ sender = Address(&bytes, address_port);
+ } else {
+ struct sockaddr_in address;
+ memset(&address, 0, sizeof(address));
+
+ socklen_t address_len = sizeof(address);
+
+ received = recvfrom(m_handle, (char *)data, size, 0,
+ (struct sockaddr *)&address, &address_len);
+
+ if (received < 0)
+ return -1;
+
+ u32 address_ip = ntohl(address.sin_addr.s_addr);
+ u16 address_port = ntohs(address.sin_port);
+
+ sender = Address(address_ip, address_port);
+ }
+
+ if (socket_enable_debug_output) {
+ // Print packet sender and size
+ dstream << (int)m_handle << " <- ";
+ sender.print(&dstream);
+ dstream << ", size=" << received;
+
+ // Print packet contents
+ dstream << ", data=";
+ for (int i = 0; i < received && i < 20; i++) {
+ if (i % 2 == 0)
+ dstream << " ";
+ unsigned int a = ((const unsigned char *)data)[i];
+ dstream << std::hex << std::setw(2) << std::setfill('0') << a;
+ }
+ if (received > 20)
+ dstream << "...";
+
+ dstream << std::endl;
+ }
+
+ return received;
+}
+
+int UDPSocket::GetHandle()
+{
+ return m_handle;
+}
+
+void UDPSocket::setTimeoutMs(int timeout_ms)
+{
+ m_timeout_ms = timeout_ms;
+}
+
+bool UDPSocket::WaitData(int timeout_ms)
+{
+ fd_set readset;
+ int result;
+
+ // Initialize the set
+ FD_ZERO(&readset);
+ FD_SET(m_handle, &readset);
+
+ // Initialize time out struct
+ struct timeval tv;
+ tv.tv_sec = 0;
+ tv.tv_usec = timeout_ms * 1000;
+
+ // select()
+ result = select(m_handle + 1, &readset, NULL, NULL, &tv);
+
+ if (result == 0)
+ return false;
+
+ if (result < 0 && (errno == EINTR || errno == EBADF)) {
+ // N.B. select() fails when sockets are destroyed on Connection's dtor
+ // with EBADF. Instead of doing tricky synchronization, allow this
+ // thread to exit but don't throw an exception.
+ return false;
+ }
+
+ if (result < 0) {
+ dstream << m_handle << ": Select failed: " << strerror(errno)
+ << std::endl;
+
+#ifdef _WIN32
+ int e = WSAGetLastError();
+ dstream << (int)m_handle << ": WSAGetLastError()=" << e << std::endl;
+ if (e == 10004 /* WSAEINTR */ || e == 10009 /* WSAEBADF */) {
+ infostream << "Ignoring WSAEINTR/WSAEBADF." << std::endl;
+ return false;
+ }
+#endif
+
+ throw SocketException("Select failed");
+ } else if (!FD_ISSET(m_handle, &readset)) {
+ // No data
+ return false;
+ }
+
+ // There is data
+ return true;
+}
--- /dev/null
+/*
+Minetest
+Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
+
+This program is free software; you can redistribute it and/or modify
+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 Lesser General Public License for more details.
+
+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.
+*/
+
+#pragma once
+
+#ifdef _WIN32
+#ifndef _WIN32_WINNT
+#define _WIN32_WINNT 0x0501
+#endif
+#include <windows.h>
+#include <winsock2.h>
+#include <ws2tcpip.h>
+#else
+#include <sys/socket.h>
+#include <netinet/in.h>
+#endif
+
+#include <ostream>
+#include <cstring>
+#include "address.h"
+#include "irrlichttypes.h"
+#include "networkexceptions.h"
+
+extern bool socket_enable_debug_output;
+
+void sockets_init();
+void sockets_cleanup();
+
+class UDPSocket
+{
+public:
+ UDPSocket() = default;
+
+ UDPSocket(bool ipv6);
+ ~UDPSocket();
+ void Bind(Address addr);
+
+ bool init(bool ipv6, bool noExceptions = false);
+
+ // void Close();
+ // bool IsOpen();
+ void Send(const Address &destination, const void *data, int size);
+ // Returns -1 if there is no data
+ int Receive(Address &sender, void *data, int size);
+ int GetHandle(); // For debugging purposes only
+ void setTimeoutMs(int timeout_ms);
+ // Returns true if there is data, false if timeout occurred
+ bool WaitData(int timeout_ms);
+
+private:
+ int m_handle;
+ int m_timeout_ms;
+ int m_addr_family;
+};
#include <iostream>
#include <queue>
#include <algorithm>
+#include "network/connection.h"
#include "network/networkprotocol.h"
#include "network/serveropcodes.h"
#include "ban.h"
m_simple_singleplayer_mode(simple_singleplayer_mode),
m_dedicated(dedicated),
m_async_fatal_error(""),
- m_con(PROTOCOL_ID,
+ m_con(std::make_shared<con::Connection>(PROTOCOL_ID,
512,
CONNECTION_TIMEOUT,
ipv6,
- this),
+ this)),
m_itemdef(createItemDefManager()),
m_nodedef(createNodeDefManager()),
m_craftdef(createCraftDefManager()),
m_event(new EventManager()),
m_uptime(0),
- m_clients(&m_con),
+ m_clients(m_con),
m_admin_chat(iface)
{
m_lag = g_settings->getFloat("dedicated_server_step");
m_thread->stop();
// Initialize connection
- m_con.SetTimeoutMs(30);
- m_con.Serve(bind_addr);
+ m_con->SetTimeoutMs(30);
+ m_con->Serve(bind_addr);
// Start thread
m_thread->start();
u16 peer_id;
try {
NetworkPacket pkt;
- m_con.Receive(&pkt);
+ m_con->Receive(&pkt);
peer_id = pkt.getPeerId();
ProcessData(&pkt);
} catch (const con::InvalidIncomingDataException &e) {
bool Server::getClientConInfo(u16 peer_id, con::rtt_stat_type type, float* retval)
{
- *retval = m_con.getPeerStat(peer_id,type);
+ *retval = m_con->getPeerStat(peer_id,type);
return *retval != -1;
}
}
m_clients.event(peer_id, CSE_SetDenied);
- m_con.DisconnectPeer(peer_id);
+ m_con->DisconnectPeer(peer_id);
}
SendAccessDenied(peer_id, reason, custom_reason);
m_clients.event(peer_id, CSE_SetDenied);
- m_con.DisconnectPeer(peer_id);
+ m_con->DisconnectPeer(peer_id);
}
// 13/03/15: remove this function when protocol version 25 will become
SendAccessDenied_Legacy(peer_id, reason);
m_clients.event(peer_id, CSE_SetDenied);
- m_con.DisconnectPeer(peer_id);
+ m_con->DisconnectPeer(peer_id);
}
void Server::acceptAuth(u16 peer_id, bool forSudoMode)
return player->getHotbarSelectedImage();
}
+Address Server::getPeerAddress(u16 peer_id)
+{
+ return m_con->GetPeerAddress(peer_id);
+}
+
bool Server::setLocalPlayerAnimations(RemotePlayer *player,
v2s32 animation_frames[4], f32 frame_speed)
{
#pragma once
-#include "network/connection.h"
#include "irr_v3d.h"
#include "map.h"
#include "hud.h"
#include "inventorymanager.h"
#include "subgame.h"
#include "tileanimation.h" // struct TileAnimationParams
+#include "network/peerhandler.h"
+#include "network/address.h"
#include "util/numeric.h"
#include "util/thread.h"
#include "util/basic_macros.h"
void hudSetHotbarSelectedImage(RemotePlayer *player, std::string name);
const std::string &hudGetHotbarSelectedImage(RemotePlayer *player) const;
- inline Address getPeerAddress(u16 peer_id)
- { return m_con.GetPeerAddress(peer_id); }
+ Address getPeerAddress(u16 peer_id);
bool setLocalPlayerAnimations(RemotePlayer *player, v2s32 animation_frames[4],
f32 frame_speed);
void DenyAccess(u16 peer_id, AccessDeniedCode reason, const std::string &custom_reason="");
void acceptAuth(u16 peer_id, bool forSudoMode);
void DenyAccess_Legacy(u16 peer_id, const std::wstring &reason);
- bool getClientConInfo(u16 peer_id, con::rtt_stat_type type,float* retval);
+ bool getClientConInfo(u16 peer_id, con::rtt_stat_type type, float* retval);
bool getClientInfo(u16 peer_id,ClientState* state, u32* uptime,
u8* ser_vers, u16* prot_vers, u8* major, u8* minor, u8* patch,
std::string* vers_string);
ServerEnvironment *m_env = nullptr;
// server connection
- con::Connection m_con;
+ std::shared_ptr<con::Connection> m_con;
// Ban checking
BanManager *m_banmanager = nullptr;
+++ /dev/null
-/*
-Minetest
-Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
-
-This program is free software; you can redistribute it and/or modify
-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 Lesser General Public License for more details.
-
-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"
-
-#include <cstdio>
-#include <iostream>
-#include <cstdlib>
-#include <cstring>
-#include <cerrno>
-#include <sstream>
-#include <iomanip>
-#include "util/string.h"
-#include "util/numeric.h"
-#include "constants.h"
-#include "debug.h"
-#include "settings.h"
-#include "log.h"
-
-#ifdef _WIN32
- // Without this some of the network functions are not found on mingw
- #ifndef _WIN32_WINNT
- #define _WIN32_WINNT 0x0501
- #endif
- #include <windows.h>
- #include <winsock2.h>
- #include <ws2tcpip.h>
- #define LAST_SOCKET_ERR() WSAGetLastError()
- typedef SOCKET socket_t;
- typedef int socklen_t;
-#else
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <fcntl.h>
- #include <netdb.h>
- #include <unistd.h>
- #include <arpa/inet.h>
- #define LAST_SOCKET_ERR() (errno)
- typedef int socket_t;
-#endif
-
-// Set to true to enable verbose debug output
-bool socket_enable_debug_output = false; // yuck
-
-static bool g_sockets_initialized = false;
-
-// Initialize sockets
-void sockets_init()
-{
-#ifdef _WIN32
- // Windows needs sockets to be initialized before use
- WSADATA WsaData;
- if(WSAStartup( MAKEWORD(2,2), &WsaData ) != NO_ERROR)
- throw SocketException("WSAStartup failed");
-#endif
- g_sockets_initialized = true;
-}
-
-void sockets_cleanup()
-{
-#ifdef _WIN32
- // On Windows, cleanup sockets after use
- WSACleanup();
-#endif
-}
-
-/*
- Address
-*/
-
-Address::Address()
-{
- memset(&m_address, 0, sizeof(m_address));
-}
-
-Address::Address(u32 address, u16 port)
-{
- memset(&m_address, 0, sizeof(m_address));
- setAddress(address);
- setPort(port);
-}
-
-Address::Address(u8 a, u8 b, u8 c, u8 d, u16 port)
-{
- memset(&m_address, 0, sizeof(m_address));
- setAddress(a, b, c, d);
- setPort(port);
-}
-
-Address::Address(const IPv6AddressBytes *ipv6_bytes, u16 port)
-{
- memset(&m_address, 0, sizeof(m_address));
- setAddress(ipv6_bytes);
- setPort(port);
-}
-
-// Equality (address family, address and port must be equal)
-bool Address::operator==(const Address &address)
-{
- if (address.m_addr_family != m_addr_family || address.m_port != m_port)
- return false;
-
- if (m_addr_family == AF_INET) {
- return m_address.ipv4.sin_addr.s_addr ==
- address.m_address.ipv4.sin_addr.s_addr;
- }
-
- if (m_addr_family == AF_INET6) {
- return memcmp(m_address.ipv6.sin6_addr.s6_addr,
- address.m_address.ipv6.sin6_addr.s6_addr, 16) == 0;
- }
-
- return false;
-}
-
-bool Address::operator!=(const Address &address)
-{
- return !(*this == address);
-}
-
-void Address::Resolve(const char *name)
-{
- if (!name || name[0] == 0) {
- if (m_addr_family == AF_INET) {
- setAddress((u32) 0);
- } else if (m_addr_family == AF_INET6) {
- setAddress((IPv6AddressBytes*) 0);
- }
- return;
- }
-
- struct addrinfo *resolved, hints;
- memset(&hints, 0, sizeof(hints));
-
- // Setup hints
- hints.ai_socktype = 0;
- hints.ai_protocol = 0;
- hints.ai_flags = 0;
- if(g_settings->getBool("enable_ipv6"))
- {
- // AF_UNSPEC allows both IPv6 and IPv4 addresses to be returned
- hints.ai_family = AF_UNSPEC;
- }
- else
- {
- hints.ai_family = AF_INET;
- }
-
- // Do getaddrinfo()
- int e = getaddrinfo(name, NULL, &hints, &resolved);
- if(e != 0)
- throw ResolveError(gai_strerror(e));
-
- // Copy data
- if(resolved->ai_family == AF_INET)
- {
- struct sockaddr_in *t = (struct sockaddr_in *) resolved->ai_addr;
- m_addr_family = AF_INET;
- m_address.ipv4 = *t;
- }
- else if(resolved->ai_family == AF_INET6)
- {
- struct sockaddr_in6 *t = (struct sockaddr_in6 *) resolved->ai_addr;
- m_addr_family = AF_INET6;
- m_address.ipv6 = *t;
- }
- else
- {
- freeaddrinfo(resolved);
- throw ResolveError("");
- }
- freeaddrinfo(resolved);
-}
-
-// IP address -> textual representation
-std::string Address::serializeString() const
-{
-// windows XP doesnt have inet_ntop, maybe use better func
-#ifdef _WIN32
- if(m_addr_family == AF_INET)
- {
- u8 a, b, c, d;
- u32 addr;
- addr = ntohl(m_address.ipv4.sin_addr.s_addr);
- a = (addr & 0xFF000000) >> 24;
- b = (addr & 0x00FF0000) >> 16;
- c = (addr & 0x0000FF00) >> 8;
- d = (addr & 0x000000FF);
- return itos(a) + "." + itos(b) + "." + itos(c) + "." + itos(d);
- }
- else if(m_addr_family == AF_INET6)
- {
- std::ostringstream os;
- for(int i = 0; i < 16; i += 2)
- {
- u16 section =
- (m_address.ipv6.sin6_addr.s6_addr[i] << 8) |
- (m_address.ipv6.sin6_addr.s6_addr[i + 1]);
- os << std::hex << section;
- if(i < 14)
- os << ":";
- }
- return os.str();
- }
- else
- return std::string("");
-#else
- char str[INET6_ADDRSTRLEN];
- if (inet_ntop(m_addr_family, (m_addr_family == AF_INET) ? (void*)&(m_address.ipv4.sin_addr) : (void*)&(m_address.ipv6.sin6_addr), str, INET6_ADDRSTRLEN) == NULL) {
- return std::string("");
- }
- return std::string(str);
-#endif
-}
-
-struct sockaddr_in Address::getAddress() const
-{
- return m_address.ipv4; // NOTE: NO PORT INCLUDED, use getPort()
-}
-
-struct sockaddr_in6 Address::getAddress6() const
-{
- return m_address.ipv6; // NOTE: NO PORT INCLUDED, use getPort()
-}
-
-u16 Address::getPort() const
-{
- return m_port;
-}
-
-int Address::getFamily() const
-{
- return m_addr_family;
-}
-
-bool Address::isIPv6() const
-{
- return m_addr_family == AF_INET6;
-}
-
-bool Address::isZero() const
-{
- if (m_addr_family == AF_INET) {
- return m_address.ipv4.sin_addr.s_addr == 0;
- }
-
- if (m_addr_family == AF_INET6) {
- static const char zero[16] = {0};
- return memcmp(m_address.ipv6.sin6_addr.s6_addr,
- zero, 16) == 0;
- }
- return false;
-}
-
-void Address::setAddress(u32 address)
-{
- m_addr_family = AF_INET;
- m_address.ipv4.sin_family = AF_INET;
- m_address.ipv4.sin_addr.s_addr = htonl(address);
-}
-
-void Address::setAddress(u8 a, u8 b, u8 c, u8 d)
-{
- m_addr_family = AF_INET;
- m_address.ipv4.sin_family = AF_INET;
- u32 addr = htonl((a << 24) | (b << 16) | (c << 8) | d);
- m_address.ipv4.sin_addr.s_addr = addr;
-}
-
-void Address::setAddress(const IPv6AddressBytes *ipv6_bytes)
-{
- m_addr_family = AF_INET6;
- m_address.ipv6.sin6_family = AF_INET6;
- if (ipv6_bytes)
- memcpy(m_address.ipv6.sin6_addr.s6_addr, ipv6_bytes->bytes, 16);
- else
- memset(m_address.ipv6.sin6_addr.s6_addr, 0, 16);
-}
-
-void Address::setPort(u16 port)
-{
- m_port = port;
-}
-
-void Address::print(std::ostream *s) const
-{
- if(m_addr_family == AF_INET6)
- *s << "[" << serializeString() << "]:" << m_port;
- else
- *s << serializeString() << ":" << m_port;
-}
-
-/*
- UDPSocket
-*/
-
-UDPSocket::UDPSocket(bool ipv6)
-{
- init(ipv6, false);
-}
-
-bool UDPSocket::init(bool ipv6, bool noExceptions)
-{
- if (!g_sockets_initialized) {
- dstream << "Sockets not initialized" << std::endl;
- return false;
- }
-
- // Use IPv6 if specified
- m_addr_family = ipv6 ? AF_INET6 : AF_INET;
- m_handle = socket(m_addr_family, SOCK_DGRAM, IPPROTO_UDP);
-
- if (socket_enable_debug_output) {
- dstream << "UDPSocket(" << (int) m_handle
- << ")::UDPSocket(): ipv6 = "
- << (ipv6 ? "true" : "false")
- << std::endl;
- }
-
- if (m_handle <= 0) {
- if (noExceptions) {
- return false;
- }
-
- throw SocketException(std::string("Failed to create socket: error ")
- + itos(LAST_SOCKET_ERR()));
- }
-
- setTimeoutMs(0);
-
- return true;
-}
-
-
-UDPSocket::~UDPSocket()
-{
- if (socket_enable_debug_output) {
- dstream << "UDPSocket( " << (int) m_handle << ")::~UDPSocket()"
- << std::endl;
- }
-
-#ifdef _WIN32
- closesocket(m_handle);
-#else
- close(m_handle);
-#endif
-}
-
-void UDPSocket::Bind(Address addr)
-{
- if(socket_enable_debug_output) {
- dstream << "UDPSocket(" << (int) m_handle << ")::Bind(): "
- << addr.serializeString() << ":"
- << addr.getPort() << std::endl;
- }
-
- if (addr.getFamily() != m_addr_family) {
- static const char *errmsg = "Socket and bind address families do not match";
- errorstream << "Bind failed: " << errmsg << std::endl;
- throw SocketException(errmsg);
- }
-
- if(m_addr_family == AF_INET6) {
- struct sockaddr_in6 address;
- memset(&address, 0, sizeof(address));
-
- address = addr.getAddress6();
- address.sin6_family = AF_INET6;
- address.sin6_port = htons(addr.getPort());
-
- if(bind(m_handle, (const struct sockaddr *) &address,
- sizeof(struct sockaddr_in6)) < 0) {
- dstream << (int) m_handle << ": Bind failed: "
- << strerror(errno) << std::endl;
- throw SocketException("Failed to bind socket");
- }
- } else {
- struct sockaddr_in address;
- memset(&address, 0, sizeof(address));
-
- address = addr.getAddress();
- address.sin_family = AF_INET;
- address.sin_port = htons(addr.getPort());
-
- if (bind(m_handle, (const struct sockaddr *) &address,
- sizeof(struct sockaddr_in)) < 0) {
- dstream << (int)m_handle << ": Bind failed: "
- << strerror(errno) << std::endl;
- throw SocketException("Failed to bind socket");
- }
- }
-}
-
-void UDPSocket::Send(const Address & destination, const void * data, int size)
-{
- bool dumping_packet = false; // for INTERNET_SIMULATOR
-
- if(INTERNET_SIMULATOR)
- dumping_packet = myrand() % INTERNET_SIMULATOR_PACKET_LOSS == 0;
-
- if(socket_enable_debug_output) {
- // Print packet destination and size
- dstream << (int)m_handle << " -> ";
- destination.print(&dstream);
- dstream << ", size=" << size;
-
- // Print packet contents
- dstream << ", data=";
- for(int i = 0; i < size && i < 20; i++) {
- if(i % 2 == 0)
- dstream << " ";
- unsigned int a = ((const unsigned char *)data)[i];
- dstream << std::hex << std::setw(2) << std::setfill('0') << a;
- }
-
- if(size > 20)
- dstream << "...";
-
- if(dumping_packet)
- dstream << " (DUMPED BY INTERNET_SIMULATOR)";
-
- dstream << std::endl;
- }
-
- if(dumping_packet) {
- // Lol let's forget it
- dstream << "UDPSocket::Send(): INTERNET_SIMULATOR: dumping packet."
- << std::endl;
- return;
- }
-
- if(destination.getFamily() != m_addr_family)
- throw SendFailedException("Address family mismatch");
-
- int sent;
- if(m_addr_family == AF_INET6) {
- struct sockaddr_in6 address = destination.getAddress6();
- address.sin6_port = htons(destination.getPort());
- sent = sendto(m_handle, (const char *)data, size,
- 0, (struct sockaddr *)&address, sizeof(struct sockaddr_in6));
- } else {
- struct sockaddr_in address = destination.getAddress();
- address.sin_port = htons(destination.getPort());
- sent = sendto(m_handle, (const char *)data, size,
- 0, (struct sockaddr *)&address, sizeof(struct sockaddr_in));
- }
-
- if(sent != size)
- throw SendFailedException("Failed to send packet");
-}
-
-int UDPSocket::Receive(Address & sender, void *data, int size)
-{
- // Return on timeout
- if (!WaitData(m_timeout_ms))
- return -1;
-
- int received;
- if (m_addr_family == AF_INET6) {
- struct sockaddr_in6 address;
- memset(&address, 0, sizeof(address));
- socklen_t address_len = sizeof(address);
-
- received = recvfrom(m_handle, (char *) data,
- size, 0, (struct sockaddr *) &address, &address_len);
-
- if(received < 0)
- return -1;
-
- u16 address_port = ntohs(address.sin6_port);
- IPv6AddressBytes bytes;
- memcpy(bytes.bytes, address.sin6_addr.s6_addr, 16);
- sender = Address(&bytes, address_port);
- } else {
- struct sockaddr_in address;
- memset(&address, 0, sizeof(address));
-
- socklen_t address_len = sizeof(address);
-
- received = recvfrom(m_handle, (char *)data,
- size, 0, (struct sockaddr *)&address, &address_len);
-
- if(received < 0)
- return -1;
-
- u32 address_ip = ntohl(address.sin_addr.s_addr);
- u16 address_port = ntohs(address.sin_port);
-
- sender = Address(address_ip, address_port);
- }
-
- if (socket_enable_debug_output) {
- // Print packet sender and size
- dstream << (int) m_handle << " <- ";
- sender.print(&dstream);
- dstream << ", size=" << received;
-
- // Print packet contents
- dstream << ", data=";
- for(int i = 0; i < received && i < 20; i++) {
- if(i % 2 == 0)
- dstream << " ";
- unsigned int a = ((const unsigned char *) data)[i];
- dstream << std::hex << std::setw(2) << std::setfill('0') << a;
- }
- if(received > 20)
- dstream << "...";
-
- dstream << std::endl;
- }
-
- return received;
-}
-
-int UDPSocket::GetHandle()
-{
- return m_handle;
-}
-
-void UDPSocket::setTimeoutMs(int timeout_ms)
-{
- m_timeout_ms = timeout_ms;
-}
-
-bool UDPSocket::WaitData(int timeout_ms)
-{
- fd_set readset;
- int result;
-
- // Initialize the set
- FD_ZERO(&readset);
- FD_SET(m_handle, &readset);
-
- // Initialize time out struct
- struct timeval tv;
- tv.tv_sec = 0;
- tv.tv_usec = timeout_ms * 1000;
-
- // select()
- result = select(m_handle+1, &readset, NULL, NULL, &tv);
-
- if (result == 0)
- return false;
-
- if (result < 0 && (errno == EINTR || errno == EBADF)) {
- // N.B. select() fails when sockets are destroyed on Connection's dtor
- // with EBADF. Instead of doing tricky synchronization, allow this
- // thread to exit but don't throw an exception.
- return false;
- }
-
- if (result < 0) {
- dstream << m_handle << ": Select failed: " << strerror(errno) << std::endl;
-
-#ifdef _WIN32
- int e = WSAGetLastError();
- dstream << (int) m_handle << ": WSAGetLastError()="
- << e << std::endl;
- if (e == 10004 /* WSAEINTR */ || e == 10009 /* WSAEBADF */) {
- infostream << "Ignoring WSAEINTR/WSAEBADF." << std::endl;
- return false;
- }
-#endif
-
- throw SocketException("Select failed");
- } else if (!FD_ISSET(m_handle, &readset)) {
- // No data
- return false;
- }
-
- // There is data
- return true;
-}
+++ /dev/null
-/*
-Minetest
-Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
-
-This program is free software; you can redistribute it and/or modify
-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 Lesser General Public License for more details.
-
-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.
-*/
-
-#pragma once
-
-#ifdef _WIN32
-#ifndef _WIN32_WINNT
- #define _WIN32_WINNT 0x0501
-#endif
- #include <windows.h>
- #include <winsock2.h>
- #include <ws2tcpip.h>
-#else
- #include <sys/socket.h>
- #include <netinet/in.h>
-#endif
-
-#include <ostream>
-#include <cstring>
-#include "irrlichttypes.h"
-#include "exceptions.h"
-
-extern bool socket_enable_debug_output;
-
-class SocketException : public BaseException
-{
-public:
- SocketException(const std::string &s):
- BaseException(s)
- {
- }
-};
-
-class ResolveError : public BaseException
-{
-public:
- ResolveError(const std::string &s):
- BaseException(s)
- {
- }
-};
-
-class SendFailedException : public BaseException
-{
-public:
- SendFailedException(const std::string &s):
- BaseException(s)
- {
- }
-};
-
-void sockets_init();
-void sockets_cleanup();
-
-class IPv6AddressBytes
-{
-public:
- u8 bytes[16];
- IPv6AddressBytes() { memset(bytes, 0, 16); }
-};
-
-class Address
-{
-public:
- Address();
- Address(u32 address, u16 port);
- Address(u8 a, u8 b, u8 c, u8 d, u16 port);
- Address(const IPv6AddressBytes *ipv6_bytes, u16 port);
- bool operator==(const Address &address);
- bool operator!=(const Address &address);
- // Resolve() may throw ResolveError (address is unchanged in this case)
- void Resolve(const char *name);
- struct sockaddr_in getAddress() const;
- unsigned short getPort() const;
- void setAddress(u32 address);
- void setAddress(u8 a, u8 b, u8 c, u8 d);
- void setAddress(const IPv6AddressBytes *ipv6_bytes);
- struct sockaddr_in6 getAddress6() const;
- int getFamily() const;
- bool isIPv6() const;
- bool isZero() const;
- void setPort(unsigned short port);
- void print(std::ostream *s) const;
- std::string serializeString() const;
-private:
- unsigned int m_addr_family = 0;
- union
- {
- struct sockaddr_in ipv4;
- struct sockaddr_in6 ipv6;
- } m_address;
- u16 m_port = 0; // Port is separate from sockaddr structures
-};
-
-class UDPSocket
-{
-public:
- UDPSocket() = default;
-
- UDPSocket(bool ipv6);
- ~UDPSocket();
- void Bind(Address addr);
-
- bool init(bool ipv6, bool noExceptions = false);
-
- //void Close();
- //bool IsOpen();
- void Send(const Address & destination, const void * data, int size);
- // Returns -1 if there is no data
- int Receive(Address & sender, void * data, int size);
- int GetHandle(); // For debugging purposes only
- void setTimeoutMs(int timeout_ms);
- // Returns true if there is data, false if timeout occurred
- bool WaitData(int timeout_ms);
-private:
- int m_handle;
- int m_timeout_ms;
- int m_addr_family;
-};
#include "test.h"
#include "log.h"
-#include "socket.h"
#include "settings.h"
#include "util/serialize.h"
#include "network/connection.h"
#include "network/networkpacket.h"
+#include "network/socket.h"
class TestConnection : public TestBase {
public:
#include "test.h"
#include "log.h"
-#include "socket.h"
#include "settings.h"
+#include "network/socket.h"
class TestSocket : public TestBase {
public:
src/network/connection.cpp
src/network/connection.h
src/network/networkpacket.cpp
-src/network/networkpacket.h
src/network/networkprotocol.h
src/network/serveropcodes.cpp
src/network/serveropcodes.h
src/shader.cpp
src/shader.h
src/sky.cpp
-src/socket.cpp
-src/socket.h
src/sound.cpp
src/sound_openal.cpp
src/sound_openal.h