void dispatch() { TRACE_MSG("Epoll","wait ..."); int num_fds = epoll_wait(epollFd, &epollEvents[0], epollEvents.size(), timeout); for(int i = 0; i < num_fds; i++) { void* data = epollEvents[i].data.ptr; int32_t ev = epollEvents[i].events; if (ev & (EPOLLHUP | EPOLLERR)) { // FIXME: handle error on listener TRACE_MSG("Epoll","close connection by hup"); handler->close(data); continue; } // handle workers int32_t rc = handler->events(ev, data); if (rc == RC_CLOSE) { TRACE_MSG("Epoll","close connection by handler"); handler->close(data); } } }
int Server::close_session(Session *sess){ Link *link = sess->link; for(int i=0; i<this->handlers.size(); i++){ Handler *handler = this->handlers[i]; handler->close(*sess); } this->link_count --; log_debug("delete link %s:%d, fd: %d, links: %d", link->remote_ip, link->remote_port, link->fd(), this->link_count); fdes->del(link->fd()); this->sessions.erase(sess->id); delete link; delete sess; return 0; }
void cnpy::npz_save_data(const std::string& zipname, const std::string& name, const unsigned char* data, const cnpy::Type dtype, const size_t elemSize, const std::vector<size_t>& shape, const char mode) { //first, append a .npy to the fname std::string fname(name); fname += ".npy"; if(mode=='w' && std::ifstream(zipname).is_open()) { // Remove the old file if present if(std::remove(zipname.c_str())!=0) throw std::runtime_error("Unable to overwrite "+zipname); } Handler<struct zip> zip = zip_open(zipname.c_str(), ZIP_CREATE, nullptr); if(zip.handle()==nullptr) throw std::runtime_error("Error opening npz file "+zipname); // Remove the old array if present int nameLookup = zip_name_locate(zip.handle(), fname.c_str(), 0); if(nameLookup>=0 && zip_delete(zip.handle(), nameLookup)!=0) throw std::runtime_error("Unable to overwrite "+name+" array"); std::vector<char> header = create_npy_header(dtype, elemSize, shape); const int dataSize = std::accumulate(shape.cbegin(), shape.cend(), elemSize, std::multiplies<size_t>()); ZipSourceCallbackData cbData(header, data, dataSize); Handler<struct zip_source> zipSource = zip_source_function(zip.handle(), zipSourceCallback, &cbData); if(zipSource.handle()==nullptr) throw std::runtime_error("Error creating "+name+" array"); zip_int64_t fid = zip_add(zip.handle(), fname.c_str(), zipSource.handle()); if(fid<0) { zip_source_free(zipSource.handle()); throw std::runtime_error("Error creating "+name+" array"); } zip.close(); }
int Handler_Factory::create_handler ( ACE_SSL_SOCK_Acceptor &acceptor, Handler * (*handler_factory) (ACE_SSL_SOCK_Stream* ), const char *handler_type) { ACE_SSL_SOCK_Stream* new_stream; ACE_NEW_RETURN (new_stream, ACE_SSL_SOCK_Stream, -1); if (acceptor.accept (*new_stream) == -1) ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("accept")), -1); Handler *handler; ACE_ALLOCATOR_RETURN (handler, (*handler_factory) (new_stream), -1); ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%P|%t) spawning %s handler\n"), handler_type)); if (handler->open () == -1) return -1; #if defined (ACE_MT_SAFE) // Spawn a new thread and run the new connection in that thread of // control using the <server> function as the entry point. return handler->activate (); #else handler->svc (); handler->close (0); return 0; #endif /* ACE_HAS_THREADS */ }