Network: First commit

Former-commit-id: ec8697acc51569f5043e4f70e4cf42f1c5dc487c
This commit is contained in:
Lynix
2015-11-09 15:02:25 +01:00
parent dfa6f06337
commit 1bbf038cc6
37 changed files with 3097 additions and 0 deletions

View File

@@ -0,0 +1,44 @@
// This file was automatically generated on 09 Nov 2015 at 13:52:47
/*
Nazara Engine - Network module
Copyright (C) 2015 Jérôme "Lynix" Leclercq (Lynix680@gmail.com)
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma once
#ifndef NAZARA_GLOBAL_NETWORK_HPP
#define NAZARA_GLOBAL_NETWORK_HPP
#include <Nazara/Network/AbstractSocket.hpp>
#include <Nazara/Network/Algorithm.hpp>
#include <Nazara/Network/Config.hpp>
#include <Nazara/Network/Enums.hpp>
#include <Nazara/Network/IpAddress.hpp>
#include <Nazara/Network/Network.hpp>
#include <Nazara/Network/SocketHandle.hpp>
#include <Nazara/Network/TcpBase.hpp>
#include <Nazara/Network/TcpClient.hpp>
#include <Nazara/Network/TcpServer.hpp>
#include <Nazara/Network/UdpSocket.hpp>
#endif // NAZARA_GLOBAL_NETWORK_HPP

View File

@@ -0,0 +1,63 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Network module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_ABSTRACTSOCKET_HPP
#define NAZARA_ABSTRACTSOCKET_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/Signal.hpp>
#include <Nazara/Network/Config.hpp>
#include <Nazara/Network/Enums.hpp>
#include <Nazara/Network/SocketHandle.hpp>
namespace Nz
{
class NAZARA_NETWORK_API AbstractSocket
{
public:
AbstractSocket(const AbstractSocket&) = delete;
AbstractSocket(AbstractSocket&& abstractSocket);
virtual ~AbstractSocket();
void Close();
void EnableBlocking(bool blocking);
inline SocketError GetLastError() const;
inline SocketHandle GetNativeHandle() const;
inline SocketState GetState() const;
inline SocketType GetType() const;
inline bool IsBlockingEnabled() const;
unsigned int QueryAvailableBytes() const;
// Slots
NazaraSignal(OnStateChange, const AbstractSocket* /*socket*/, SocketState /*oldState*/, SocketState /*newState*/);
protected:
AbstractSocket(SocketType type);
inline void ChangeState(SocketState newState);
virtual void OnClose();
virtual void OnOpened();
bool Open(NetProtocol protocol);
void Open(SocketHandle existingHandle);
NetProtocol m_protocol;
SocketError m_lastError;
SocketHandle m_handle;
SocketState m_state;
SocketType m_type;
bool m_isBlockingEnabled;
};
}
#include <Nazara/Network/AbstractSocket.inl>
#endif // NAZARA_ABSTRACTSOCKET_HPP

View File

@@ -0,0 +1,45 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Network module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Network/Debug.hpp>
namespace Nz
{
inline SocketError AbstractSocket::GetLastError() const
{
return m_lastError;
}
inline SocketHandle AbstractSocket::GetNativeHandle() const
{
return m_handle;
}
inline SocketState AbstractSocket::GetState() const
{
return m_state;
}
inline SocketType AbstractSocket::GetType() const
{
return m_type;
}
inline bool AbstractSocket::IsBlockingEnabled() const
{
return m_isBlockingEnabled;
}
inline void AbstractSocket::ChangeState(SocketState newState)
{
if (m_state != newState)
{
SocketState oldState = m_state;
m_state = newState;
OnStateChange(this, oldState, m_state);
}
}
}
#include <Nazara/Network/DebugOff.hpp>

View File

@@ -0,0 +1,21 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Network module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_ALGORITHM_NETWORK_HPP
#define NAZARA_ALGORITHM_NETWORK_HPP
#include <Nazara/Prerequesites.hpp>
#include <functional>
#include <tuple>
namespace Nz
{
bool ParseIPAddress(const char* addressPtr, UInt8 result[16], UInt16* port = nullptr, bool* isIPv6 = nullptr, const char** endOfRead = nullptr);
}
#include <Nazara/Network/Algorithm.inl>
#endif // NAZARA_ALGORITHM_NETWORK_HPP

View File

@@ -0,0 +1,7 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Network/Debug.hpp>
#include <Nazara/Network/DebugOff.hpp>

