mirror of
https://git.sfja.dk/Mikkel/slige.git
synced 2025-01-18 19:16:35 +00:00
close socket on error
This commit is contained in:
parent
ef528ccb3f
commit
d67b55e570
@ -124,12 +124,14 @@ public:
|
|||||||
auto err
|
auto err
|
||||||
= ::bind(socket, (struct sockaddr*)&address, sizeof(address));
|
= ::bind(socket, (struct sockaddr*)&address, sizeof(address));
|
||||||
if (err < 0) {
|
if (err < 0) {
|
||||||
|
close(socket);
|
||||||
return Err { .msg = std::format("could not bind ({})", err) };
|
return Err { .msg = std::format("could not bind ({})", err) };
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
auto err = ::listen(socket, 0);
|
auto err = ::listen(socket, 0);
|
||||||
if (err < 0) {
|
if (err < 0) {
|
||||||
|
close(socket);
|
||||||
return Err { .msg = std::format("could not listen ({})", err) };
|
return Err { .msg = std::format("could not listen ({})", err) };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -140,6 +142,7 @@ public:
|
|||||||
int client = ::accept(
|
int client = ::accept(
|
||||||
socket, (struct sockaddr*)&client_address, &address_size);
|
socket, (struct sockaddr*)&client_address, &address_size);
|
||||||
if (client < 0) {
|
if (client < 0) {
|
||||||
|
close(socket);
|
||||||
return Err { .msg
|
return Err { .msg
|
||||||
= std::format("could not accept ({})", client) };
|
= std::format("could not accept ({})", client) };
|
||||||
}
|
}
|
||||||
@ -149,10 +152,7 @@ public:
|
|||||||
std::string message = {};
|
std::string message = {};
|
||||||
while (true) {
|
while (true) {
|
||||||
ssize_t bytes_read = read(client, buffer, buf_len);
|
ssize_t bytes_read = read(client, buffer, buf_len);
|
||||||
if (bytes_read < 0) {
|
if (bytes_read <= 0) {
|
||||||
return Err { .msg
|
|
||||||
= std::format("could not read ({})", bytes_read) };
|
|
||||||
} else if (bytes_read == 0) {
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
for (size_t i = 0; i < (size_t)bytes_read; ++i) {
|
for (size_t i = 0; i < (size_t)bytes_read; ++i) {
|
||||||
@ -168,6 +168,7 @@ public:
|
|||||||
"error parsing rpc message: '{}' @ {}:{}\n",
|
"error parsing rpc message: '{}' @ {}:{}\n",
|
||||||
err.msg, err.pos.line, err.pos.col);
|
err.msg, err.pos.line, err.pos.col);
|
||||||
close(client);
|
close(client);
|
||||||
|
close(socket);
|
||||||
return Err {
|
return Err {
|
||||||
.msg = msg,
|
.msg = msg,
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user