赞
踩
class SerAgent{ protected: char recv_buf[PACK_SIZE]; char send_buf[PACK_SIZE]; CommunicateCli *p_client; public: void init(CommunicateCli *ap_client); };class IntCalcAgent :public SerAgent { public: gint32 add(gint32 a, gint32 b, gint32 &c); gint32 sub(gint32 a, gint32 b, gint32 &c); protected: struct Arg_Data { gint32 a; gint32 b; gint32 c; };};class FloatCalcAgent :public SerAgent { public: gint32 add(double a, double b, double &c); gint32 sub(double a, double b, double &c); protected: struct Arg_Data { double a; double b; double c; };};
gint32 IntCalcAgent::add(gint32 a, gint32 b, gint32 &c){ char *p_buf; guint32 *p_obj_id; guint32 *p_fun_id; gint32 ret_val; gint32 *p_ret_val; Arg_Data add_data; p_buf = send_buf; p_obj_id = (guint32*)p_buf; *p_obj_id = CLASS_INT_CALC; p_buf += sizeof(guint32); p_fun_id = (guint32*)p_buf; *p_fun_id = FUNC_ADD; p_buf += sizeof(guint32); add_data.a = a; add_data.b = b; memmove(p_buf, (char*)&add_data, sizeof(add_data)); ret_val = p_client->call_ser(send_buf, recv_buf); if (ret_val < 0) return ret_val; p_buf = recv_buf; p_ret_val = (gint32*)p_buf; ret_val = *p_ret_val; p_buf += sizeof(gint32); memmove((char*)&add_data, p_buf, sizeof(add_data)); c = add_data.c; return ret_val;}
gint32 CommunicateCli::call_ser(char* ap_send, char* ap_recv){ gint32 ret_val = 0; gint32 ret_send = send(sd, ap_send, PACK_SIZE, 0); //std::cout << "client send: " << ret_send << std::endl; gint32 ret_recv = recv(sd, ap_recv, PACK_SIZE, 0); //std::cout << "client read: " << ret_recv << std::endl; if (ret_recv != PACK_SIZE) ret_val = -1; return ret_val;}
#define PACK_SIZE 4096class ModuleServer { protected: Glib::Mutex mutex_map; public: virtual void call_switch(char *p_recv, char *p_send) = 0; };class CommunicateSer{ private: typedef std::pair<guint32, ModuleServer*> Ps; struct sockaddr_in sa; int err; int listen_sd; int sd; struct sockaddr_in sa_serv; struct sockaddr_in sa_cli; int client_len; std::map<guint32, ModuleServer*> map_servers; std::map<guint32, ModuleServer*>::iterator it_map; Glib::Mutex mutex_map; std::queue<int> queue_sd; Glib::Thread *p_server_thread; Glib::Mutex mutex_sd; public: void reg_server(guint32 a_ser_id, ModuleServer* ap_server); void init(){}; void listen_client(); void server(); };class IntCalcSer :public ModuleServer { public: void call_switch(char *ap_recv, char *ap_send); void init(); protected: struct Arg_Data { gint32 a; gint32 b; gint32 c; }; protected: void add(char *ap_recv, char *ap_send); void sub(char *ap_recv, char *ap_send); protected: typedef void (IntCalcSer::*P_FUNC)(char *ap_recv, char *ap_send); P_FUNC p_func; std::map<guint32, P_FUNC> map_func; std::map<guint32, P_FUNC>::iterator it_map_func; typedef std::pair<guint32, P_FUNC> Ps_Func;};class FloatCalcSer :public ModuleServer { public: void call_switch(char *ap_recv, char *ap_send); void init(); protected: struct Arg_Data { double a; double b; double c; }; protected: void add(char *ap_recv, char *ap_send); void sub(char *ap_recv, char *ap_send); protected: typedef void (FloatCalcSer::*P_FUNC)(char *ap_recv, char *ap_send); P_FUNC p_func; std::map<guint32, P_FUNC> map_func; std::map<guint32, P_FUNC>::iterator it_map_func; typedef std::pair<guint32, P_FUNC> Ps_Func;};void CommunicateSer::reg_server(guint32 a_ser_id, ModuleServer* ap_server){ map_servers.insert(Ps(a_ser_id, ap_server)); }
void CommunicateSer::server(){ int server_sd = 0; char recv_buf[PACK_SIZE]; char send_buf[PACK_SIZE]; ModuleServer* p_server; mutex_sd.lock(); server_sd = queue_sd.front(); queue_sd.pop(); mutex_sd.unlock(); while (true) { gint32 ret_recv = recv(server_sd, recv_buf, PACK_SIZE, 0); std::cout << "server read: " << ret_recv << std::endl; if (ret_recv != PACK_SIZE) { #ifdef _WIN32 closesocket(server_sd); #else close(server_sd); #endif std::cout << "close server socket" << std::endl; break; } guint32 *p_obj_id; p_obj_id = (guint32*)recv_buf; guint32 id_obj; id_obj = *p_obj_id; std::cout << "id_obj = " << id_obj << std::endl; mutex_map.lock(); it_map = map_servers.find(id_obj); p_server = it_map->second; mutex_map.unlock(); std::cout << it_map->first << std::endl; p_server->call_switch(recv_buf, send_buf); gint32 ret_send = send(server_sd, send_buf, PACK_SIZE, 0); std::cout << "server send: " << ret_send << std::endl; }}
void IntCalcSer::call_switch(char *ap_recv, char *ap_send){ char *p_buf; guint32 *p_obj_id; guint32 *p_fun_id; P_FUNC p_func_tmp; p_buf = ap_recv; p_obj_id = (guint32*)p_buf; p_buf += sizeof(guint32); p_fun_id = (guint32*)p_buf; p_buf += sizeof(guint32); mutex_map.lock(); it_map_func = map_func.find(*p_fun_id); p_func_tmp = it_map_func->second; mutex_map.unlock(); (this->*p_func_tmp)(ap_recv, ap_send);}void IntCalcSer::add(char *ap_recv, char *ap_send){ char *p_buf; guint32 *p_obj_id; guint32 *p_fun_id; gint32 *p_ret_val; Arg_Data add_data; p_buf = ap_recv; p_obj_id = (guint32*)p_buf; p_buf += sizeof(guint32); p_fun_id = (guint32*)p_buf; p_buf += sizeof(guint32); memmove((char*)&add_data, p_buf, sizeof(add_data)); add_data.c = add_data.a + add_data.b; p_buf = ap_send; p_ret_val = (gint32*)p_buf; p_buf += sizeof(gint32); memmove(p_buf, (char*)&add_data, sizeof(add_data)); *p_ret_val = 0;}