New version
This commit is contained in:
@ -1,27 +0,0 @@
|
||||
#ifndef CONFIG_HPP
|
||||
#define CONFIG_HPP
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
#include "Utility.hpp"
|
||||
|
||||
struct Config
|
||||
{
|
||||
static void Initialize();
|
||||
|
||||
static void SetUsingConsole(bool value);
|
||||
static bool GetUsingConsole();
|
||||
|
||||
static void SetLogToFile(bool value);
|
||||
static bool GetLogToFile();
|
||||
|
||||
static Utility::ConfigReader Configuration;
|
||||
|
||||
private:
|
||||
static bool using_console;
|
||||
static bool log_to_file;
|
||||
};
|
||||
|
||||
#endif
|
||||
100
include/Cookies.hpp
Normal file
100
include/Cookies.hpp
Normal file
@ -0,0 +1,100 @@
|
||||
// https://github.com/mfichman/http
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <map>
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
class Cookie
|
||||
{
|
||||
public:
|
||||
Cookie(const std::string& text);
|
||||
|
||||
Cookie() : m_httpOnly(false), m_secure(false)
|
||||
{
|
||||
}
|
||||
|
||||
const std::string& GetName() const
|
||||
{
|
||||
return m_name;
|
||||
}
|
||||
|
||||
const std::string& GetValue() const
|
||||
{
|
||||
return m_value;
|
||||
}
|
||||
|
||||
const std::string& GetPath() const
|
||||
{
|
||||
return m_path;
|
||||
}
|
||||
|
||||
bool IsHttpOnly() const
|
||||
{
|
||||
return m_httpOnly;
|
||||
}
|
||||
|
||||
bool IsSecure() const
|
||||
{
|
||||
return m_secure;
|
||||
}
|
||||
|
||||
void SetName(const std::string& name)
|
||||
{
|
||||
m_name = name;
|
||||
}
|
||||
|
||||
void SetValue(const std::string& value)
|
||||
{
|
||||
m_value = value;
|
||||
}
|
||||
|
||||
void SetPath(const std::string& path)
|
||||
{
|
||||
m_path = path;
|
||||
}
|
||||
|
||||
void SetHttpOnly(bool httpOnly)
|
||||
{
|
||||
m_httpOnly = httpOnly;
|
||||
}
|
||||
|
||||
void SetSecure(bool secure)
|
||||
{
|
||||
m_secure = secure;
|
||||
}
|
||||
|
||||
private:
|
||||
std::string m_name;
|
||||
std::string m_value;
|
||||
std::string m_path;
|
||||
bool m_httpOnly;
|
||||
bool m_secure;
|
||||
};
|
||||
|
||||
class Cookies
|
||||
{
|
||||
public:
|
||||
const Cookie operator[](const std::string &name) const;
|
||||
|
||||
std::map<std::string, Cookie>::const_iterator begin() const
|
||||
{
|
||||
return m_cookie.begin();
|
||||
}
|
||||
|
||||
std::map<std::string, Cookie>::const_iterator end() const
|
||||
{
|
||||
return m_cookie.end();
|
||||
}
|
||||
|
||||
void SetCookie(Cookie const& cookie);
|
||||
|
||||
static const std::string HOST;
|
||||
static const std::string CONTENT_LENGTH;
|
||||
static const std::string ACCEPT_ENCODING;
|
||||
static const std::string CONNECTION;
|
||||
private:
|
||||
std::map<std::string, Cookie> m_cookie;
|
||||
};
|
||||
}
|
||||
256
include/Defs.hpp
256
include/Defs.hpp
@ -1,256 +0,0 @@
|
||||
#ifndef DEFS_HPP
|
||||
#define DEFS_HPP
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#define _WINSOCKAPI_
|
||||
#define _CRT_SECURE_NO_DEPRECATE
|
||||
#pragma comment(lib, "ws2_32.lib")
|
||||
#include <windows.h>
|
||||
#include <winsock2.h>
|
||||
#include <WS2tcpip.h>
|
||||
|
||||
#undef GetBinaryType
|
||||
#undef GetShortPathName
|
||||
#undef GetLongPathName
|
||||
#undef GetEnvironmentStrings
|
||||
#undef SetEnvironmentStrings
|
||||
#undef FreeEnvironmentStrings
|
||||
#undef FormatMessage
|
||||
#undef EncryptFile
|
||||
#undef DecryptFile
|
||||
#undef CreateMutex
|
||||
#undef OpenMutex
|
||||
#undef CreateEvent
|
||||
#undef OpenEvent
|
||||
#undef CreateSemaphore
|
||||
#undef OpenSemaphore
|
||||
#undef LoadLibrary
|
||||
#undef GetModuleFileName
|
||||
#undef CreateProcess
|
||||
#undef GetCommandLine
|
||||
#undef GetEnvironmentVariable
|
||||
#undef SetEnvironmentVariable
|
||||
#undef ExpandEnvironmentStrings
|
||||
#undef OutputDebugString
|
||||
#undef FindResource
|
||||
#undef UpdateResource
|
||||
#undef FindAtom
|
||||
#undef AddAtom
|
||||
#undef GetSystemDirectory
|
||||
#undef GetTempPath
|
||||
#undef GetTempFileName
|
||||
#undef SetCurrentDirectory
|
||||
#undef GetCurrentDirectory
|
||||
#undef CreateDirectory
|
||||
#undef RemoveDirectory
|
||||
#undef CreateFile
|
||||
#undef DeleteFile
|
||||
#undef SearchPath
|
||||
#undef CopyFile
|
||||
#undef MoveFile
|
||||
#undef ReplaceFile
|
||||
#undef GetComputerName
|
||||
#undef SetComputerName
|
||||
#undef GetUserName
|
||||
#undef LogonUser
|
||||
#undef GetVersion
|
||||
#undef GetObject
|
||||
#undef SendMessage
|
||||
|
||||
typedef signed char int8, sbyte;
|
||||
typedef unsigned char uint8, byte;
|
||||
typedef signed short int16;
|
||||
typedef unsigned short uint16;
|
||||
typedef signed int int32;
|
||||
typedef unsigned int uint32;
|
||||
typedef signed __int64 int64;
|
||||
typedef unsigned __int64 uint64;
|
||||
|
||||
#ifdef _WIN64
|
||||
typedef signed __int64 int_ptr;
|
||||
typedef unsigned __int64 uint_ptr;
|
||||
#else
|
||||
typedef signed long int_ptr;
|
||||
typedef unsigned long uint_ptr;
|
||||
#endif // win64
|
||||
|
||||
#elif defined(__GNUC__) || defined(__clang__)
|
||||
typedef signed char int8, sbyte;
|
||||
typedef unsigned char uint8, byte;
|
||||
typedef signed short int16;
|
||||
typedef unsigned short uint16;
|
||||
typedef signed int int32;
|
||||
typedef unsigned int uint32;
|
||||
|
||||
#ifdef _WIN64
|
||||
typedef signed long long int_ptr, int64;
|
||||
typedef unsigned long long uint_ptr, uint64;
|
||||
#else
|
||||
typedef signed long int_ptr;
|
||||
typedef unsigned long uint_ptr;
|
||||
|
||||
#ifdef __LP64__
|
||||
typedef signed long int64;
|
||||
typedef unsigned long uint64;
|
||||
#else
|
||||
typedef signed long long int64;
|
||||
typedef unsigned long long uint64;
|
||||
#endif // __LP64__
|
||||
|
||||
#endif // win64
|
||||
|
||||
#elif defined(__DECCXX)
|
||||
|
||||
typedef signed char int8, sbyte;
|
||||
typedef unsigned char uint8, byte;
|
||||
typedef signed short int16;
|
||||
typedef unsigned short uint16;
|
||||
typedef signed int int32;
|
||||
typedef unsigned int uint32;
|
||||
typedef signed __int64 int64;
|
||||
typedef unsigned __int64 uint64;
|
||||
|
||||
#ifdef __VMS
|
||||
#ifdef __32BITS
|
||||
typedef signed long int_ptr;
|
||||
typedef unsigned long uint_ptr;
|
||||
#else
|
||||
typedef signed __int64 int_ptr;
|
||||
typedef unsigned __int64 uint_ptr;
|
||||
#endif // __32BITS
|
||||
#else
|
||||
typedef signed long int_ptr;
|
||||
typedef unsigned long uint_ptr
|
||||
#endif // __VMS
|
||||
|
||||
#elif defined(__HP_aCC)
|
||||
|
||||
typedef signed char int8, sbyte;
|
||||
typedef unsigned char uint8, byte;
|
||||
typedef signed short int16;
|
||||
typedef unsigned short uint16;
|
||||
typedef signed int int32;
|
||||
typedef unsigned int uint32;
|
||||
typedef signed long int_ptr;
|
||||
typedef unsigned long uint_ptr;
|
||||
|
||||
#ifdef __LP64__
|
||||
typedef signed long int64;
|
||||
typedef unsigned long uint64;
|
||||
#else
|
||||
typedef signed long long int64;
|
||||
typedef unsigned long long uint64;
|
||||
#endif // __LP64__
|
||||
|
||||
#elif defined(__SUNPRO_CC)
|
||||
|
||||
typedef signed char int8, sbyte;
|
||||
typedef unsigned char uint8, byte;
|
||||
typedef signed short int16;
|
||||
typedef unsigned short uint16;
|
||||
typedef signed int int32;
|
||||
typedef unsigned int uint32;
|
||||
typedef signed long int_ptr;
|
||||
typedef unsigned long uint_otr
|
||||
|
||||
#ifdef __sparcv9
|
||||
typedef signed long int64;
|
||||
typedef unsigned long uint64;
|
||||
#else
|
||||
typedef signed long long int64;
|
||||
typedef unsigned long long uint64;
|
||||
#endif // __sparcv9
|
||||
|
||||
#elif defined(__IBMCPP__)
|
||||
|
||||
typedef signed char int8, sbyte;
|
||||
typedef unsigned char uint8, byte;
|
||||
typedef signed short int16;
|
||||
typedef unsigned short uint16;
|
||||
typedef signed int int32;
|
||||
typedef unsigned int uint32;
|
||||
typedef signed long int_ptr;
|
||||
typedef unsigned long uint_ptr;
|
||||
|
||||
#ifdef __64BIT__
|
||||
typedef signed long int64;
|
||||
typedef unsigned long uint64;
|
||||
#else
|
||||
typedef signed long long int64;
|
||||
typedef unsigned long long uint64;
|
||||
#endif // __64BIT__
|
||||
|
||||
#elif defined(__sgi)
|
||||
|
||||
typedef signed char int8, sbyte;
|
||||
typedef unsigned char uint8, byte;
|
||||
typedef signed short int16;
|
||||
typedef unsigned short uint16;
|
||||
typedef signed int int32;
|
||||
typedef unsigned int uint32;
|
||||
typedef signed long int_ptr;
|
||||
typedef unsigned long uint_ptr;
|
||||
|
||||
#if _MIPS_SZLONG == 64
|
||||
typedef signed long int64;
|
||||
typedef unsigned long uint64;
|
||||
#else
|
||||
typedef signed long long int64;
|
||||
typedef unsigned long long uint64;
|
||||
#endif // _MIPS_SZLONG
|
||||
|
||||
#elif defined(_DIAB_TOOL)
|
||||
|
||||
typedef signed char int8, sbyte;
|
||||
typedef unsigned char uint8, byte;
|
||||
typedef signed short int16;
|
||||
typedef unsigned short uint16;
|
||||
typedef signed int in32;
|
||||
typedef unsigned int uint32;
|
||||
typedef signed long int_ptr;
|
||||
typedef unsigned long uint_ptr;
|
||||
typedef signed long long int64;
|
||||
typedef unsigned long long uint64;
|
||||
|
||||
#endif // compiler data type defenitions
|
||||
|
||||
typedef long long longlong;
|
||||
|
||||
const uint16 default_client_port = 60250;
|
||||
const uint16 default_server_port = 61250;
|
||||
|
||||
enum DistributionType
|
||||
{
|
||||
All = 1, // Others and Server
|
||||
AllAndMe, // Other, Server and the user sending the message
|
||||
Server,
|
||||
Others,
|
||||
ID,
|
||||
};
|
||||
|
||||
enum ConnectionCode
|
||||
{
|
||||
Accept,
|
||||
Reject,
|
||||
Close
|
||||
};
|
||||
|
||||
enum InternalTags
|
||||
{
|
||||
ConnectTag = 254,
|
||||
DisconnectTag = 255,
|
||||
};
|
||||
|
||||
#define IS_HANDSHAKE(name) name.subject == 1 || (name.tag == DisconnectTag || name.tag == ConnectTag || name.tag == Accept || name.tag == Close || name.tag == Reject)
|
||||
|
||||
#ifdef __linux__
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
void closesocket(int socket) { close(socket); }
|
||||
#endif
|
||||
|
||||
#endif // DEFS_HPP
|
||||
171
include/Enums.hpp
Normal file
171
include/Enums.hpp
Normal file
@ -0,0 +1,171 @@
|
||||
#pragma once
|
||||
|
||||
#undef DELETE
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
enum class SocketParam
|
||||
{
|
||||
CanRead,
|
||||
CanWrite,
|
||||
HasError,
|
||||
};
|
||||
|
||||
enum class SocketReturn
|
||||
{
|
||||
Yes,
|
||||
No,
|
||||
EncounteredError,
|
||||
};
|
||||
|
||||
enum class SocketErrors
|
||||
{
|
||||
SE_SOCKET_ERROR = -1,
|
||||
SE_NO_ERROR,
|
||||
SE_EINTR,
|
||||
SE_EBADF,
|
||||
SE_EACCES,
|
||||
SE_EFAULT,
|
||||
SE_EINVAL,
|
||||
SE_EMFILE,
|
||||
SE_EWOULDBLOCK,
|
||||
SE_EINPROGRESS,
|
||||
SE_EALREADY,
|
||||
SE_ENOTSOCK,
|
||||
SE_EDESTADDRREQ,
|
||||
SE_EMSGSIZE,
|
||||
SE_EPROTOTYPE,
|
||||
SE_ENOPROTOOPT,
|
||||
SE_EPROTONOSUPPORT,
|
||||
SE_ESOCKTNOSUPPORT,
|
||||
SE_EOPNOTSUPP,
|
||||
SE_EPFNOSUPPORT,
|
||||
SE_EAFNOSUPPORT,
|
||||
SE_EADDRINUSE,
|
||||
SE_EADDRNOTAVAIL,
|
||||
SE_ENETDOWN,
|
||||
SE_ENETUNREACH,
|
||||
SE_ENETRESET,
|
||||
SE_ECONNABORTED,
|
||||
SE_ECONNRESET,
|
||||
SE_ENOBUFS,
|
||||
SE_EISCONN,
|
||||
SE_ENOTCONN,
|
||||
SE_ESHUTDOWN,
|
||||
SE_ETOOMANYREFS,
|
||||
SE_ETIMEDOUT,
|
||||
SE_ECONNREFUSED,
|
||||
SE_ELOOP,
|
||||
SE_ENAMETOOLONG,
|
||||
SE_EHOSTDOWN,
|
||||
SE_EHOSTUNREACH,
|
||||
SE_ENOTEMPTY,
|
||||
SE_EPROCLIM,
|
||||
SE_EUSERS,
|
||||
SE_EDQUOT,
|
||||
SE_ESTALE,
|
||||
SE_EREMOTE,
|
||||
SE_EDISCON,
|
||||
SE_SYSNOTREADY,
|
||||
SE_VERNOTSUPPORTED,
|
||||
SE_NOTINITIALISED,
|
||||
SE_HOST_NOT_FOUND,
|
||||
SE_TRY_AGAIN,
|
||||
SE_NO_RECOVERY,
|
||||
SE_NO_DATA,
|
||||
SE_UDP_ERR_PORT_UNREACH,
|
||||
SE_ADDRFAMILY,
|
||||
SE_SYSTEM,
|
||||
SE_NODEV,
|
||||
SE_GET_LAST_ERROR_CODE,
|
||||
};
|
||||
|
||||
enum class SocketType
|
||||
{
|
||||
Unknown = -1,
|
||||
Datagram = 2,
|
||||
Streaming = 1,
|
||||
};
|
||||
|
||||
enum class SocketProtocol
|
||||
{
|
||||
IPv4 = 2, // AF_INET
|
||||
IPv6 = 23 // AF_INET6
|
||||
};
|
||||
|
||||
enum class SocketReceiveFlags
|
||||
{
|
||||
None = 0,
|
||||
Peek = 2,
|
||||
WaitAll = 0x100,
|
||||
};
|
||||
|
||||
enum class SocketWaitConditions
|
||||
{
|
||||
WaitForRead,
|
||||
WaitForWrite,
|
||||
WaitForReadOrWrite,
|
||||
};
|
||||
|
||||
enum class SocketConnectionState
|
||||
{
|
||||
NotConnected,
|
||||
Connected,
|
||||
ConnectionError,
|
||||
};
|
||||
|
||||
enum class HttpStatus
|
||||
{
|
||||
INVALID_CODE = 0,
|
||||
CONTINUE = 100,
|
||||
SWITCHING_PROTOCOLS = 101,
|
||||
OK = 200,
|
||||
CREATED = 201,
|
||||
ACCEPTED = 202,
|
||||
NON_AUTHORITATIVE_INFO = 203,
|
||||
NO_CONTENT = 204,
|
||||
RESET_CONTENT = 205,
|
||||
PARTIAL_CONTENT = 206,
|
||||
MULTIPLE_CHOICES = 300,
|
||||
MOVED_PERMANENTLY = 301,
|
||||
FOUND = 302,
|
||||
SEE_OTHER = 303,
|
||||
NOT_MODIFIED = 304,
|
||||
USE_PROXY = 305,
|
||||
TEMPORARY_REDIRECT = 307,
|
||||
BAD_REQUEST = 400,
|
||||
UNAUTHORIZED = 401,
|
||||
PAYMENT_REQUIRED = 402,
|
||||
FORBIDDEN = 403,
|
||||
NOT_FOUND = 404,
|
||||
METHOD_NOT_ALLOWED = 405,
|
||||
NOT_ACCEPTABLE = 406,
|
||||
PROXY_AUTHENTICATION_REQUIRED = 407,
|
||||
REQUEST_TIMEOUT = 408,
|
||||
CONFLICT = 409,
|
||||
GONE = 410,
|
||||
LENGTH_REQUIRED = 411,
|
||||
PRECONDITION_FAILED = 412,
|
||||
REQUEST_ENTITY_TOO_LARGE = 413,
|
||||
UNSUPPORTED_MEDIA_TYPE = 415,
|
||||
REQUESTED_RANGE_NOT_SATISFIABLE = 416,
|
||||
EXPECTATION_FAILED = 417,
|
||||
INTERNAL_SERVER_ERROR = 500,
|
||||
NOT_IMPLEMENTED = 501,
|
||||
BAD_GATEWAY = 502,
|
||||
SERVICE_UNAVAILABLE = 503,
|
||||
GATEWAY_TIMEOUT = 504,
|
||||
VERSION_NOT_SUPPORTED = 505,
|
||||
};
|
||||
|
||||
enum class Method
|
||||
{
|
||||
GET,
|
||||
HEAD,
|
||||
POST,
|
||||
PUT,
|
||||
DELETE,
|
||||
TRACE,
|
||||
CONNECT,
|
||||
};
|
||||
}
|
||||
@ -1,29 +0,0 @@
|
||||
#ifndef HANDSHAKE_HPP
|
||||
#define HANDSHAKE_HPP
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
#include "Defs.hpp"
|
||||
#include "NetworkMessage.hpp"
|
||||
|
||||
#include <vector>
|
||||
|
||||
struct Handshake
|
||||
{
|
||||
Handshake();
|
||||
Handshake(uint16 id, byte con_code, byte distribution_mode);
|
||||
~Handshake();
|
||||
|
||||
static const std::vector<byte> &EncodeHandshake(const Handshake &handshake);
|
||||
static Handshake &DecodeHandshake(const std::vector<byte> &bytes);
|
||||
static const NetworkMessage &HandshakeToNetworkMessage(const Handshake &handshake);
|
||||
static const Handshake &NetworkMessageToHandshake(const NetworkMessage &message);
|
||||
|
||||
uint16 id;
|
||||
byte con_code;
|
||||
byte distribution_mode;
|
||||
};
|
||||
|
||||
#endif
|
||||
33
include/Headers.hpp
Normal file
33
include/Headers.hpp
Normal file
@ -0,0 +1,33 @@
|
||||
// https://github.com/mfichman/http
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <map>
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
class Headers
|
||||
{
|
||||
public:
|
||||
const std::string operator[](const std::string &name) const;
|
||||
|
||||
std::multimap<std::string, std::string>::const_iterator begin() const
|
||||
{
|
||||
return m_header.begin();
|
||||
}
|
||||
|
||||
std::multimap<std::string, std::string>::const_iterator end() const
|
||||
{
|
||||
return m_header.end();
|
||||
}
|
||||
|
||||
void AddHeader(std::string const& name, std::string const& value);
|
||||
|
||||
static std::string const HOST;
|
||||
static std::string const CONTENT_LENGTH;
|
||||
static std::string const ACCEPT_ENCODING;
|
||||
static std::string const CONNECTION;
|
||||
private:
|
||||
std::multimap<std::string, std::string> m_header;
|
||||
};
|
||||
}
|
||||
20
include/Http.hpp
Normal file
20
include/Http.hpp
Normal file
@ -0,0 +1,20 @@
|
||||
// https://github.com/mfichman/http
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Response.hpp"
|
||||
#include "Request.hpp"
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
class Http
|
||||
{
|
||||
public:
|
||||
static Response Get(std::string const& path, std::string const& data = "");
|
||||
static Response Post(std::string const& path, std::string const& data = "");
|
||||
|
||||
private:
|
||||
static Response Send(Request const& request);
|
||||
static std::string Str(Request const& request);
|
||||
};
|
||||
}
|
||||
108
include/IPAddress.hpp
Normal file
108
include/IPAddress.hpp
Normal file
@ -0,0 +1,108 @@
|
||||
#pragma once
|
||||
|
||||
#include <chrono>
|
||||
#include <istream>
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
|
||||
#include "Net.hpp"
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
class IPAddress
|
||||
{
|
||||
public:
|
||||
inline IPAddress(const IPAddress &addr, uint16_t port)
|
||||
: m_address(addr.ToInteger())
|
||||
, m_valid(true)
|
||||
, m_port(port)
|
||||
{
|
||||
}
|
||||
|
||||
inline IPAddress()
|
||||
: m_address(0)
|
||||
, m_valid(false)
|
||||
, m_port(DEFAULT_SERVER_PORT)
|
||||
{
|
||||
}
|
||||
|
||||
inline IPAddress(const std::string& address, uint16_t port = DEFAULT_SERVER_PORT)
|
||||
: m_address(0)
|
||||
, m_valid(false)
|
||||
, m_port(port)
|
||||
{
|
||||
Resolve(address);
|
||||
}
|
||||
|
||||
inline IPAddress(const char* address, uint16_t port = DEFAULT_SERVER_PORT)
|
||||
: m_address(0)
|
||||
, m_valid(false)
|
||||
, m_port(port)
|
||||
{
|
||||
Resolve(address);
|
||||
}
|
||||
|
||||
inline IPAddress(uint8_t byte0, uint8_t byte1, uint8_t byte2, uint8_t byte3, uint16_t port = DEFAULT_SERVER_PORT)
|
||||
: m_address(htonl((byte0 << 24) | (byte1 << 16) | (byte2 << 8) | byte3))
|
||||
, m_valid(true)
|
||||
, m_port(port)
|
||||
{
|
||||
}
|
||||
|
||||
inline explicit IPAddress(uint32_t address, uint16_t port = DEFAULT_SERVER_PORT)
|
||||
: m_address(htonl(address))
|
||||
, m_valid(true)
|
||||
, m_port(port)
|
||||
{
|
||||
}
|
||||
|
||||
std::string ToString() const;
|
||||
inline uint32_t ToInteger() const { return ntohl(m_address); }
|
||||
inline uint16_t GetPort() const { return m_port; }
|
||||
|
||||
static const IPAddress None;
|
||||
static const IPAddress Any;
|
||||
static const IPAddress LocalHost;
|
||||
static const IPAddress Broadcast;
|
||||
|
||||
inline sockaddr_in ToCAddr() const
|
||||
{
|
||||
sockaddr_in addr;
|
||||
std::memset(&addr, 0, sizeof(addr));
|
||||
addr.sin_addr.s_addr = htonl(ToInteger());
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = htons(GetPort());
|
||||
|
||||
return addr;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
friend bool operator <(const IPAddress& left, const IPAddress& right);
|
||||
|
||||
void Resolve(const std::string& address);
|
||||
|
||||
private:
|
||||
uint32_t m_address;
|
||||
bool m_valid;
|
||||
uint16_t m_port;
|
||||
};
|
||||
|
||||
inline bool operator ==(const IPAddress& left, const IPAddress& right) { return !(left < right) && !(right < left); }
|
||||
inline bool operator !=(const IPAddress& left, const IPAddress& right) { return !(left == right); }
|
||||
inline bool operator <(const IPAddress& left, const IPAddress& right) { return std::make_pair(left.m_valid, left.m_address) < std::make_pair(right.m_valid, right.m_address); }
|
||||
inline bool operator >(const IPAddress& left, const IPAddress& right) { return right < left; }
|
||||
inline bool operator <=(const IPAddress& left, const IPAddress& right) { return !(right < left); }
|
||||
inline bool operator >=(const IPAddress& left, const IPAddress& right) { return !(left < right); }
|
||||
|
||||
inline std::istream& operator >>(std::istream& stream, IPAddress& address)
|
||||
{
|
||||
std::string str;
|
||||
stream >> str;
|
||||
address = IPAddress(str);
|
||||
|
||||
return stream;
|
||||
}
|
||||
|
||||
inline std::ostream& operator <<(std::ostream& stream, const IPAddress& address) { return stream << address.ToString(); }
|
||||
}
|
||||
74
include/ISocket.hpp
Normal file
74
include/ISocket.hpp
Normal file
@ -0,0 +1,74 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <chrono>
|
||||
#include <memory>
|
||||
|
||||
#include "Net.hpp"
|
||||
#include "IPAddress.hpp"
|
||||
#include "Enums.hpp"
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
class Socket;
|
||||
|
||||
class ISocket
|
||||
{
|
||||
public:
|
||||
inline ISocket()
|
||||
: m_socketType(SocketType::Unknown)
|
||||
, m_protocol(SocketProtocol::IPv4)
|
||||
{
|
||||
}
|
||||
|
||||
inline ISocket(SocketType InSocketType, SocketProtocol protocol = SocketProtocol::IPv4)
|
||||
: m_socketType(InSocketType)
|
||||
, m_protocol(protocol)
|
||||
{
|
||||
}
|
||||
|
||||
inline virtual ~ISocket()
|
||||
{
|
||||
}
|
||||
|
||||
virtual bool Close() = 0;
|
||||
virtual bool Bind(const IPAddress &addr) = 0;
|
||||
virtual bool Connect(const IPAddress& addr) = 0;
|
||||
virtual bool Listen() = 0;
|
||||
virtual bool WaitForPendingConnection(bool& hasPendingConnection, std::chrono::milliseconds t) = 0;
|
||||
virtual bool HasPendingData(uint32_t& pendingDataSize) = 0;
|
||||
virtual std::unique_ptr<Socket> Accept() = 0;
|
||||
virtual bool SendTo(const uint8_t* data, int32_t count, int32_t& sent, const IPAddress& addrDest) = 0;
|
||||
virtual bool Send(const uint8_t* data, int32_t count, int32_t& sent) = 0;
|
||||
virtual bool RecvFrom(uint8_t* data, int32_t size, int32_t& read, IPAddress& srcAddr, SocketReceiveFlags flags = SocketReceiveFlags::None) = 0;
|
||||
virtual bool Recv(uint8_t* data, int32_t size, int32_t& read, SocketReceiveFlags flags = SocketReceiveFlags::None) = 0;
|
||||
virtual bool Wait(SocketWaitConditions cond, std::chrono::milliseconds t) = 0;
|
||||
virtual SocketConnectionState GetConnectionState() = 0;
|
||||
virtual void GetAddress(IPAddress& outAddr) = 0;
|
||||
virtual bool GetPeerAddress(IPAddress& outAddr) = 0;
|
||||
virtual bool SetNonBlocking(bool isNonBlocking = true) = 0;
|
||||
virtual bool JoinMulticastGroup(const IPAddress& addrStr) = 0;
|
||||
virtual bool LeaveMulticastGroup(const IPAddress& addrStr) = 0;
|
||||
virtual bool SetMulticastLoopback(bool loopback) = 0;
|
||||
virtual bool SetMulticastTtl(uint8_t timeToLive) = 0;
|
||||
virtual bool SetReuseAddr(bool allowReuse = true) = 0;
|
||||
virtual bool SetLinger(bool shouldLinger = true, int32_t t = 0) = 0;
|
||||
virtual bool SetSendBufferSize(int32_t size, int32_t& newSize) = 0;
|
||||
virtual bool SetReceiveBufferSize(int32_t size, int32_t& newSize) = 0;
|
||||
virtual uint32_t GetPort() = 0;
|
||||
|
||||
inline SocketType GetSocketType() const
|
||||
{
|
||||
return m_socketType;
|
||||
}
|
||||
|
||||
inline SocketProtocol GetSocketProtocol() const
|
||||
{
|
||||
return m_protocol;
|
||||
}
|
||||
|
||||
private:
|
||||
const SocketType m_socketType;
|
||||
const SocketProtocol m_protocol;
|
||||
};
|
||||
}
|
||||
@ -1,25 +1,40 @@
|
||||
#ifndef INIT_HPP
|
||||
#define INIT_HPP
|
||||
#pragma once
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
#include "Defs.hpp"
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
namespace priv
|
||||
{
|
||||
static WSADATA WsaData;
|
||||
static bool Initialized;
|
||||
}
|
||||
|
||||
struct Initialization
|
||||
{
|
||||
static bool Initialize();
|
||||
inline static bool Initialize()
|
||||
{
|
||||
if (priv::Initialized) return true;
|
||||
return (priv::Initialized = WSAStartup(MAKEWORD(2, 2), &priv::WsaData)) == 0;
|
||||
}
|
||||
|
||||
#ifdef _MSC_VER
|
||||
const WSADATA &GetData();
|
||||
#endif
|
||||
inline static void Cleanup()
|
||||
{
|
||||
if (priv::Initialized)
|
||||
{
|
||||
WSACleanup();
|
||||
priv::Initialized = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
#ifdef _MSC_VER
|
||||
static WSADATA wsa_data;
|
||||
#endif
|
||||
};
|
||||
#else
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
inline static bool Initialize() { return true; }
|
||||
inline static void Cleanup() { }
|
||||
}
|
||||
|
||||
#endif
|
||||
40
include/Net.hpp
Normal file
40
include/Net.hpp
Normal file
@ -0,0 +1,40 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
#undef SendMessage
|
||||
#undef SetPort
|
||||
|
||||
#define poll WSAPoll
|
||||
#define ioctl ioctlsocket
|
||||
|
||||
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
#define SOCKET_ERROR -1
|
||||
#define NO_ERROR 0
|
||||
#define INVALID_SOCKET NO_ERROR
|
||||
|
||||
#define SOCKET int
|
||||
#define closesocket close
|
||||
#define ioctlsocket ioctl
|
||||
|
||||
int closesocket(SOCKET soc)
|
||||
{
|
||||
return close(soc);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#define DEFAULT_SERVER_PORT 61250
|
||||
@ -1,24 +0,0 @@
|
||||
#ifndef NETWORK_BUFFER_HPP
|
||||
#define NETWORK_BUFFER_HPP
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
#include "Defs.hpp"
|
||||
#include "Utility.hpp"
|
||||
|
||||
#include <vector>
|
||||
|
||||
struct NetworkBuffer
|
||||
{
|
||||
NetworkBuffer();
|
||||
~NetworkBuffer();
|
||||
|
||||
std::vector<byte> header; // size must always be sizeof(int32)
|
||||
std::vector<byte> body;
|
||||
|
||||
bool valid = false;
|
||||
};
|
||||
|
||||
#endif
|
||||
@ -1,32 +0,0 @@
|
||||
#ifndef NETWORK_MESSAGE_HPP
|
||||
#define NETWORK_MESSAGE_HPP
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
#include "Defs.hpp"
|
||||
#include "NetworkBuffer.hpp"
|
||||
|
||||
struct NetworkMessage
|
||||
{
|
||||
NetworkMessage();
|
||||
NetworkMessage(const NetworkBuffer &buffer);
|
||||
NetworkMessage(uint16 sender, byte distribution_mode, uint16 destination_id, byte tag, byte subject);
|
||||
~NetworkMessage();
|
||||
|
||||
static const NetworkBuffer &EncodeMessage(const NetworkMessage &message);
|
||||
|
||||
static const NetworkMessage &DecodeMessage(const NetworkBuffer &buffer);
|
||||
|
||||
uint16 sender = -2;
|
||||
byte distribution_mode;
|
||||
uint16 destination_id;
|
||||
byte tag;
|
||||
byte subject;
|
||||
void *data;
|
||||
|
||||
bool valid = false;
|
||||
};
|
||||
|
||||
#endif
|
||||
67
include/Parse.hpp
Normal file
67
include/Parse.hpp
Normal file
@ -0,0 +1,67 @@
|
||||
// https://github.com/mfichman/http
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
template <typename T>
|
||||
class ParseResult
|
||||
{
|
||||
public:
|
||||
T value;
|
||||
char const* ch;
|
||||
};
|
||||
|
||||
template <typename F>
|
||||
static ParseResult<std::string> ParseUntil(char const* str, F func)
|
||||
{
|
||||
ParseResult<std::string> result{};
|
||||
char const* ch = str;
|
||||
for (; *ch && !func(*ch); ++ch)
|
||||
{
|
||||
}
|
||||
|
||||
result.value = std::string(str, ch - str);
|
||||
result.ch = ch;
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename F>
|
||||
static inline ParseResult<std::string> ParseWhile(char const* str, F func)
|
||||
{
|
||||
ParseResult<std::string> result{};
|
||||
char const* ch = str;
|
||||
for (; *ch && func(*ch); ++ch)
|
||||
{
|
||||
}
|
||||
|
||||
result.value = std::string(str, ch - str);
|
||||
result.ch = ch;
|
||||
return result;
|
||||
}
|
||||
|
||||
static inline ParseResult<std::string> ParseToken(char const* str)
|
||||
{
|
||||
auto token = ParseUntil(str, isspace);
|
||||
token.ch = ParseWhile(token.ch, isspace).ch;
|
||||
return token;
|
||||
}
|
||||
|
||||
static inline ParseResult<std::string> parseCrLf(char const* str)
|
||||
{
|
||||
auto cr = ParseUntil(str, [](char ch) { return ch == '\r'; });
|
||||
if (*cr.ch == '\r')
|
||||
cr.ch++;
|
||||
return ParseWhile(cr.ch, [](char ch)
|
||||
{
|
||||
return isspace(ch) && ch != '\r';
|
||||
});
|
||||
}
|
||||
|
||||
static inline ParseResult<std::string> ParseWhitespace(char const* str)
|
||||
{
|
||||
return ParseWhile(str, isspace);
|
||||
}
|
||||
}
|
||||
@ -1,13 +0,0 @@
|
||||
#ifndef PLUGIN_MANAGER_HPP
|
||||
#define PLUGIN_MANAGER_HPP
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
struct PluginManager
|
||||
{
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
52
include/Request.hpp
Normal file
52
include/Request.hpp
Normal file
@ -0,0 +1,52 @@
|
||||
// https://github.com/mfichman/http
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Uri.hpp"
|
||||
#include "Headers.hpp"
|
||||
#include "Enums.hpp"
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
class Request
|
||||
{
|
||||
public:
|
||||
Method GetMethod() const
|
||||
{
|
||||
return m_method;
|
||||
}
|
||||
|
||||
const Uri& GetUri() const
|
||||
{
|
||||
return m_uri;
|
||||
}
|
||||
|
||||
const std::string& GetPath() const
|
||||
{
|
||||
return m_uri.GetPath();
|
||||
}
|
||||
|
||||
const std::string& GetData() const
|
||||
{
|
||||
return m_data;
|
||||
}
|
||||
|
||||
const std::string GetHeaderElement(const std::string& name) const;
|
||||
|
||||
const Headers& GetHeaders() const
|
||||
{
|
||||
return m_headers;
|
||||
}
|
||||
|
||||
void SetMethod(Method method);
|
||||
void SetUri(const Uri& path);
|
||||
void SetData(const std::string& data);
|
||||
void AddHeader(const std::string& name, const std::string& value);
|
||||
|
||||
private:
|
||||
Method m_method = Method::GET;
|
||||
Uri m_uri;
|
||||
std::string m_data;
|
||||
Headers m_headers;
|
||||
};
|
||||
}
|
||||
43
include/Response.hpp
Normal file
43
include/Response.hpp
Normal file
@ -0,0 +1,43 @@
|
||||
// https://github.com/mfichman/http
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Headers.hpp"
|
||||
#include "Cookies.hpp"
|
||||
#include "Enums.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
class Response
|
||||
{
|
||||
public:
|
||||
Response(const std::string& text);
|
||||
Response() {};
|
||||
|
||||
HttpStatus GetStatus() const
|
||||
{
|
||||
return m_status;
|
||||
}
|
||||
|
||||
const std::string& daGetDatata() const
|
||||
{
|
||||
return m_data;
|
||||
}
|
||||
|
||||
const std::string GetHeader(const std::string& name) const;
|
||||
const Cookie GetCookie(const std::string& name) const;
|
||||
|
||||
void SetStatus(HttpStatus status);
|
||||
void SetData(const std::string& data);
|
||||
void SetHeader(const std::string& name, const std::string& value);
|
||||
void SetCookie(const Cookie& cookie);
|
||||
|
||||
private:
|
||||
HttpStatus m_status = HttpStatus::INVALID_CODE;
|
||||
std::string m_data;
|
||||
Headers m_headers;
|
||||
Cookies m_cookies;
|
||||
};
|
||||
}
|
||||
43
include/SecureSocket.hpp
Normal file
43
include/SecureSocket.hpp
Normal file
@ -0,0 +1,43 @@
|
||||
#pragma once
|
||||
|
||||
#include "Socket.hpp"
|
||||
|
||||
//#include <openssl/rand.h>
|
||||
//#include <openssl/ssl.h>
|
||||
//#include <openssl/err.h>
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
class SecureSocket
|
||||
{
|
||||
public:
|
||||
SecureSocket();
|
||||
|
||||
bool Connect(const IPAddress& addr);
|
||||
bool Close() const;
|
||||
bool HasPendingData(uint32_t& pendingDataSize) const;
|
||||
bool Send(uint8_t* data, int32_t count, int32_t &sent, int flags = 0); // Execute 1 write() syscall
|
||||
bool Recv(uint8_t* data, int32_t count, int32_t &read, int flags = 0); // Execte 1 read() syscall
|
||||
bool Wait(SocketWaitConditions cond, std::chrono::milliseconds t) const;
|
||||
SocketConnectionState GetConnectionState() const;
|
||||
void GetAddress(IPAddress& outAddr) const;
|
||||
int32_t GetPort() const;
|
||||
|
||||
void UseCertificateFile(std::string const& path);
|
||||
void UsePrivateKeyFile(std::string const& path);
|
||||
|
||||
private:
|
||||
bool SendRaw(uint8_t* buf, size_t len, int flags = 0);
|
||||
bool SendFromBio(int flags = 0);
|
||||
bool RecvToBio(int flags = 0);
|
||||
void HandleReturn(size_t ret);
|
||||
|
||||
std::unique_ptr<Socket> m_socket;
|
||||
|
||||
//SSL_CTX* m_context;
|
||||
//SSL* m_conn;
|
||||
//BIO* m_in;
|
||||
//BIO* m_out;
|
||||
bool m_eof;
|
||||
};
|
||||
}
|
||||
@ -1,17 +0,0 @@
|
||||
#ifndef SERIALIZER_HPP
|
||||
#define SERIALIZER_HPP
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
#include "Defs.hpp"
|
||||
#include <vector>
|
||||
|
||||
struct Serializer
|
||||
{
|
||||
template<typename T> static const std::vector<byte> &to_bytes(const T &object);
|
||||
template<typename T> static const T& from_bytes(const std::vector<byte> &bytes, T& object);
|
||||
};
|
||||
|
||||
#endif
|
||||
84
include/Socket.hpp
Normal file
84
include/Socket.hpp
Normal file
@ -0,0 +1,84 @@
|
||||
#pragma once
|
||||
|
||||
#include "ISocket.hpp"
|
||||
#include "Enums.hpp"
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
class Socket : public ISocket
|
||||
{
|
||||
public:
|
||||
inline Socket(SocketType socketType, SocketProtocol protocol = SocketProtocol::IPv4)
|
||||
: ISocket(socketType, protocol)
|
||||
{
|
||||
init();
|
||||
}
|
||||
|
||||
inline Socket(SOCKET newSocket, SocketType socketType, SocketProtocol protocol = SocketProtocol::IPv4)
|
||||
: ISocket(socketType, protocol)
|
||||
, m_socket(newSocket)
|
||||
{
|
||||
init();
|
||||
}
|
||||
|
||||
virtual ~Socket() { Close(); }
|
||||
|
||||
public:
|
||||
|
||||
// ISocket overrides
|
||||
|
||||
virtual bool Close() override;
|
||||
virtual bool Bind(const IPAddress &addr) override;
|
||||
virtual bool Connect(const IPAddress& addr) override;
|
||||
inline virtual bool Listen() override { return listen(m_socket, SOMAXCONN) == 0; }
|
||||
virtual bool WaitForPendingConnection(bool& hasPendingConnection, std::chrono::milliseconds t) override;
|
||||
virtual bool HasPendingData(uint32_t& pendingDataSize) override;
|
||||
virtual std::unique_ptr<Socket> Accept() override;
|
||||
virtual bool SendTo(const uint8_t* data, int32_t count, int32_t& sent, const IPAddress& addrDest) override;
|
||||
virtual bool Send(const uint8_t* data, int32_t count, int32_t& sent) override;
|
||||
virtual bool RecvFrom(uint8_t* data, int32_t size, int32_t& read, IPAddress& srcAddr, SocketReceiveFlags flags = SocketReceiveFlags::None) override;
|
||||
virtual bool Recv(uint8_t* data, int32_t size, int32_t& read, SocketReceiveFlags flags = SocketReceiveFlags::None) override;
|
||||
virtual bool Wait(SocketWaitConditions cond, std::chrono::milliseconds t) override;
|
||||
virtual SocketConnectionState GetConnectionState() override;
|
||||
virtual void GetAddress(IPAddress& outAddr) override;
|
||||
virtual bool GetPeerAddress(IPAddress& outAddr) override;
|
||||
virtual bool SetNonBlocking(bool isNonBlocking = true) override;
|
||||
|
||||
virtual bool JoinMulticastGroup(const IPAddress& addrStr) override;
|
||||
virtual bool LeaveMulticastGroup(const IPAddress& addrStr) override;
|
||||
inline virtual bool SetMulticastLoopback(bool loopback) override { return (setsockopt(m_socket, IPPROTO_IP, IP_MULTICAST_LOOP, (char*)&loopback, sizeof(loopback)) == 0); }
|
||||
inline virtual bool SetMulticastTtl(uint8_t timeToLive) override { return (setsockopt(m_socket, IPPROTO_IP, IP_MULTICAST_TTL, (char*)&timeToLive, sizeof(timeToLive)) == 0); }
|
||||
inline virtual bool SetReuseAddr(bool allowReuse = true) override
|
||||
{
|
||||
int param = allowReuse ? 1 : 0;
|
||||
return setsockopt(m_socket, SOL_SOCKET, SO_REUSEADDR, (char*)¶m, sizeof(param)) == 0;
|
||||
}
|
||||
|
||||
virtual bool SetLinger(bool shouldLinger = true, int32_t t = 0) override;
|
||||
virtual bool SetSendBufferSize(int32_t size, int32_t& newSize) override;
|
||||
virtual bool SetReceiveBufferSize(int32_t size, int32_t& newSize) override;
|
||||
virtual uint32_t GetPort() override;
|
||||
|
||||
SOCKET GetNativeSocket()
|
||||
{
|
||||
return m_socket;
|
||||
}
|
||||
|
||||
private:
|
||||
void init();
|
||||
|
||||
virtual SocketReturn HasState(SocketParam state, std::chrono::milliseconds t = std::chrono::milliseconds(0));
|
||||
virtual SocketErrors TranslateErrorCode(int32_t code);
|
||||
virtual int TranslateFlags(SocketReceiveFlags flags);
|
||||
|
||||
inline void UpdateActivity()
|
||||
{
|
||||
m_lastActivityTime = std::chrono::system_clock::now().time_since_epoch().count();
|
||||
}
|
||||
|
||||
private:
|
||||
SOCKET m_socket = INVALID_SOCKET;
|
||||
|
||||
long long m_lastActivityTime = 0;
|
||||
};
|
||||
}
|
||||
@ -1,79 +1,33 @@
|
||||
#ifndef TCP_CLIENT_HPP
|
||||
#define TCP_CLIENT_HPP
|
||||
|
||||
#include "Defs.hpp"
|
||||
#include "NetworkMessage.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
#include <future>
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
struct TcpClient
|
||||
#include "Socket.hpp"
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
TcpClient(const SOCKET &socket);
|
||||
TcpClient(const std::string &ip);
|
||||
TcpClient(const std::string &ip, uint16 port = default_client_port);
|
||||
~TcpClient();
|
||||
class TcpConnectionHandler;
|
||||
}
|
||||
|
||||
void Shutdown();
|
||||
namespace std::net
|
||||
{
|
||||
class TcpClient
|
||||
{
|
||||
friend class std::net::TcpConnectionHandler;
|
||||
|
||||
const std::string &GetIP();
|
||||
void SetIP(const std::string &ip);
|
||||
public:
|
||||
TcpClient(Socket *soc);
|
||||
TcpClient(SocketProtocol protocol = SocketProtocol::IPv4);
|
||||
|
||||
uint16 GetPort();
|
||||
void SetPort(uint16 port);
|
||||
bool Connect(const IPAddress& addrStr);
|
||||
bool Close() const;
|
||||
bool HasPendingData(uint32_t& pendingDataSize) const;
|
||||
bool Send(const uint8_t* data, int32_t count, int32_t& sent) const;
|
||||
bool Recv(uint8_t* data, int32_t size, int32_t& read, SocketReceiveFlags flags = SocketReceiveFlags::None) const;
|
||||
bool Wait(SocketWaitConditions cond, std::chrono::milliseconds t) const;
|
||||
SocketConnectionState GetConnectionState() const;
|
||||
void GetAddress(IPAddress& outAddr) const;
|
||||
int32_t GetPort() const;
|
||||
|
||||
uint16 GetID();
|
||||
void SetID(uint16 id);
|
||||
|
||||
bool Connect();
|
||||
|
||||
bool DataAvailable(int32 &size);
|
||||
|
||||
//this method will receive the messages automaticaly and use the callback methods
|
||||
void ReceiveMessages();
|
||||
|
||||
//this is a more manual method with no callbacks
|
||||
const NetworkMessage &ReceiveMessage();
|
||||
|
||||
std::future<bool> SendMessage(const NetworkMessage &message);
|
||||
bool SendBytes(const std::vector<byte> &bytes);
|
||||
bool SendBytes(byte *bytes, uint32 lenght);
|
||||
|
||||
void SetOnDisconnectCallback(std::function<void(uint16)> func);
|
||||
void SetOnConnectCallback(std::function<void(uint16)> func);
|
||||
void SetOnMessageCallback(std::function<void(uint16, byte, byte, void*)> func);
|
||||
|
||||
static const TcpClient &DefaultTcpClient();
|
||||
|
||||
private:
|
||||
TcpClient();
|
||||
|
||||
const NetworkBuffer &receive_data_array();
|
||||
static void receive_data(TcpClient *client);
|
||||
static bool send_network_message(const NetworkMessage &message, TcpClient *client);
|
||||
static void close_connection(TcpClient *client);
|
||||
|
||||
bool initialize(const std::string &ip, uint16 port = default_client_port);
|
||||
|
||||
uint16 id = -2;
|
||||
std::string ip;
|
||||
uint16 port = 0;
|
||||
bool initialized = false;
|
||||
bool receive = false;
|
||||
|
||||
std::function<void(uint16)> OnDisconnect;
|
||||
std::function<void(uint16)> OnConnect;
|
||||
std::function<void(uint16, byte, byte, void*)> OnMessage;
|
||||
|
||||
SOCKET tcp_socket = INVALID_SOCKET;
|
||||
struct addrinfo *result = nullptr;
|
||||
struct addrinfo hints;
|
||||
};
|
||||
|
||||
#endif
|
||||
private:
|
||||
std::unique_ptr<Socket> m_socket;
|
||||
};
|
||||
}
|
||||
32
include/TcpListener.hpp
Normal file
32
include/TcpListener.hpp
Normal file
@ -0,0 +1,32 @@
|
||||
#pragma once
|
||||
|
||||
#include <thread>
|
||||
#include <queue>
|
||||
|
||||
#include "Socket.hpp"
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
class TcpConnectionHandler;
|
||||
}
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
class TcpClient;
|
||||
|
||||
class TcpListener
|
||||
{
|
||||
friend class std::net::TcpConnectionHandler;
|
||||
|
||||
public:
|
||||
TcpListener(uint16_t port, std::chrono::milliseconds inSleepTime = std::chrono::milliseconds(1));
|
||||
TcpListener(Socket *InSocket, std::chrono::milliseconds inSleepTime = std::chrono::milliseconds(1));
|
||||
|
||||
TcpClient *AcceptClient();
|
||||
|
||||
private:
|
||||
std::chrono::milliseconds m_sleepTime;
|
||||
std::unique_ptr<Socket> m_socket;
|
||||
uint16_t m_port = 0;
|
||||
};
|
||||
}
|
||||
@ -1,64 +0,0 @@
|
||||
#ifndef TCP_SERVER_HPP
|
||||
#define TCP_SERVER_HPP
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
#include "Defs.hpp"
|
||||
#include "TcpClient.hpp"
|
||||
#include "Handshake.hpp"
|
||||
|
||||
#include <functional>
|
||||
|
||||
struct TcpServer
|
||||
{
|
||||
TcpServer();
|
||||
TcpServer(uint16 port = default_server_port);
|
||||
~TcpServer();
|
||||
|
||||
void Shutdown();
|
||||
|
||||
bool StartServer(bool accept_connections); // if accept_connections is false the user must call the funcion AcceptConnections()
|
||||
void AcceptConnections();
|
||||
|
||||
bool SendMessage(const NetworkMessage &message);
|
||||
|
||||
void RejectConnection(TcpClient &client);
|
||||
void AcceptConnection(uint16 client);
|
||||
|
||||
void CloseSocket(TcpClient &client);
|
||||
void CloseSocket(uint16 id);
|
||||
|
||||
uint16 GetMaxConnections();
|
||||
void SetMaxConnections(uint16 value);
|
||||
|
||||
const TcpClient &GetClientByID(uint16 id);
|
||||
|
||||
std::vector<std::function<void(const NetworkMessage &message)>> OnMessageFunctions; // this is going to be used for plugins
|
||||
|
||||
private:
|
||||
static void process_client_messages(TcpServer *server, TcpClient &client);
|
||||
static void accept_connections(TcpServer *server);
|
||||
|
||||
void add_to_clients_list(TcpClient &client);
|
||||
|
||||
uint16 allocate_id();
|
||||
|
||||
void shutdown_internal();
|
||||
|
||||
bool initialize(uint16 port = default_server_port);
|
||||
|
||||
bool initialized = false;
|
||||
bool running = false;
|
||||
|
||||
uint16 max_connections = 0;
|
||||
|
||||
std::vector<TcpClient> clients;
|
||||
|
||||
SOCKET server_tcp_socket = INVALID_SOCKET;
|
||||
struct addrinfo *result = nullptr;
|
||||
struct addrinfo hints;
|
||||
};
|
||||
|
||||
#endif
|
||||
112
include/TcpSocketBuilder.hpp
Normal file
112
include/TcpSocketBuilder.hpp
Normal file
@ -0,0 +1,112 @@
|
||||
#pragma once
|
||||
|
||||
#include "Enums.hpp"
|
||||
#include "IPAddress.hpp"
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
class Socket;
|
||||
class TcpClient;
|
||||
class TcpListener;
|
||||
|
||||
class TcpSocketBuilder
|
||||
{
|
||||
public:
|
||||
inline TcpSocketBuilder()
|
||||
: m_blocking(false)
|
||||
, m_bound(false)
|
||||
, m_boundAddr(IPAddress::Any, 0)
|
||||
, m_linger(false)
|
||||
, m_lingerTimeout(0)
|
||||
, m_listen(false)
|
||||
, m_receiveBufferSize(0)
|
||||
, m_reusable(false)
|
||||
, m_sendBufferSize(0)
|
||||
, m_socketProtocol(SocketProtocol::IPv4)
|
||||
{
|
||||
}
|
||||
|
||||
inline TcpSocketBuilder AsBlocking()
|
||||
{
|
||||
m_blocking = true;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline TcpSocketBuilder AsNonBlocking()
|
||||
{
|
||||
m_blocking = false;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline TcpSocketBuilder AsReusable()
|
||||
{
|
||||
m_reusable = true;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline TcpSocketBuilder Bind(const IPAddress &addr)
|
||||
{
|
||||
m_boundAddr = addr;
|
||||
m_bound = true;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline TcpSocketBuilder Lingering(int32_t Timeout)
|
||||
{
|
||||
m_linger = true;
|
||||
m_lingerTimeout = Timeout;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline TcpSocketBuilder Listening()
|
||||
{
|
||||
m_listen = true;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline TcpSocketBuilder WithReceiveBufferSize(int32_t SizeInBytes)
|
||||
{
|
||||
m_receiveBufferSize = SizeInBytes;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline TcpSocketBuilder WithSendBufferSize(int32_t SizeInBytes)
|
||||
{
|
||||
m_sendBufferSize = SizeInBytes;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline TcpSocketBuilder Protocol(SocketProtocol prot)
|
||||
{
|
||||
m_socketProtocol = prot;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
public:
|
||||
std::unique_ptr<Socket> Build() const;
|
||||
std::unique_ptr<TcpClient> BuildClient() const;
|
||||
std::unique_ptr<TcpListener> BuildListener() const;
|
||||
|
||||
private:
|
||||
bool m_blocking;
|
||||
bool m_bound;
|
||||
IPAddress m_boundAddr;
|
||||
bool m_linger;
|
||||
int32_t m_lingerTimeout;
|
||||
bool m_listen;
|
||||
int32_t m_receiveBufferSize;
|
||||
bool m_reusable;
|
||||
int32_t m_sendBufferSize;
|
||||
|
||||
SocketProtocol m_socketProtocol;
|
||||
};
|
||||
}
|
||||
@ -1,63 +0,0 @@
|
||||
#ifndef UDP_CLIENT_HPP
|
||||
#define UDP_CLIENT_HPP
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
#include "Defs.hpp"
|
||||
#include "NetworkMessage.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
#include <future>
|
||||
|
||||
struct UdpClient
|
||||
{
|
||||
UdpClient(const SOCKET &socket);
|
||||
UdpClient(const std::string &ip);
|
||||
UdpClient(const std::string &ip, uint16 = default_client_port);
|
||||
~UdpClient();
|
||||
|
||||
void Shutdown();
|
||||
|
||||
const std::string &GetIP();
|
||||
void SetIP(const std::string &ip);
|
||||
|
||||
uint16 GetPort();
|
||||
void SetPort(uint16 port);
|
||||
|
||||
uint16 GetID();
|
||||
void SetID(uint16 id);
|
||||
|
||||
void ReceiveMessages();
|
||||
|
||||
const NetworkMessage &ReceiveMessage();
|
||||
|
||||
std::future<bool> SendMessage(const NetworkMessage &message);
|
||||
bool SendBytes(const std::vector<byte> &bytes);
|
||||
bool SendBytes(byte *bytes, uint32 lenght);
|
||||
|
||||
private:
|
||||
const NetworkBuffer &receive_data_array();
|
||||
static void receive_data(UdpClient *client);
|
||||
bool initialize(const std::string &ip, uint16 port = default_client_port);
|
||||
|
||||
static bool send_network_message(const NetworkMessage &message, UdpClient *client);
|
||||
|
||||
uint16 id = -2;
|
||||
|
||||
std::string ip;
|
||||
uint16 port = 0;
|
||||
bool initialized = false;
|
||||
bool receive = false;
|
||||
|
||||
std::function<void(uint16)> OnDisconnect;
|
||||
std::function<void(uint16)> OnConnect;
|
||||
std::function<void(uint16, byte, byte, void*)> OnMessage;
|
||||
|
||||
SOCKET udp_socket = INVALID_SOCKET;
|
||||
};
|
||||
|
||||
#endif
|
||||
@ -1,34 +0,0 @@
|
||||
#ifndef UDP_SERVER_HPP
|
||||
#define UDP_SERVER_HPP
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
#include "Defs.hpp"
|
||||
#include "UdpClient.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
struct UdpServer
|
||||
{
|
||||
private:
|
||||
bool initialize(uint16 port = default_server_port);
|
||||
|
||||
uint16 allocate_id();
|
||||
|
||||
void shutdown_internal();
|
||||
|
||||
bool initialized = false;
|
||||
bool running = false;
|
||||
|
||||
uint16 max_connections = 0;
|
||||
|
||||
std::vector<UdpClient> clients;
|
||||
|
||||
SOCKET server_udp_socket = INVALID_SOCKET;
|
||||
struct sockaddr_in server;
|
||||
};
|
||||
|
||||
#endif
|
||||
29
include/UdpSocket.hpp
Normal file
29
include/UdpSocket.hpp
Normal file
@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
#include "Socket.hpp"
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
class UdpSocket
|
||||
{
|
||||
public:
|
||||
UdpSocket(Socket *soc);
|
||||
UdpSocket(SocketProtocol protocol = SocketProtocol::IPv4);
|
||||
|
||||
bool Bind(const IPAddress &addr);
|
||||
bool SendTo(const uint8_t* data, int32_t count, int32_t& sent, const IPAddress& addrDest);
|
||||
bool RecvFrom(uint8_t* data, int32_t size, int32_t& read, IPAddress& srcAddr, SocketReceiveFlags flags = SocketReceiveFlags::None);
|
||||
bool GetPeerAddress(IPAddress& outAddr);
|
||||
bool JoinMulticastGroup(const IPAddress& addrStr);
|
||||
bool LeaveMulticastGroup(const IPAddress& addrStr);
|
||||
bool SetMulticastLoopback(bool loopback);
|
||||
bool SetMulticastTtl(uint8_t timeToLive);
|
||||
uint32_t GetPort();
|
||||
bool SetReuseAddr(bool allowReuse = true);
|
||||
|
||||
private:
|
||||
std::unique_ptr<Socket> m_socket;
|
||||
};
|
||||
}
|
||||
152
include/UdpSocketBuilder.hpp
Normal file
152
include/UdpSocketBuilder.hpp
Normal file
@ -0,0 +1,152 @@
|
||||
#pragma once
|
||||
|
||||
#include "UdpSocket.hpp"
|
||||
#include "IPAddress.hpp"
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
class UdpSocketBuilder
|
||||
{
|
||||
public:
|
||||
UdpSocketBuilder()
|
||||
: m_blocking(false)
|
||||
, m_bound(false)
|
||||
, m_boundEndpoint(IPAddress::Any, 0)
|
||||
, m_multicastLoopback(false)
|
||||
, m_multicastTtl(1)
|
||||
, m_receiveBufferSize(0)
|
||||
, m_reusable(false)
|
||||
, m_sendBufferSize(0)
|
||||
{ }
|
||||
|
||||
public:
|
||||
UdpSocketBuilder AsBlocking()
|
||||
{
|
||||
m_blocking = true;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
UdpSocketBuilder AsNonBlocking()
|
||||
{
|
||||
m_blocking = false;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
UdpSocketBuilder AsReusable()
|
||||
{
|
||||
m_reusable = true;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
UdpSocketBuilder BoundToAddress(const IPAddress& addr)
|
||||
{
|
||||
m_boundEndpoint = IPAddress(addr);
|
||||
m_bound = true;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
UdpSocketBuilder BoundToPort(uint16_t port)
|
||||
{
|
||||
m_boundEndpoint = IPAddress(m_boundEndpoint.ToInteger(), port);
|
||||
m_bound = true;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
UdpSocketBuilder JoinedToGroup(const IPAddress& group_addr)
|
||||
{
|
||||
m_joinedGroups.emplace_back(group_addr);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
UdpSocketBuilder WithMulticastLoopback()
|
||||
{
|
||||
m_multicastLoopback = true;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
UdpSocketBuilder WithMulticastTtl(uint8_t time_to_live)
|
||||
{
|
||||
m_multicastTtl = time_to_live;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
UdpSocketBuilder WithReceiveBufferSize(uint32_t size)
|
||||
{
|
||||
m_receiveBufferSize = size;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
UdpSocketBuilder WithSendBufferSize(uint32_t size)
|
||||
{
|
||||
m_sendBufferSize = size;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
public:
|
||||
std::unique_ptr<UdpSocket> Build() const
|
||||
{
|
||||
std::unique_ptr<Socket> soc = std::make_unique<Socket>(SocketType::Datagram);
|
||||
|
||||
if (soc)
|
||||
{
|
||||
bool Error =
|
||||
!soc->SetNonBlocking(!m_blocking) ||
|
||||
!soc->SetReuseAddr(m_reusable);
|
||||
|
||||
if (!Error)
|
||||
Error = m_bound && !soc->Bind(m_boundEndpoint);
|
||||
if (!Error)
|
||||
Error = !soc->SetMulticastLoopback(m_multicastLoopback) || !soc->SetMulticastTtl(m_multicastTtl);
|
||||
|
||||
if (!Error)
|
||||
{
|
||||
for (const auto& Group : m_joinedGroups)
|
||||
{
|
||||
if (!soc->JoinMulticastGroup(IPAddress(Group, 0)))
|
||||
{
|
||||
Error = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!Error)
|
||||
{
|
||||
int32_t out_new_size;
|
||||
if (m_receiveBufferSize > 0)
|
||||
soc->SetReceiveBufferSize(m_receiveBufferSize, out_new_size);
|
||||
if (m_sendBufferSize > 0)
|
||||
soc->SetSendBufferSize(m_sendBufferSize, out_new_size);
|
||||
}
|
||||
|
||||
if (Error)
|
||||
throw inl::RuntimeException("Couldnt create socket"); // make parameter a string depending on the error
|
||||
return std::make_unique<UdpSocket>();
|
||||
}
|
||||
return std::unique_ptr<UdpSocket>(nullptr);
|
||||
}
|
||||
|
||||
private:
|
||||
bool m_blocking;
|
||||
bool m_bound;
|
||||
IPAddress m_boundEndpoint;
|
||||
std::vector<IPAddress> m_joinedGroups;
|
||||
bool m_multicastLoopback;
|
||||
uint8_t m_multicastTtl;
|
||||
uint32_t m_receiveBufferSize;
|
||||
bool m_reusable;
|
||||
uint32_t m_sendBufferSize;
|
||||
};
|
||||
}
|
||||
78
include/Uri.hpp
Normal file
78
include/Uri.hpp
Normal file
@ -0,0 +1,78 @@
|
||||
// https://github.com/mfichman/http
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
class Authority
|
||||
{
|
||||
public:
|
||||
Authority(const std::string& user, const std::string& host, uint16_t port);
|
||||
Authority();
|
||||
|
||||
const std::string& GetUser() const
|
||||
{
|
||||
return m_user;
|
||||
}
|
||||
|
||||
const std::string& GetHost() const
|
||||
{
|
||||
return m_host;
|
||||
}
|
||||
|
||||
uint16_t GetPort() const
|
||||
{
|
||||
return m_port;
|
||||
}
|
||||
|
||||
void SetUser(const std::string& user);
|
||||
void SetHost(const std::string& host);
|
||||
void SetPort(uint16_t port);
|
||||
private:
|
||||
std::string m_user;
|
||||
std::string m_host;
|
||||
uint16_t m_port;
|
||||
};
|
||||
|
||||
class Uri {
|
||||
public:
|
||||
Uri(const char* value);
|
||||
Uri(const std::string& value);
|
||||
Uri();
|
||||
|
||||
const std::string& GetScheme() const
|
||||
{
|
||||
return m_scheme;
|
||||
}
|
||||
|
||||
const Authority& GetAuthority() const
|
||||
{
|
||||
return m_authority;
|
||||
}
|
||||
|
||||
const std::string& GetPath() const
|
||||
{
|
||||
return m_path;
|
||||
}
|
||||
|
||||
const std::string& GetHost() const
|
||||
{
|
||||
return m_authority.GetHost();
|
||||
}
|
||||
|
||||
uint16_t GetPort() const
|
||||
{
|
||||
return m_authority.GetPort();
|
||||
}
|
||||
|
||||
void SetScheme(const std::string& scheme);
|
||||
void SetAuthority(const Authority& authority);
|
||||
void SetPath(const std::string& path);
|
||||
private:
|
||||
std::string m_scheme;
|
||||
Authority m_authority;
|
||||
std::string m_path;
|
||||
};
|
||||
}
|
||||
37
include/Util.hpp
Normal file
37
include/Util.hpp
Normal file
@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <sstream>
|
||||
|
||||
#include "Net.hpp"
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
inline static std::vector<std::string> Split(const std::string &str, const std::string &delimiter)
|
||||
{
|
||||
std::vector<std::string> splited;
|
||||
if (str.empty() && delimiter.empty())
|
||||
return std::vector<std::string>();
|
||||
std::string::size_type lastPos = str.find_first_not_of(delimiter, 0);
|
||||
std::string::size_type pos = str.find_first_of(delimiter, lastPos);
|
||||
|
||||
while (std::string::npos != pos || std::string::npos != lastPos)
|
||||
{
|
||||
splited.push_back(str.substr(lastPos, pos - lastPos));
|
||||
lastPos = str.find_first_not_of(delimiter, pos);
|
||||
pos = str.find_first_of(delimiter, lastPos);
|
||||
}
|
||||
return splited;
|
||||
}
|
||||
|
||||
sockaddr_in CreateAddress(uint32_t address, uint16_t port)
|
||||
{
|
||||
sockaddr_in addr;
|
||||
std::memset(&addr, 0, sizeof(addr));
|
||||
addr.sin_addr.s_addr = htonl(address);
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = htons(port);
|
||||
|
||||
return addr;
|
||||
}
|
||||
}
|
||||
@ -1,94 +0,0 @@
|
||||
#ifndef UTILITY_HPP
|
||||
#define UTILITY_HPP
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
#include "Defs.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
struct Utility
|
||||
{
|
||||
static void Delete(void *pointer);
|
||||
static void DeleteArray(void *pointer);
|
||||
|
||||
struct BitConverter
|
||||
{
|
||||
static const std::vector<byte> &ToBytes(uint8 number);
|
||||
static uint8 ToUint8(const std::vector<byte> &bytes, uint16 start_index = 0);
|
||||
|
||||
static const std::vector<byte> &ToBytes(uint16 number);
|
||||
static uint16 ToUint16(const std::vector<byte> &bytes, uint16 start_index = 0);
|
||||
|
||||
static const std::vector<byte> &ToBytes(uint32 number);
|
||||
static uint32 ToUint32(const std::vector<byte> &bytes, uint16 start_index = 0);
|
||||
|
||||
static const std::vector<byte> &ToBytes(uint64 number);
|
||||
static uint64 ToUint64(const std::vector<byte> &bytes, uint16 start_index = 0);
|
||||
|
||||
static const std::vector<byte> &ToBytes(int8 number);
|
||||
static int8 ToInt8(const std::vector<byte> &bytes, uint16 start_index = 0);
|
||||
|
||||
static const std::vector<byte> &ToBytes(int16 number);
|
||||
static int16 ToInt16(const std::vector<byte> &bytes, uint16 start_index = 0);
|
||||
|
||||
static const std::vector<byte> &ToBytes(int32 number);
|
||||
static int32 ToInt32(const std::vector<byte> &bytes, uint16 start_index = 0);
|
||||
|
||||
static const std::vector<byte> &ToBytes(int64 number);
|
||||
static int64 ToInt64(const std::vector<byte> &bytes, uint16 start_index = 0);
|
||||
};
|
||||
|
||||
struct StringConverter
|
||||
{
|
||||
static const std::string &ToString(bool value);
|
||||
static const std::string &ToString(uint8 value);
|
||||
static const std::string &ToString(uint16 value);
|
||||
static const std::string &ToString(uint32 value);
|
||||
static const std::string &ToString(uint64 value);
|
||||
static const std::string &ToString(int8 value);
|
||||
static const std::string &ToString(int16 value);
|
||||
static const std::string &ToString(int32 value);
|
||||
static const std::string &ToString(int64 value);
|
||||
static const std::string &ToString(const std::vector<byte> &bytes);
|
||||
|
||||
static uint8 ToUint8(const std::string &str);
|
||||
static uint16 ToUint16(const std::string &str);
|
||||
static uint32 ToUint32(const std::string &str);
|
||||
static uint64 ToUint64(const std::string &str);
|
||||
static int8 ToInt8(const std::string &str);
|
||||
static int16 ToInt16(const std::string &str);
|
||||
static int32 ToInt32(const std::string &str);
|
||||
static int64 ToInt64(const std::string &str);
|
||||
|
||||
static const std::vector<byte> &ToBytes(const std::string &str);
|
||||
|
||||
static const std::string &ToString(const std::vector<byte> &bytes, uint16 start_index = 0, uint16 lenght = 0);
|
||||
|
||||
static const std::string &Trim(std::string &str, char ch);
|
||||
static std::vector<std::string> Split(const std::string &str, const std::string &delimiter);
|
||||
};
|
||||
|
||||
struct IPUtil
|
||||
{
|
||||
static bool ValidIPV4(const std::string &ip);
|
||||
};
|
||||
|
||||
struct ConfigReader
|
||||
{
|
||||
void ReadConfig(const std::string &file_name);
|
||||
void ReadNodes();
|
||||
|
||||
const std::string &operator[](const std::string &key);
|
||||
|
||||
private:
|
||||
std::map<std::string, std::string> nodes;
|
||||
std::string file_content;
|
||||
};
|
||||
};
|
||||
|
||||
#endif
|
||||
@ -1,39 +0,0 @@
|
||||
#ifndef VOID_NET_HPP
|
||||
#define VOID_NET_HPP
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
#include "Defs.hpp"
|
||||
#include "Init.hpp"
|
||||
#include "TcpClient.hpp"
|
||||
#include "NetworkBuffer.hpp"
|
||||
#include "NetworkMessage.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <thread>
|
||||
|
||||
struct VoidNetClientAPI
|
||||
{
|
||||
static bool Connect(const std::string &ip, uint16 port = default_client_port);
|
||||
static void Disconnect();
|
||||
|
||||
static void SendMessageToServer(byte tag, byte subject, void *data);
|
||||
static void SendMessageToID(uint16 id, byte tag, byte subject, void *data);
|
||||
static void SendMessageToOthers(byte tag, byte subject, void *data);
|
||||
static void SendMessageToAll(byte tag, byte subject, void *data);
|
||||
static void SendMessageToAllAndMe(byte tag, byte subject, void *data);
|
||||
|
||||
static void SendMessage(byte distribution_mode, uint16 destination_id, byte tag, byte subject, void *data);
|
||||
|
||||
static void Receive();
|
||||
|
||||
private:
|
||||
static void receive_data();
|
||||
|
||||
static TcpClient tcp_client;
|
||||
static uint16 id;
|
||||
};
|
||||
|
||||
#endif
|
||||
@ -1,18 +0,0 @@
|
||||
#ifndef VOID_NET_SERVER_HPP
|
||||
#define VOID_NET_SERVER_HPP
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
#include "TcpServer.hpp"
|
||||
|
||||
struct VoidNetServer
|
||||
{
|
||||
void StartServer(uint16 port);
|
||||
void StopServer();
|
||||
private:
|
||||
TcpServer server;
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user