title: libev sets sockets to blocking with no timeout
body: <p>Rant: I really dislike boost::asio, So I've been looking at alternatives and came across libev. Which seems simple enough for me, but is doing a few things I cannot understand. If those are too many questions in one thread, please let me know.</p>
  
  <p>1) I set the listening socket to NON_BLOCK, I also set each accepted incoming connection as NON_BLOCK, yet somewhere in the code the socket(s) turns into BLOCK.
  Ex:</p>
  
  <pre><code>bool Server::Start()
  {
      // Setup event loop
      loop = ev_default_loop(EVBACKEND_SELECT); //EVFLAG_AUTO ?
      // Create Socket
      sockfd = socket(PF_INET, SOCK_STREAM, 0);
      addr_len = sizeof(addr)
      // Set Socket to non blocking
      fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFL) | O_NONBLOCK);
      if (fcntl(sockfd, F_GETFL) &amp; O_NONBLOCK) std::cout &lt;&lt; "Socket is NONBLOCK" &lt;&lt; std::endl;
      else std::cout &lt;&lt; "Socket is BLOCK" &lt;&lt; std::endl;
      if (sockfd &lt; 0) {
          std::cout &lt;&lt; "ERROR opening socket" &lt;&lt; std::endl;
          return false;
      }
      bzero((char *)&amp;addr, sizeof(addr));
      addr.sin_family = AF_INET;
      addr.sin_port = htons(port);
      addr.sin_addr.s_addr = INADDR_ANY;
      // Bind port to socket
      if (bind(sockfd,(struct sockaddr*)&amp;addr, sizeof(addr))!=0) {
          std::cout &lt;&lt; "bind error" &lt;&lt; std::endl;
          return false;
      }
      // Listen
      if (listen(sockfd, 2) &lt; 0) {
          std::cout &lt;&lt; "listen error" &lt;&lt; std::endl;
          return false;
      }
      // Initialize and start a watcher to accepts client requests
      ev_io_init(&amp;w_accept, accept_cb, sockfd, EV_READ);
      ev_io_start(loop, &amp;w_accept);
      return true;
  }
  </code></pre>
  
  <p>I have tried to make the main loop also not to block:</p>
  
  <pre><code>void Server::MainLoop()
  {
      // Start infinite loop
      while (1) {
          ev_loop(loop, EVLOOP_NONBLOCK);
      }
  }
  </code></pre>
  
  <p>But it doesnt seem to have made a different. PLEASE DO NOT redirect me to the documentation (the only available source of documentation on the internet) I have read it.</p>
  
  <p>I do this for the client socket that has been accepted:</p>
  
  <pre><code>void accept_cb(struct ev_loop *loop, struct ev_io *watcher, int revents)
  ....
  c-&gt;client_sd = accept(watcher-&gt;fd, (struct sockaddr *)&amp;c-&gt;client_addr, &amp;c-&gt;client_len);
  ....
  ev_io *w_client = (struct ev_io*) malloc (sizeof(struct ev_io));
  ev_io_init(w_client, read_cb, c-&gt;client_sd, EV_READ);
  ev_io_start(loop, w_client);
  fcntl(watcher-&gt;fd, F_SETFL, fcntl(watcher-&gt;fd, F_GETFL) | O_NONBLOCK);
  </code></pre>
  
  <p>Yet every time my read callback is executed, the socket is magically set to BLOCK</p>
  
  <p>2) I have tried setting a timeout for the socket:
  struct timeval timeout;      </p>
  
  <pre><code>timeout.tv_sec = 10;
  timeout.tv_usec = 0;
  
  if (setsockopt (sockfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&amp;timeout,
              sizeof(timeout)) &lt; 0)
      error("setsockopt failed\n");
  
  if (setsockopt (sockfd, SOL_SOCKET, SO_SNDTIMEO, (char *)&amp;timeout,
              sizeof(timeout)) &lt; 0)
      error("setsockopt failed\n");
  </code></pre>
  
  <p>(Taken from here: <a href="https://stackoverflow.com/questions/4181784/how-to-set-socket-timeout-in-c-when-making-multiple-connections">this question</a>)
  It simply doesn't work. Is this because the sockets are reset to BLOCKing mode ?</p>
  
  <p>3) I have seen a C++ wrapper for libev. I absolutely hate the fact I have to make the callbacks static functions, it ruins everything for me. Yet all the examples I have seen use:</p>
  
  <pre><code>signal.loop.break_loop();
  </code></pre>
  
  <p>and</p>
  
  <pre><code>loop.run(0);
  </code></pre>
  
  <p>which, funnily enough produces:</p>
  
  <blockquote>
    <p>error: ‘struct ev::loop_ref’ has no member named ‘break_loop’ error:
    ‘struct ev::default_loop’ has no member named ‘run’</p>
  </blockquote>
  
  <p>on Debian Squeeze.</p>
  
  <p>So, what I am asking is:</p>
  
  <ol>
  <li>What, who, where is the socket changed from NON_BLOCK to BLOCK ?</li>
  <li>How (if) can I set a timeout for the socket (blocking or non-blocking)</li>
  <li>What is wrong with ev++.h and why are those nice people using the wrappers I can't use?</li>
  </ol>
  
  <p>Please, bear in mind that I can use the sockets to read and send data, but in a blocking manner, without timeouts. Furthermore, as this is a server, I NEED to keep the code in classes, as I have to save messages per connected clients. Making this static or non-class methods simply ruins it, or forces me to take a very different approach.</p>
  
  <p>PS: Any alternatives to libev ?</p>