void 
stream_handler::watch_stream(int file_descriptor, bool should_watch) {
   if(fd && fd != file_descriptor) {
      assert(!"Only allowed to handle one stream at a time");
   }
   if(!should_watch) {
      if(fd) {
         tracer.trace0("Cleaning up");
         watch_readable(fd, false);
         watch_writable(fd, false);
         delete[] read_buf;
         delete[] write_buf;
         read_buf = NULL;
         write_buf = NULL;
         fd = 0;
         next_message_length = 0;
         delimiter = NULL;
         delimiter_length = 0;
      }
   } else {
      tracer.trace0("Watching stream %d", file_descriptor);
      fd = file_descriptor;
      watch_readable(fd, true);
      read_buf = new char[buf_size];
      write_buf = new char[buf_size];
      bytes_read = 0;
      bytes_to_write = 0;
   }
}
Beispiel #2
0
void SessionHandler::WatchStream(int socketDescriptor, bool doWatch)
{
    assert((m_fd != 0 && m_fd != socketDescriptor) == false); // only one stream for a socket

    if (doWatch)
    {
        m_tracer.trace0("watching socket %d", socketDescriptor);

        m_fd = socketDescriptor;
        watch_readable(m_fd, true);

        m_readBuffer = new char[m_bufferSize];
        m_writeBuffer = new char[m_bufferSize];
        m_bytesRead = 0;
        m_bytesToWrite = 0;
        OnConnect();
    }
    else if (m_fd != 0)
    {
        m_tracer.trace0("cleaning up");

        watch_readable(m_fd, false);
        watch_writable(m_fd, false);
        delete [] m_readBuffer;
        delete [] m_writeBuffer;
        m_readBuffer = nullptr;
        m_writeBuffer = nullptr;
        m_fd = 0;
        m_nextMessageLength = 0;
    }
}
Beispiel #3
0
void
libevent_loop::watch_exception(int fd, bool interested) {
   // libevent doesn't allow watching just for "exceptions".  Rather when a
   // connection is closed, we'll be notified the fd is "readable" and we'll
   // read EOF.
   watch_readable(fd, interested);
}
void
server_handler::serve(int port, int backlog) {
   // Open up a socket for incoming connections
   server_socket = socket(AF_INET, SOCK_STREAM, 0);
   
   int yes = 1;
   setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
   
   // Bind to our port
   struct sockaddr_in address;
   address.sin_family = AF_INET;
   address.sin_addr.s_addr = INADDR_ANY;
   address.sin_port = htons(port);
   int result = bind(server_socket, (sockaddr *) &address, sizeof(address));
   assert(result == 0 && "Could not bind to port");
   
   result = listen(server_socket, backlog);
   assert(result == 0 && "Could not listen on port");
   watch_readable(server_socket, true);
}