View File

@@ -0,0 +1,53 @@
/*
Nazara Engine - Network module
Copyright (C) 2015 Jérôme "Lynix" Leclercq (Lynix680@gmail.com)
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma once
#ifndef NAZARA_CONFIG_NETWORK_HPP
#define NAZARA_CONFIG_NETWORK_HPP
/// Chaque modification d'un paramètre du module nécessite une recompilation de celui-ci
// Utilise le MemoryManager pour gérer les allocations dynamiques (détecte les leaks au prix d'allocations/libérations dynamiques plus lentes)
#define NAZARA_NETWORK_MANAGE_MEMORY 0
// Active les tests de sécurité basés sur le code (Conseillé pour le développement)
#define NAZARA_NETWORK_SAFE 1
/// Chaque modification d'un paramètre ci-dessous implique une modification (souvent mineure) du code
/// Vérification des valeurs et types de certaines constantes
#include <Nazara/Network/ConfigCheck.hpp>
#if defined(NAZARA_STATIC)
#define NAZARA_NETWORK_API
#else
#ifdef NAZARA_NETWORK_BUILD
#define NAZARA_NETWORK_API NAZARA_EXPORT
#else
#define NAZARA_NETWORK_API NAZARA_IMPORT
#endif
#endif
#endif // NAZARA_CONFIG_NETWORK_HPP

View File

@@ -0,0 +1,20 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Network module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_CONFIG_CHECK_NETWORK_HPP
#define NAZARA_CONFIG_CHECK_NETWORK_HPP
/// Ce fichier sert à vérifier la valeur des constantes du fichier Config.hpp
#include <type_traits>
// On force la valeur de MANAGE_MEMORY en mode debug
#if defined(NAZARA_DEBUG) && !NAZARA_NETWORK_MANAGE_MEMORY
#undef NAZARA_NETWORK_MANAGE_MEMORY
#define NAZARA_NETWORK_MANAGE_MEMORY 0
#endif
#endif // NAZARA_CONFIG_CHECK_NETWORK_HPP

View File

@@ -0,0 +1,8 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Network module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Network/Config.hpp>
#if NAZARA_MODULENAME_MANAGE_MEMORY
#include <Nazara/Core/Debug/NewRedefinition.hpp>
#endif

View File

@@ -0,0 +1,9 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Network module"
// For conditions of distribution and use, see copyright notice in Config.hpp
// On suppose que Debug.hpp a déjà été inclus, tout comme Config.hpp
#if NAZARA_MODULENAME_MANAGE_MEMORY
#undef delete
#undef new
#endif

View File

@@ -0,0 +1,61 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Network module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_ENUMS_NETWORK_HPP
#define NAZARA_ENUMS_NETWORK_HPP
namespace Nz
{
enum NetProtocol
{
NetProtocol_Any,
NetProtocol_IPv4,
NetProtocol_IPv6,
NetProtocol_Max = NetProtocol_IPv6
};
enum SocketError
{
SocketError_NoError,
SocketError_AddressNotAvailable, //< The address is already in use (when binding/listening)
SocketError_ConnectionClosed, //< The connection has been closed
SocketError_ConnectionRefused, //< The connection attempt was refused
SocketError_DatagramSize, //< The datagram size is over the system limit
SocketError_Internal, //< The error is coming from the engine
SocketError_NetworkError, //< The network system has failed (maybe network is down)
SocketError_NotInitialized, //< Nazara network has not been initialized
SocketError_NotSupported, //< The operation is not supported (e.g. creating a bluetooth socket on a system without any bluetooth adaptater)
SocketError_ResourceError, //< The operating system lacks the resources to proceed (e.g. memory/socket descriptor)
SocketError_UnreachableHost, //< The host is not reachable
SocketError_TimedOut, //< The operation timed out
SocketError_Unknown, //< The last operation failed with an unlisted error code
SocketError_Max = SocketError_Unknown
};
enum SocketState
{
SocketState_Bound, //< The socket is currently bound
SocketState_Connecting, //< The socket is currently connecting
SocketState_Connected, //< The socket is currently connected
SocketState_NotConnected, //< The socket is not connected (or has been disconnected)
SocketState_Max = SocketState_NotConnected
};
enum SocketType
{
SocketType_Raw,
SocketType_TCP,
SocketType_UDP,
SocketType_Max = SocketType_UDP
};
}
#endif // NAZARA_ENUMS_NETWORK_HPP

View File

@@ -0,0 +1,102 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Network module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_IPADDRESS_HPP
#define NAZARA_IPADDRESS_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/String.hpp>
#include <Nazara/Network/Config.hpp>
#include <Nazara/Network/Enums.hpp>
#include <array>
#include <iosfwd>
namespace Nz
{
struct HostnameInfo;
class NAZARA_NETWORK_API IpAddress
{
public:
using IPv4 = std::array<UInt8, 4>; //< four 8bits blocks
using IPv6 = std::array<UInt16, 8>; //< eight 16bits blocks
inline IpAddress();
inline IpAddress(const IPv4& ip, UInt16 port = 0);
inline IpAddress(const IPv6& ip, UInt16 port = 0);
inline IpAddress(const UInt8& a, const UInt8& b, const UInt8& c, const UInt8& d, UInt16 port = 0);
inline IpAddress(const UInt16& a, const UInt16& b, const UInt16& c, const UInt16& d, const UInt16& e, const UInt16& f, const UInt16& g, const UInt16& h, UInt16 port = 0);
inline IpAddress(const char* address);
inline IpAddress(const String& address);
IpAddress(const IpAddress&) = default;
IpAddress(IpAddress&&) = default;
~IpAddress() = default;
bool BuildFromAddress(const char* address);
inline UInt16 GetPort() const;
inline NetProtocol GetProtocol() const;
bool IsLoopback() const;
inline bool IsValid() const;
inline void SetPort(UInt16 port);
inline IPv4 ToIPv4() const;
inline IPv6 ToIPv6() const;
String ToString() const;
inline UInt32 ToUInt32() const;
inline operator bool() const;
IpAddress& operator=(const IpAddress&) = default;
IpAddress& operator=(IpAddress&&) = default;
static String ResolveAddress(const IpAddress& address, String* service = nullptr);
static std::vector<HostnameInfo> ResolveHostname(NetProtocol procol, const String& hostname, const String& protocol = "http");
inline friend std::ostream& operator<<(std::ostream& out, const IpAddress& address);
inline friend bool operator==(const IpAddress& first, const IpAddress& second);
inline friend bool operator!=(const IpAddress& first, const IpAddress& second);
inline friend bool operator<(const IpAddress& first, const IpAddress& second);
inline friend bool operator<=(const IpAddress& first, const IpAddress& second);
inline friend bool operator>(const IpAddress& first, const IpAddress& second);
inline friend bool operator>=(const IpAddress& first, const IpAddress& second);
static IpAddress AnyIpV4;
static IpAddress AnyIpV6;
static IpAddress BroadcastIpV4;
static IpAddress Invalid;
static IpAddress LoopbackIpV4;
static IpAddress LoopbackIpV6;
private:
union
{
IPv4 m_ipv4;
IPv6 m_ipv6;
};
NetProtocol m_protocol;
UInt16 m_port;
bool m_isValid;
};
struct HostnameInfo
{
IpAddress address;
String canonicalName;
int flags;
int family;
int socketType;
};
}
#include <Nazara/Network/IpAddress.inl>
#endif // NAZARA_IPADDRESS_HPP

View File

@@ -0,0 +1,205 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Network module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Error.hpp>
#include <Nazara/Network/Debug.hpp>
namespace Nz
{
inline IpAddress::IpAddress() :
m_isValid(false)
{
}
inline IpAddress::IpAddress(const IPv4& ip, UInt16 port) :
m_isValid(true),
m_ipv4(ip),
m_protocol(NetProtocol_IPv4),
m_port(port)
{
}
inline IpAddress::IpAddress(const IPv6& ip, UInt16 port) :
m_isValid(true),
m_ipv6(ip),
m_protocol(NetProtocol_IPv6),
m_port(port)
{
}
inline IpAddress::IpAddress(const UInt8& a, const UInt8& b, const UInt8& c, const UInt8& d, UInt16 port) :
IpAddress(IPv4{a, b, c, d}, port)
{
}
inline IpAddress::IpAddress(const UInt16& a, const UInt16& b, const UInt16& c, const UInt16& d, const UInt16& e, const UInt16& f, const UInt16& g, const UInt16& h, UInt16 port) :
IpAddress(IPv6{a, b, c, d, e, f, g, h}, port)
{
}
inline IpAddress::IpAddress(const char* address)
{
BuildFromAddress(address);
}
inline IpAddress::IpAddress(const String& address)
{
BuildFromAddress(address.GetConstBuffer());
}
inline UInt16 IpAddress::GetPort() const
{
return m_port;
}
inline NetProtocol IpAddress::GetProtocol() const
{
return m_protocol;
}
inline bool IpAddress::IsValid() const
{
return m_isValid;
}
inline void IpAddress::SetPort(UInt16 port)
{
m_port = port;
}
inline IpAddress::IPv4 IpAddress::ToIPv4() const
{
NazaraAssert(m_isValid && m_protocol == NetProtocol_IPv4, "Address is not a valid IPv4");
return m_ipv4;
}
inline IpAddress::IPv6 IpAddress::ToIPv6() const
{
NazaraAssert(m_isValid && m_protocol == NetProtocol_IPv6, "IP is not a valid IPv6");
return m_ipv6;
}
inline UInt32 IpAddress::ToUInt32() const
{
NazaraAssert(m_isValid && m_protocol == NetProtocol_IPv4, "Address is not a valid IPv4");
return UInt32(m_ipv4[0]) << 24 |
UInt32(m_ipv4[1]) << 16 |
UInt32(m_ipv4[2]) << 8 |
UInt32(m_ipv4[3]) << 0;
}
inline IpAddress::operator bool() const
{
return IsValid();
}
inline std::ostream& operator<<(std::ostream& out, const IpAddress& address)
{
out << "IpAddress(" << address.ToString() << ')';
return out;
}
inline bool operator==(const IpAddress& first, const IpAddress& second)
{
// We need to check the validity of each address before comparing them
if (!first.m_isValid || !second.m_isValid)
return first.m_isValid == second.m_isValid;
// Then the protocol
if (first.m_protocol != second.m_protocol)
return false;
// Each protocol has its variables to compare
switch (first.m_protocol)
{
case NetProtocol_IPv4:
{
if (first.m_ipv4 != second.m_ipv4)
return false;
break;
}
case NetProtocol_IPv6:
{
if (first.m_ipv6 != second.m_ipv6)
return false;
break;
}
}
// Check the port, in case there is one
if (first.m_port != second.m_port)
return false;
return true;
}
inline bool operator!=(const IpAddress& first, const IpAddress& second)
{
return !operator==(first, second);
}
inline bool operator<(const IpAddress& first, const IpAddress& second)
{
// If the second address is invalid, there's no way we're lower than it
if (!second.m_isValid)
return false;
// By this point, the second address is valid, thus check our validity
if (!first.m_isValid)
return true; // Invalid address are lower than valid one
// Compare protocols
if (first.m_protocol != second.m_protocol)
return first.m_protocol < second.m_protocol;
// Compare IP (thanks to std::array comparison operator)
switch (first.m_protocol)
{
case NetProtocol_IPv4:
{
if (first.m_ipv4 != second.m_ipv4)
return first.m_ipv4 < second.m_ipv4;
break;
}
case NetProtocol_IPv6:
{
if (first.m_ipv6 != second.m_ipv6)
return first.m_ipv6 < second.m_ipv6;
break;
}
}
// Compare port
if (first.m_port != second.m_port)
return first.m_port < second.m_port;
return false; //< Same address
}
inline bool operator<=(const IpAddress& first, const IpAddress& second)
{
return !operator<(second, first);
}
inline bool operator>(const IpAddress& first, const IpAddress& second)
{
return second < first;
}
inline bool operator>=(const IpAddress& first, const IpAddress& second)
{
return !operator<(first, second);
}
}
#include <Nazara/Network/DebugOff.hpp>

View File

@@ -0,0 +1,33 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Network module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_MODULENAME_HPP
#define NAZARA_MODULENAME_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/Initializer.hpp>
#include <Nazara/Network/Config.hpp>
namespace Nz
{
class NAZARA_NETWORK_API Network
{
public:
Network() = delete;
~Network() = delete;
static bool Initialize();
static bool IsInitialized();
static void Uninitialize();
private:
static unsigned int s_moduleReferenceCounter;
};
}
#endif // NAZARA_MODULENAME_HPP

View File

@@ -0,0 +1,27 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Network module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_SOCKETHANDLE_HPP
#define NAZARA_SOCKETHANDLE_HPP
#include <Nazara/Prerequesites.hpp>
#if defined(NAZARA_PLATFORM_WINDOWS)
#include <basetsd.h>
#endif
namespace Nz
{
#if defined(NAZARA_PLATFORM_WINDOWS)
using SocketHandle = UINT_PTR;
#elif defined(NAZARA_PLATFORM_POSIX)
using SocketHandle = int;
#else
#error Lack of implementation: SocketHandle
#endif
}
#endif // NAZARA_SOCKETHANDLE_HPP

View File

@@ -0,0 +1,42 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Network module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_TCPBASE_HPP
#define NAZARA_TCPBASE_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Network/AbstractSocket.hpp>
namespace Nz
{
class NAZARA_NETWORK_API TcpBase : public AbstractSocket
{
public:
~TcpBase() = default;
inline bool IsLowDelayEnabled() const;
inline bool IsKeepAliveEnabled() const;
// Slots
NazaraSignal(OnStateChange, const TcpBase* /*socket*/, SocketState /*newState*/);
protected:
TcpBase();
TcpBase(TcpBase&& tcpBase);
virtual void OnOpened() override;
SocketState m_state;
UInt64 m_keepAliveInterval;
UInt64 m_keepAliveTime;
bool m_isLowDelayEnabled;
bool m_isKeepAliveEnabled;
};
}
#include <Nazara/Network/TcpBase.inl>
#endif // NAZARA_TCPBASE_HPP

