add comment thread safe
This commit is contained in:
parent
923349635d
commit
a7457281e5
28
server.c
28
server.c
@ -17,8 +17,11 @@
|
||||
/*========
|
||||
*Operation
|
||||
*========*/
|
||||
/*send user error message
|
||||
80 character limit
|
||||
|
||||
/**
|
||||
* send user error message
|
||||
* 80 character limit
|
||||
* thread safe
|
||||
*/
|
||||
int send_fail(int sock,const char * msg){
|
||||
struct TransferResult res;
|
||||
@ -37,7 +40,10 @@ int send_fail(int sock,const char * msg){
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* send errno to client
|
||||
* thread safe
|
||||
*/
|
||||
int send_errno(int sock){
|
||||
struct TransferResult r;
|
||||
r.res = RES_ERR;
|
||||
@ -50,8 +56,9 @@ int send_errno(int sock){
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
/* return fd, if success.
|
||||
otherwise, return -1.
|
||||
/**
|
||||
* return fd, if success. otherwise, return -1.
|
||||
* thread safe
|
||||
*/
|
||||
int read_request(int sock,uint8_t * buf,size_t bufsize){
|
||||
struct ReadOp p;
|
||||
@ -62,7 +69,7 @@ int read_request(int sock,uint8_t * buf,size_t bufsize){
|
||||
else perror("receive fail");
|
||||
return -1;
|
||||
}
|
||||
if(bufsize <= p.file_url_size + sizeof(p) + 1){
|
||||
if(bufsize <= ((size_t)p.file_url_size) + sizeof(p) + 1){
|
||||
send_fail(sock,"buffer overflow");
|
||||
return -1;
|
||||
}
|
||||
@ -75,8 +82,8 @@ int read_request(int sock,uint8_t * buf,size_t bufsize){
|
||||
return -1;
|
||||
}
|
||||
n = recv_until_byte(sock,buf,p.file_url_size,TIMEOUT);
|
||||
buf[p.file_url_size] = '\0';
|
||||
printf("str size: %d, request %s\n",p.file_url_size,buf);
|
||||
buf[p.file_url_size] = '\0'; //truncate
|
||||
fprintf(stdout,"str size: %d, request %s\n",p.file_url_size,buf);
|
||||
if(strchr((char *)buf,'/') != NULL){
|
||||
send_fail(sock,"Illegal character /");
|
||||
return -1;
|
||||
@ -89,7 +96,10 @@ int read_request(int sock,uint8_t * buf,size_t bufsize){
|
||||
}
|
||||
return fd;
|
||||
}
|
||||
|
||||
/**
|
||||
* send response to client
|
||||
* thread safe
|
||||
*/
|
||||
int send_response(int sock,int fd, uint8_t * buf, size_t bufsize){
|
||||
struct TransferResult r;
|
||||
struct stat st;
|
||||
|
@ -11,6 +11,7 @@
|
||||
int getBufferSizeFrom(int sock){
|
||||
int buffer_sz;
|
||||
socklen_t len = sizeof(buffer_sz);
|
||||
//getsockopt is thread-safe
|
||||
if(getsockopt(sock,SOL_SOCKET,SO_SNDBUF,&buffer_sz,&len) < 0){
|
||||
perror("failed to get sock buffer size: getsockopt");
|
||||
buffer_sz = DEFAULT_BUF_SIZE;/*set to default*/
|
||||
@ -37,11 +38,13 @@ ssize_t timeout_recv(int fd,void * buf,size_t n,int timeout)
|
||||
case POLLNVAL:
|
||||
return -1;
|
||||
case POLL_IN:
|
||||
return recv(fd,buf,n,0);
|
||||
ret = recv(fd,buf,n,0);
|
||||
assert(ret != 0);
|
||||
return ret;
|
||||
default:
|
||||
assert(0 && "unreachable");
|
||||
}
|
||||
return ret;
|
||||
assert(0 && "unreachable");
|
||||
}
|
||||
|
||||
ssize_t recv_until_byte(int fd,void * buf, size_t n,int timeout){
|
||||
|
@ -41,16 +41,22 @@ struct TransferResult{
|
||||
|
||||
#endif
|
||||
|
||||
/**
|
||||
* find buffer size from sock
|
||||
* thread safe
|
||||
*/
|
||||
int getBufferSizeFrom(int sock);
|
||||
|
||||
/*
|
||||
return -2 if timeout occur.
|
||||
otherwise, implement equal to 'recv'.
|
||||
`timeout` unit is second.
|
||||
/**
|
||||
* return -2 if timeout occur.
|
||||
* otherwise, implement equal to 'recv'.
|
||||
* `timeout` unit is second.
|
||||
* thread safe
|
||||
*/
|
||||
ssize_t timeout_recv(int fd,void * buf,size_t n,int timeout);
|
||||
/*
|
||||
recieve data to buf until all bytes.
|
||||
* recieve data to buf until all bytes.
|
||||
* thread safe
|
||||
*/
|
||||
ssize_t recv_until_byte(int fd,void * buf, size_t n,int timeout);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user