-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.cpp
174 lines (160 loc) · 6.51 KB
/
client.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <cstdlib>
#include <deque>
#include <iostream>
#include <thread>
#include <fstream>
#include <boost/asio.hpp>
#include "app_protocol.hpp"
#include <random>
using boost::asio::ip::tcp;
using namespace std;
typedef std::deque<app_message> app_message_queue;
class chat_client
{
public:
chat_client(boost::asio::io_context &io_context,
const tcp::resolver::results_type &endpoints)
: io_context_(io_context),
socket_(io_context)
{
do_connect(endpoints);
}
void write(app_message &msg, char* input)
{
if (player_num == 0)
{
msg.set_x1(atoi(input));
msg.set_y1(atoi(input));
msg.set_num(0);
}
else
{
msg.set_x2(atoi(input));
msg.set_y2(atoi(input));
msg.set_num(1);
}
boost::asio::post(io_context_,
[this, msg]()
{
bool write_in_progress = !write_msgs_.empty();
write_msgs_.push_back(msg);
if (!write_in_progress)
{
do_write();
}
});
}
void close()
{
boost::asio::post(io_context_, [this]()
{ socket_.close(); });
}
private:
void do_connect(const tcp::resolver::results_type &endpoints)
{
boost::asio::async_connect(socket_, endpoints,
[this](boost::system::error_code ec, tcp::endpoint)
{
if (!ec)
{
printf("Connected to the Server\n");
// uint32_t sent_bytes = 0;
// std::random_device rd;
// std::mt19937 gen(rd());
// std::uniform_int_distribution<> dis(30000, 50000);
// port_receiver = dis(gen);
// sent_bytes = boost::asio::write(socket_,
// boost::asio::buffer(&port_receiver, sizeof(port_receiver)));
// std::unique_lock<std::mutex> lock(mutex_);
// input_received = true;
// input_cv.notify_one();
do_read();
}
else
{
socket_.close();
}
});
}
void do_read()
{
boost::asio::async_read(socket_,
boost::asio::buffer(&read_msg, sizeof(read_msg)), // read from read_msg_body only till the length identified in the header
[this](boost::system::error_code ec, std::size_t length)
{
if (!ec)
{
std::cout << "Message From the server: " << length << endl;
printf("Receiving Image: %d %d %d", read_msg.get_x1(), read_msg.get_x2(), read_msg.get_num());
player_num = read_msg.get_num();
std::cout << "\n";
do_read(); // Continue reading the next message
}
else
{
socket_.close();
}
});
}
void do_write()
{
boost::asio::async_write(socket_,
boost::asio::buffer(&write_msgs_.front(), // write the queue message to the server
sizeof(write_msgs_.front())),
[this](boost::system::error_code ec, std::size_t length)
{
if (!ec)
{
printf("Client writing a message to the server %lu\n", length);
// printf("Sending Image: %d %d\n", write_msgs_.front().get_x1(), write_msgs_.front().get_y1());
write_msgs_.pop_front(); // pop the message
if (!write_msgs_.empty()) // check if other messages are to be sent
{
do_write();
}
}
else
{
socket_.close();
}
});
}
private:
boost::asio::io_context &io_context_;
tcp::socket socket_;
app_message read_msg;
app_message_queue write_msgs_;
uint8_t player_num;
};
int main(int argc, char *argv[])
{
try
{
if (argc != 3)
{
std::cerr << "Usage: chat_client <host> <port>";
return 1;
}
boost::asio::io_context io_context;
tcp::resolver resolver(io_context);
auto endpoints = resolver.resolve(argv[1], argv[2]);
chat_client c(io_context, endpoints);
std::thread t([&io_context]()
{ io_context.run(); });
char line[app_message::msg_length];
while (std::cin.getline(line, 8))
{
if (strcmp(line, "q") == 0)
break;
app_message msg;
c.write(msg, line);
}
c.close();
t.join();
}
catch (std::exception &e)
{
std::cerr << "Exception: " << e.what() << "\n";
}
return 0;
}