📄 common.h.svn-base
字号:
/* ------------------------------------------------------------------------- * common.h - common data * Copyright (C) 2008 Dimitar Atanasov <datanasov@deisytechbg.com> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * ------------------------------------------------------------------------- */#ifndef __COMMON_H#define __COMMON_H#include <string>#include <vector>#include <errno.h>#include <netinet/ip.h>#include <arpa/inet.h>//#define MYSQLPP_MYSQL_HEADERS_BURIED#include <mysql++/mysql++.h>#include <map>#include <pcrecpp.h>#include <sstream>#include <poll.h>#include <sys/time.h>#include <unistd.h>#include <signal.h>#include "linked_ptr.h"#include "log.h"#include "tpool.h"using std::string;using std::vector;using std::map;using std::pair;#define CONF_NAME "base_server.cfg"#define TCP_MSG_LENGTH 16384#define logger ctxt.log->lprintf#define dbg(N) std::cout<<#N<<"="<<N<<std::endl;typedef unsigned short uint16;//#define PRQ(N) std::cout<<N.str()<<std::endl;typedef struct config{ config() {} ~config(){} int using_pms; int using_rc; int using_cards; int using_enddate; int using_eraseonlast; int using_autocheckin;// for DB string username; string password; string database; string hostname; int db_port;// socket connections// rc_host; string rc_host; int rc_port; string rc_broadcast; string mng_host; int mng_port; string php_host; int php_port; string his_host; int his_port; string client_host; int client_port; string pms_host; int pms_port; string inet_host; string radio_host; int radio_port;// for alarm server ? string s_hostname; int s_port; int st_port; string st_serv_ip; int st_serv_N; string path;// paths //pl_path = /var/www/htdocs/page/new/music/rooms/// log info string log_file;// /var/log/mylog.log// custom settings int auto_inet; int second_interface; string pms_host_2; int pms_port_2; vector<int> client_TypeId; vector<int> guest_TypeId; int read_conf();}conf_t;typedef struct room_status{ room_status(int rm); ~room_status() {}; int room; char status; char oldstatus; time_t lastchange; time_t lastping; char tryprog; bool prog; pthread_mutex_t lock; pthread_cond_t queue_not_empty; std::map<string, bool> key_map; std::map<string, bool> key_map_send;}room_status_t;typedef struct tag_synh_room_data { tag_synh_room_data(std::string& Gname, int Glang) : GuestName(Gname), GuestLang(Glang) {} std::string GuestName; int GuestLang;}synh_room_data_t;typedef struct tag_php_status{ tag_php_status(int nhandle, int ntype, int itype) : handle(nhandle), type(ntype), inettype(itype) { datetime = time(NULL); } int handle; time_t datetime; int type; int inettype;}php_status_t;typedef struct context{ context() : conf(NULL), log(NULL), pool(NULL), pool_tcp(NULL), rcthread(0), mngthread(0), phpthread(0), pmsthread(0), histhread(0), clientthread(0), shutdown(0), pmsanswered(0), SF_RoomNo(0) { int rtn; if((rtn = pthread_mutex_init(&(clients_lock),NULL)) != 0) { shutdown = 1; } if((rtn = pthread_mutex_init(&(php_lock),NULL)) != 0) { shutdown = 1; } if((rtn = pthread_mutex_init(&(his_lock),NULL)) != 0) { shutdown = 1; } if((rtn = pthread_mutex_init(&(mng_lock),NULL)) != 0) { shutdown = 1; } if((rtn = pthread_mutex_init(&(synh_lock),NULL)) != 0) { shutdown = 1; } if((rtn = pthread_mutex_init(&(pms_lock),NULL)) != 0) { shutdown = 1; } if((rtn = pthread_mutex_init(&(pms_lock2),NULL)) != 0) { shutdown = 1; } } conf_t* conf; log_t* log; tpool_t* pool; tpool_t* pool_tcp; int rcsock; //room controler socket int mngsock; int phpsock; int pmssock; int pmssock2; int hissock; int clientsock; int testsocket; pthread_t rcthread; pthread_t mngthread; pthread_t phpthread; pthread_t pmsthread; pthread_t pmsthread2; pthread_t histhread; pthread_t clientthread; pthread_t testthread; int shutdown; int pmsanswered; int pmsanswered2; int SF_RoomNo; std::string pmsbuffer; std::map<std::string, linked_ptr<room_status_t> > rooms; std::map<std::string, int> ks; pthread_mutex_t clients_lock; std::map<std::string, int> clients; pthread_mutex_t his_lock; std::map<std::string, int> his; pthread_mutex_t mng_lock; std::map<std::string, int> mng; pthread_mutex_t php_lock; std::map<int, linked_ptr<php_status_t> > php; pthread_mutex_t synh_lock; std::map<int, linked_ptr<synh_room_data_t> > synh_data; pthread_mutex_t pms_lock; pthread_mutex_t pms_lock2; DBPool db_pool; void add_to_pmsbuffer(std::ostringstream& msg) { pmsbuffer += msg.str(); int len = pmsbuffer.length(); if(len > 8192) { int pos = pmsbuffer.find("\r\n", 2048); pmsbuffer = pmsbuffer.substr(pos + 2, len); } } void add_his(std::string& ip, int cfd) { pthread_mutex_lock(&his_lock); his.insert(std::make_pair(ip, cfd)); pthread_mutex_unlock(&his_lock); } void del_his(std::string& ip) { pthread_mutex_lock(&his_lock); std::map<string, int>::iterator i = his.find(ip); if(i != his.end()) his.erase(i); pthread_mutex_unlock(&his_lock); } void add_mng(std::string& ip, int cfd) { pthread_mutex_lock(&mng_lock); mng.insert(std::make_pair(ip, cfd)); pthread_mutex_unlock(&mng_lock); } void del_mng(std::string& ip) { pthread_mutex_lock(&mng_lock); std::map<string, int>::iterator i = mng.find(ip); if(i != mng.end()) mng.erase(i); pthread_mutex_unlock(&mng_lock); } void add_client(std::string& ip, int cfd) { pthread_mutex_lock(&clients_lock); clients.insert(std::make_pair(ip, cfd)); pthread_mutex_unlock(&clients_lock); } void del_client(std::string& ip) { pthread_mutex_lock(&clients_lock); std::map<string, int>::iterator i = clients.find(ip); if(i != clients.end()) clients.erase(i); pthread_mutex_unlock(&clients_lock); } void insert_php(int handle, int RoomNo, int Type, int Inet) { pthread_mutex_lock(&php_lock); php.insert(std::make_pair(RoomNo, new php_status_t(handle, Type, Inet))); pthread_mutex_unlock(&php_lock); } /** * function logic is in reverce * return false if room is in and true if there is timeout or no info for room */ bool is_in_php(int RoomNo) { bool bRet = false; pthread_mutex_lock(&php_lock); map<int, linked_ptr<php_status_t> >::iterator i = php.find(RoomNo); if(i != php.end()) { time_t now = time(NULL); if(difftime(now, php[RoomNo]->datetime) > 60) { php.erase(i); bRet = true; } } else bRet = true; pthread_mutex_unlock(&php_lock); return bRet; } void delete_php(int RoomNo) { pthread_mutex_lock(&php_lock); map<int, linked_ptr<php_status_t> >::iterator i = php.find(RoomNo); if(i != php.end()) { php.erase(i); } pthread_mutex_unlock(&php_lock); }}context_t;typedef struct typemsg{ typemsg(const char* buffer) : command(buffer), save_handle(NULL), del_handle(NULL) {} string command; string ip; int sd; void (*message_loop)(void*); void (context_t::*save_handle)(std::string&, int); void (context_t::*del_handle)(std::string&);}msg_t;extern context_t ctxt;#define TRANSACTION_BEGIN(C) \ bool bOK = true;\ \ try{ \ mysqlpp::Transaction trans(C);#define TRANSACTION_END() \ trans.commit(); \ } \ catch (const mysqlpp::BadQuery& er) { \ std::cerr << "Query error: " << er.what() << std::endl; \ bOK = false; \ } \ catch (const mysqlpp::BadConversion& er) { \ std::cerr << "Conversion error: " << er.what() << std::endl << "\tretrieved data size: " << er.retrieved << ", actual size: " << er.actual_size << std::endl; \ bOK = false; \ } \ catch (const mysqlpp::Exception& er) { \ std::cerr << "Error: " << er.what() << std::endl; \ bOK = false; \ } \ catch (...) { \ std::cerr << "Error" << std::endl; \ } \#define DECODE_BEGIN(C) \ std::vector<std::string> message; \ pcrecpp::StringPiece input(C); \ std::string command; \ \ while(pcrecpp::RE("([\\x20-\\x7B\\x7D-\\xFF]+)[\\|]*", pcrecpp::RE_Options().set_dotall(true)).Consume(&input, &command)) \ { \ message.push_back(command); \ \ } \ \ for(uint16 i = 0 ; i < message.size(); i++)\ {\ pcrecpp::StringPiece message_real(message[i]); \#define EXTRACT(PAT, C) \ if(pcrecpp::RE(PAT).FullMatch(message_real, &C)) \ { \ } \#define DECODE_END() }#define BEGIN_DB() \ { \ mysqlpp::Connection* conn = NULL; \ try{ \ conn = ctxt.db_pool.grab(); \ if (conn && conn->connected()) \ { \ mysqlpp::Query qr = conn->query(); \#define END_DB_RETURN() \ ctxt.db_pool.release(conn); \#define END_DB() \ ctxt.db_pool.release(conn); \ } \ }catch(mysqlpp::Exception err) \ { \ if (conn && conn->connected()) \ { \ ctxt.db_pool.release(conn); \ } \ logger(ERROR, "Cannot open new connection to DB %s", err.what()); \ } \ } \#define DB_EXECUTE(N) qr.execute(N)#define DB_STORE(N) qr.store(N)#define CRC(message) \ std::string _t = message.str(); \ message << std::setw(4) << std::hex << std::uppercase <<get_crc(_t) << "\r\n"; \inline unsigned short get_crc(string& cmd){ unsigned short crc16 = 0; int len = cmd.length(); const char* c = cmd.c_str(); for(int i = 0; i < len; i++) { crc16 = crc16 ^ static_cast<unsigned short>(c[i] << 8); for(int bit = 0; bit < 8; bit++) { if(crc16 & 0x8000) { crc16 = (crc16 << 1) ^ 0x1021; } else { crc16 = (crc16 << 1); } } } return crc16;}inline std::ostream& get_date(std::ostream& str){ time_t now = time(NULL); tm local; localtime_r(&now, &local); str.fill('0'); return str << std::setw(2) << local.tm_year - 100 << std::setw(2) << local.tm_mon+1 << std::setw(2) << local.tm_mday;}inline std::ostream& get_time(std::ostream& str){ time_t now = time(NULL); tm local; localtime_r(&now, &local); str.fill('0'); return str << std::setw(2) << local.tm_hour << std::setw(2) << local.tm_min << std::setw(2) << local.tm_sec;}inline std::ostream& get_date_time(std::ostream& str){// std::ostringstream str1; time_t now = time(NULL); tm local; localtime_r(&now, &local);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -