MCPcopy Create free account
hub / github.com/chronoxor/CppServer / main

Function main

performance/ws_echo_server.cpp:53–127  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

51};
52
53int main(int argc, char** argv)
54{
55 auto parser = optparse::OptionParser().version("1.0.0.0");
56
57 parser.add_option("-p", "--port").dest("port").action("store").type("int").set_default(8080).help("Server port. Default: %default");
58 parser.add_option("-t", "--threads").dest("threads").action("store").type("int").set_default(CPU::PhysicalCores()).help("Count of working threads. Default: %default");
59
60 optparse::Values options = parser.parse_args(argc, argv);
61
62 // Print help
63 if (options.get("help"))
64 {
65 parser.print_help();
66 return 0;
67 }
68
69 // Server port
70 int port = options.get("port");
71 int threads = options.get("threads");
72
73 std::cout << "Server port: " << port << std::endl;
74 std::cout << "Working threads: " << threads << std::endl;
75
76 std::cout << std::endl;
77
78 // Create a new Asio service
79 auto service = std::make_shared<Service>(threads);
80
81 // Start the Asio service
82 std::cout << "Asio service starting...";
83 service->Start();
84 std::cout << "Done!" << std::endl;
85
86 // Create a new echo server
87 auto server = std::make_shared<EchoServer>(service, port);
88 // server->SetupNoDelay(true);
89 server->SetupReuseAddress(true);
90 server->SetupReusePort(true);
91
92 // Start the server
93 std::cout << "Server starting...";
94 server->Start();
95 std::cout << "Done!" << std::endl;
96
97 std::cout << "Press Enter to stop the server or '!' to restart the server..." << std::endl;
98
99 // Perform text input
100 std::string line;
101 while (getline(std::cin, line))
102 {
103 if (line.empty())
104 break;
105
106 // Restart the server
107 if (line == "!")
108 {
109 std::cout << "Server restarting...";
110 server->Restart();

Callers

nothing calls this directly

Calls 7

getMethod · 0.45
StartMethod · 0.45
SetupReuseAddressMethod · 0.45
SetupReusePortMethod · 0.45
emptyMethod · 0.45
RestartMethod · 0.45
StopMethod · 0.45

Tested by

no test coverage detected