View File

@@ -0,0 +1,36 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Network module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <utility>
#include <Nazara/Network/Debug.hpp>
namespace Nz
{
inline TcpBase::TcpBase() :
AbstractSocket(SocketType_TCP)
{
}
inline TcpBase::TcpBase(TcpBase&& tcpBase) :
AbstractSocket(std::move(tcpBase)),
m_state(tcpBase.m_state),
m_keepAliveInterval(tcpBase.m_keepAliveInterval),
m_keepAliveTime(tcpBase.m_keepAliveTime),
m_isLowDelayEnabled(tcpBase.m_isLowDelayEnabled),
m_isKeepAliveEnabled(tcpBase.m_isKeepAliveEnabled)
{
}
inline bool TcpBase::IsLowDelayEnabled() const
{
return m_isLowDelayEnabled;
}
inline bool TcpBase::IsKeepAliveEnabled() const
{
return m_isKeepAliveEnabled;
}
}
#include <Nazara/Network/DebugOff.hpp>

View File

@@ -0,0 +1,54 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Network module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_TCPCLIENT_HPP
#define NAZARA_TCPCLIENT_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/Signal.hpp>
#include <Nazara/Network/TcpBase.hpp>
#include <Nazara/Network/IpAddress.hpp>
namespace Nz
{
class NAZARA_NETWORK_API TcpClient : public TcpBase
{
friend class TcpServer;
public:
TcpClient() = default;
inline TcpClient(TcpClient&& tcpClient);
~TcpClient() = default;
SocketState Connect(const IpAddress& remoteAddress, UInt64 msTimeout = 3000);
inline void Disconnect();
void EnableLowDelay(bool lowDelay);
void EnableKeepAlive(bool keepAlive, UInt64 msTime = 10000, UInt64 msInterval = 1000);
inline UInt64 GetKeepAliveInterval() const;
inline UInt64 GetKeepAliveTime() const;
inline IpAddress GetRemoteAddress() const;
SocketState QueryState();
bool Receive(void* buffer, std::size_t size, std::size_t* received);
bool Send(const void* buffer, std::size_t size, std::size_t* sent);
private:
void OnClose() override;
void OnOpened() override;
void Reset(SocketHandle handle, const IpAddress& peerAddress);
IpAddress m_peerAddress;
};
}
#include <Nazara/Network/TcpClient.inl>
#endif // NAZARA_TCPCLIENT_HPP

