예제 #1
0
Socket *IPv4Socket::accept(bool nonblocking) {
    //fudeb("Socket::accept(s:%d)", sock_);
    LOG_DEBUG((use_rs_ ? "RDMA" : "TCP"));
    int asock = 0;
    while (!stop_) {
        if (nonblocking_) {
            wait_for_read();
        }
        asock = rs_accept(sock_, 0, 0);
        if (asock >= 0)
            break;
        LOG_WARN("accept encountered " << strerror(errno));
        if (asock < 0 && errno != EAGAIN && errno != EINTR
#ifndef _WIN32
                && errno != ECONNABORTED && errno != EPROTO
#endif
           ) {
            throw IOException("accept", "", getErrorNumber());
        }

    }
    if (stop_) {
        return 0;
    }

    uint16_t remote_port = 0;
    char str[INET_ADDRSTRLEN];

    struct sockaddr_in clientaddr;
    socklen_t addrlen = sizeof(clientaddr);
    int res = rs_getpeername(asock, (struct sockaddr *) &clientaddr, &addrlen);
    if (res < 0) {
        throw IOException("Socket::accept() Failed to find peer IP/port", "",
                          res);
    }
    char * buf = inet_ntoa(clientaddr.sin_addr);
    if (buf != 0) {
        str[0] = 0;
#pragma warning( push )
#pragma warning( disable: 4996) // we use strncpy in a safe way
        strncpy(str, buf, INET_ADDRSTRLEN);
#pragma warning( pop )
        LOG_DEBUG("IPv4 Client address is " << str);
        LOG_DEBUG("Client port is " << ntohs(clientaddr.sin_port));
    } else {
        throw IOException(
            "Socket::accept() Failure to resolve remote peer for accepted connection");
    }

    remote_port = ntohs(clientaddr.sin_port);

    Socket *asock2 = new IPv4Socket(this, asock, sock_type_, str, remote_port);
    if (nonblocking) {
        asock2->setNonBlocking();
    }
    // TODO make sendbuffersize
    // asock2->setSendbufferssize(32 * 1024);
    return asock2;
}
예제 #2
0
void
DbUtil::printError(const char *msg)
{
  if (m_mysql && mysql_errno(m_mysql))
  {
    if (m_mysql->server_version)
      printf("\n [MySQL-%s]", m_mysql->server_version);
    else
      printf("\n [MySQL]");
      printf("[%d] %s\n", getErrorNumber(), getError());
  }
  else if (msg)
    printf(" [MySQL] %s\n", msg);
}
예제 #3
0
// nonblocking connect is described in detail in chapter 15.4 of stevens unix network programming
bool IPv4Socket::connect_nb(const std::string &host, uint16_t port)
{
    LOG_DEBUG("IPv4Socket::connect_nb "<< host.c_str() << ":" << port);
    connectionInProgress_ = true;
    struct sockaddr *saddr = 0;
    struct sockaddr_in inaddr;
    setup_sockaddr_v4_(inaddr, host, port);
    socklen_t len = sizeof(sockaddr_in);
    saddr = (struct sockaddr *) &inaddr;
    setNonBlocking();
    int res = rs_connect(sock_, saddr, len);
    if (res == 0)
    {
        connectionInProgress_ = false;
        clearNonBlocking();
        return true;
    }
    const int err = getErrorNumber();
#ifdef WIN32
    if (err == WSAEWOULDBLOCK)
        return true;

    if (err != WSAEINPROGRESS && err != WSAEINTR) {
#else
    if (err != EINPROGRESS && err != EINTR) {
#endif
        std::stringstream ss;
        ss << host << ":" << port;
        std::string str = ss.str();
        throw ConnectionRefusedError("Failure to connect to", str.c_str(), err);
    };
    return false;
}


}