/* close down the given client */ void shutdownClient (ClInfo *cp) { Msg *mp; /* close connection */ shutdown (cp->s, SHUT_RDWR); close (cp->s); /* free memory */ delLilXML (cp->lp); free (cp->props); /* decrement and possibly free any unsent messages for this client */ while ((mp = (Msg*) popFQ(cp->msgq)) != NULL) if (--mp->count == 0) freeMsg (mp); delFQ (cp->msgq); /* ok now to recycle */ cp->active = 0; if (verbose > 0) fprintf (stderr, "%s: Client %d: shut down complete - bye!\n", indi_tstamp(NULL), cp->s); #ifdef OSX_EMBEDED_MODE int active = 0; for (int i = 0; i < nclinfo; i++) if (clinfo[i].active) active++; fprintf(stderr, "CLIENTS %d\n", active); fflush(stderr); #endif }
void INDI::BaseClientQt::processSocketError( QAbstractSocket::SocketError socketError ) { if (sConnected == false) return; // TODO Handle what happens on socket failure! INDI_UNUSED(socketError); IDLog("Socket Error: %s\n", client_socket.errorString().toLatin1().constData()); fprintf (stderr,"INDI server %s/%d disconnected.\n", cServer.c_str(), cPort); delLilXML(lillp); client_socket.close(); // Let client handle server disconnection serverDisconnected(-1); }
/* close down the given driver and restart */ void shutdownDvr (DvrInfo *dp, int restart) { Msg *mp; /* make sure it's dead, reclaim resources */ if (dp->pid == REMOTEDVR) { /* socket connection */ shutdown (dp->wfd, SHUT_RDWR); close (dp->wfd); /* same as rfd */ } else { /* local pipe connection */ kill (dp->pid, SIGKILL); /* we've insured there are no zombies */ close (dp->wfd); close (dp->rfd); close (dp->efd); } #ifdef OSX_EMBEDED_MODE fprintf(stderr, "STOPPED \"%s\"\n", dp->name); fflush(stderr); #endif /* free memory */ free (dp->sprops); free(dp->dev); delLilXML (dp->lp); /* ok now to recycle */ dp->active = 0; dp->ndev = 0; /* decrement and possibly free any unsent messages for this client */ while ((mp = (Msg*) popFQ(dp->msgq)) != NULL) if (--mp->count == 0) freeMsg (mp); delFQ (dp->msgq); if (restart) { fprintf (stderr, "%s: Driver %s: restart #%d\n", indi_tstamp(NULL), dp->name, ++dp->restarts); startDvr (dp); } }
/* close down the given driver and restart if not too many already */ static void restartDvr (DvrInfo *dp) { /* make sure it's dead, reclaim resources */ kill (dp->pid, SIGKILL); fclose (dp->wfp); close (dp->rfd); delLilXML (dp->lp); /* restart unless too many already */ if (++dp->restarts > maxdrs) { fprintf (stderr, "Driver %s: died after %d restarts\n", dp->name, maxdrs); exit(1); } fprintf (stderr, "Driver %s: restart #%d\n", dp->name, dp->restarts); startDvr (dp); }
bool INDI::BaseClientQt::disconnectServer() { if (sConnected == false) return true; sConnected = false; client_socket.close(); if (lillp) { delLilXML(lillp); lillp=NULL; } clear(); cDeviceNames.clear(); return true; }
/* this function is the thread to perform all writes to client carg. * return with client closed when we have problems or when shutdown flag is set. * N.B. coordinate all access to clinfo via client_m/c. * N.B. clinfo can move (be realloced) when unlocked so beware pointers thereto. */ static void * clientWThread(void *carg) { int c = (int)carg; ClInfo *cp; Msg *mp; /* start off wanting exclusive access to client info */ pthread_mutex_lock (&client_m); /* loop until told to shut down or get write error */ while (1) { /* check for message or shutdown, unlock while waiting */ while (nFQ(clinfo[c].msgq) == 0 && !clinfo[c].shutdown) { if (verbose > 2) fprintf (stderr,"Client %d: thread sleeping\n",clinfo[c].s); pthread_cond_wait (&client_c, &client_m); if (verbose > 2) fprintf (stderr, "Client %d: thread awake\n", clinfo[c].s); } if (clinfo[c].shutdown) break; /* get next message for this client */ mp = popFQ (clinfo[c].msgq); /* unlock client info while writing */ pthread_mutex_unlock (&client_m); prXMLEle (clinfo[c].wfp, mp->ep, 0); pthread_mutex_lock (&client_m); /* trace */ cp = &clinfo[c]; /* ok to use pointer while locked */ if (verbose > 2) { fprintf (stderr, "Client %d: send:\n", cp->s); prXMLEle (stderr, mp->ep, 0); } else if (verbose > 1) fprintf (stderr, "Client %d: send %s\n", cp->s, xmlLog(mp->ep)); /* update message usage count, free if goes to 0 */ if (--mp->count == 0) freeMsg (mp); /* exit this thread if encountered write errors */ if (ferror(cp->wfp)) { fprintf (stderr, "Client %d: %s\n", cp->s, strerror(errno)); break; } } /* close down this client */ cp = &clinfo[c]; /* ok to use pointer while locked */ fclose (cp->wfp); /* also closes cp->s */ delLilXML (cp->lp); myfree (cp->devs); /* decrement and possibly free any unsent messages for this client */ while ((mp = (Msg*) popFQ(cp->msgq)) != NULL) if (--mp->count == 0) freeMsg (mp); delFQ (cp->msgq); /* this thread is now finished with client info */ pthread_mutex_unlock (&client_m); /* exit thread */ return (0); }