| 18 |
|
|
| 19 |
using namespace std; |
using namespace std; |
| 20 |
|
|
| 21 |
UINT base_passive::uThreadCount = 0; |
// UINT base_passive::uThreadCount = 0; |
| 22 |
|
|
| 23 |
base_passive::base_passive(void) |
base_passive::base_passive(void) |
|
: p_ParentThread(NULL) |
|
| 24 |
{ |
{ |
| 25 |
} |
} |
| 26 |
|
|
| 102 |
if (this->load_priv() != 0) |
if (this->load_priv() != 0) |
| 103 |
return -1; |
return -1; |
| 104 |
|
|
| 105 |
|
this->p_ThreadPool = new thread_pool(MAX_CLIENT); |
| 106 |
|
if (this->p_ThreadPool->GetLastError() != 0) |
| 107 |
|
{ |
| 108 |
|
return -1; |
| 109 |
|
} |
| 110 |
|
|
| 111 |
|
hThreadCurrent = GetCurrentThread(); |
| 112 |
|
|
| 113 |
if (this->s_connect(this->w_address,this->w_port) == 0) |
if (this->s_connect(this->w_address,this->w_port) == 0) |
| 114 |
{ |
{ |
| 115 |
this->Startup(); |
this->Startup(); |
| 124 |
continue; |
continue; |
| 125 |
} |
} |
| 126 |
|
|
| 127 |
p->p_ParentThread = this; |
p->SetParentThread(this); |
| 128 |
p->configure(this->innd_id,this->innd_name,this->innd_server,this->innd_uid,this->w_address,this->w_port,this->w_conn_str); |
p->configure(this->innd_id,this->innd_name,this->innd_server,this->innd_uid,this->w_address,this->w_port,this->w_conn_str); |
| 129 |
|
|
| 130 |
this->running = true; |
this->running = true; |
| 131 |
|
|
| 132 |
|
this->GetThreadPool()->SetThreadStatus(hThreadCurrent,thread_pool::S_WAITING); |
| 133 |
|
|
| 134 |
namelen = sizeof(sin); |
namelen = sizeof(sin); |
| 135 |
if ((p->s = accept(this->s,(LPSOCKADDR)&sin,&namelen)) == INVALID_SOCKET) |
if ((p->s = accept(this->s,(LPSOCKADDR)&sin,&namelen)) == INVALID_SOCKET) |
| 136 |
{ |
{ |
| 141 |
return -2; |
return -2; |
| 142 |
} |
} |
| 143 |
|
|
| 144 |
|
this->GetThreadPool()->SetThreadStatus(hThreadCurrent,thread_pool::S_WORKING); |
| 145 |
|
|
| 146 |
if (this->IsShutdown()) |
if (this->IsShutdown()) |
| 147 |
{ |
{ |
| 148 |
break; |
break; |
| 168 |
delete(p); |
delete(p); |
| 169 |
continue; |
continue; |
| 170 |
} |
} |
| 171 |
if (base_passive::uThreadCount < MAX_CLIENT) |
|
| 172 |
|
// if (base_passive::uThreadCount < MAX_CLIENT) |
| 173 |
if (this->GetThreadPool()->GetThreadCount() < MAX_CLIENT) |
if (this->GetThreadPool()->GetThreadCount() < MAX_CLIENT) |
| 174 |
{ |
{ |
| 175 |
if (hThreadCurrent = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)base_passive::AcceptThread,(LPVOID)p,0,&ulThreadId)) |
if (hThreadCurrent = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)base_passive::AcceptThread,(LPVOID)p,0,&ulThreadId)) |
| 176 |
base_passive::uThreadCount++; |
// base_passive::uThreadCount++; |
| 177 |
CloseHandle(hThreadCurrent); |
CloseHandle(hThreadCurrent); |
| 178 |
syslog << logfile::log_head << "Create accept thread ... OK" << endl; |
syslog << logfile::log_head << "Create accept thread ... OK" << endl; |
| 179 |
} |
} |
| 193 |
Sleep(1000*5); // Wait several seconds for all threads to exit |
Sleep(1000*5); // Wait several seconds for all threads to exit |
| 194 |
} |
} |
| 195 |
this->s_close(); |
this->s_close(); |
| 196 |
|
delete(this->p_ThreadPool); |
| 197 |
|
|
| 198 |
|
|
| 199 |
this->unload_priv(); |
this->unload_priv(); |
| 200 |
|
|
| 204 |
} |
} |
| 205 |
|
|
| 206 |
DWORD base_passive::AcceptThread(LPVOID pParam) |
DWORD base_passive::AcceptThread(LPVOID pParam) |
| 207 |
|
HANDLE hThreadCurrent; |
| 208 |
{ |
{ |
| 209 |
base_passive* p; |
base_passive* p; |
| 210 |
|
|
| 211 |
try |
try |
| 212 |
{ |
{ |
| 213 |
|
hThreadCurrent = GetCurrentThread(); |
| 214 |
|
p = (base_passive*)pParam; |
| 215 |
|
if (p->GetParentThread()->GetThreadPool()->AddThread(hThreadCurrent, CLOCKS_PER_SEC * 30) != 0) |
| 216 |
|
if (p->GetParentThread()->GetThreadPool()->AddThread(GetCurrentThreadId(), CLOCKS_PER_SEC * 30) != 0) |
| 217 |
|
{ |
| 218 |
|
syslog << logfile::log_head << "Register thread ... Failed" << endl; |
| 219 |
|
p->GetParentThread()->GetThreadPool()->SetThreadStatus(hThreadCurrent,thread_pool::S_WORKING); |
| 220 |
|
p->GetParentThread()->GetThreadPool()->SetThreadStatus(GetCurrentThreadId(),thread_pool::S_WORKING); |
| 221 |
|
|
| 222 |
if (p->db_env_init() == 0) |
if (p->db_env_init() == 0) |
| 223 |
{ |
{ |
| 226 |
else |
else |
| 227 |
{ |
{ |
| 228 |
syslog << logfile::log_head << "DB_ENV_INIT failed" << endl; |
syslog << logfile::log_head << "DB_ENV_INIT failed" << endl; |
| 229 |
|
} |
| 230 |
|
if (p->GetParentThread()->GetThreadPool()->RemoveThread(hThreadCurrent) != 0) |
| 231 |
|
if (p->GetParentThread()->GetThreadPool()->RemoveThread(GetCurrentThreadId()) != 0) |
| 232 |
|
{ |
| 233 |
|
syslog << logfile::log_head << "Unregister thread ... Failed" << endl; |
| 234 |
|
} |
| 235 |
|
// base_passive::uThreadCount--; |
| 236 |
|
|
|
base_passive::uThreadCount--; |
|
| 237 |
delete(p); |
delete(p); |
| 238 |
syslog << logfile::log_head << "Delete accept thread ... OK" << endl; |
syslog << logfile::log_head << "Delete accept thread ... OK" << endl; |
| 239 |
} |
} |