int nd_net_ioctl(nd_netui_handle socket_node, int cmd, void *val, int *size) { int ret = -1 ; NDUINT32 inputVal ; switch(cmd) { case NDIOCTL_SET_SENDVBUF: if (*size <4){ return -1 ; } if (socket_node->type ==NDHANDLE_TCPNODE|| socket_node->type ==NDHANDLE_UDPNODE ){ if (!socket_node->is_session){ inputVal = *(NDUINT32*)val ; ret = ndlbuf_realloc (&socket_node->send_buffer,inputVal) ; } } break ; case NDIOCTL_GET_SENDBUF: if (*size <4){ return -1 ; } if (socket_node->type ==NDHANDLE_TCPNODE|| socket_node->type ==NDHANDLE_UDPNODE ){ if (!socket_node->is_session){ *(NDUINT32*)val = (NDUINT32)ndlbuf_capacity(&socket_node->send_buffer) ; *size = sizeof(NDUINT32) ; ret = 0; } } break ; case NDIOCTL_SET_RECVVBUF: if (*size <4){ return -1 ; } if (socket_node->type ==NDHANDLE_TCPNODE|| socket_node->type ==NDHANDLE_UDPNODE ){ if (!socket_node->is_session){ inputVal = *(NDUINT32*)val ; ret = ndlbuf_realloc (&socket_node->recv_buffer,inputVal) ; } } break ; case NDIOCTL_GET_RECVBUF: if (*size <4){ return -1 ; } if (socket_node->type ==NDHANDLE_TCPNODE|| socket_node->type ==NDHANDLE_UDPNODE ){ if (!socket_node->is_session){ *(NDUINT32*)val = (NDUINT32)ndlbuf_capacity(&socket_node->recv_buffer) ; *size = sizeof(NDUINT32) ; ret = 0; } } break ; case NDIOCTL_SET_TCP_RECV_WNDSIZE: if (socket_node->type ==NDHANDLE_TCPNODE|| socket_node->type ==NDHANDLE_UDPNODE ){ if (socket_node->fd){ ret = setsockopt(socket_node->fd, SOL_SOCKET, SO_RCVBUF, (sock_opval_t)val, *size) ; } } break ; case NDIOCTL_GET_TCP_RECV_WNDSIZE: if (socket_node->type ==NDHANDLE_TCPNODE|| socket_node->type ==NDHANDLE_UDPNODE ){ if (socket_node->fd){ inputVal = *(NDUINT32*)val ; ret = getsockopt(socket_node->fd, SOL_SOCKET, SO_RCVBUF, (sock_opval_t)val, size) ; } } break ; case NDIOCTL_SET_TCP_SEND_WNDSIZE: if (socket_node->type ==NDHANDLE_TCPNODE|| socket_node->type ==NDHANDLE_UDPNODE ){ if (socket_node->fd){ inputVal = *(NDUINT32*)val ; ret = setsockopt(socket_node->fd, SOL_SOCKET, SO_SNDBUF,(sock_opval_t)val, *size) ; } } break ; case NDIOCTL_GET_TCP_SEND_WNDSIZE: if (socket_node->type ==NDHANDLE_TCPNODE|| socket_node->type ==NDHANDLE_UDPNODE ){ if (socket_node->fd){ inputVal = *(NDUINT32*)val ; ret = getsockopt(socket_node->fd, SOL_SOCKET, SO_SNDBUF, (sock_opval_t)val, size) ; } } break ; case NDIOCTL_SET_TIMEOUT: socket_node->disconn_timeout = (*(ndtime_t*)val) *1000 ; ret = 0; break; case NDIOCTL_GET_TIMEOUT: *(ndtime_t*)val = socket_node->disconn_timeout /1000 ; *size = sizeof(ndtime_t) ; ret = 0; break ; case NDIOCTL_GET_LAST_RECV_TIME: *(ndtime_t*)val = socket_node->last_recv ; *size = sizeof(ndtime_t) ; ret = 0; break ; case NDIOCTL_GET_LAST_SEND_TIME: *(ndtime_t*)val = socket_node->last_push ; *size = sizeof(ndtime_t) ; ret = 0; break ; case NDIOCTL_GET_USERDATA: *(nd_userdata_t*)val = socket_node->user_data ; ret = 0; break ; case NDIOCTL_SET_USERDATA: socket_node->user_data = val; ret = 0; break ; /// case NDIOCTL_GET_PEER_IP: *(ndip_t*) val = nd_net_peer_getip(socket_node); ret = 0; break; case NDIOCTL_GET_PEER_PORT: *(ndport_t*) val = nd_net_peer_getport(socket_node); ret = 0; break; case NDIOCTL_HOOK_DATA: nd_hook_data(socket_node,(data_in_entry) val ) ; ret = 0; break ; case NDIOCTL_HOOK_PACKET: nd_hook_packet(socket_node,(net_msg_entry) val ) ; ret = 0; break ; case NDIOCTL_SET_UNREG_MSG_CLOSED: //close peers when received unregister message nd_net_set_unregmsg_handler(socket_node, *(int*)val); ret = 0; break ; case NDIOCTL_SET_UNAUTHORIZE_CLOSE: nd_net_set_unauthorize_handler(socket_node, *(int*)val); ret = 0; break ; case NDIOCTL_SET_CRYPT_KEY: nd_connector_set_crypt(socket_node,val, *size); ret = 0; break ; case NDIOCTL_GET_CRYPT_KEY: { int keysize = 0 ; void *addr = nd_connector_get_crypt(socket_node, &keysize) ; ret = -1 ; if (keysize <= *size) { memcpy(val, addr, keysize) ; *size = keysize ; ret = 0 ; } } break; case NDIOCTL_SET_LEVEL: socket_node->level = (NDUINT8) (*(int*)val ) ; ret = 0; break; case NDIOCTL_GET_LEVEL: *(int*)val = socket_node->level ; ret = 0; break; case NDIOCTL_LOG_SEND_MSG: if (*(int*)val){ socket_node->is_log_send = 1; } else { socket_node->is_log_send = 0; } ret = 0; break; case NDIOCTL_LOG_RECV_MSG: if (*(int*)val){ socket_node->is_log_recv = 1; } else { socket_node->is_log_recv = 0; } ret = 0; break; case NDIOCTL_SYS_SET_LOG_FUNC: nd_setlog_func((logfunc)val); break; case NDIOCTL_SYS_GET_LOG_FUNC: { logfunc oldval = nd_setlog_func((logfunc)0); logfunc *retval = (logfunc *) val; *retval = oldval; nd_setlog_func(oldval); } break; case NDIOCTL_GET_SESSIONID: *(int*)val = socket_node->session_id ; ret = 0; break; case NDIOCTL_SET_SESSIONID: socket_node->session_id = (NDUINT16) (*(int*)val); ret = 0; break; case NDIOCTL_GET_WRITABLE_CALLBACK: *(net_writable_callback*)val = socket_node->writable_callback ; ret = 0 ; break ; case NDIOCTL_SET_WRITABLE_CALLBACK: socket_node->writable_callback =(net_writable_callback) *(nd_userdata_t*)val ; ret = 0 ; break ; case NDIOCTL_GET_WRITABLE_CALLBACK_PARAM: *(nd_userdata_t*)val = socket_node->writable_param ; ret = 0 ; break ; case NDIOCTL_SET_WRITABLE_CALLBACK_PARAM: socket_node->writable_param = *(nd_userdata_t*)val ; ret = 0 ; break ; case NDIOCTL_SET_BLOCK: ret = nd_socket_nonblock(socket_node->fd, 0) ; break ; case NDIOCTL_GET_BLOCK: ret = nd_socket_nonblock(socket_node->fd, 1) ; break ; case NDIOCTL_GET_RECV_PACK_NUM: if (socket_node->type ==NDHANDLE_TCPNODE|| socket_node->type ==NDHANDLE_UDPNODE ){ *(NDUINT32*)val = (NDUINT32) nd_atomic_read(&socket_node->recv_pack_times) ; *size = sizeof(NDUINT32) ; ret = 0; } break ; case NDIOCTL_SET_RECV_PACK_NUM: if (socket_node->type ==NDHANDLE_TCPNODE|| socket_node->type ==NDHANDLE_UDPNODE ){ inputVal = *(NDUINT32*)val ; nd_atomic_set(&socket_node->recv_pack_times, inputVal ); ret = 0; } break ; case NDIOCTL_GET_SEND_PACK_NUM: if (socket_node->type ==NDHANDLE_TCPNODE|| socket_node->type ==NDHANDLE_UDPNODE ){ *(NDUINT32*)val = (NDUINT32) nd_atomic_read(&socket_node->send_pack_times ); *size = sizeof(NDUINT32) ; ret = 0; } break ; case NDIOCTL_SET_SEND_PACK_NUM: if (socket_node->type ==NDHANDLE_TCPNODE|| socket_node->type ==NDHANDLE_UDPNODE ){ inputVal = *(NDUINT32*)val ; nd_atomic_set(&socket_node->send_pack_times, inputVal ); ret = 0; } break ; case NDIOCTL_LOG_SEND_STRAM_FILE: ret = -1 ; if (val) { socket_node->save_send_stream = 1 ; if (_s_send_stream) { free(_s_send_stream) ; } _s_send_stream = (char*) malloc(*size + 1) ; if (_s_send_stream) { strncpy(_s_send_stream, (const char*)val, *size +1) ; ret = 0 ; } } else { socket_node->save_send_stream = 0; if (_s_send_stream) { free(_s_send_stream) ; } ret = 0 ; } break ; case NDIOCTL_LOG_RECV_STRAM_FILE: ret = -1 ; if (val) { socket_node->save_recv_stream = 1 ; if (_s_recv_stream) { free(_s_recv_stream) ; } _s_recv_stream = (char*) malloc(*size + 1) ; if (_s_recv_stream) { strncpy(_s_recv_stream, (const char*)val, *size +1) ; ret = 0 ; } } else { socket_node->save_recv_stream = 0; if (_s_recv_stream) { free(_s_recv_stream) ; } ret = 0 ; } break ; default : ret =-1 ; break ; } return ret ; }
int NDInstanceBase::Create(int argc,const char *argv[]) { int i ; const char *logfileName = NULL ; if (m_bCreated) { return 0; } system_signals_init() ; nd_arg(argc, argv); //get config file for (i=1; i<argc; i++){ if(0 == strcmp(argv[i],"-f" ) && i< argc-1) { NDInstanceBase::config_file = argv[++i] ; } else if(0== strcmp(argv[i], "-c") && i< argc-1) { m_config_name = argv[++i] ; } else if(0==strcmp(argv[i],"-nodev")) { m_un_develop = 1; } else if(0==strcmp(argv[i],"-log") && i<argc -1) { logfileName = argv[++i]; } else if(0==strcmp(argv[i],"-workdir") &&i<argc -1 ) { nd_chdir(argv[++i]) ; } else if(0==strcmp(argv[i],"-v") || 0==strcmp(argv[i],"--version") ) { fprintf(stdout, "version : %s \n" , __g_version_desc) ; exit(0) ; } else if(0==strcmp(argv[i],"-l") || 0==strcmp(argv[i],"--rlimit") ) { char buf[1024] ; fprintf(stdout, "rlimit: \n %s\n" , get_rlimit_info(buf, sizeof(buf)) ) ; exit(0) ; } else if(0==strcmp(argv[i],"-pid") && i<argc -1){ FILE *pf = fopen(argv[i+1], "w") ; if (pf) { fprintf(pf, "%d", nd_processid()) ; fclose(pf) ; } else { fprintf(stderr,"write pid error %s file not exist\n", argv[i+1]) ; exit(1) ; } } } if(!config_file|| !m_config_name) { printf("usage: -f config-file -c config-name\n press ANY key to continue\n") ; //getch() ; exit(1) ; //return -1 ; } if(-1==ReadConfig(m_config_name) ) { printf("Read config %s file error \n press ANY key to continue\n", m_config_name) ; //getch() ; exit(1) ; } nd_set_exit_callback(applib_exit_callback) ; if (m_config.i_cfg.open_dump){ #ifndef ND_UNIX ::SetUnhandledExceptionFilter((LPTOP_LEVEL_EXCEPTION_FILTER)nd_unhandler_except); #else enable_core_dump() ; #endif } size_t max_connector = m_config.l_cfg.max_connect ; if ( max_connector > get_maxopen_fd() ) { size_t new_fd_num = set_maxopen_fd(max_connector) ; nd_logmsg("set max connect %d real max connect =%d\n",m_config.l_cfg.max_connect, (int) new_fd_num); } if(logfileName && logfileName[0]) { strncpy(m_config.i_cfg.log_file, logfileName, sizeof(m_config.i_cfg.log_file)) ; } if (m_config.i_cfg.log_file[0]){ set_log_file(m_config.i_cfg.log_file) ; if (m_config.i_cfg.log_file_size > 0) { nd_setlog_maxsize(m_config.i_cfg.log_file_size) ; } nd_setlog_func(nd_default_filelog) ; } if (m_config.i_cfg.callstack_file[0]){ if(CALLSTACK_INIT(m_config.i_cfg.callstack_file)==-1) { nd_logfatal("create map file %s error!\n" AND m_config.i_cfg.callstack_file) ; return -1 ; } } nd_net_set_crypt((nd_netcrypt)nd_TEAencrypt, (nd_netcrypt)nd_TEAdecrypt, sizeof(tea_v)) ; // if (g_base_inst==NULL){ // g_base_inst = this ; // } OnCreate(); m_bCreated = 1; return 0 ; }
BOOL CapolloParseEditorApp::InitInstance() { // 如果一个运行在 Windows XP 上的应用程序清单指定要 // 使用 ComCtl32.dll 版本 6 或更高版本来启用可视化方式, //则需要 InitCommonControlsEx()。 否则,将无法创建窗口。 INITCOMMONCONTROLSEX InitCtrls; InitCtrls.dwSize = sizeof(InitCtrls); // 将它设置为包括所有要在应用程序中使用的 // 公共控件类。 InitCtrls.dwICC = ICC_WIN95_CLASSES; InitCommonControlsEx(&InitCtrls); CWinApp::InitInstance(); nd_common_init(); nd_setxml_log(_xml_error_output); nd_log_no_file(1); nd_log_no_date(1); ndstr_set_code(E_SRC_CODE_GBK); // 创建 shell 管理器,以防对话框包含 // 任何 shell 树视图控件或 shell 列表视图控件。 CShellManager *pShellManager = new CShellManager; // 激活“Windows Native”视觉管理器,以便在 MFC 控件中启用主题 CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerWindows)); // 标准初始化 // 如果未使用这些功能并希望减小 // 最终可执行文件的大小,则应移除下列 // 不需要的特定初始化例程 // 更改用于存储设置的注册表项 // TODO: 应适当修改该字符串, // 例如修改为公司或组织名 SetRegistryKey(_T("apolloEditor")); CapolloParseEditorDlg dlg; m_pMainWnd = &dlg; nd_setlog_func(out_log); nd_register_error_convert(apollo_error); INT_PTR nResponse = dlg.DoModal(); if (nResponse == IDOK) { // TODO: 在此放置处理何时用 // “确定”来关闭对话框的代码 } else if (nResponse == IDCANCEL) { // TODO: 在此放置处理何时用 // “取消”来关闭对话框的代码 } else if (nResponse == -1) { TRACE(traceAppMsg, 0, "警告: 对话框创建失败,应用程序将意外终止。\n"); TRACE(traceAppMsg, 0, "警告: 如果您在对话框上使用 MFC 控件,则无法 #define _AFX_NO_MFC_CONTROLS_IN_DIALOGS。\n"); } // 删除上面创建的 shell 管理器。 if (pShellManager != NULL) { delete pShellManager; } // 由于对话框已关闭,所以将返回 FALSE 以便退出应用程序, // 而不是启动应用程序的消息泵。 nd_common_release(); return FALSE; }