int sendHeaderProto(int method, Song **songs, int numSongs, int sock) { fflush(stdout); Header header = HEADER__INIT; unsigned len; void *buf; header.method = method; header.n_songs = numSongs; header.songs = songs; len = header__get_packed_size(&header); buf = malloc(len); header__pack(&header, buf); int totalBytesSent = 0; int numBytesSent = 0; int network_length = htonl(len); send(sock, &network_length, LENGTH_PREFIX_SIZE, 0); /* while there are still bytes left to send */ while(totalBytesSent < len) { /* send header */ numBytesSent = send(sock, &(buf[totalBytesSent]), len-totalBytesSent, 0); if(numBytesSent<0) fatal_error("header send failed"); totalBytesSent+=numBytesSent; } free(buf); //free(header.songs); return totalBytesSent; }
void init_msgheader(int id) { MS.mh.message_id = 1; MS.mh.message_flag = id; mh_length = header__get_packed_size(&MS.mh); buf_mh = malloc(mh_length); //printf("%d\n",id); header__pack(&MS.mh,buf_mh); //printf("%d,%d\n",sizeof(buf_mh),strlen(buf_mh)); //Header *hd; //hd = header__unpack(NULL,sizeof(buf_mh),buf_mh); //printf("id:%d flag:%d\n",hd->message_id,hd->message_flag); //fwrite(buf_mh,mh_length,1,stdout); }
/* pass in header struct, send struct as is, at the end of the method the header struct is freed */ int sendHeader(int method,int numBytesToSend,int index, int sock) { Header header = HEADER__INIT; unsigned len; void *buf; header.method = method; /* replace with repeated songs header.length = numBytesToSend; header.indexes = index; */ len = header__get_packed_size(&header); buf = malloc(len); header__pack(&header, buf); int totalBytesSent = 0; int numBytesSent = 0; int network_length = htonl(len); send(sock, &network_length, LENGTH_PREFIX_SIZE, 0); /* while there are still bytes left to send */ while(totalBytesSent < len) { /* send header */ numBytesSent = send(sock, &(buf[totalBytesSent]), len-totalBytesSent, 0); if(numBytesSent<0) fatal_error("header send failed"); totalBytesSent+=numBytesSent; } free(buf); return totalBytesSent; }