From 6173c6938f02c19376c723b355e38cf12d7332f9 Mon Sep 17 00:00:00 2001 From: Ivan Mahonin Date: Oct 07 2021 17:54:17 +0000 Subject: connection timeout --- diff --git a/main.cpp b/main.cpp index abb01e1..b45311d 100644 --- a/main.cpp +++ b/main.cpp @@ -12,7 +12,7 @@ int main(int argc, char** argv) { Tcp::Address address; address.resolve(argv[2]); - if (0 == strcmp(argv[1], "tcpconnect")) { + if (0 == strcmp(argv[1], "tcpclient")) { printf("tcpconnect\n"); Tcp::Connection connection; diff --git a/tcp.cpp b/tcp.cpp index 3653bd6..10952a8 100644 --- a/tcp.cpp +++ b/tcp.cpp @@ -18,6 +18,7 @@ static const int pollTimeoutMs = 100; +static const int connTimeoutMs = 300000; using namespace Tcp; @@ -139,7 +140,8 @@ bool Connection::read(void *data, size_t size) { pfd.fd = sockId; pfd.events = POLLIN; - while(check() && size) { + int remainMs = connTimeoutMs; + while(check() && size && remainMs >= 0) { int pr = poll(&pfd, 1, pollTimeoutMs); if (pr < 0) { close(ERR_TCP_CONNECTION_LOST); return false; } @@ -154,6 +156,7 @@ bool Connection::read(void *data, size_t size) { data = (char*)data + r; } } + remainMs -= pollTimeoutMs; } return size == 0; @@ -167,7 +170,8 @@ bool Connection::write(const void *data, size_t size) { pfd.fd = sockId; pfd.events = POLLOUT; - while(check() && size) { + int remainMs = connTimeoutMs; + while(check() && size && remainMs >= 0) { int pr = poll(&pfd, 1, pollTimeoutMs); if (pr < 0) { close(ERR_TCP_CONNECTION_LOST); return false; } @@ -179,9 +183,10 @@ bool Connection::write(const void *data, size_t size) { { close(ERR_TCP_CONNECTION_LOST); return false; } } else { size -= r; - data = (char*)data + r; + data = (const char*)data + r; } } + remainMs -= pollTimeoutMs; } return size == 0;