add comment thread safe

This commit is contained in:
ubuntu201711081 2020-12-04 05:25:10 +00:00
parent 923349635d
commit a7457281e5
3 changed files with 35 additions and 16 deletions

View File

@ -17,8 +17,11 @@
/*======== /*========
*Operation *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){ int send_fail(int sock,const char * msg){
struct TransferResult res; struct TransferResult res;
@ -37,7 +40,10 @@ int send_fail(int sock,const char * msg){
} }
return 0; return 0;
} }
/**
* send errno to client
* thread safe
*/
int send_errno(int sock){ int send_errno(int sock){
struct TransferResult r; struct TransferResult r;
r.res = RES_ERR; r.res = RES_ERR;
@ -50,8 +56,9 @@ int send_errno(int sock){
} }
return 0; 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){ int read_request(int sock,uint8_t * buf,size_t bufsize){
struct ReadOp p; struct ReadOp p;
@ -62,7 +69,7 @@ int read_request(int sock,uint8_t * buf,size_t bufsize){
else perror("receive fail"); else perror("receive fail");
return -1; 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"); send_fail(sock,"buffer overflow");
return -1; return -1;
} }
@ -75,8 +82,8 @@ int read_request(int sock,uint8_t * buf,size_t bufsize){
return -1; return -1;
} }
n = recv_until_byte(sock,buf,p.file_url_size,TIMEOUT); n = recv_until_byte(sock,buf,p.file_url_size,TIMEOUT);
buf[p.file_url_size] = '\0'; buf[p.file_url_size] = '\0'; //truncate
printf("str size: %d, request %s\n",p.file_url_size,buf); fprintf(stdout,"str size: %d, request %s\n",p.file_url_size,buf);
if(strchr((char *)buf,'/') != NULL){ if(strchr((char *)buf,'/') != NULL){
send_fail(sock,"Illegal character /"); send_fail(sock,"Illegal character /");
return -1; return -1;
@ -89,7 +96,10 @@ int read_request(int sock,uint8_t * buf,size_t bufsize){
} }
return fd; return fd;
} }
/**
* send response to client
* thread safe
*/
int send_response(int sock,int fd, uint8_t * buf, size_t bufsize){ int send_response(int sock,int fd, uint8_t * buf, size_t bufsize){
struct TransferResult r; struct TransferResult r;
struct stat st; struct stat st;

View File

@ -11,6 +11,7 @@
int getBufferSizeFrom(int sock){ int getBufferSizeFrom(int sock){
int buffer_sz; int buffer_sz;
socklen_t len = sizeof(buffer_sz); socklen_t len = sizeof(buffer_sz);
//getsockopt is thread-safe
if(getsockopt(sock,SOL_SOCKET,SO_SNDBUF,&buffer_sz,&len) < 0){ if(getsockopt(sock,SOL_SOCKET,SO_SNDBUF,&buffer_sz,&len) < 0){
perror("failed to get sock buffer size: getsockopt"); perror("failed to get sock buffer size: getsockopt");
buffer_sz = DEFAULT_BUF_SIZE;/*set to default*/ 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: case POLLNVAL:
return -1; return -1;
case POLL_IN: case POLL_IN:
return recv(fd,buf,n,0); ret = recv(fd,buf,n,0);
assert(ret != 0);
return ret;
default: default:
assert(0 && "unreachable"); assert(0 && "unreachable");
} }
return ret; assert(0 && "unreachable");
} }
ssize_t recv_until_byte(int fd,void * buf, size_t n,int timeout){ ssize_t recv_until_byte(int fd,void * buf, size_t n,int timeout){

View File

@ -41,16 +41,22 @@ struct TransferResult{
#endif #endif
/**
* find buffer size from sock
* thread safe
*/
int getBufferSizeFrom(int sock); int getBufferSizeFrom(int sock);
/* /**
return -2 if timeout occur. * return -2 if timeout occur.
otherwise, implement equal to 'recv'. * otherwise, implement equal to 'recv'.
`timeout` unit is second. * `timeout` unit is second.
* thread safe
*/ */
ssize_t timeout_recv(int fd,void * buf,size_t n,int timeout); 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); ssize_t recv_until_byte(int fd,void * buf, size_t n,int timeout);