Skip to content

Commit 9e7733e

Browse files
committed
Initial networking
1 parent f46edcd commit 9e7733e

File tree

7 files changed

+185
-0
lines changed

7 files changed

+185
-0
lines changed

CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,15 @@ add_library(AthenaZelda EXCLUDE_FROM_ALL
153153
include/Athena/SkywardSwordQuest.hpp
154154
)
155155

156+
add_library(AthenaNet
157+
src/Athena/Socket.cpp
158+
src/Athena/OAuth.cpp
159+
src/Athena/IPAddress.cpp
160+
161+
include/Athena/Socket.hpp
162+
include/Athena/OAuth.hpp
163+
include/Athena/IPAddress.hpp)
164+
156165
# Icon
157166
set(ATHENA_ICO ${CMAKE_CURRENT_SOURCE_DIR}/Athena.ico)
158167

include/Athena/IPAddress.hpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#ifndef IPADDRESS_HPP
2+
#define IPADDRESS_HPP
3+
4+
#include "Athena/Global.hpp"
5+
#include <string.h>
6+
7+
#include <cstring>
8+
#include <utility>
9+
10+
namespace Athena
11+
{
12+
namespace net
13+
{
14+
class IPAddress
15+
{
16+
public:
17+
18+
static const IPAddress None;
19+
static const IPAddress Any;
20+
static const IPAddress Localhost;
21+
static const IPAddress Broadcast;
22+
23+
IPAddress() : m_address(~0u), m_valid(false) {}
24+
25+
IPAddress(const std::string& address);
26+
27+
IPAddress(atUint8 a, atUint8 b, atUint8 c, atUint8 d);
28+
29+
IPAddress(atUint32 address);
30+
31+
const std::string toString() const;
32+
const atUint32 toInt() const;
33+
34+
static IPAddress localAddress();
35+
private:
36+
37+
atUint32 m_address;
38+
bool m_valid;
39+
void resolve(const std::string& address);
40+
};
41+
}
42+
}
43+
44+
#endif

include/Athena/OAuth.hpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef OAUTH_HPP
2+
#define OAUTH_HPP
3+
4+
#include <Athena/Global.hpp>
5+
6+
namespace Athena
7+
{
8+
namespace net
9+
{
10+
class OAuth
11+
{
12+
13+
};
14+
}
15+
}
16+
17+
#endif

include/Athena/Socket.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef SOCKET_HPP
2+
#define SOCKET_HPP
3+
4+
namespace Athena
5+
{
6+
namespace net
7+
{
8+
class Socket
9+
{
10+
public:
11+
private:
12+
};
13+
}
14+
}
15+
16+
#endif

src/Athena/IPAddress.cpp

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#include "Athena/IPAddress.hpp"
2+
3+
#ifdef __unix__
4+
#include <sys/types.h>
5+
#include <sys/socket.h>
6+
#include <netinet/in.h>
7+
#include <netinet/tcp.h>
8+
#include <arpa/inet.h>
9+
#include <netdb.h>
10+
#include <unistd.h>
11+
#elif defined(_WIN32)
12+
#ifdef _WIN32_WINDOWS
13+
#undef _WIN32_WINDOWS
14+
#endif
15+
#ifdef _WIN32_WINNT
16+
#undef _WIN32_WINNT
17+
#endif
18+
#define _WIN32_WINDOWS 0x0501
19+
#define _WIN32_WINNT 0x0501
20+
#include <winsock2.h>
21+
#include <ws2tcpip.h>
22+
#endif
23+
24+
namespace Athena
25+
{
26+
namespace net
27+
{
28+
const IPAddress IPAddress::Any;
29+
const IPAddress IPAddress::None = IPAddress( 0, 0, 0, 0);
30+
const IPAddress IPAddress::Localhost = IPAddress(127, 0, 0, 1);
31+
const IPAddress IPAddress::Broadcast = IPAddress(255, 255, 255, 255);
32+
33+
IPAddress::IPAddress(const std::string& address)
34+
: m_valid(false)
35+
{
36+
resolve(address);
37+
}
38+
39+
IPAddress::IPAddress(atUint8 a, atUint8 b, atUint8 c, atUint8 d)
40+
: m_address(htonl((a << 24)| (b << 16) | (c << 8) | d)),
41+
m_valid(true)
42+
{
43+
}
44+
45+
IPAddress::IPAddress(atUint32 address)
46+
: m_address(htonl(address)),
47+
m_valid(true)
48+
{
49+
}
50+
51+
const std::string IPAddress::toString() const
52+
{
53+
in_addr address;
54+
address.s_addr = m_address;
55+
return inet_ntoa(address);
56+
}
57+
58+
void IPAddress::resolve(const std::string& address)
59+
{
60+
if (address == "0.0.0.0")
61+
{
62+
m_address = 0;
63+
m_valid = true;
64+
}
65+
else if(address == "255.255.255.255")
66+
{
67+
m_address = ~0u;
68+
m_valid = true;
69+
}
70+
else
71+
{
72+
atUint32 ip = inet_addr(address.c_str());
73+
if (ip == INADDR_NONE)
74+
{
75+
addrinfo hints = {0};
76+
hints.ai_family = AF_INET;
77+
addrinfo* result = nullptr;
78+
if (getaddrinfo(address.c_str(), NULL, &hints, &result))
79+
{
80+
if (result)
81+
{
82+
ip = reinterpret_cast<sockaddr_in*>(result->ai_addr)->sin_addr.s_addr;
83+
freeaddrinfo(result);
84+
m_address = ip;
85+
m_valid = true;
86+
}
87+
}
88+
}
89+
else
90+
{
91+
m_address = ip;
92+
m_valid = true;
93+
}
94+
}
95+
}
96+
}
97+
}

src/Athena/OAuth.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "Athena/OAuth.hpp"

src/Athena/Socket.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "Athena/Socket.hpp"

0 commit comments

Comments
 (0)