Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add errString to store dynamic error message #3229

Merged
merged 2 commits into from
Apr 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions include/coroutine_socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class Socket
swSocket *socket = nullptr;
int errCode = 0;
const char *errMsg = "";
std::string errString;

bool open_length_check = false;
bool open_eof_check = false;
Expand Down Expand Up @@ -251,6 +252,14 @@ class Socket
errMsg = s;
}


inline void set_err(int e, std::string s)
{
errCode = errno = e;
errString = s;
errMsg = errString.c_str();
}

/* set connect read write timeout */
inline void set_timeout(double timeout, int type = SW_TIMEOUT_ALL)
{
Expand Down
16 changes: 8 additions & 8 deletions src/coroutine/socket.cc
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,7 @@ bool Socket::connect(string _host, int _port, int flags)
}
else if (_port == 0 || _port >= 65536)
{
set_err(EINVAL, cpp_string::format("Invalid port [%d]", _port).c_str());
set_err(EINVAL, cpp_string::format("Invalid port [%d]", _port));
return false;
}
}
Expand Down Expand Up @@ -1133,7 +1133,7 @@ bool Socket::bind(std::string address, int port)
}
if ((sock_domain == AF_INET || sock_domain == AF_INET6) && (port < 0 || port > 65535))
{
set_err(EINVAL, cpp_string::format("Invalid port [%d]", port).c_str());
set_err(EINVAL, cpp_string::format("Invalid port [%d]", port));
return false;
}

Expand Down Expand Up @@ -1164,7 +1164,7 @@ bool Socket::bind(std::string address, int port)
cpp_string::format(
"UNIXSocket bind path(%s) is too long, the maxium limit of bytes number is %zu",
bind_address.c_str(), sizeof(sa->sun_path)
).c_str()
)
);
return false;
}
Expand Down Expand Up @@ -1475,7 +1475,7 @@ bool Socket::sendfile(const char *filename, off_t offset, size_t length)
int file_fd = ::open(filename, O_RDONLY);
if (file_fd < 0)
{
set_err(errno, cpp_string::format("open(%s) failed, %s", filename, strerror(errno)).c_str());
set_err(errno, cpp_string::format("open(%s) failed, %s", filename, strerror(errno)));
return false;
}

Expand All @@ -1484,7 +1484,7 @@ bool Socket::sendfile(const char *filename, off_t offset, size_t length)
struct stat file_stat;
if (::fstat(file_fd, &file_stat) < 0)
{
set_err(errno, cpp_string::format("fstat(%s) failed, %s", filename, strerror(errno)).c_str());
set_err(errno, cpp_string::format("fstat(%s) failed, %s", filename, strerror(errno)));
::close(file_fd);
return false;
}
Expand Down Expand Up @@ -1523,7 +1523,7 @@ bool Socket::sendfile(const char *filename, off_t offset, size_t length)
}
else if (errno != EAGAIN)
{
set_err(errno, cpp_string::format("sendfile(%d, %s) failed, %s", sock_fd, filename, strerror(errno)).c_str());
set_err(errno, cpp_string::format("sendfile(%d, %s) failed, %s", sock_fd, filename, strerror(errno)));
::close(file_fd);
return false;
}
Expand Down Expand Up @@ -1559,7 +1559,7 @@ ssize_t Socket::sendto(const char *address, int port, const void *__buf, size_t
{
if (::inet_aton(address, &addr.in.sin_addr) == 0)
{
set_err(EINVAL, cpp_string::format("ip[%s] is invalid", address).c_str());
set_err(EINVAL, cpp_string::format("ip[%s] is invalid", address));
retval = -1;
break;
}
Expand All @@ -1572,7 +1572,7 @@ ssize_t Socket::sendto(const char *address, int port, const void *__buf, size_t
{
if (::inet_pton(AF_INET6, address, &addr.in6.sin6_addr) < 0)
{
set_err(EINVAL, cpp_string::format("ip[%s] is invalid", address).c_str());
set_err(EINVAL, cpp_string::format("ip[%s] is invalid", address));
retval = -1;
break;
}
Expand Down