Skip to content

Commit 1e9b8cb

Browse files
committed
Update error info if socket call failed (do not warn anymore if not necessary)
1 parent 5a9a860 commit 1e9b8cb

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

src/coroutine/socket.cc

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -806,12 +806,12 @@ bool Socket::connect(string _host, int _port, int flags)
806806
{
807807
if (_port == -1)
808808
{
809-
swWarn("Socket of type AF_INET/AF_INET6 requires port argument");
809+
set_err(EINVAL, "Socket of type AF_INET/AF_INET6 requires port argument");
810810
return false;
811811
}
812812
else if (_port == 0 || _port >= 65536)
813813
{
814-
swWarn("Invalid port argument[%d]", _port);
814+
set_err(EINVAL, cpp_string::format("Invalid port [%d]", _port).c_str());
815815
return false;
816816
}
817817
}
@@ -1136,7 +1136,7 @@ bool Socket::bind(std::string address, int port)
11361136
}
11371137
if ((sock_domain == AF_INET || sock_domain == AF_INET6) && (port < 0 || port > 65535))
11381138
{
1139-
swWarn("invalid port [%d]", port);
1139+
set_err(EINVAL, cpp_string::format("Invalid port [%d]", port).c_str());
11401140
return false;
11411141
}
11421142

@@ -1172,8 +1172,13 @@ bool Socket::bind(std::string address, int port)
11721172

11731173
if (bind_address.size() >= sizeof(sa->sun_path))
11741174
{
1175-
set_err(EINVAL);
1176-
swWarn("UNIXSocket bind path(%s) is too long, the maxium limit of bytes number is %zu", bind_address.c_str(), sizeof(sa->sun_path));
1175+
set_err(
1176+
EINVAL,
1177+
cpp_string::format(
1178+
"UNIXSocket bind path(%s) is too long, the maxium limit of bytes number is %zu",
1179+
bind_address.c_str(), sizeof(sa->sun_path)
1180+
).c_str()
1181+
);
11771182
return false;
11781183
}
11791184
memcpy(&sa->sun_path, bind_address.c_str(), bind_address.size());
@@ -1436,7 +1441,7 @@ bool Socket::sendfile(const char *filename, off_t offset, size_t length)
14361441
int file_fd = ::open(filename, O_RDONLY);
14371442
if (file_fd < 0)
14381443
{
1439-
swSysWarn("open(%s) failed", filename);
1444+
set_err(errno, cpp_string::format("open(%s) failed, %s", filename, strerror(errno)).c_str());
14401445
return false;
14411446
}
14421447

@@ -1445,7 +1450,7 @@ bool Socket::sendfile(const char *filename, off_t offset, size_t length)
14451450
struct stat file_stat;
14461451
if (::fstat(file_fd, &file_stat) < 0)
14471452
{
1448-
swSysWarn("fstat(%s) failed", filename);
1453+
set_err(errno, cpp_string::format("fstat(%s) failed, %s", filename, strerror(errno)).c_str());
14491454
::close(file_fd);
14501455
return false;
14511456
}
@@ -1478,14 +1483,13 @@ bool Socket::sendfile(const char *filename, off_t offset, size_t length)
14781483
}
14791484
else if (n == 0)
14801485
{
1481-
swWarn("sendfile return zero");
1486+
set_err(SW_ERROR_SYSTEM_CALL_FAIL, "sendfile return zero");
14821487
::close(file_fd);
14831488
return false;
14841489
}
14851490
else if (errno != EAGAIN)
14861491
{
1487-
swSysWarn("sendfile(%d, %s) failed", sock_fd, filename);
1488-
set_err(errno);
1492+
set_err(errno, cpp_string::format("sendfile(%d, %s) failed, %s", sock_fd, filename, strerror(errno)).c_str());
14891493
::close(file_fd);
14901494
return false;
14911495
}
@@ -1521,9 +1525,8 @@ ssize_t Socket::sendto(const char *address, int port, const void *__buf, size_t
15211525
{
15221526
if (::inet_aton(address, &addr.in.sin_addr) == 0)
15231527
{
1524-
swWarn("ip[%s] is invalid", address);
1528+
set_err(EINVAL, cpp_string::format("ip[%s] is invalid", address).c_str());
15251529
retval = -1;
1526-
errno = EINVAL;
15271530
break;
15281531
}
15291532
addr.in.sin_family = AF_INET;
@@ -1535,8 +1538,9 @@ ssize_t Socket::sendto(const char *address, int port, const void *__buf, size_t
15351538
{
15361539
if (::inet_pton(AF_INET6, address, &addr.in6.sin6_addr) < 0)
15371540
{
1538-
swWarn("ip[%s] is invalid", address);
1539-
return SW_ERR;
1541+
set_err(EINVAL, cpp_string::format("ip[%s] is invalid", address).c_str());
1542+
retval = -1;
1543+
break;
15401544
}
15411545
addr.in6.sin6_port = (uint16_t) htons(port);
15421546
addr.in6.sin6_family = AF_INET6;
@@ -1551,8 +1555,8 @@ ssize_t Socket::sendto(const char *address, int port, const void *__buf, size_t
15511555
break;
15521556
}
15531557
default:
1558+
set_err(EPROTONOSUPPORT);
15541559
retval = -1;
1555-
errno = EPROTONOSUPPORT;
15561560
break;
15571561
}
15581562

@@ -1563,9 +1567,9 @@ ssize_t Socket::sendto(const char *address, int port, const void *__buf, size_t
15631567
retval = ::sendto(sock_fd, __buf, __n, 0, (struct sockaddr *) &addr, addr_size);
15641568
swTraceLog(SW_TRACE_SOCKET, "sendto %ld/%ld bytes, errno=%d", retval, __n, errno);
15651569
} while (retval < 0 && (errno == EINTR || (swConnection_error(errno) == SW_WAIT && timer.start() && wait_event(SW_EVENT_WRITE, &__buf, __n))));
1570+
set_err(retval < 0 ? errno : 0);
15661571
}
15671572

1568-
set_err(retval < 0 ? errno : 0);
15691573
return retval;
15701574
}
15711575

0 commit comments

Comments
 (0)