/* The message is returned in a static buffer, so don't call this
   again until you're done with the previous response or it will be overwritten. */
const char* recv_msg(char *buf, int bufsize) {
  int val;
  fd_set read_fdset;
  fd_set except_fdset;
  struct timeval tv;
  char readbuf[bufsize];
  unsigned int totalbytes;

  /* Is there already a reply in the buffer from before? (can happen if
     we get multiple lines in one read) */
  const char *reply_msg = extract_reply(buf);
  if (reply_msg != NULL)
    return reply_msg;

  while (1) {
    tv.tv_sec = 1;
    tv.tv_usec = 0;
    FD_ZERO(&read_fdset);
    FD_ZERO(&except_fdset);
    FD_SET(sock, &read_fdset);
    FD_SET(sock, &except_fdset);
    if (select(sock+1, &read_fdset, NULL, &except_fdset, &tv) == 0) {
      /* we've waited 2 seconds and got no response - too long - conclude
	 the socket is dead */
      printf("timed out waiting response\n");
      //connect_to_robot();
      //initialize_robot();
      return 0;
    }
    if (FD_ISSET(sock, &except_fdset)) {
      connect_to_robot();
      re_initialize_robot();
      return 0;
    }
  
    assert(FD_ISSET(sock, &read_fdset));
    val = read(sock, readbuf, bufsize-1);
    if (val > 0) {
    } else {
      /* the write failed - likely the robot was switched off - attempt
	 to reconnect and reinitialize */
      connect_to_robot();
      re_initialize_robot();
      buf[0]='\0';
      return 0;
    }
    /* ensure readbuf is null terminated */
    readbuf[val] = '\0';
    totalbytes = strlen(buf);
    add_to_buffer(buf, &totalbytes, readbuf);
    reply_msg = extract_reply(buf);
    if (reply_msg != NULL)
      return reply_msg;
  }
}
示例#2
0
 void extract_array(redisReply *node, Rcpp::List& retlist) {
     for(unsigned int i = 0;i < node->elements;i++) {
         retlist[i] = extract_reply(node->element[i]);
     }
 }
示例#3
0
 // execute given string
 SEXP exec(std::string cmd) {
     redisReply *reply = static_cast<redisReply*>(redisCommand(prc_, cmd.c_str()));
     SEXP rep = extract_reply(reply);
     freeReplyObject(reply);
     return(rep);
 }