Skip to content

Commit 8bfa71e

Browse files
committed
The server starts up in a new thread. Some fixes.
1 parent 1de3fe1 commit 8bfa71e

File tree

2 files changed

+78
-72
lines changed

2 files changed

+78
-72
lines changed

main_server.cpp

Lines changed: 74 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -3,90 +3,96 @@
33
#include <cli/cli.h>
44
#include <cli/clifilesession.h>
55

6+
#include <boost/thread.hpp>
7+
68
using namespace cli;
79

810
int main(void)
911
{
10-
boost::asio::io_service io_service;
11-
unsigned int port;
12-
bool set_port = false;
13-
14-
auto rootMenu = std::make_unique< Menu >("SERVER");
15-
rootMenu->Insert(
16-
"start",
17-
[&](std::ostream& out)
18-
{
19-
if (set_port)
12+
boost::asio::io_service io_service;
13+
unsigned int port;
14+
bool set_port = false;
15+
16+
auto rootMenu = std::make_unique< Menu >("SERVER");
17+
rootMenu->Insert(
18+
"start",
19+
[&](std::ostream& out)
20+
{
21+
if (set_port)
22+
{
23+
try
2024
{
21-
try
22-
{
23-
server s(io_service, port);
24-
std::cout << "\tSERVER STARTED ON " << port << std::endl;
25-
if(io_service.stopped())
26-
io_service.restart();
27-
io_service.run();
28-
}
29-
catch (const std::exception& e)
30-
{
31-
std::cerr << "SERVER> Exception: " << e.what() << "\n";
32-
}
33-
}
34-
else {
35-
out << "You must set port for server. Use: \"setPort <int>\"" << "\n";
25+
server s(std::move(io_service), port);
26+
std::cout << "\tSERVER STARTED ON " << port << std::endl;
27+
28+
if (io_service.stopped())
29+
io_service.restart();
30+
31+
std::thread run_service([&] { io_service.run(); });
32+
run_service.join();
33+
3634
}
37-
},
38-
"Start server");
39-
40-
rootMenu->Insert(
41-
"setPort",
42-
[&port, &set_port](std::ostream& out, int x)
43-
{
44-
if (x > 0 && x < 65536)
35+
catch (const std::exception& e)
4536
{
46-
port = x;
47-
out << "\tSERVER ON PORT: " << port << std::endl;
48-
set_port = true;
37+
std::cerr << "SERVER> Exception: " << e.what() << "\n";
4938
}
50-
else
39+
}
40+
else {
41+
out << "You must set port for server. Use: \"setPort <int>\"" << "\n";
42+
}
43+
},
44+
"Start server");
45+
46+
rootMenu->Insert(
47+
"setPort",
48+
[&port, &set_port](std::ostream& out, const int& x)
49+
{
50+
if (x > 0 && x < 65536)
51+
{
52+
port = x;
53+
out << "\tSERVER ON PORT: " << port << std::endl;
54+
set_port = true;
55+
}
56+
else
57+
{
58+
out << "\tPort must be greter than 0 and less than 65536" << std::endl;
59+
}
60+
},
61+
"Set port for server");
62+
63+
rootMenu->Insert(
64+
"stop",
65+
[&](std::ostream& out)
66+
{
67+
if (set_port) {
68+
try
5169
{
52-
out << "\tPort must be greter than 0 and less than 65536" << std::endl;
53-
}
54-
},
55-
"Set port for server");
56-
57-
rootMenu->Insert(
58-
"stop",
59-
[&](std::ostream& out)
60-
{
61-
if (set_port){
62-
try
63-
{
64-
if (!io_service.stopped())
65-
io_service.stop();
66-
out << "\tSTOPED ON PORT " << port << std::endl;
67-
}
68-
catch (const std::exception& e)
69-
{
70-
std::cerr << "SERVER> Exception: " << e.what() << "\n";
71-
}
70+
if (!io_service.stopped())
71+
io_service.stop();
72+
out << "\tSTOPED ON PORT " << port << std::endl;
7273
}
73-
else {
74-
out << "You don't specify the port or don't start the server." << "\n";
74+
catch (const std::exception& e)
75+
{
76+
std::cerr << "SERVER> Exception: " << e.what() << "\n";
7577
}
76-
},
77-
"Stop server");
78+
}
79+
else {
80+
out << "You don't specify the port or don't start the server." << "\n";
81+
}
82+
},
83+
"Stop server");
7884

79-
Cli cli(std::move(rootMenu));
80-
cli.ExitAction([&io_service](auto& out)
81-
{
85+
Cli cli(std::move(rootMenu));
86+
cli.ExitAction([&io_service](auto& out)
87+
{
8288
if (!io_service.stopped())
8389
io_service.stop();
84-
85-
out << "\tEXIT. Goodbye!\n";
90+
91+
out << "\tEXIT. Goodbye!\n";
8692
});
8793

88-
CliFileSession input(cli);
89-
input.Start();
94+
CliFileSession input(cli);
95+
input.Start();
9096

9197
return 0;
9298
}

server.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ class session : public std::enable_shared_from_this<session>
3939

4040
tcp::socket socket_;
4141
enum { max_length = 1024 };
42-
char data_[max_length];
43-
unsigned int session_id;
42+
char data_[max_length] = {0};
43+
unsigned int session_id = 0 ;
4444
};
4545

4646
class server
4747
{
4848
public:
49-
server(boost::asio::io_service& io_service, const unsigned int port) :
49+
server(boost::asio::io_service&& io_service, const unsigned int& port) :
5050
ep(tcp::v4(), port),
5151
acceptor_(io_service, ep),
5252
session_id(0)
@@ -60,5 +60,5 @@ class server
6060
const tcp::endpoint ep;
6161
tcp::acceptor acceptor_;
6262

63-
unsigned int session_id;
63+
unsigned int session_id = 0;
6464
};

0 commit comments

Comments
 (0)