static void processClient(fd_set* master, int clientFd , int solFd, bufStore *buf) { char data[BUF_SIZE]; int nbytes = 0; TlvHeader header; struct iovec vec[2]; vec[0].iov_base = &header; vec[0].iov_len = sizeof(header); vec[1].iov_base = &data; vec[1].iov_len = BUF_SIZE; /* TODO: server should be able to handle data for a tlv over multiple reads */ nbytes = readv(clientFd, vec, 2); if (nbytes <= 0) { if (nbytes == 0) { syslog(LOG_INFO, "mTerm_server: Client socket %d hung up\n", clientFd); } else { syslog(LOG_ERR, "mTerm_server: Error on read fd=%d\n", clientFd); } closeClient(master, clientFd); } else if (nbytes < sizeof(TlvHeader)) { /* TODO: Potentially we should use a per-client buffer, for now close Client connection */ syslog(LOG_ERR, "mTerm_server: Error on read fd=%d\n", clientFd); closeClient(master, clientFd); } else if (header.length > (nbytes - sizeof(header))) { syslog(LOG_ERR, "mTerm_server: Error on read fd=%d\n", clientFd); closeClient(master, clientFd); } else { switch (header.type) { case ASCII_ESC: /* TODO: Server should store client pointers for last reference of buffer read per client, thus subsequent reads can be based on the last reference */ bufferGetLines(buf->file, clientFd, atoi(vec[1].iov_base), 0); break; case ASCII_DELETE: syslog(LOG_INFO, "mTerm_server: Client socket %d hung up\n", clientFd); closeClient(master, clientFd); break; case ASCII_CARAT: writeData(solFd, vec[1].iov_base, header.length, "tty"); break; default: syslog(LOG_ERR, "mTerm_server: Received unknown tlv\n"); break; } } }
int main(int argc, char *argv[]) { QApplication a(argc, argv); QApplication::setQuitOnLastWindowClosed(false); PI2Client* client = new PI2Client(); QRect rect = QApplication::desktop()->screenGeometry(); client->setWindowSize(rect); QObject::connect( client, SIGNAL(closeClient()), QApplication::instance(), SLOT(quit()) ); client->startClient(); return a.exec(); }
/* TODO: We should support non-blocking writes to clients, and buffer data if they don't read it fast enough. If we end up buffering too much data for any particular client then we should disconnect that client. */ static int processSol(fd_set* master, int serverfd, int fdmax, int solFd, bufStore *buf) { char data[SEND_SIZE]; int nbytes; int currFd; nbytes = read(solFd, data, sizeof(data)); if (nbytes > 0) { for (currFd = 0; currFd <= fdmax; currFd++) { if (FD_ISSET(currFd, master)) { if ((currFd != serverfd) && (currFd != solFd)) { if (send(currFd, data, nbytes, 0) == -1) { syslog(LOG_ERR, "mTerm_server: Error on send fd=%d\n", currFd); closeClient(master, currFd); syslog(LOG_ERR, "mTerm_server: Terminated client fd=%d\n", currFd); } } } } writeToBuffer(buf, data, nbytes); } else if (nbytes < 0) { syslog(LOG_ERR, "mTerm_server: Error on read fd=%d\n", solFd); return -1; } return 1; }
void videoServer_destroy() { uint32_t iterator; TClient *curClient; /* Do not accept more clients */ vp_com_close(&globalCom, &videoServerSocket); /* Close all client sockets */ if (videoServer_clientList != NULL) { clientList_startIterator(&iterator); while((curClient = clientList_getNextClient(videoServer_clientList, &iterator)) != NULL) { closeClient(curClient); } clientList_destroy(videoServer_clientList); videoServer_clientList = NULL; } /* Free resources */ vp_os_cond_destroy(&frameBufferCond); vp_os_mutex_destroy(&frameBufferMutex); vp_os_free(frameBuffer); videoTranscoder_destroy(); vp_os_mutex_destroy(&settingsMutex); videoServerStarted = FALSE; }
void WebServer::_handleRequest() { RequestHandler* handler; for (handler = _firstHandler; handler; handler = handler->next) { if (handler->method != HTTP_ANY && handler->method != _currentMethod) continue; if (handler->uri != _currentUri) continue; handler->fn(); break; } if (!handler){ #ifdef DEBUG DEBUG_OUTPUT.println("request handler not found"); #endif if(_notFoundHandler) { _notFoundHandler(); } else { printHead(404,"text/plain"); print(String("Not found: ") + _currentUri); closeClient(); } } _currentClient = WiFiClient(); _currentUri = String(); }
void quit(Command* com) { for(int i=0;i<GClientCounts;i++) { closeClient(i); } ROLE_LIVING = 0; }
int apolloReturnPacket(struct bufferevent *bev, client_t *client, char *buffer, int length) { evbuffer_add(client->output_buffer, buffer, length); if (bufferevent_write_buffer(bev, client->output_buffer)) { closeClient(client); } return 0; }
bool Thumbnail::handleMousePress(int x, int y) { int closeButtonXSpan = Settings::instance()->closeButtonXSpan(); int closeButtonYSpan = Settings::instance()->closeButtonYSpan(); int xmin = _x + _clientOffsetX + _clientScaledWidth - closeButtonXSpan; int xmax = xmin + closeButtonXSpan; int ymin = _y + _clientOffsetY - Resources::instance()->headerMiddle()->height(); int ymax = ymin + closeButtonYSpan; if (xmin < x && x < xmax && ymin < y && y < ymax) { closeClient(); return true; } else if (_x < x && x < _x + _width && _y < y && y < _y + _height) { switchToClient(); _teleWindow->hide(); return true; } return false; }
void ClientApp::stopClient() { closeClient(m_client); closeClientScreen(m_clientScreen); m_client = nullptr; m_clientScreen = nullptr; }
void ClientApp::stopClient() { closeClient(m_client); closeClientScreen(m_clientScreen); m_client = NULL; m_clientScreen = NULL; }
static void stopClient() { closeClient(s_client); closeClientScreen(s_clientScreen); s_client = NULL; s_clientScreen = NULL; }
void CWSAThread::doKeepaliveTimeout() { time_t curtime = time(NULL); if (curtime - m_checkkeepalivetime < m_keepaliveinterval) { return; } list<int> timelist; map<int, SOCKET_SET*>::iterator itersockmap = m_socketmap.begin(); for (; itersockmap != m_socketmap.end(); ++itersockmap) { if (itersockmap->second != NULL) { //TODO check here, > or < ? if (itersockmap->second->refresh_time + m_keepalivetimeout < curtime) { timelist.push_back(itersockmap->first); } } else { timelist.push_back(itersockmap->first); } }//end for bool bclosed = false; for (list<int>::iterator itertimeout = timelist.begin(); itertimeout != timelist.end(); ++itertimeout) { bclosed = false; itersockmap = m_socketmap.find(*itertimeout); if (itersockmap != m_socketmap.end()) { if (itersockmap->second != NULL) { if (itersockmap->second->key != NULL) { bclosed = true; //LOG(_ERROR_, "CWSAThread::doKeepaliveTimeout() error, close socket_set for timeout, fd=%d, time=%u, peerip=%s, port=%d", // itersocket->first, itersocket->second->key->connect_time, GETNULLSTR(itersocket->second->peer_ip), itersocket->second->peer_port); closeClient(itersockmap->first, itersockmap->second->key->connect_time); } } if (!bclosed) { LOG(_ERROR_, "CWSAThread::doKeepaliveTimeout() error, close socket_set->key->fd close for timeout"); closesocket(itersockmap->first); delete itersockmap->second; m_socketmap.erase(itersockmap); } } }//end for timelist.clear(); m_checkkeepalivetime = time(NULL); }
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); this->setWindowIcon(QIcon(":/resource/icon/telescope.png")); this->setWindowTitle(tr("Telescope")); //For common initialization isConnected = false; hostIsSet = false; client= NULL; background=QImage(":/resource/image/colourback.jpg"); net.setParentWidget(this); connect(&net,SIGNAL(hideIMSignal()),this,SLOT(hideIM())); //For exit Tool ui->actionExit->setIcon(QIcon(":/resource/icon/exit.png")); ui->actionExit->setStatusTip(tr("Exit from Telescope.")); connect(ui->actionExit, SIGNAL(triggered()), this, SLOT(closeClient())); //For connect Tool ui->actionConnect->setIcon(QIcon(":/resource/icon/connect.png")); ui->actionConnect->setStatusTip(tr("Set up connection.")); connect(ui->actionConnect, SIGNAL(triggered()), this, SLOT(getConnectInfo())); //For IM Tool ui->actionIM->setIcon(QIcon(":/resource/icon/chat2.png")); ui->actionIM->setStatusTip(tr("Open the IM Dialog.")); ui->actionIM->setCheckable(true); //单击后保持按下状态 connect(ui->actionIM,SIGNAL(triggered()),this,SLOT(showIM())); //For disconnect Tool ui->actionDisConnect->setIcon(QIcon(":/resource/icon/disconnect.png")); ui->actionDisConnect->setStatusTip(tr("Close up connection.")); connect(ui->actionDisConnect, SIGNAL(triggered()), this, SLOT(closeConnect())); //For ToolBar ui->mainToolBar->setMovable(false); //固定工具栏 ui->mainToolBar->addAction(ui->actionConnect); ui->mainToolBar->addAction(ui->actionIM); ui->mainToolBar->addAction(ui->actionDisConnect); ui->mainToolBar->addAction(ui->actionExit); ui->mainToolBar->setIconSize(QSize(50,45)); //设置工具栏图标大小 //For MainWindow setAttribute(Qt::WA_TranslucentBackground); //窗口半透明 setWindowFlags(Qt::Window | Qt::FramelessWindowHint); //无边框窗口 //For central Widget shPic = new ShowPic(this); //shPic->setMinimumSize(QSize(80, 60)); this->setCentralWidget(shPic); }
void INetwork::shutdown () { info(LOG_GENERAL, "shutting down the network"); closeClient(); closeServer(); for (CountMap::iterator i = _count.begin(); i != _count.end(); ++i) { info(LOG_NET, String::format("used protocol id %i %i times", static_cast<int>(i->first), i->second)); } _count.clear(); }
void onClientStatusChanged(GtkWidget *widget, gpointer data) { if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget))) { initClient(); } else { closeClient(); } }
void TextService::onLangProfileActivated(REFIID lang) { // Sometimes, Windows does not deactivate the old language profile before // activating the new one. So here we do it by ourselves. // If a new profile is activated, but there is an old one remaining active, // deactive it first. if (client_ != nullptr) closeClient(); // create a new client connection to the input method server for the language profile client_ = std::make_unique<Client>(this, lang); client_->onActivate(); }
void Worker::finishWriting(int fd) { assert(clients.find(fd) != clients.end()); event_del(clients[fd]->writeEvent); event_free(clients[fd]->writeEvent); clients[fd]->writeEvent = nullptr; if (!clients[fd]->readEvent) { /* Close client */ closeClient(fd); } }
int main() { SocketDescriptor listenfd = 0; SocketDescriptor connfd = 0; SocketAddressIn serv_addr; char sendBuff[1025]; int numrv; makeSocketTCP(&listenfd); printf("Socket created\n"); makeSocketAddressAny(&serv_addr, 5200); printf("Address created\n"); bindSocket(&listenfd, &serv_addr); printf("Socket bound\n"); if(setListener(&listenfd, 10) == -1) { printf("Failed to listen\n"); return -1; } printf("Socket listening\n"); tryAcceptClient(&connfd, &listenfd); printf("Socket accepted client\n"); int dio; for(dio = 0; dio < 100; ++dio) { sprintf(sendBuff, "Message from server %d", dio); trySendStr(&connfd, sendBuff); printf("Socket sent message\n"); // sleep(10); } closeClient(&connfd); printf("Socket closed client\n"); close(listenfd); close(connfd); printf("Server shutdown\n"); return 0; }
void CClientApp::stopClient() { #if SYSAPI_WIN32 && GAME_DEVICE_SUPPORT if (args().m_gameDevice) { LOG((CLOG_DEBUG "removing xinput hook")); RemoveXInputHook(); } #endif closeClient(s_client); closeClientScreen(s_clientScreen); s_client = NULL; s_clientScreen = NULL; }
void abortClient( SocketData *socket_data, int socket_client ) { struct linger so_linger; int z; so_linger.l_onoff = 1; so_linger.l_linger = 0; z = setsockopt( socket_client, SOL_SOCKET, SO_LINGER, &so_linger, sizeof( so_linger ) ); if ( z ) { if (errno == EBADF ) { printf( "setsockopt( linger ) failed - bad socket or socket already closed?\n" ); } else { printf( "setsockopt( linger ) failed - %d\n", errno ); } } closeClient( socket_data, socket_client ); } // abort_client
int TVUClient::connectServer() { if(m_protocal!=SOCK_STREAM && m_protocal!=SOCK_DGRAM) { return -1; } closeClient(); //int keepalive=1; sockaddr_in addr; addr.sin_family=AF_INET; addr.sin_port=htons(m_peerPort); addr.sin_addr.s_addr=m_peerIp; if (addr.sin_addr.s_addr == INADDR_NONE) { printf("\n无效ip \n"); return -1; } bzero(&(addr.sin_zero),8); int sock = socket(AF_INET,m_protocal,0); if(sock<1) { return -1; } //setsockopt(sock,SOL_SOCKET,SO_KEEPALIVE,&keepalive,4); //keepalive=0; //setsockopt(sock,SOL_TCP,TCP_NODELAY,&keepalive,4); if(m_protocal==SOCK_STREAM) { //TCP连接 //alarm(5); if(connect(sock,(struct sockaddr*)&addr,sizeof(addr))<0){ // cout<<"+++++++++++连接失败+++++++++"<<endl; close(sock); // closeClient(); //alarm(0); return -1; } alarm(0); // cout<<"+++++++++++连接成功+++++++++"<<endl; } m_pChannel = new TVUChannel(sock,m_peerPort,m_peerIp); return sock; }
void buffered_on_read(struct bufferevent *bev, void *arg) { client_t *client = (client_t *)arg; char data[4096]; int nbytes; while (bev->input->off > 0) { nbytes = (bev->input->off > 4096) ? 4096 : bev->input->off; evbuffer_remove(bev->input, data, nbytes); evbuffer_add(client->output_buffer, data, nbytes); } if (bufferevent_write_buffer(bev, client->output_buffer)) { errorOut("Error sending data to client on fd %d\n", client->fd); closeClient(client); } }
static void closeAndFreeClient(client_t *client) { if (client != NULL) { closeClient(client); if (client->buf_ev != NULL) { bufferevent_free(client->buf_ev); client->buf_ev = NULL; } if (client->evbase != NULL) { event_base_free(client->evbase); client->evbase = NULL; } if (client->output_buffer != NULL) { evbuffer_free(client->output_buffer); client->output_buffer = NULL; } free(client); } }
TextService::~TextService(void) { if (client_) { closeClient(); } if(popupMenu_) ::DestroyMenu(popupMenu_); if (candidateWindow_) { hideCandidates(); } if(messageWindow_) hideMessage(); if(font_) ::DeleteObject(font_); }
int main(int argc, char const *argv[]) { if (argc != 3) { fprintf(stderr, "usage: %s <server address> <port>\n", argv[0]); exit(EXIT_FAILURE); } if (!isValidArguments(argc, argv)) { fprintf(stderr, "Invalid Arguments\n"); exit(EXIT_FAILURE); } init(); int port; sscanf(argv[2], "%d", &port); int sockfd = clientInit(argv[1], port); printf("\n\nNetwork Programming Homework 1\n\nConnected to %s:%s\n", argv[1], argv[2]); TCPClient(sockfd, argv[1]); closeClient(sockfd); return 0; }
void repairDatabases() { dblock lk; vector< string > dbNames; getDatabaseNames( dbNames ); for ( vector< string >::iterator i = dbNames.begin(); i != dbNames.end(); ++i ) { string dbName = *i; assert( !setClientTempNs( dbName.c_str() ) ); MongoDataFile *p = database->getFile( 0 ); MDFHeader *h = p->getHeader(); if ( !h->currentVersion() ) { log() << "****" << endl; log() << "****" << endl; log() << "need to upgrade database " << dbName << " with pdfile version " << h->version << "." << h->versionMinor << ", " << "new version: " << VERSION << "." << VERSION_MINOR << endl; if ( shouldRepairDatabases ){ // QUESTION: Repair even if file format is higher version than code? log() << "\t starting repair" << endl; string errmsg; assert( repairDatabase( dbName.c_str(), errmsg ) ); } else { log() << "\t Not repairing, exiting!" << endl; log() << "\t run --upgrade to upgrade dbs, then start again" << endl; log() << "****" << endl; dbexit( EXIT_NEED_UPGRADE ); shouldRepairDatabases = 1; return; } } else { closeClient( dbName.c_str() ); } } if ( shouldRepairDatabases ){ log() << "finished checking dbs" << endl; dbexit( EXIT_CLEAN ); } }
void communicate(void) { long serverpid; FILE *file = fopen(SERVER_PID_FILE, "rb"); if (file == NULL) { printf("Error abriendo el archivo de server_pid.\n"); exit(EXIT_FAILURE); } if(fread(&serverpid, sizeof(serverpid), 1, file) == -1) { perror("Erorr en fread"); return; } fclose(file); if(kill(serverpid, SIGUSR1) == -1) { if(errno == EPERM) { printf("Existe el proceso. No tenemos permisos para matarlo.\n"); } else if(errno == ESRCH) { printf("El proceso no existe.\n"); } closeClient(); } }
/*____________________________________________________________________________*/ MSFunctionType(void) MSClose (short ref, TMSGlobalPtr g) { TClientsPtr clients = Clients(g); if (!CheckApplRefNum(clients, ref) || (ref == MidiShareRef)) return; msOpenMutex (kOpenCloseMutex); closeClient (ref, g); clients->nbAppls--; if (clients->nbAppls == 1) { FreeAppl(clients->appls[MidiShareRef]); clients->appls[MidiShareRef] = 0; clients->nbAppls = 0; MidiShareSleep(g); FreeAppl(clients->appls[MidiShareDriverRef]); clients->appls[MidiShareDriverRef] = 0; clients->nbDrivers--; } else { CallAlarm(ref, MIDICloseAppl, clients); } msCloseMutex (kOpenCloseMutex); }
/** * Called by libevent when there is data to read. */ void buffered_on_read(struct bufferevent *bev, void *arg) { client_t *client = (client_t *)arg; char data[4096]; int nbytes; /* Copy the data from the input buffer to the output buffer in 4096-byte chunks. * There is a one-liner to do the whole thing in one shot, but the purpose of this server * is to show actual real-world reading and writing of the input and output buffers, * so we won't take that shortcut here. */ while ((nbytes = EVBUFFER_LENGTH(bev->input)) > 0) { /* Remove a chunk of data from the input buffer, copying it into our local array (data). */ if (nbytes > 4096) nbytes = 4096; evbuffer_remove(bev->input, data, nbytes); /* Add the chunk of data from our local array (data) to the client's output buffer. */ evbuffer_add(client->output_buffer, data, nbytes); } /* Send the results to the client. This actually only queues the results for sending. * Sending will occur asynchronously, handled by libevent. */ if (bufferevent_write_buffer(bev, client->output_buffer)) { errorOut("Error sending data to client on fd %d\n", client->fd); closeClient(client); } }
void WorkThread::recvDevInfo() { ZTPprotocol ztp; ztpmRecvDevInfo->getOneZtp(ztp); // if(ztp.getPara("CARNO") == "9") // ztp.print(); if(ztp.getPara("T") != "VERSION") return; QString ip = ztp.getPara("RemoteHost"); quint32 key =QHostAddress(ip).toIPv4Address() ; if(!devMap.contains(key)) { bool state = true; if(ztp.getPara("STATE") == "OFFLINE") state = false; else state = true; DevInfo* devInfo = new DevInfo(ztp.getPara("DEVICE"),ip,ztp.getPara("MajorVersion").toInt(),ztp.getPara("MinorVersion").toInt(),ztp.getPara("MicroVersion").toInt(),state); connect(devInfo,SIGNAL(closeClient()),this,SLOT(deleteDev()),Qt::QueuedConnection); devMap.insert(key,devInfo); } else { devMap[key]->MajorVersion = ztp.getPara("MajorVersion").toInt(); devMap[key]->MinorVersion = ztp.getPara("MinorVersion").toInt(); devMap[key]->MicroVersion = ztp.getPara("MicroVersion").toInt(); if(ztp.getPara("STATE") == "ONLINE") { devMap[key]->setOnline(true); devMap[key]->updateTimer(); } else if(ztp.getPara("STATE") == "OFFLINE") devMap[key]->setOnline(false); } }