-void STG_CLIENT::Impl::runImpl()
-{
- m_running = true;
-
- while (m_running) {
- fd_set fds;
-
- FD_ZERO(&fds);
- FD_SET(m_sock, &fds);
-
- struct timeval tv;
- tv.tv_sec = 0;
- tv.tv_usec = 500000;
-
- int res = select(m_sock + 1, &fds, NULL, NULL, &tv);
- if (res < 0)
- {
- //m_error = std::string("'select' is failed: '") + strerror(errno) + "'.";
- //m_logger(m_error);
- break;
- }
-
- if (!m_running)
- break;
-
- if (res > 0)
- {
- if (FD_ISSET(m_sock, &fds))
- m_running = read();
- }
- else
- m_running = tick();
- }
-
- m_stopped = true;
-}
-
-int STG_CLIENT::Impl::connect()
-{
- if (m_config.transport == "tcp")
- return connectTCP();
- else if (m_config.transport == "unix")
- return connectUNIX();
- throw Error("Invalid transport type: '" + m_config.transport + "'. Should be 'tcp' or 'unix'.");
-}
-
-int STG_CLIENT::Impl::connectTCP()
-{
- addrinfo hints;
- memset(&hints, 0, sizeof(addrinfo));
-
- hints.ai_family = AF_INET; /* Allow IPv4 */
- hints.ai_socktype = SOCK_STREAM; /* Stream socket */
- hints.ai_flags = 0; /* For wildcard IP address */
- hints.ai_protocol = 0; /* Any protocol */
- hints.ai_canonname = NULL;
- hints.ai_addr = NULL;
- hints.ai_next = NULL;
-
- addrinfo* ais = NULL;
- int res = getaddrinfo(m_config.address.c_str(), m_config.portStr.c_str(), &hints, &ais);
- if (res != 0)
- throw Error("Error resolvin address '" + m_config.address + "': " + gai_strerror(res));
-
- for (addrinfo* ai = ais; ai != NULL; ai = ai->ai_next)
- {
- int fd = socket(AF_INET, SOCK_STREAM, 0);
- if (fd == -1)
- {
- Error error(std::string("Error creating TCP socket: ") + strerror(errno));
- freeaddrinfo(ais);
- throw error;
- }
- if (::connect(fd, ai->ai_addr, ai->ai_addrlen) == -1)
- {
- shutdown(fd, SHUT_RDWR);
- close(fd);
- // TODO: log it.
- continue;
- }
- freeaddrinfo(ais);
- return fd;
- }
-
- freeaddrinfo(ais);
-
- throw Error("Failed to resolve '" + m_config.address);
-};
-
-int STG_CLIENT::Impl::connectUNIX()