mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-05 11:01:49 +00:00
add error callback to network.tcp_connect
This commit is contained in:
parent
553ee8b1f2
commit
f8be3e74f1
6 changed files with 79 additions and 17 deletions
|
|
@ -106,6 +106,7 @@ class SocketTcpConnection : public TcpConnection {
|
|||
std::vector<char> readBatch;
|
||||
util::Buffer<char> buffer;
|
||||
std::mutex mutex;
|
||||
std::string errorMessage;
|
||||
|
||||
void connectSocket() {
|
||||
state = ConnectionState::CONNECTING;
|
||||
|
|
@ -115,7 +116,8 @@ class SocketTcpConnection : public TcpConnection {
|
|||
auto error = handle_socket_error("Connect failed");
|
||||
closesocket(descriptor);
|
||||
state = ConnectionState::CLOSED;
|
||||
logger.error() << error.what();
|
||||
errorMessage = error.what();
|
||||
logger.error() << errorMessage;
|
||||
return;
|
||||
}
|
||||
logger.info() << "connected to " << to_string(addr);
|
||||
|
|
@ -182,13 +184,15 @@ public:
|
|||
thread = std::make_unique<std::thread>([this]() { startListen();});
|
||||
}
|
||||
|
||||
void connect(runnable callback) override {
|
||||
thread = std::make_unique<std::thread>([this, callback]() {
|
||||
void connect(runnable callback, stringconsumer errorCallback) override {
|
||||
thread = std::make_unique<std::thread>([this, callback, errorCallback]() {
|
||||
connectSocket();
|
||||
if (state == ConnectionState::CONNECTED) {
|
||||
callback();
|
||||
startListen();
|
||||
} else {
|
||||
errorCallback(errorMessage);
|
||||
}
|
||||
startListen();
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -263,7 +267,10 @@ public:
|
|||
}
|
||||
|
||||
static std::shared_ptr<SocketTcpConnection> connect(
|
||||
const std::string& address, int port, runnable callback
|
||||
const std::string& address,
|
||||
int port,
|
||||
runnable callback,
|
||||
stringconsumer errorCallback
|
||||
) {
|
||||
addrinfo hints {};
|
||||
|
||||
|
|
@ -274,7 +281,11 @@ public:
|
|||
if (int res = getaddrinfo(
|
||||
address.c_str(), nullptr, &hints, &addrinfo
|
||||
)) {
|
||||
throw std::runtime_error(gai_strerror(res));
|
||||
std::string errorMessage = gai_strerror(res);
|
||||
if (errorCallback) {
|
||||
errorCallback(errorMessage);
|
||||
}
|
||||
throw std::runtime_error(errorMessage);
|
||||
}
|
||||
|
||||
sockaddr_in serverAddress;
|
||||
|
|
@ -284,10 +295,14 @@ public:
|
|||
|
||||
SOCKET descriptor = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (descriptor == -1) {
|
||||
throw std::runtime_error("Could not create socket");
|
||||
std::string errorMessage = "could not create socket";
|
||||
if (errorCallback) {
|
||||
errorCallback(errorMessage);
|
||||
}
|
||||
throw std::runtime_error(errorMessage);
|
||||
}
|
||||
auto socket = std::make_shared<SocketTcpConnection>(descriptor, std::move(serverAddress));
|
||||
socket->connect(std::move(callback));
|
||||
socket->connect(std::move(callback), std::move(errorCallback));
|
||||
return socket;
|
||||
}
|
||||
|
||||
|
|
@ -670,9 +685,14 @@ public:
|
|||
|
||||
namespace network {
|
||||
std::shared_ptr<TcpConnection> connect_tcp(
|
||||
const std::string& address, int port, runnable callback
|
||||
const std::string& address,
|
||||
int port,
|
||||
runnable callback,
|
||||
stringconsumer errorCallback
|
||||
) {
|
||||
return SocketTcpConnection::connect(address, port, std::move(callback));
|
||||
return SocketTcpConnection::connect(
|
||||
address, port, std::move(callback), std::move(errorCallback)
|
||||
);
|
||||
}
|
||||
|
||||
std::shared_ptr<TcpServer> open_tcp_server(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue