Run catching functions
This commit is contained in:
parent
e5f8a08238
commit
e1e10a92f6
48
main.cpp
48
main.cpp
@ -1,33 +1,37 @@
|
||||
import std;
|
||||
import ru.landgrafhomyak.BGTU.networks_1.exceptions;
|
||||
import ru.landgrafhomyak.BGTU.networks_1.threads;
|
||||
import ru.landgrafhomyak.BGTU.networks_1.sockets;
|
||||
|
||||
|
||||
int main() {
|
||||
LdH::Sockets::init_sockets();
|
||||
LdH::run_catching_main([] {
|
||||
LdH::Sockets::init_sockets();
|
||||
|
||||
std::cout << LdH::Sockets::Address::parse("google.com", "443").to_string() << std::endl;
|
||||
std::cout << LdH::Sockets::Address::parse("google.com", "443").to_string() << std::endl;
|
||||
std::cout << LdH::Sockets::Address::parse("localhost123123", "443").to_string() << std::endl;
|
||||
|
||||
auto server = LdH::Sockets::listen_udp(LdH::Sockets::Address::parse("127.0.0.2", "8081"));
|
||||
auto server = LdH::Sockets::listen_udp(LdH::Sockets::Address::parse("127.0.0.2", "8081"));
|
||||
|
||||
auto server_thread = LdH::fork("server", [&] {
|
||||
char buffer[10];
|
||||
server.recvOneTruncating(10, buffer);
|
||||
std::cout << buffer << std::endl;
|
||||
server.close();
|
||||
auto server_thread = LdH::fork("server", [&] {
|
||||
char buffer[10];
|
||||
server.recvOneTruncating(10, buffer);
|
||||
std::cout << buffer << std::endl;
|
||||
server.close();
|
||||
});
|
||||
|
||||
auto client_thread = LdH::fork("client", [&] {
|
||||
auto client = LdH::Sockets::connect_udp(LdH::Sockets::Address::parse("127.0.0.2", "8081"));
|
||||
char buffer[10] = "hello\n";
|
||||
client.sendOne(10, buffer);
|
||||
client.close();
|
||||
});
|
||||
|
||||
server_thread.join();
|
||||
client_thread.join();
|
||||
server_thread.destroy();
|
||||
client_thread.destroy();
|
||||
|
||||
LdH::Sockets::deinit_sockets();
|
||||
});
|
||||
|
||||
auto client_thread = LdH::fork("client", [&] {
|
||||
auto client = LdH::Sockets::connect_udp(LdH::Sockets::Address::parse("127.0.0.2", "8081"));
|
||||
char buffer[10] = "hello\n";
|
||||
client.sendOne(10, buffer);
|
||||
client.close();
|
||||
});
|
||||
|
||||
server_thread.join();
|
||||
client_thread.join();
|
||||
server_thread.destroy();
|
||||
client_thread.destroy();
|
||||
|
||||
LdH::Sockets::deinit_sockets();
|
||||
}
|
||||
|
||||
@ -9,11 +9,14 @@ namespace LdH {
|
||||
const std::string message;
|
||||
const std::vector<std::stacktrace_entry> stacktrace;
|
||||
|
||||
explicit inline Exception(char const *message) : message{message}, stacktrace{Exception::generateStacktrace(-1)} {}
|
||||
explicit inline Exception(char const *message) : message{message}, stacktrace{Exception::generateStacktrace(-1)} {
|
||||
}
|
||||
|
||||
explicit inline Exception(std::string const &message) : message{message}, stacktrace{Exception::generateStacktrace(-1)} {}
|
||||
explicit inline Exception(std::string const &message) : message{message}, stacktrace{Exception::generateStacktrace(-1)} {
|
||||
}
|
||||
|
||||
explicit inline Exception(std::string const &&message) : message{message}, stacktrace{Exception::generateStacktrace(-1)} {}
|
||||
explicit inline Exception(std::string const &&message) : message{message}, stacktrace{Exception::generateStacktrace(-1)} {
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
static std::vector<std::stacktrace_entry> generateStacktrace(std::int_fast16_t hidden_frames_count);
|
||||
@ -36,6 +39,29 @@ namespace LdH {
|
||||
std::cerr << "Fatal error:\n" << (Exception{msg}).formatOutput() << std::endl;
|
||||
std::abort();
|
||||
}
|
||||
|
||||
export
|
||||
template<class prefix_printer_t, class routine_t>
|
||||
requires std::invocable<routine_t> && std::same_as<std::invoke_result_t<routine_t>, void>
|
||||
&& std::invocable<prefix_printer_t> && std::same_as<std::invoke_result_t<prefix_printer_t>, void>
|
||||
void run_catching(prefix_printer_t const &prefix_printer, routine_t const &routine) {
|
||||
try {
|
||||
routine();
|
||||
} catch (LdH::Exception const &e) {
|
||||
prefix_printer();
|
||||
e.printStackTrace();
|
||||
} catch (std::exception const &e) {
|
||||
prefix_printer();
|
||||
std::cerr << e.what() << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
export
|
||||
template<class routine_t>
|
||||
requires std::invocable<routine_t> && std::same_as<std::invoke_result_t<routine_t>, void>
|
||||
void run_catching_main(routine_t const &routine) {
|
||||
LdH::run_catching([] { std::cerr << "Uncaught exception in main thread:\n"; }, routine);
|
||||
}
|
||||
}
|
||||
|
||||
module : private;
|
||||
|
||||
@ -38,8 +38,21 @@ void LdH::throwFromWindowsErrCode(DWORD code) {
|
||||
0,
|
||||
nullptr
|
||||
);
|
||||
size_t trim_size = std::strlen(msg);
|
||||
while (trim_size > 0) {
|
||||
switch (msg[--trim_size]) {
|
||||
case '\n':
|
||||
case '\r':
|
||||
msg[trim_size] = '\0';
|
||||
continue;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
std::string msgpp{msg};
|
||||
LocalFree(msg);
|
||||
|
||||
throw LdH::Exception{std::move(msgpp)};
|
||||
}
|
||||
|
||||
|
||||
@ -104,14 +104,7 @@ namespace LdH {
|
||||
thread_routine_t thread_routine = std::move(init->thread_routine);
|
||||
init->notifier.notify();
|
||||
|
||||
try {
|
||||
thread_routine();
|
||||
} catch (LdH::Exception const &e) {
|
||||
std::cerr << "Uncaught exception in thread" << thread_name << ":\n";
|
||||
e.printStackTrace();
|
||||
} catch (std::exception const &e) {
|
||||
std::cerr << "Uncaught exception in thread" << thread_name << ":\n" << e.what() << "\n";
|
||||
}
|
||||
LdH::run_catching([&] { std::cerr << "Uncaught exception in thread" << thread_name << ":\n"; }, thread_routine);
|
||||
return native_wrapper_t::kernel_ret();
|
||||
};
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user