src/viewer/svutil.cpp
char *SVNetwork::Receive() {
  char *result = nullptr;
  if (buffer_ptr_ != nullptr) {
    result = strtok_r(nullptr, "\n", &buffer_ptr_);
  }

  // This means there is something left in the buffer and we return it.
  if (result != nullptr) {
    return result;
    // Otherwise, we read from the stream_.
  } else {
    buffer_ptr_ = nullptr;

    // The timeout length is not really important since we are looping anyway
    // until a new message is delivered.
    struct timeval tv;
    tv.tv_sec = 10;
    tv.tv_usec = 0;

    // Set the flags to return when the stream_ is ready to be read.
    fd_set readfds;
    FD_ZERO(&readfds);
    FD_SET(stream_, &readfds);

    int i = select(stream_ + 1, &readfds, nullptr, nullptr, &tv);

    // The stream_ died.
    if (i == 0) {
      return nullptr;
    }

    // Read the message buffer.
    i = recv(stream_, msg_buffer_in_, kMaxMsgSize, 0);

    // Server quit (0) or error (-1).
    if (i <= 0) {
      return nullptr;
    }
    msg_buffer_in_[i] = '\0';
    // Setup a new string tokenizer.
    return strtok_r(msg_buffer_in_, "\n", &buffer_ptr_);
  }
}
// Receive a message from the server.
// This will always return one line of char* (denoted by \n).
Receive a message from the server.
This will always return one line of char* (denoted by \n).
Receive a message from the server . This will always return one line of char * ( denoted by \ n ) .
char *SVNetwork::Receive() {
  char *result = nullptr;
  if (buffer_ptr_ != nullptr) {
    result = strtok_r(nullptr, "\n", &buffer_ptr_);
  }
  if (result != nullptr) {
    return result;
  } else {
    buffer_ptr_ = nullptr;
    struct timeval tv;
    tv.tv_sec = 10;
    tv.tv_usec = 0;
    fd_set readfds;
    FD_ZERO(&readfds);
    FD_SET(stream_, &readfds);
    int i = select(stream_ + 1, &readfds, nullptr, nullptr, &tv);
    if (i == 0) {
      return nullptr;
    }
    i = recv(stream_, msg_buffer_in_, kMaxMsgSize, 0);
    if (i <= 0) {
      return nullptr;
    }
    msg_buffer_in_[i] = '\0';
    return strtok_r(msg_buffer_in_, "\n", &buffer_ptr_);
  }
}
char * SVNetwork :: Receive ( ) { char * result = nullptr ; if ( buffer_ptr_ != nullptr ) { result = strtok_r ( nullptr , " \n " , & buffer_ptr_ ) ; } if ( result != nullptr ) { return result ; } else { buffer_ptr_ = nullptr ; struct timeval tv ; tv . tv_sec = 10 ; tv . tv_usec = 0 ; fd_set readfds ; FD_ZERO ( & readfds ) ; FD_SET ( stream_ , & readfds ) ; int i = select ( stream_ + 1 , & readfds , nullptr , nullptr , & tv ) ; if ( i == 0 ) { return nullptr ; } i = recv ( stream_ , msg_buffer_in_ , kMaxMsgSize , 0 ) ; if ( i <= 0 ) { return nullptr ; } msg_buffer_in_ [ i ] = ' \0 ' ; return strtok_r ( msg_buffer_in_ , " \n " , & buffer_ptr_ ) ; } }
Receive a message from the server.
Receive a message from the server .
// This means there is something left in the buffer and we return it. // Otherwise, we read from the stream_. // The timeout length is not really important since we are looping anyway // until a new message is delivered. // Set the flags to return when the stream_ is ready to be read. // The stream_ died. // Read the message buffer. // Server quit (0) or error (-1). // Setup a new string tokenizer.