首先对文件按内容分块(有块大小的约束),然后对于每个chunk构造单独的一个UDP 数据报进行传输,在应用层的开始是自定义的包头,有块号,块长度,块指纹等元数据信息,这些信息便于接收端能够按序正确接收。
/*--vonzhou
---this project is to upload file after chunking using
rabin fingerprint, here use UDP so that every packet nodelay.
but we need to ensure the reliability.
*/
#include "global.h"
#define SERV_PORT 2500
typedef struct {
unsigned char fp[20];
int chunk_id;
short flags;
short chunk_len; // The max is 32767 enough
char data[MAX_CHUNK_SIZE];
}TransferUnit;
// some flags for this chunk transfered.
enum{
CHUNK_NEED_DEDU = 0x0001, // need deduplication
CHUNK_OTHER = 0x0002 // other for extension
};
FileInfo *fi;
int sendUDP(FileInfo *fi,int sockfd,struct sockaddr *pservaddr,socklen_t servlen){
char buf[sizeof(TransferUnit)];
fd_set wrset;
struct timeval tv;
int rlen,wlen, len = 1;
int fd;
int ret;
int i,j;
FingerChunk *p;
TransferUnit unit;