-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
93 lines (86 loc) · 1.95 KB
/
Copy pathmain.cpp
File metadata and controls
93 lines (86 loc) · 1.95 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
#include "ChatWin.h"
#include "Login.h"
void cb_button();
void cb_cnbutton();
bool exit_(GdkEventAny *);
chat_client *client = nullptr;
ChatWin *win;
Login *login;
io_context ioc;
tcp::resolver rs(ioc);
bool ipgetted = false;
Glib::RefPtr<Application> app;
int main(int argc, char **argv)
{
app = Application::create(argc, argv);
win = new ChatWin((char *)"chat");
login = new Login();
win->signal_delete_event().connect(sigc::ptr_fun(exit_));
win->snd_button.signal_clicked().connect(sigc::ptr_fun(cb_button));
login->show_all();
do
{
if (login->run() == RESPONSE_APPLY)
cb_cnbutton();
else
return 1;
} while (!ipgetted);
login->close();
win->show_all();
app->run(*win);
return 0;
}
void cb_cnbutton()
{
auto endpoint =
rs.resolve(login->iptext->get_text(), login->porttext->get_text());
if (client == nullptr)
client = new chat_client(ioc, endpoint);
else
client->do_connect(endpoint);
ioc.run_one();
while (!client->tried_connect)
;
ioc.restart();
client->tried_connect = false;
if (client->connect_result)
return;
ipgetted = true;
std::thread t([]() { ioc.run(); });
t.detach();
}
void cb_button()
{
auto &&line = win->snd_msg.get_text();
if (line.empty())
return;
chat_message msg;
msg.body_length(line.length());
std::memcpy(msg.body(), line.c_str(), msg.body_length());
msg.encode_header();
client->write(msg);
}
void handler(boost::system::error_code ec, std::size_t length)
{
if (!ec)
{
TextBuffer::iterator start, end;
win->buffer->get_bounds(start, end);
win->buffer->insert(end, Glib::ustring(client->read_msg_.body(),
client->read_msg_.body_length()));
win->buffer->get_bounds(start, end);
win->buffer->insert(end, "\n");
client->do_read_header();
}
else
{
client->close();
}
}
bool exit_(GdkEventAny *event)
{
client->close();
win->close();
app->quit();
return ipgetted;
}