refactor Network
This commit is contained in:
parent
7e0c95d126
commit
6af6eda78e
@ -246,7 +246,7 @@ static inline int sendsocket(
|
|||||||
static std::string to_string(const sockaddr_in* addr) {
|
static std::string to_string(const sockaddr_in* addr) {
|
||||||
char ip[INET_ADDRSTRLEN];
|
char ip[INET_ADDRSTRLEN];
|
||||||
if (inet_ntop(AF_INET, &(addr->sin_addr), ip, INET_ADDRSTRLEN)) {
|
if (inet_ntop(AF_INET, &(addr->sin_addr), ip, INET_ADDRSTRLEN)) {
|
||||||
return std::string(ip)+":"+std::to_string(addr->sin_port);
|
return std::string(ip)+":"+std::to_string(htons(addr->sin_port));
|
||||||
}
|
}
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
@ -258,7 +258,7 @@ static std::string to_string(const addrinfo* addr) {
|
|||||||
auto psai = reinterpret_cast<sockaddr_in6*>(addr->ai_addr);
|
auto psai = reinterpret_cast<sockaddr_in6*>(addr->ai_addr);
|
||||||
char ip[INET6_ADDRSTRLEN];
|
char ip[INET6_ADDRSTRLEN];
|
||||||
if (inet_ntop(addr->ai_family, &(psai->sin6_addr), ip, INET6_ADDRSTRLEN)) {
|
if (inet_ntop(addr->ai_family, &(psai->sin6_addr), ip, INET6_ADDRSTRLEN)) {
|
||||||
return std::string(ip)+":"+std::to_string(psai->sin6_port);
|
return std::string(ip)+":"+std::to_string(htons(psai->sin6_port));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return "";
|
return "";
|
||||||
@ -267,8 +267,7 @@ static std::string to_string(const addrinfo* addr) {
|
|||||||
class SocketConnection : public Connection {
|
class SocketConnection : public Connection {
|
||||||
SOCKET descriptor;
|
SOCKET descriptor;
|
||||||
bool open = true;
|
bool open = true;
|
||||||
addrinfo* addr;
|
sockaddr_in addr;
|
||||||
std::string addrString;
|
|
||||||
size_t totalUpload = 0;
|
size_t totalUpload = 0;
|
||||||
size_t totalDownload = 0;
|
size_t totalDownload = 0;
|
||||||
ConnectionState state = ConnectionState::INITIAL;
|
ConnectionState state = ConnectionState::INITIAL;
|
||||||
@ -279,22 +278,21 @@ class SocketConnection : public Connection {
|
|||||||
|
|
||||||
void connectSocket() {
|
void connectSocket() {
|
||||||
state = ConnectionState::CONNECTING;
|
state = ConnectionState::CONNECTING;
|
||||||
logger.info() << "connecting to " << to_string(addr);
|
logger.info() << "connecting to " << to_string(&addr);
|
||||||
int res = connectsocket(descriptor, addr->ai_addr, addr->ai_addrlen);
|
int res = connectsocket(descriptor, (const sockaddr*)&addr, sizeof(sockaddr_in));
|
||||||
if (res < 0) {
|
if (res < 0) {
|
||||||
auto error = handle_socket_error("Connect failed");
|
auto error = handle_socket_error("Connect failed");
|
||||||
closesocket(descriptor);
|
closesocket(descriptor);
|
||||||
freeaddrinfo(addr);
|
|
||||||
state = ConnectionState::CLOSED;
|
state = ConnectionState::CLOSED;
|
||||||
logger.error() << error.what();
|
logger.error() << error.what();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
logger.info() << "connected to " << to_string(addr);
|
logger.info() << "connected to " << to_string(&addr);
|
||||||
state = ConnectionState::CONNECTED;
|
state = ConnectionState::CONNECTED;
|
||||||
}
|
}
|
||||||
public:
|
public:
|
||||||
SocketConnection(SOCKET descriptor, addrinfo* addr, const std::string& addrString)
|
SocketConnection(SOCKET descriptor, sockaddr_in addr)
|
||||||
: descriptor(descriptor), addr(addr), addrString(addrString), buffer(16'384) {}
|
: descriptor(descriptor), addr(std::move(addr)), buffer(16'384) {}
|
||||||
|
|
||||||
~SocketConnection() {
|
~SocketConnection() {
|
||||||
if (state != ConnectionState::CLOSED) {
|
if (state != ConnectionState::CLOSED) {
|
||||||
@ -304,9 +302,6 @@ public:
|
|||||||
if (thread) {
|
if (thread) {
|
||||||
thread->join();
|
thread->join();
|
||||||
}
|
}
|
||||||
if (addr) {
|
|
||||||
freeaddrinfo(addr);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void connect(runnable callback) override {
|
void connect(runnable callback) override {
|
||||||
@ -318,13 +313,13 @@ public:
|
|||||||
while (state == ConnectionState::CONNECTED) {
|
while (state == ConnectionState::CONNECTED) {
|
||||||
int size = recvsocket(descriptor, buffer.data(), buffer.size());
|
int size = recvsocket(descriptor, buffer.data(), buffer.size());
|
||||||
if (size == 0) {
|
if (size == 0) {
|
||||||
logger.info() << "closed connection " << to_string(addr);
|
logger.info() << "closed connection with " << to_string(&addr);
|
||||||
closesocket(descriptor);
|
closesocket(descriptor);
|
||||||
state = ConnectionState::CLOSED;
|
state = ConnectionState::CLOSED;
|
||||||
break;
|
break;
|
||||||
} else if (size < 0) {
|
} else if (size < 0) {
|
||||||
logger.info() << "an error ocurred while receiving from "
|
logger.info() << "an error ocurred while receiving from "
|
||||||
<< to_string(addr);
|
<< to_string(&addr);
|
||||||
auto error = handle_socket_error("recv(...) error");
|
auto error = handle_socket_error("recv(...) error");
|
||||||
closesocket(descriptor);
|
closesocket(descriptor);
|
||||||
state = ConnectionState::CLOSED;
|
state = ConnectionState::CLOSED;
|
||||||
@ -338,7 +333,7 @@ public:
|
|||||||
}
|
}
|
||||||
totalDownload += size;
|
totalDownload += size;
|
||||||
}
|
}
|
||||||
logger.info() << "read " << size << " bytes from " << to_string(addr);
|
logger.info() << "read " << size << " bytes from " << to_string(&addr);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -403,18 +398,21 @@ public:
|
|||||||
|
|
||||||
addrinfo* addrinfo;
|
addrinfo* addrinfo;
|
||||||
if (int res = getaddrinfo(
|
if (int res = getaddrinfo(
|
||||||
address.c_str(), std::to_string(port).c_str(), &hints, &addrinfo
|
address.c_str(), nullptr, &hints, &addrinfo
|
||||||
)) {
|
)) {
|
||||||
throw std::runtime_error(gai_strerror(res));
|
throw std::runtime_error(gai_strerror(res));
|
||||||
}
|
}
|
||||||
SOCKET descriptor = socket(
|
|
||||||
addrinfo->ai_family, addrinfo->ai_socktype, addrinfo->ai_protocol
|
sockaddr_in serverAddress = *(sockaddr_in*)addrinfo->ai_addr;
|
||||||
);
|
freeaddrinfo(addrinfo);
|
||||||
|
serverAddress.sin_port = htons(port);
|
||||||
|
|
||||||
|
SOCKET descriptor = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
if (descriptor == -1) {
|
if (descriptor == -1) {
|
||||||
freeaddrinfo(addrinfo);
|
freeaddrinfo(addrinfo);
|
||||||
throw std::runtime_error("Could not create socket");
|
throw std::runtime_error("Could not create socket");
|
||||||
}
|
}
|
||||||
auto socket = std::make_shared<SocketConnection>(descriptor, addrinfo, to_string(addrinfo));
|
auto socket = std::make_shared<SocketConnection>(descriptor, std::move(serverAddress));
|
||||||
socket->connect(std::move(callback));
|
socket->connect(std::move(callback));
|
||||||
return socket;
|
return socket;
|
||||||
}
|
}
|
||||||
@ -457,7 +455,7 @@ public:
|
|||||||
}
|
}
|
||||||
logger.info() << "client connected: " << to_string(&address);
|
logger.info() << "client connected: " << to_string(&address);
|
||||||
auto socket = std::make_shared<SocketConnection>(
|
auto socket = std::make_shared<SocketConnection>(
|
||||||
clientDescriptor, nullptr, to_string(&address)
|
clientDescriptor, address
|
||||||
);
|
);
|
||||||
u64id_t id = network->addConnection(socket);
|
u64id_t id = network->addConnection(socket);
|
||||||
{
|
{
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user