View File

@@ -0,0 +1,37 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Network module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <utility>
#include <Nazara/Network/Debug.hpp>
namespace Nz
{
inline TcpClient::TcpClient(TcpClient&& tcpClient) :
TcpBase(std::move(tcpClient)),
m_peerAddress(std::move(tcpClient.m_peerAddress))
{
}
inline void TcpClient::Disconnect()
{
Close();
}
inline UInt64 TcpClient::GetKeepAliveInterval() const
{
return m_keepAliveInterval;
}
inline UInt64 TcpClient::GetKeepAliveTime() const
{
return m_keepAliveTime;
}
inline IpAddress TcpClient::GetRemoteAddress() const
{
return m_peerAddress;
}
}
#include <Nazara/Network/DebugOff.hpp>

View File

@@ -0,0 +1,43 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Network module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_TCPSERVER_HPP
#define NAZARA_TCPSERVER_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Network/TcpBase.hpp>
#include <Nazara/Network/IpAddress.hpp>
namespace Nz
{
class TcpClient;
class NAZARA_NETWORK_API TcpServer : public TcpBase
{
public:
TcpServer() = default;
inline TcpServer(TcpServer&& tcpServer);
~TcpServer() = default;
bool AcceptClient(TcpClient* newClient);
inline IpAddress GetBoundAddress() const;
inline UInt16 GetBoundPort() const;
inline SocketState Listen(NetProtocol protocol, UInt16 port, unsigned int queueSize = 10);
SocketState Listen(const IpAddress& address, unsigned int queueSize = 10);
private:
void OnClose() override;
void OnOpened() override;
IpAddress m_boundAddress;
};
}
#include <Nazara/Network/TcpServer.inl>
#endif // NAZARA_TCPSERVER_HPP

