/
network.cpp
154 lines (129 loc) · 3.19 KB
/
network.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* timer.hpp
*
* Created on: Sep 11, 2015
* Author: jaronhalt
*/
#include "network.hpp"
int resolve_host(const std::string& host, sockaddr_in& addrinfo)
{
int status;
struct addrinfo hints, *res, *p;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
//hints.ai_socktype = socket::SOCKET_TYPE::UDP;
if((status = getaddrinfo(host.c_str(), nullptr, &hints, &res)) != 0)
{
std::cerr << "getaddrinfo error: " << status << " " << gai_strerror(status) << std::endl;
}
else
{
for(p = res;p != nullptr; p = p->ai_next)
{
if (p->ai_family == AF_INET)
{
addrinfo = *(struct sockaddr_in *)p->ai_addr;
std::cout << "found addr " << inet_ntoa(addrinfo.sin_addr) << std::endl;
break;
}
}
freeaddrinfo(res); // free the linked list
}
return status;
}
NetworkSocket::NetworkSocket(const std::string& host, const uint16_t& port, const socket::SOCKET_TYPE stype)
: socket(stype)
, host(host)
, port(port)
{
std::cout << "creating network socket to host " << this->host << std::endl;
if(0 != resolve_host(this->host, this->addr))
{
std::cout << "Error resolving host " << std::endl;
}
this->addr.sin_port = htons(this->port);
}
UDPSocket::UDPSocket(const std::string& host, const uint16_t& port)
: NetworkSocket(host, port, socket::SOCKET_TYPE::UDP)
{
}
int
UDPSocket::send(const std::string& data) const
{
int sent = 0;
sent = sendto((int)this->get(), (const void*)data.c_str(), data.length(), 0, (struct sockaddr*)&this->addr, sizeof(this->addr));
return sent;
}
int
UDPSocket::recv(uint8_t* buffer, int length, bool allocate_buffer)
{
int received = 0;
uint32_t alen = sizeof(this->addr);
if(allocate_buffer)
{
buffer = new uint8_t[length];
}
received = recvfrom((int)this->get(), (void*)buffer, length, 0, (struct sockaddr*)&this->addr, &alen);
return received;
}
int
UDPSocket::bind()
{
int res = 0;
res = ::bind(this->get(), (const sockaddr*)&this->addr, sizeof(this->addr));
return res;
}
#include <chrono>
#include <thread>
TCPSocket::TCPSocket(const std::string& host, const uint16_t& port)
: NetworkSocket(host, port, socket::SOCKET_TYPE::TCP)
, connected(false)
{
}
int
TCPSocket::connect()
{
int res = 0;
bool expected = false;
if(this->connected.compare_exchange_strong(expected, true))
{
res = ::connect(this->get(), (struct sockaddr*)&this->addr, sizeof(this->addr));
if (res < 0)
{
std::cout << "Error connecting... " << res << std::endl;
this->connected.store(false);
}
else
{
std::cout << "Connected successfully..." << std::endl;
}
}
return res;
}
int
TCPSocket::send(const std::string& data)
{
this->connect();
int sent = 0;
sent = write(this->get(), (const void*)data.c_str(), data.length());
std::cout << "TCP sent " << sent << " bytes" << std::endl;
uint8_t buffer[1024] = {0};
int read_res = read(this->get(), (void*)buffer, sizeof(buffer));
std::cout << "read " << read_res << " bytes" << std::endl;
if(read_res == 0)
{
this->connected.store(false);
}
return sent;
}
int
TCPSocket::recv(uint8_t* buffer, int length, bool allocate_buffer)
{
int received = 0;
if(allocate_buffer)
{
buffer = new uint8_t[length];
}
received = ::recv((int)this->get(), (void*)buffer, length, 0);
return received;
}