-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNetworkClientService.cpp
More file actions
135 lines (112 loc) · 2.93 KB
/
NetworkClientService.cpp
File metadata and controls
135 lines (112 loc) · 2.93 KB
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
#include "NetworkClientService.hpp"
#include <chrono>
#include <iostream>
#include <mutex>
#include <stdexcept>
#include <thread>
#ifdef _WIN32
#include "../../Utils/Networks/WindowsSocket.hpp"
#endif
namespace Bored::Net {
Client::Client(std::string compatible_id) {
initSocket();
compatible_id_ = compatible_id;
};
Client::~Client() { Disconnect(); };
bool Client::Connect(int port, std::string addr, int server_port) {
if (running_.load())
return false;
running_ = true;
try {
sock_->Open(IPv4, Datagram, UDP);
sock_->Bind("", port);
} catch (const std::runtime_error &err) {
std::cout << "Unable to connect: " << err.what() << std::endl;
running_ = false;
}
server_addr = Conn(addr, server_port);
sock_->SendTo(addr, server_port, compatible_id_ + ":connect_req");
bool success = false;
auto start_time = std::chrono::high_resolution_clock().now();
while (std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::high_resolution_clock::now() - start_time)
.count() < kConnectTimeOut * 1000) {
if (!sock_->HasReadable(100))
continue;
try {
std::string ack =
sock_->ReceiveFrom(server_addr.address, server_addr.port);
if (ack == compatible_id_) {
success = true;
}
} catch (const std::runtime_error &e) {
continue;
}
}
if (!success) {
running_ = false;
sock_->Close();
std::cout << "Time out while connecting with server" << std::endl;
return false;
}
listener_ = std::thread(&Client::listenLoop, this, port);
return true;
};
void Client::Disconnect() {
if (!running_.load()) {
return;
}
running_ = false;
sock_->Close();
if (listener_.joinable())
listener_.join();
};
void Client::initSocket() {
#ifdef _WIN32
sock_ = std::make_shared<WindowsSocket>();
#else
throw std::runtime_error("No implementation for other platform yet");
#endif
};
void Client::listenLoop(int port) {
while (running_.load()) {
bool have_msg = false;
#ifdef _WIN32
auto *ws = dynamic_cast<WindowsSocket *>(sock_.get());
if (ws->HasReadable(100)) {
have_msg = true;
}
#endif
if (!have_msg) {
continue;
};
std::string from, payload;
int port;
try {
payload = sock_->ReceiveFrom(from, port);
if (Conn(from, port) != server_addr) {
continue;
};
} catch (const std::runtime_error &err) {
continue;
}
{
std::lock_guard<std::mutex> lk(q_mtx_);
mqueue_.push_back(payload);
}
}
};
std::vector<std::string> Client::GetAllMessage() {
std::lock_guard<std::mutex> lk(q_mtx_);
std::vector<std::string> out;
out.swap(mqueue_);
return out;
};
void Client::SendToServer(std::string payload) {
try {
sock_->SendTo(server_addr.address, server_addr.port, payload);
} catch (const std::runtime_error &e) {
std::cout << "Failed to send " << e.what() << std::endl;
}
}
} // namespace Bored::Net