View File

@@ -0,0 +1,47 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Network module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <utility>
#include <Nazara/Network/Debug.hpp>
namespace Nz
{
inline TcpServer::TcpServer(TcpServer&& tcpServer) :
TcpBase(std::move(tcpServer)),
m_boundAddress(std::move(tcpServer.m_boundAddress))
{
}
inline IpAddress TcpServer::GetBoundAddress() const
{
return m_boundAddress;
}
inline UInt16 TcpServer::GetBoundPort() const
{
return m_boundAddress.GetPort();
}
inline SocketState TcpServer::Listen(NetProtocol protocol, UInt16 port, unsigned int queueSize)
{
NazaraAssert(protocol != NetProtocol_Any, "Any protocol not supported for Listen"); //< TODO
IpAddress any;
switch (protocol)
{
case NetProtocol_IPv4:
any = IpAddress::AnyIpV4;
break;
case NetProtocol_IPv6:
any = IpAddress::AnyIpV6;
break;
}
any.SetPort(port);
return Listen(any, queueSize);
}
}
#include <Nazara/Network/DebugOff.hpp>

View File

@@ -0,0 +1,50 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Network module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_UDPSOCKET_HPP
#define NAZARA_UDPSOCKET_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Network/AbstractSocket.hpp>
#include <Nazara/Network/IpAddress.hpp>
namespace Nz
{
class NAZARA_NETWORK_API UdpSocket : public AbstractSocket
{
public:
inline UdpSocket();
inline UdpSocket(NetProtocol protocol);
inline UdpSocket(UdpSocket&& udpSocket);
~UdpSocket() = default;
inline SocketState Bind(UInt16 port);
SocketState Bind(const IpAddress& address);
inline bool Create(NetProtocol protocol);
inline IpAddress GetBoundAddress() const;
inline UInt16 GetBoundPort() const;
inline SocketState GetState() const;
unsigned int QueryMaxDatagramSize();
bool Receive(void* buffer, std::size_t size, IpAddress* from, std::size_t* received);
bool Send(const IpAddress& to, const void* buffer, std::size_t size, std::size_t* sent);
private:
void OnClose() override;
void OnOpened() override;
IpAddress m_boundAddress;
SocketState m_state;
};
}
#include <Nazara/Network/UdpSocket.inl>
#endif // NAZARA_UDPSOCKET_HPP

