Fix CDC::connect

The function used the m_fd member variable before it was updated to point
to the actual file descriptor of the connection. This caused the test to
fail.

Also fixed the nointr_write function to correctly process multiple
consecutive writes.
This commit is contained in:
Markus Mäkelä
2018-01-30 09:00:47 +02:00
parent b7af191f40
commit cbfee5698f

View File

@ -254,7 +254,12 @@ bool Connection::connect(const std::string& table, const std::string& gtid)
m_error = "Failed to set socket non-blocking: "; m_error = "Failed to set socket non-blocking: ";
m_error += strerror_r(errno, err, sizeof(err)); m_error += strerror_r(errno, err, sizeof(err));
} }
else if (do_auth() && do_registration()) else
{
m_fd = c_fd.release();
m_connected = true;
if (do_auth() && do_registration())
{ {
std::string req_msg(REQUEST_MSG); std::string req_msg(REQUEST_MSG);
req_msg += table; req_msg += table;
@ -274,8 +279,7 @@ bool Connection::connect(const std::string& table, const std::string& gtid)
else if ((m_first_row = read())) else if ((m_first_row = read()))
{ {
rval = true; rval = true;
m_connected = true; }
m_fd = c_fd.release();
} }
} }
} }
@ -438,11 +442,12 @@ bool Connection::do_auth()
std::string auth_str = generateAuthString(m_user, m_password); std::string auth_str = generateAuthString(m_user, m_password);
/** Send the auth string */ /** Send the auth string */
if (nointr_write(auth_str.c_str(), auth_str.length()) == -1) int rc = nointr_write(auth_str.c_str(), auth_str.length());
if (rc <= 0)
{ {
char err[ERRBUF_SIZE]; char err[ERRBUF_SIZE];
m_error = "Failed to write authentication data: "; m_error = "Failed to write authentication data: ";
m_error += strerror_r(errno, err, sizeof(err)); m_error += rc == -1 ? strerror_r(errno, err, sizeof(err)) : "Write timeout";
} }
else else
{ {
@ -688,11 +693,12 @@ int Connection::nointr_read(void *dest, size_t size)
int Connection::nointr_write(const void *src, size_t size) int Connection::nointr_write(const void *src, size_t size)
{ {
int rc = 0; int rc = 0;
int n_bytes = 0; size_t n_bytes = 0;
const uint8_t* ptr = static_cast<const uint8_t*>(src);
if (wait_for_event(POLLOUT) > 0) do
{ {
while ((rc = ::write(m_fd, src, size)) < 0 && errno == EINTR) while ((rc = ::write(m_fd, ptr, size)) < 0 && errno == EINTR)
{ {
; ;
} }
@ -707,8 +713,11 @@ int Connection::nointr_write(const void *src, size_t size)
else if (rc > 0) else if (rc > 0)
{ {
n_bytes += rc; n_bytes += rc;
ptr += rc;
size -= rc;
} }
} }
while (n_bytes < size && wait_for_event(POLLOUT) > 0);
return n_bytes; return n_bytes;
} }