示例#1
0
char * readChunkedMessage(xentoollog_logger *xc_logger, struct libxenvchan *ctrl, int *sz){
   int headerSize = 8;
   char * header = (char *) malloc ( headerSize * sizeof(char));
   char * mesg;
   int size;
   //int i = 0;
   char * chunk;
   int chunkSize = 0;
   char * p;
   int messageSize = 0;
   
   if ( header == NULL){
      fprintf(stderr, "Error: Failed to allocate space for buffer\n");
      perror("readChunkedMessage: read failed.");
      exit(1);
   }

   //get Header
   while(!libxenvchan_data_ready(ctrl)){
     sleep(0.10);
   }
   size = libxenvchan_read(ctrl,header, headerSize);  
   if ( size != headerSize){
    fprintf(stderr, "Error: libxenvchan_read return=%d. should be %d\n", size, headerSize);
    perror("readChunkedMessage: read failed 1.");
    exit(1);
   }
    
    messageSize = getSize(header+1);
  //  printf("messageSize: %d\n",messageSize);
    *sz = messageSize;
/*
    printf("HexDump\n");
    for (i = 0; i < 5;i++){
        printf("%2x ",header[i]);
    }
    printf("\n");
*/
   //allocate space for entire message 
   mesg = (char *)malloc(sizeof(char) * messageSize);   
    if(mesg == NULL) {
         char * lclErrStr = strerror(errno);
         fprintf(stderr, "Error: %s: readChunkedMessage failed\n",
             lclErrStr);
         perror("readChunkedMessage: read failed");
    }
   p = mesg;
   
   if (isChunked(header) ){
       
/*     printf("Chunked Data\n");
     printf("DataReady: %d\n",libxenvchan_data_ready(ctrl));
*/
   while(!libxenvchan_data_ready(ctrl)){
      sleep(0.10);
   }
     //get second size
     size = libxenvchan_read(ctrl,header, headerSize-1);
     //printf("finished read\n");

     if ( size != headerSize-1){
       fprintf(stderr, "libxenvchan_read return=%d. should be %d\n", size, headerSize-1);
       perror("readChunkedMessage: read failed 2.");
       //exit(1);
     }
     
     //printf("getting size\n");
     chunkSize = getSize(header);
     //printf("done with size: %d\n",chunkSize);
     
     //allocate chunk
     chunk = (char *)malloc(sizeof(char) *chunkSize); 
     if ( chunk == NULL){
        fprintf(stderr, "Error: Failed to allocate space for chunk\n");
        perror("readChunkedMessage: read failed.");
        exit(1);
     }
 
     //read chunk
     while(!libxenvchan_data_ready(ctrl)){
       sleep(0.10);
     }
     size = libxenvchan_read(ctrl,chunk, chunkSize);
     //printf("Reading Chunk\n");
     
     if ( size != chunkSize){
       fprintf(stderr, "libxenvchan_read return=%d. should be %d\n", size, chunkSize);
       perror("readChunkedMessage: read failed 3.");
       exit(1);
     }
     memcpy(p,chunk,chunkSize);
     p= p+chunkSize;

     free(chunk);
/*     printf("HexDump\n");
     for (i = 0; i < messageSize;i++){
       printf("%2x ",mesg[i]);
     }
     printf("\n");
*/
     while(p - mesg < messageSize){
        //printf("DataReady: %d\n",libxenvchan_data_ready(ctrl));
        if (libxenvchan_data_ready(ctrl)>headerSize){
          size = libxenvchan_read(ctrl,header, headerSize);  


          if ( size != headerSize){
            fprintf(stderr, "libxenvchan_read return=%d. should be %d\n", size, headerSize);
            perror("readChunkedMessage: read failed 4.");
            exit(1);
          }
          //printf("getting size2\n");
          chunkSize = getSize(header+1);
          //printf("done with size2: %d\n",chunkSize);
          
          //allocate chunk
          chunk = (char *)malloc(sizeof(char) *chunkSize); 
          if ( chunk == NULL){
             fprintf(stderr, "Error: Failed to allocate space for chunk\n");
             perror("readChunkedMessage: read failed.");
             exit(1);
          }

          
          //loop until we have data ready
          while (libxenvchan_data_ready(ctrl)<chunkSize){
            sleep(0.10);
          };
          size = libxenvchan_read(ctrl,chunk, chunkSize);
          if ( size != chunkSize){
            fprintf(stderr, "libxenvchan_read return=%d. should be %d\n", size, chunkSize);
            perror("readChunkedMessage: read failed 5.");
            exit(1);
          }
          memcpy(p,chunk,chunkSize);
          p= p+chunkSize;

          free(chunk);
/*          printf("Math: %d\n",p-mesg);
          printf("HexDump\n");
          for (i = 0; i < messageSize;i++){
            printf("%2x ",mesg[i]);
          }
*/
        }
     }
   }else{
     //printf("Not Chunked\n");
     size = libxenvchan_read(ctrl,mesg,messageSize);
   }
     free(header);
/*     printf("Size Data: %d\n",messageSize);

     for (i = 0; i < messageSize; i++){
        printf("%c",mesg[i]);
     }
     printf ("\n");
     printf("HexDump\n");
     for (i = 0; i < messageSize;i++){
        printf("%2x ",mesg[i]);
     }
     printf("\n");
*/
  return mesg;
}
示例#2
0
int libvchan_data_ready(libvchan_t *ctrl) {
    return libxenvchan_data_ready(ctrl->xenvchan);
}