View File

@@ -0,0 +1,66 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Network module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Network/Debug.hpp>
namespace Nz
{
inline UdpSocket::UdpSocket() :
AbstractSocket(SocketType_UDP)
{
}
inline UdpSocket::UdpSocket(NetProtocol protocol) :
UdpSocket()
{
Create(protocol);
}
inline UdpSocket::UdpSocket(UdpSocket&& udpSocket) :
AbstractSocket(std::move(udpSocket)),
m_boundAddress(std::move(udpSocket.m_boundAddress)),
m_state(udpSocket.m_state)
{
}
inline SocketState UdpSocket::Bind(UInt16 port)
{
IpAddress any;
switch (m_protocol)
{
case NetProtocol_IPv4:
any = IpAddress::AnyIpV4;
break;
case NetProtocol_IPv6:
any = IpAddress::AnyIpV6;
break;
}
any.SetPort(port);
return Bind(any);
}
bool UdpSocket::Create(NetProtocol protocol)
{
return Open(protocol);
}
inline IpAddress UdpSocket::GetBoundAddress() const
{
return m_boundAddress;
}
inline UInt16 UdpSocket::GetBoundPort() const
{
return m_boundAddress.GetPort();
}
inline SocketState UdpSocket::GetState() const
{
return m_state;
}
}
#include <Nazara/Network/DebugOff.hpp>