代码优化下面的代码:DWORD WINAPI SoapServerThread( LPVOID pParam ) { CUserManagerServerDlg *pUserManServer = (CUserManagerServerDlg *)pParam; struct soap Soap_service; soap_init(&Soap_service); Soap_service.pUserManagerServerDlg = pUserManServer; Soap_service.bind_flags = SO_REUSEADDR; USES_CONVERSION; std::string strIP(W2A(pUserManServer->m_csIP)); const char* chIP = strIP.c_str(); CString csInfo; int nBindErr; if ((nBindErr = soap_bind(&Soap_service,chIP, pUserManServer->m_nPort, 100))< 0) //soap_bind(&math_service, address, port, 100); { soap_print_fault(&Soap_service, stderr); //csInfo.Format(_T("用户服务器绑定失败")); csInfo.LoadString(IDS_BIND_FAILD); pUserManServer->RecordOperaInfo(csInfo,6); pUserManServer->RecordSQL(csInfo,6); return -1; } if (pUserManServer->m_csIP.IsEmpty() || !pUserManServer->m_nPort) { //csInfo.Format(_T("IP和端口号不允许为空!")); csInfo.LoadString(IDS_CANNOT_EMPTY); pUserManServer->RecordOperaInfo(csInfo,6); pUserManServer->RecordSQL(csInfo,6); return -1; } //csInfo.Format(_T("用户服务器已启动...")); csInfo.LoadString(IDS_STARTED); pUserManServer->RecordOperaInfo(csInfo,0); pUserManServer->RecordSQL(csInfo,0); CString csListen; csListen.LoadString(IDS_LISTEN); csInfo.Format(_T("%s:IP(%s),Port:(%d)"),csListen,pUserManServer->m_csIP,pUserManServer->m_nPort); pUserManServer->RecordOperaInfo(csInfo,0); pUserManServer->RecordSQL(csInfo,0); while (true) { int nSlave = (int)soap_accept(&Soap_service); if (nSlave < 0) { //csInfo.Format(_T("soap_accept调用失败...")); csInfo.LoadString(IDS_CALL_FAILD); pUserManServer->RecordOperaInfo(csInfo,6); pUserManServer->RecordSQL(csInfo,6); soap_print_fault(&Soap_service, stderr); return -1; } soap_serve(&Soap_service); soap_destroy(&Soap_service); soap_end(&Soap_service); } return 0; }
时间: 2024-02-18 13:01:27 浏览: 21
以下是对代码进行的优化:
1. 声明变量时最好直接初始化,避免未初始化的变量被使用。
2. 将字符串转换为const char*时,可以直接使用CString的GetString()函数。
3. 在循环中调用soap_destroy和soap_end函数是不必要的,只需要在循环之前调用一次即可。
4. 将错误码与字符串绑定在一起,可以减少代码量。
5. 将常量字符串存储在全局变量中,避免重复加载。
6. 可以尝试将一些重复的代码封装为函数,提高代码的复用性。
优化后的代码如下所示:
```
DWORD WINAPI SoapServerThread(LPVOID pParam) {
CUserManagerServerDlg* pUserManServer = static_cast<CUserManagerServerDlg*>(pParam);
struct soap Soap_service;
soap_init(&Soap_service);
Soap_service.pUserManagerServerDlg = pUserManServer;
Soap_service.bind_flags = SO_REUSEADDR;
const char* chIP = CStringA(pUserManServer->m_csIP.GetString());
static const int BIND_FAILD = 6;
static const int CALL_FAILD = 6;
static const int INFO = 0;
static const CStringA STR_BIND_FAILD("用户服务器绑定失败");
static const CStringA STR_CANNOT_EMPTY("IP和端口号不允许为空!");
static const CStringA STR_STARTED("用户服务器已启动...");
static const CStringA STR_LISTEN("监听");
if (pUserManServer->m_csIP.IsEmpty() || !pUserManServer->m_nPort) {
pUserManServer->RecordOperaInfo(STR_CANNOT_EMPTY, BIND_FAILD);
pUserManServer->RecordSQL(STR_CANNOT_EMPTY, BIND_FAILD);
return -1;
}
CStringA strInfo;
strInfo.Format("%s:IP(%s),Port:(%d)", STR_LISTEN.GetString(), chIP, pUserManServer->m_nPort);
pUserManServer->RecordOperaInfo(CString(strInfo), INFO);
pUserManServer->RecordSQL(CString(strInfo), INFO);
int nBindErr;
if ((nBindErr = soap_bind(&Soap_service, chIP, pUserManServer->m_nPort, 100)) < 0) {
strInfo = STR_BIND_FAILD;
pUserManServer->RecordOperaInfo(CString(strInfo), BIND_FAILD);
pUserManServer->RecordSQL(CString(strInfo), BIND_FAILD);
soap_print_fault(&Soap_service, stderr);
return -1;
}
strInfo = STR_STARTED;
pUserManServer->RecordOperaInfo(CString(strInfo), INFO);
pUserManServer->RecordSQL(CString(strInfo), INFO);
while (true) {
int nSlave = (int)soap_accept(&Soap_service);
if (nSlave < 0) {
strInfo = STR_CALL_FAILD;
pUserManServer->RecordOperaInfo(CString(strInfo), CALL_FAILD);
pUserManServer->RecordSQL(CString(strInfo), CALL_FAILD);
soap_print_fault(&Soap_service, stderr);
return -1;
}
soap_serve(&Soap_service);
}
soap_destroy(&Soap_service);
soap_end(&Soap_service);
return 0;
}
```