Initial networking

This commit is contained in:
Phillip Stephens 2015-10-27 01:16:56 -07:00
parent f46edcd4b8
commit 9e7733eb02
7 changed files with 185 additions and 0 deletions

View File

@ -153,6 +153,15 @@ add_library(AthenaZelda EXCLUDE_FROM_ALL
include/Athena/SkywardSwordQuest.hpp include/Athena/SkywardSwordQuest.hpp
) )
add_library(AthenaNet
src/Athena/Socket.cpp
src/Athena/OAuth.cpp
src/Athena/IPAddress.cpp
include/Athena/Socket.hpp
include/Athena/OAuth.hpp
include/Athena/IPAddress.hpp)
# Icon # Icon
set(ATHENA_ICO ${CMAKE_CURRENT_SOURCE_DIR}/Athena.ico) set(ATHENA_ICO ${CMAKE_CURRENT_SOURCE_DIR}/Athena.ico)

View File

@ -0,0 +1,44 @@
#ifndef IPADDRESS_HPP
#define IPADDRESS_HPP
#include "Athena/Global.hpp"
#include <string.h>
#include <cstring>
#include <utility>
namespace Athena
{
namespace net
{
class IPAddress
{
public:
static const IPAddress None;
static const IPAddress Any;
static const IPAddress Localhost;
static const IPAddress Broadcast;
IPAddress() : m_address(~0u), m_valid(false) {}
IPAddress(const std::string& address);
IPAddress(atUint8 a, atUint8 b, atUint8 c, atUint8 d);
IPAddress(atUint32 address);
const std::string toString() const;
const atUint32 toInt() const;
static IPAddress localAddress();
private:
atUint32 m_address;
bool m_valid;
void resolve(const std::string& address);
};
}
}
#endif

17
include/Athena/OAuth.hpp Normal file
View File

@ -0,0 +1,17 @@
#ifndef OAUTH_HPP
#define OAUTH_HPP
#include <Athena/Global.hpp>
namespace Athena
{
namespace net
{
class OAuth
{
};
}
}
#endif

16
include/Athena/Socket.hpp Normal file
View File

@ -0,0 +1,16 @@
#ifndef SOCKET_HPP
#define SOCKET_HPP
namespace Athena
{
namespace net
{
class Socket
{
public:
private:
};
}
}
#endif

97
src/Athena/IPAddress.cpp Normal file
View File

@ -0,0 +1,97 @@
#include "Athena/IPAddress.hpp"
#ifdef __unix__
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#elif defined(_WIN32)
#ifdef _WIN32_WINDOWS
#undef _WIN32_WINDOWS
#endif
#ifdef _WIN32_WINNT
#undef _WIN32_WINNT
#endif
#define _WIN32_WINDOWS 0x0501
#define _WIN32_WINNT 0x0501
#include <winsock2.h>
#include <ws2tcpip.h>
#endif
namespace Athena
{
namespace net
{
const IPAddress IPAddress::Any;
const IPAddress IPAddress::None = IPAddress( 0, 0, 0, 0);
const IPAddress IPAddress::Localhost = IPAddress(127, 0, 0, 1);
const IPAddress IPAddress::Broadcast = IPAddress(255, 255, 255, 255);
IPAddress::IPAddress(const std::string& address)
: m_valid(false)
{
resolve(address);
}
IPAddress::IPAddress(atUint8 a, atUint8 b, atUint8 c, atUint8 d)
: m_address(htonl((a << 24)| (b << 16) | (c << 8) | d)),
m_valid(true)
{
}
IPAddress::IPAddress(atUint32 address)
: m_address(htonl(address)),
m_valid(true)
{
}
const std::string IPAddress::toString() const
{
in_addr address;
address.s_addr = m_address;
return inet_ntoa(address);
}
void IPAddress::resolve(const std::string& address)
{
if (address == "0.0.0.0")
{
m_address = 0;
m_valid = true;
}
else if(address == "255.255.255.255")
{
m_address = ~0u;
m_valid = true;
}
else
{
atUint32 ip = inet_addr(address.c_str());
if (ip == INADDR_NONE)
{
addrinfo hints = {0};
hints.ai_family = AF_INET;
addrinfo* result = nullptr;
if (getaddrinfo(address.c_str(), NULL, &hints, &result))
{
if (result)
{
ip = reinterpret_cast<sockaddr_in*>(result->ai_addr)->sin_addr.s_addr;
freeaddrinfo(result);
m_address = ip;
m_valid = true;
}
}
}
else
{
m_address = ip;
m_valid = true;
}
}
}
}
}

1
src/Athena/OAuth.cpp Normal file
View File

@ -0,0 +1 @@
#include "Athena/OAuth.hpp"

1
src/Athena/Socket.cpp Normal file
View File

@ -0,0 +1 @@
#include "Athena/Socket.hpp"