netObject ndOpenConnect(const char *host, int port) { nd_handle handle = 0 ; ndInitNet() ; handle = nd_object_create("tcp-connector" ) ; if(!handle){ nd_logerror((char*)"connect error :%s!" AND nd_last_error()) ; return NULL; } if(-1==nd_msgtable_open(handle, 16) ) { nd_object_destroy(handle, 0) ; return NULL ; } //open if(-1==nd_connector_open( handle, (char*)host, port,NULL ) ){ nd_logerror("connect error :%s!" AND nd_last_error()) ; nd_object_destroy(handle,1) ; return NULL; } nd_hook_packet((netObject) handle,(net_msg_entry )_translate_message); ndMsgfuncInit( (netObject) handle ) ; return (netObject) handle; }
int HttpConnector::Create(const char *host, int port) { if (m_conn) { nd_object_destroy(m_conn, 0) ; //DestroyConnectorObj(m_conn) ; m_conn = 0 ; } m_conn = nd_object_create("tcp-connector" ) ; if(!m_conn){ nd_logerror((char*)"connect error :%s!" , nd_last_error()) ; return -1; } ((nd_netui_handle)m_conn)->user_data =(void*) this ; int ret = nd_connector_open(m_conn,host, port, NULL); if(ret == 0 ) { m_host = host ; m_port = port ; } else { nd_object_destroy(m_conn, 0) ; m_conn = 0 ; return -1; } return 0 ; }
int nd_net_bind(int port, int listen_nums,nd_handle net_handle) { ndsocket_t fd; struct nd_netsocket *node = (struct nd_netsocket *)net_handle ; //SOCKADDR_IN *remote = NULL; nd_assert(node) ; node->myerrno = NDERR_SUCCESS ; fd = nd_socket_openport(port,node->sock_type, node->sock_protocol, node->bindip, listen_nums) ; if(-1==fd){ nd_logfatal("open port %s" AND nd_last_error()) ; node->myerrno = NDERR_OPENFILE ; return -1 ; } if(0==port && SOCK_DGRAM==node->sock_type) { short tmp = nd_sock_getport(fd) ; port =(int) ntohs(tmp); } node->fd = fd ; node->port = htons(port) ; node->status = 1 ; return 0 ; }
static int code_convert(const char *from_charset,const char *to_charset, char *inbuf,int inlen,char *outbuf,int outlen) { iconv_t cd; //int rc; char **pin = &inbuf; char **pout = &outbuf; size_t in_buf=inlen; size_t out_buf=outlen; cd = iconv_open(to_charset,from_charset); if (cd==0) { nd_logerror("iconv_open %s\n", nd_last_error() ) ; return -1; } memset(outbuf,0,outlen); if (iconv(cd,pin,&in_buf,pout,&out_buf)==-1) { nd_logerror("iconv %s\n", nd_last_error() ) ; return -1; } iconv_close(cd); return 0; }
int read_dbconfig(const char *fileName, const char *dbCfgname ,struct nd_db_config *db_cfg) { //ND_TRACE_FUNC() ; int ret = -1; ndxml *xmlroot, *xmlsub, *xmlnode ; ndxml_root xmlfile; #define GET_VAL(name) \ xmlnode = ndxml_refsub(xmlsub, #name) ; \ if (!xmlnode){ \ ret = -1; \ goto READ_EXIT ; \ } \ strncpy(db_cfg->db_##name, ndxml_getval(xmlnode), sizeof(db_cfg->db_##name) ) if ( 0 != ndxml_load(fileName , &xmlfile ) ){ nd_logfatal("read file %s error %s\n", fileName, nd_last_error() ) ; return -1; } xmlroot = ndxml_getnode( &xmlfile, "root" ) ; if ( !xmlroot ) { goto READ_EXIT; } //read connect config xmlsub = ndxml_refsub( xmlroot, dbCfgname ) ; if ( !xmlsub ) { goto READ_EXIT; } GET_VAL( host ) ; GET_VAL( database ) ; GET_VAL( user ) ; GET_VAL( password) ; ret = 0 ; READ_EXIT: ndxml_destroy( &xmlfile ); return ret ; }
//read udp socket data int nd_udp_read(struct nd_udp_node*node , char *buf, size_t buf_size, ndtime_t outval) { int ret ; int readlen = 0; nd_assert(buf && buf_size>0); TCPNODE_READ_AGAIN(node) = 0; if(outval ) { ret = nd_socket_wait_read(node->fd, outval) ; if(ret <= 0) { node->myerrno = (ret == 0) ? NDERR_WOULD_BLOCK: NDERR_IO; return ret ; } } readlen = nd_socket_udp_read(node->fd, buf, (int)buf_size, &node->last_read); if (-1 == readlen) { node->sys_error = nd_socket_last_error(); if (node->sys_error == ESOCKETTIMEOUT) { node->myerrno = NDERR_WOULD_BLOCK; return 0; } else { node->myerrno = NDERR_READ; nd_logdebug("recvfrom : %s\n", nd_last_error()); return -1; } } TCPNODE_READ_AGAIN(node) = 1; node->last_recv = nd_time(); node->recv_len += readlen; return readlen ; }
int ipaddr_mac_cmp(const char *input_addr) { PIP_ADAPTER_INFO pAdapterInfo; PIP_ADAPTER_INFO pAdapter = NULL; DWORD dwRetVal = 0; pAdapterInfo = (IP_ADAPTER_INFO *) malloc( sizeof(IP_ADAPTER_INFO) ); ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO); dwRetVal = GetAdaptersInfo( pAdapterInfo, &ulOutBufLen) ; if ( dwRetVal != ERROR_SUCCESS) { free (pAdapterInfo); pAdapterInfo = (IP_ADAPTER_INFO *) malloc (ulOutBufLen); } if((dwRetVal=GetAdaptersInfo(pAdapterInfo ,&ulOutBufLen))==NO_ERROR){ pAdapter= pAdapterInfo; while(pAdapter) { char mac_buf[128] ; char *p = mac_buf ; for(UINT i=0;i<pAdapter->AddressLength;i++) { p +=snprintf(p,128 , "%02x-" , pAdapter->Address[i]) ; } --p ; *p = 0 ; if (0==ndstricmp((char*)mac_buf,(char*) input_addr)){ return 0 ; } pAdapter=pAdapter->Next; } } else { nd_logerror("last error :%s\n" AND nd_last_error()) ; } free (pAdapterInfo); return -1; }
//build datatype int build_dataType(ndxml_root *xmlfile, const char *out_file,bool saveDB) { ndxml *xnode = ndxml_getnode(xmlfile, "DataType") ; if (!xnode) { fprintf(stderr, " build marco error MarcoDefine node unexist\n" ) ; return -1 ; } ndxml *xInfo = ndxml_getnode(xmlfile, "DataDesc") ; if (!xInfo) { return -1; } const char *version = ndxml_getattr_val(xInfo, "version"); if (!version) { return -1; } const char *comm_version = ndxml_getattr_val(xInfo, "comm_version"); const char *nameSpace = ndxml_getattr_val(xnode, "namespace"); name_list_t res_list ; get_name_sort(xmlfile, res_list); NDUINT64 nowTime = (NDUINT64) time(NULL); if (saveDB) { char buf[128] ; snprintf(buf, sizeof(buf), "%s_v%s.h",out_file, version ); FILE *pf = fopen(buf, "w") ; if (!pf){ fprintf(stderr, " open out put file %s error \n", buf ) ; return -1 ; } snprintf(buf, sizeof(buf), "%s_v%s.cpp",out_file, version ); FILE *pfCpp = fopen(buf, "w") ; if (!pf){ fprintf(stderr, " open out put file %s error \n", buf ) ; return -1 ; } _OUT_PUT_TIME(pfCpp) ; fprintf(pfCpp, "\n\n#include \"netMessage/dataStream.h\"\n"); fprintf(pfCpp, "\n\n#include \"netMessage/auto_dataTypeDb_v%s.h\"\n", version ); //fprintf(pfCpp, "namespace NetMessage \n{\n" ); if (nameSpace) { fprintf(pfCpp, "namespace %s \n{\n", nameSpace); } else { fprintf(pfCpp, "/* undefined namespace*/\n{\n"); } _OUT_PUT_TIME(pf) ; fprintf(pf, "#ifndef _AUTO_DATA_TYPE_DEFINE_DB_%s_H_\n",version ); fprintf(pf, "#define _AUTO_DATA_TYPE_DEFINE_DB_%s_H_\n",version ); fprintf(pf, "#include \"netMessage/auto_macroDefine.h\"\n\n") ; fprintf(pf, "#include \"netMessage/dataStream.h\"\n\n"); fprintf(pf, "#include \"netMessage/dataBinary.h\"\n\n"); if (nameSpace) { fprintf(pf, "namespace %s \n{\n", nameSpace); } else { fprintf(pf, "/* undefined namespace*/\n{\n"); } name_list_t save_list; get_saveDB_sort(xmlfile,res_list,save_list); int total =(int) save_list.size(); //ndxml_getsub_num(xnode) ; for (int i=0; i<total; ++i) { ndxml *sub = ndxml_refsub(xnode,save_list[i].name.c_str()) ; _save_dataTypeNode(sub, pf,pfCpp ,version, true) ; fprintf(pf, "\n\n") ; } fprintf(pf,"};\n\n#endif\n\n") ; fclose(pf) ; fprintf(pfCpp,"};\n\n") ; fclose(pfCpp) ; } else { char buf[128] ; snprintf(buf, sizeof(buf), "%s.h",out_file ); FILE *pf = fopen(buf, "w") ; if (!pf){ fprintf(stderr, " open out put file %s error: %s \n", buf , nd_last_error() ) ; return -1 ; } snprintf(buf, sizeof(buf), "%s.cpp",out_file ); FILE *pfCpp = fopen(buf, "w") ; if (!pf){ fprintf(stderr, " open out put file %s error: %s \n", buf, nd_last_error()); return -1 ; } _OUT_PUT_TIME(pfCpp) ; fprintf(pfCpp, "\n\n#include \"netMessage/auto_dataType.h\"\n" ); //fprintf(pfCpp, "namespace NetMessage \n{\n" ); if (nameSpace) { fprintf(pfCpp, "namespace %s \n{\n", nameSpace); } else { fprintf(pfCpp, "/* undefined namespace*/\n{\n"); } _OUT_PUT_TIME(pf) ; fprintf(pf, "#ifndef _AUTO_DATA_TYPE_DEFINE_H_\n" ); fprintf(pf, "#define _AUTO_DATA_TYPE_DEFINE_H_\n" );; fprintf(pf, "#include \"netMessage/auto_macroDefine.h\"\n\n") ; fprintf(pf, "#include \"netMessage/dataStream.h\"\n\n") ; fprintf(pf, "#include \"netMessage/dataBinary.h\"\n\n"); fprintf(pf, "#define DATA_IN_DB_VERSION %s \n\n", version); fprintf(pf, "#define NET_COMMUNATION_VERSION %s \n\n", comm_version); fprintf(pf, "#define NET_MESSAGE_EXPORT_TM %lld \n", nowTime); if (nameSpace) { fprintf(pf, "namespace %s \n{\n", nameSpace); } else { fprintf(pf, "/* undefined namespace*/\n{\n"); } int total =(int) res_list.size(); //ndxml_getsub_num(xnode) ; for (int i=0; i<total; ++i) { ndxml *sub = ndxml_refsub(xnode,res_list[i].name.c_str()) ; // if (0 == ndstricmp(sub->name, "CityBase")) { // ND_DebugBreak(); // } _save_dataTypeNode(sub, pf,pfCpp, NULL, false) ; fprintf(pf, "\n") ; } fprintf(pf,"};\n\n#endif\n\n") ; fclose(pf) ; fprintf(pfCpp,"};\n\n") ; fclose(pfCpp) ; } return 0 ; }
//#include <QProcess> bool startDialog::expExcel() { WriteLog("==============Begin export excel==============="); DBLDatabase::destroy_Instant(); const char *exp_cmd = _getFromIocfg("game_data_export_cmd"); std::string stdstr_excel_path = _getFromIocfg("excel_data_in_path"); std::string stdstr_text_path = _getFromIocfg("text_data_out_path"); std::string stdstr_package_file = _getFromIocfg("game_data_package_file"); std::string stdstr_excel_list = _getFromIocfg("game_data_listfile"); const char *excel_path = stdstr_excel_path.c_str(); const char *text_path = stdstr_text_path.c_str(); const char *package_file = stdstr_package_file.c_str(); const char *excel_list = stdstr_excel_list.c_str(); char exp_cmdbuf[1024]; if (!exp_cmd || !excel_path || !text_path || !package_file){ WriteLog("export excel error : on read config file\n"); return false; } WorkingPathSwitchHelper __pathHelper(m_projectPath.c_str()); const char *encodeName = getGameDateEncodeType(); const char *inputCode = encodeName; #ifdef WIN32 snprintf(exp_cmdbuf, sizeof(exp_cmdbuf), " %s %s %s %s %s ", exp_cmd, excel_list, excel_path, text_path, encodeName); #else snprintf(exp_cmdbuf, sizeof(exp_cmdbuf), "sh %s %s %s %s %s ", exp_cmd, excel_list, excel_path, text_path, encodeName); #endif int ret = ::system(exp_cmdbuf); if (0 != ret) { nd_logerror("[%s] run export shell %s\nerror: %s \n",nd_getcwd(), exp_cmdbuf,nd_last_error()); return false; } WriteLog("===============export excel to text file success ========="); //get byte order int orderType = ND_L_ENDIAN; const char *orderName = apoEditorSetting::getInstant()->getValueFromSetting("bin_data_byte_order"); if (orderName) { orderType = atoi(orderName); } //DUMP FOR WINDOWS only #ifdef WIN32 do { std::string strWinPack = package_file; strWinPack += ".gbk"; DBLDatabase dbwin; if (0 != dbwin.LoadFromText(text_path, excel_list, inputCode, "gbk")) { nd_logerror("can not read from text gbk\n"); return false; } if (0 != dbwin.Dump(strWinPack.c_str(), "gamedbGBK", orderType)) { nd_logmsg("EXPORT game data for windows error\n"); } dbwin.Destroy(); } while (0); #endif DBLDatabase dbtmp; if (0 != dbtmp.LoadFromText(text_path, excel_list, inputCode, encodeName)) { nd_logerror("load data from text file error"); return false; } if (0 == dbtmp.Dump(package_file, "gamedatadb", orderType)) { nd_logmsg("package game data SUCCESS!!\n"); WriteLog("==========write excel to bin-stream success ===========\n"); } else{ nd_logerror("write excel to bin-stream FAILED"); } //before run test need load dbl DBLDatabase *pdbl = DBLDatabase::get_Instant(); if (pdbl){ if (0 != pdbl->LoadBinStream(package_file)) { WriteLog("load data from bin-stream error "); dbtmp.Destroy(); DBLDatabase::destroy_Instant(); return false; } } if (!(*pdbl == dbtmp)){ nd_logmsg("test data load error , read from text and stream-bin not match!!!!"); dbtmp.Destroy(); DBLDatabase::destroy_Instant(); return false ; } const char *exp_luapath = _getFromIocfg("lua_data_out_path"); if (exp_luapath && *exp_luapath ) { if (!expLua(exp_luapath, dbtmp)) { nd_logmsg("export lua error"); dbtmp.Destroy(); DBLDatabase::destroy_Instant(); return false; } } dbtmp.Destroy(); std::string strOutTextPath = text_path; strOutTextPath += "/test_outputData"; nd_mkdir(strOutTextPath.c_str()); if (0 != pdbl->TestOutput(strOutTextPath.c_str())) { nd_logmsg("dump from bin-stream to text file error\n"); DBLDatabase::destroy_Instant(); return false; } DBLDatabase::destroy_Instant(); return true; }