mirror of
https://github.com/eddyem/SBIG_340.git
synced 2025-12-06 18:55:12 +03:00
fix bug with server disconnect
This commit is contained in:
parent
5ac8803ca3
commit
006a8bd9eb
@ -7,3 +7,6 @@ Run `make` to compile three binary files:
|
|||||||
* `sbig340_daemon` --- pure grabbing daemon (driven by socket)
|
* `sbig340_daemon` --- pure grabbing daemon (driven by socket)
|
||||||
* `sbig340_client` --- pure client (take images from daemon and save locally)
|
* `sbig340_client` --- pure client (take images from daemon and save locally)
|
||||||
|
|
||||||
|
When connected to daemon you can send commands "heater=1" or "heater=0":
|
||||||
|
first command will turn heater on for 10 minutes, second will turn it off.
|
||||||
|
Receiving these commands daemon won't send image, immediately disconnect.
|
||||||
|
|||||||
35
socket.c
35
socket.c
@ -42,28 +42,32 @@
|
|||||||
/**
|
/**
|
||||||
* wait for answer from socket
|
* wait for answer from socket
|
||||||
* @param sock - socket fd
|
* @param sock - socket fd
|
||||||
* @return 0 in case of error or timeout, 1 in case of socket ready
|
* @return 0 if data is absent, 1 in case of socket ready, -1 if socket closed or error occured
|
||||||
*/
|
*/
|
||||||
static int waittoread(int sock){
|
static int waittoread(int sock){
|
||||||
fd_set fds;
|
fd_set fds, efds;
|
||||||
struct timeval timeout;
|
struct timeval timeout;
|
||||||
int rc;
|
int rc;
|
||||||
timeout.tv_sec = 1; // wait not more than 1 second
|
timeout.tv_sec = 1; // wait not more than 1 second
|
||||||
timeout.tv_usec = 0;
|
timeout.tv_usec = 0;
|
||||||
FD_ZERO(&fds);
|
FD_ZERO(&fds);
|
||||||
|
FD_ZERO(&efds);
|
||||||
FD_SET(sock, &fds);
|
FD_SET(sock, &fds);
|
||||||
|
FD_SET(sock, &efds);
|
||||||
do{
|
do{
|
||||||
rc = select(sock+1, &fds, NULL, NULL, &timeout);
|
rc = select(sock+1, &fds, NULL, &efds, &timeout);
|
||||||
if(rc < 0){
|
if(rc < 0){
|
||||||
if(errno != EINTR){
|
if(errno != EINTR){
|
||||||
|
putlog("Server not available");
|
||||||
WARN("select()");
|
WARN("select()");
|
||||||
return 0;
|
return -1;
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}while(1);
|
}while(1);
|
||||||
if(FD_ISSET(sock, &fds)) return 1;
|
if(FD_ISSET(sock, &fds)) return 1;
|
||||||
|
if(FD_ISSET(sock, &efds)) return -1; // exception - socket closed
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -229,7 +233,12 @@ void *handle_socket(void *asock){
|
|||||||
char buff[BUFLEN];
|
char buff[BUFLEN];
|
||||||
ssize_t _read;
|
ssize_t _read;
|
||||||
while(1){
|
while(1){
|
||||||
if(!waittoread(sock)){ // no data incoming
|
int rd = waittoread(sock);
|
||||||
|
if(rd < 0){
|
||||||
|
putlog("Disconnected");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if(!rd){ // no data incoming
|
||||||
pthread_mutex_lock(&mutex);
|
pthread_mutex_lock(&mutex);
|
||||||
if(imctr != locctr){
|
if(imctr != locctr){
|
||||||
red("Send image, imctr = %ld, locctr = %ld\n", imctr, locctr);
|
red("Send image, imctr = %ld, locctr = %ld\n", imctr, locctr);
|
||||||
@ -289,7 +298,12 @@ void *server(void *asock){
|
|||||||
socklen_t size = sizeof(struct sockaddr_in);
|
socklen_t size = sizeof(struct sockaddr_in);
|
||||||
struct sockaddr_in their_addr;
|
struct sockaddr_in their_addr;
|
||||||
int newsock;
|
int newsock;
|
||||||
if(!waittoread(sock)) continue;
|
int rd = waittoread(sock);
|
||||||
|
if(rd < 0){
|
||||||
|
putlog("Socket error");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
if(!rd) continue;
|
||||||
red("Got connection\n");
|
red("Got connection\n");
|
||||||
newsock = accept(sock, (struct sockaddr*)&their_addr, &size);
|
newsock = accept(sock, (struct sockaddr*)&their_addr, &size);
|
||||||
if(newsock <= 0){
|
if(newsock <= 0){
|
||||||
@ -398,7 +412,12 @@ static void client_(imstorage *img, int sock){
|
|||||||
size_t Bufsiz = BUFLEN10;
|
size_t Bufsiz = BUFLEN10;
|
||||||
uint8_t *recvBuff = MALLOC(uint8_t, Bufsiz);
|
uint8_t *recvBuff = MALLOC(uint8_t, Bufsiz);
|
||||||
while(1){
|
while(1){
|
||||||
if(!waittoread(sock)) continue;
|
int rd = waittoread(sock);
|
||||||
|
if(rd < 0){
|
||||||
|
putlog("Server disconnected");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if(!rd) continue;
|
||||||
size_t offset = 0;
|
size_t offset = 0;
|
||||||
do{
|
do{
|
||||||
if(offset >= Bufsiz){
|
if(offset >= Bufsiz){
|
||||||
@ -418,7 +437,7 @@ static void client_(imstorage *img, int sock){
|
|||||||
if(n < 0){
|
if(n < 0){
|
||||||
putlog("error in read()");
|
putlog("error in read()");
|
||||||
WARN("read");
|
WARN("read");
|
||||||
break;
|
return;
|
||||||
}
|
}
|
||||||
offset += n;
|
offset += n;
|
||||||
}while(waittoread(sock));
|
}while(waittoread(sock));
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user