📄 socket_server.cpp
字号:
if (count < 9999) count++; else count = 1; add_file(file); filelist_changed_notify(); Glib::Mutex::Lock lock(fax_to_send_mutex); // use a Glib::Cond object to avoid overwriting an earlier fax to send entry while (fax_to_send.second) fax_to_send_cond.wait(fax_to_send_mutex); fax_to_send.first = file; fax_to_send.second = count; fax_to_send_notify(); } else write_error("Error in receiving print job on socket\n");}void Socket_server::stop(void) { if (serve_sock_fd >= 0) { { // mutex critical section Glib::Mutex::Lock lock(socket_mutex); closing_socket = true; shutdown(serve_sock_fd, SHUT_RDWR); close(serve_sock_fd); serve_sock_fd = -1; } // end of mutex critical section close_sem.wait(); }}void Socket_server::add_file(const std::string& filename) { Glib::Mutex::Lock lock(filenames_mutex); std::pair<std::string, unsigned int> file_item; file_item.first = filename; file_item.second = count; filenames_s->push_back(file_item); save_queued_faxes();} std::pair<Shared_ptr<FilenamesList>, Shared_ptr<Glib::Mutex::Lock> > Socket_server::get_filenames(void) const { // we will use a Shared_ptr, which automatically controls object life, to hold a Glib::Mutex::Lock // this tunnelling lock will automatically be released once the object using the FilenamesList // object has finished with it and the lock is deleted by the destructor of the last Shared_ptr // holding it (namely when SocketListDialog::set_socket_list_rows() has completed its task) Shared_ptr<Glib::Mutex::Lock> lock_s(new Glib::Mutex::Lock(filenames_mutex)); return std::pair<Shared_ptr<FilenamesList>, Shared_ptr<Glib::Mutex::Lock> >(filenames_s, lock_s);}std::pair<std::string, unsigned int> Socket_server::get_fax_to_send(void) { Glib::Mutex::Lock lock(fax_to_send_mutex); std::pair<std::string, unsigned int> return_val(fax_to_send); fax_to_send.second = 0; // release the Glib::Cond object if it is blocking fax_to_send_cond.signal(); return return_val;} int Socket_server::remove_file(const std::string& filename) { Glib::Mutex::Lock lock(filenames_mutex); int return_val = 0; FilenamesList::iterator iter; bool found_file = false; for (iter = filenames_s->begin(); !found_file && iter != filenames_s->end(); ++iter) { if (iter->first == filename) { filenames_s->erase(iter); found_file = true; filelist_changed_notify(); // clean up by deleting the temporary file we created earlier // in accept_on_client() above unlink(filename.c_str()); } } if (!found_file) return_val = -1; else save_queued_faxes(); return return_val;}void Socket_server::read_queued_faxes(void) { // we do not need a mutex here as it is only called in // Socket_server::Socket_server() before a new thread // has been launched // we do not need to use homedir_mutex here either, for the same reason std::string filename(homedir); filename += "/" SAVED_FAX_FILENAME; std::ifstream filein(filename.c_str(), std::ios::in); if (filein) { filenames_s->clear(); // may sure the files list is empty std::string file_read; while (std::getline(filein, file_read)) { if (!file_read.empty() && file_read[0] != '#' && file_read[0] != '!') { // find the last '?' (in theory, mkstemp() could use it in the filename, // so there may be a preceding one, so use a reverse find) std::string::size_type pos = file_read.rfind('?'); if (pos != std::string::npos && file_read.size() > pos + 1) { if (!access(file_read.substr(0, pos).c_str(), R_OK)) { // file exists and can be read std::pair<std::string, unsigned int> file_item; // file_item.first is the filename // file_item second is the print job number file_item.first = file_read.substr(0, pos); file_item.second = std::atoi(file_read.substr(pos + 1).c_str()); filenames_s->push_back(file_item); } } } else if (file_read[0] == '!' && file_read.size() > 1) { count = std::atoi(file_read.substr(1).c_str()); } } }}void Socket_server::save_queued_faxes(void) { // we do not need a filenames mutex here as this method is only called by // Socket_server::add_file() and Socket_server::remove_file() // which are already protected by a mutex std::string filename; { Glib::Mutex::Lock lock(homedir_mutex); filename = homedir; } filename += "/" SAVED_FAX_FILENAME; std::ofstream fileout(filename.c_str(), std::ios::out); if (!fileout) { std::string message("Can't open file "); message += filename; message += ", so the list of queued files can't be saved\n"; write_error(message.c_str()); } else { FilenamesList::const_iterator iter; for (iter = filenames_s->begin(); iter != filenames_s->end(); ++iter) { fileout << iter->first << '?' << iter->second << '\n'; } fileout << '!' << count << '\n'; }}void Socket_server::write_stdout_from_thread(const char* message) { Glib::Mutex::Lock lock(stdout_mutex); // use a Glib::Cond object to avoid overwriting an earlier message while (!stdout_text.empty()) stdout_cond.wait(stdout_mutex); stdout_text = message; stdout_notify();}void Socket_server::write_stdout_dispatcher_slot(void) { Glib::Mutex::Lock lock(stdout_mutex); stdout_message(stdout_text.c_str()); stdout_text = ""; // release the Glib::Cond object if it is blocking stdout_cond.signal();}bool Socket_server::is_valid_peer(const sockaddr_in& address) { bool return_val = false; hostent* hostinfo_p; // gethostbyaddr is not guaranteed by IEEE Std 1003.1 to be thread safe - // that doesn't matter as this program only uses it in this thread if ((hostinfo_p = gethostbyaddr((const char*)&address.sin_addr.s_addr, sizeof(address.sin_addr.s_addr), AF_INET)) == 0) { write_error("Cannot get hostname of peer\n"); } else { std::vector<std::string> valid_names; valid_names.push_back("localhost"); // localhost is always valid // include the fully qualified localhost address hostent* my_info_p; long localhost_address = inet_addr("127.0.0.1"); if (localhost_address == -1) { write_error("Cannot create network address for localhost\n"); } else if ((my_info_p = gethostbyaddr((const char*)&localhost_address, sizeof(address.sin_addr.s_addr), AF_INET)) == 0) { write_error("Cannot get the fully qualified localhost name\n"); } else { valid_names.push_back(my_info_p->h_name); // and our fq localhost name is also valid } char my_hostname_p[256]; if (gethostname(my_hostname_p, 256) < 0) { write_error("Cannot get our hostname\n"); } else { my_hostname_p[255] = 0; // make sure it is null terminated valid_names.push_back(my_hostname_p); // our own hostname is also always valid // gethostbyname is not guaranteed by IEEE Std 1003.1 to be thread safe - // that doesn't matter as this program only uses it in this thread if ((my_info_p = gethostbyname(my_hostname_p)) == 0) { write_error("Cannot get our fully qualified hostname\n"); } else { valid_names.push_back(my_info_p->h_name); // and our fq hostname is also valid } } // and now add the permitted clients from the settings dialog // lock the Prog_config object to stop it being modified or accessed in the initial // (GUI) thread while we are accessing it here Glib::Mutex::Lock lock(*prog_config.mutex_p); std::copy(prog_config.permitted_clients_list.begin(), prog_config.permitted_clients_list.end(), std::back_inserter(valid_names)); std::vector<std::string>::const_iterator iter; for (iter = valid_names.begin(); !return_val && iter != valid_names.end(); ++iter) { if (*iter == std::string(hostinfo_p->h_name)) return_val = true; } } return return_val;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -