| 1 |
/*******************************************************/ |
/*******************************************************/ |
| 2 |
/* */ |
/* */ |
| 3 |
/* LeafOK Innd */ |
/* LeafOK Innbbsd */ |
| 4 |
/* Copyright (C) LeafOK.com, 2003-2004 */ |
/* Copyright (C) LeafOK.com, 2003-2004 */ |
| 5 |
/* */ |
/* */ |
| 6 |
/* Programmed by Leaf */ |
/* Programmed by Leaf */ |
| 7 |
/* E-mail:leaf@leafok.com QQ:6049044 */ |
/* E-mail:leaflet@leafok.com QQ:6049044 */ |
| 8 |
/* */ |
/* */ |
| 9 |
/* http://bbs.leafok.com */ |
/* http://bbs.leafok.com */ |
| 10 |
/* http://bbs.leafok.net */ |
/* http://bbs.leafok.net */ |
| 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); |
this->SetThreadPool(new thread_pool(MAX_CLIENT)); |
| 106 |
if (this->p_ThreadPool->GetLastError() != 0) |
if (this->p_ThreadPool->GetLastError() != 0) |
| 107 |
{ |
{ |
| 108 |
return -1; |
return -1; |
| 109 |
} |
} |
| 110 |
|
|
|
hThreadCurrent = GetCurrentThread(); |
|
|
|
|
| 111 |
if (this->s_connect(this->w_address,this->w_port) == 0) |
if (this->s_connect(this->w_address,this->w_port) == 0) |
| 112 |
{ |
{ |
| 113 |
this->Startup(); |
this->Startup(); |
| 127 |
|
|
| 128 |
this->running = true; |
this->running = true; |
| 129 |
|
|
| 130 |
this->GetThreadPool()->SetThreadStatus(hThreadCurrent,thread_pool::S_WAITING); |
this->GetParentThread()->GetThreadPool()->SetThreadStatus(GetCurrentThreadId(),thread_pool::S_WAITING); |
| 131 |
|
|
| 132 |
namelen = sizeof(sin); |
namelen = sizeof(sin); |
| 133 |
if ((p->s = accept(this->s,(LPSOCKADDR)&sin,&namelen)) == INVALID_SOCKET) |
if ((p->s = accept(this->s,(LPSOCKADDR)&sin,&namelen)) == INVALID_SOCKET) |
| 139 |
return -2; |
return -2; |
| 140 |
} |
} |
| 141 |
|
|
|
this->GetThreadPool()->SetThreadStatus(hThreadCurrent,thread_pool::S_WORKING); |
|
|
|
|
| 142 |
if (this->IsShutdown()) |
if (this->IsShutdown()) |
| 143 |
{ |
{ |
| 144 |
break; |
break; |
| 145 |
} |
} |
| 146 |
|
|
| 147 |
|
this->GetParentThread()->GetThreadPool()->SetThreadStatus(GetCurrentThreadId(),thread_pool::S_WORKING); |
| 148 |
|
|
| 149 |
strcpy(p->hostaddr,inet_ntoa(sin.sin_addr)); |
strcpy(p->hostaddr,inet_ntoa(sin.sin_addr)); |
| 150 |
p->port = sin.sin_port; |
p->port = sin.sin_port; |
| 151 |
p->isConnected = true; |
p->isConnected = true; |
| 152 |
|
|
| 153 |
// strcpy(p->w_conn_str,this->w_conn_str); |
// strcpy(p->w_conn_str,this->w_conn_str); |
| 154 |
if (this->>get_priv(p)!=0) |
|
| 155 |
if (this->get_priv(p)!=0) |
if (this->get_priv(p)!=0) |
| 156 |
{ |
{ |
| 157 |
p->s_send("502 Load access file failed.\r\n"); |
p->s_send("502 Load access file failed.\r\n"); |
| 159 |
continue; |
continue; |
| 160 |
} |
} |
| 161 |
|
|
| 162 |
if (!p->access.get) |
if (!p->check_priv()) |
| 163 |
{ |
{ |
| 164 |
p->s_send("502 You are not in my access file.\r\n"); |
p->s_send("502 You are not in my access file.\r\n"); |
| 165 |
delete(p); |
delete(p); |
| 170 |
if (this->GetThreadPool()->GetThreadCount() < MAX_CLIENT) |
if (this->GetThreadPool()->GetThreadCount() < MAX_CLIENT) |
| 171 |
{ |
{ |
| 172 |
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)) |
| 173 |
// base_passive::uThreadCount++; |
{ |
| 174 |
CloseHandle(hThreadCurrent); |
CloseHandle(hThreadCurrent); |
| 175 |
syslog << logfile::log_head << "Create accept thread ... OK" << endl; |
syslog << logfile::log_head << "Create accept thread ... OK" << endl; |
| 176 |
} |
} |
| 190 |
Sleep(1000*5); // Wait several seconds for all threads to exit |
Sleep(1000*5); // Wait several seconds for all threads to exit |
| 191 |
} |
} |
| 192 |
this->s_close(); |
this->s_close(); |
|
delete(this->p_ThreadPool); |
|
|
|
|
| 193 |
|
|
| 194 |
this->unload_priv(); |
this->unload_priv(); |
| 195 |
|
|
| 199 |
} |
} |
| 200 |
|
|
| 201 |
DWORD base_passive::AcceptThread(LPVOID pParam) |
DWORD base_passive::AcceptThread(LPVOID pParam) |
|
HANDLE hThreadCurrent; |
|
| 202 |
{ |
{ |
| 203 |
base_passive* p; |
base_passive* p; |
| 204 |
|
|
| 205 |
try |
try |
| 206 |
{ |
{ |
|
hThreadCurrent = GetCurrentThread(); |
|
| 207 |
p = (base_passive*)pParam; |
p = (base_passive*)pParam; |
| 208 |
if (p->GetParentThread()->GetThreadPool()->AddThread(hThreadCurrent, CLOCKS_PER_SEC * 30) != 0) |
|
| 209 |
|
p->ulMainThreadId = GetCurrentThreadId(); |
| 210 |
|
|
| 211 |
|
syslog << logfile::log_head << "Accept connection from " << p->hostaddr |
| 212 |
|
<< ":" << p->port << endl; |
| 213 |
|
|
| 214 |
if (p->GetParentThread()->GetThreadPool()->AddThread(GetCurrentThreadId(), CLOCKS_PER_SEC * 30) != 0) |
if (p->GetParentThread()->GetThreadPool()->AddThread(GetCurrentThreadId(), CLOCKS_PER_SEC * 30) != 0) |
| 215 |
{ |
{ |
| 216 |
syslog << logfile::log_head << "Register thread ... Failed" << endl; |
syslog << logfile::log_head << "Register thread ... Failed" << endl; |
| 217 |
p->GetParentThread()->GetThreadPool()->SetThreadStatus(hThreadCurrent,thread_pool::S_WORKING); |
} |
| 218 |
p->GetParentThread()->GetThreadPool()->SetThreadStatus(GetCurrentThreadId(),thread_pool::S_WORKING); |
p->GetParentThread()->GetThreadPool()->SetThreadStatus(GetCurrentThreadId(),thread_pool::S_WORKING); |
| 219 |
|
|
| 220 |
if (p->db_env_init() == 0) |
if (p->db_env_init() == 0) |
| 225 |
{ |
{ |
| 226 |
syslog << logfile::log_head << "DB_ENV_INIT failed" << endl; |
syslog << logfile::log_head << "DB_ENV_INIT failed" << endl; |
| 227 |
} |
} |
| 228 |
if (p->GetParentThread()->GetThreadPool()->RemoveThread(hThreadCurrent) != 0) |
|
| 229 |
if (p->GetParentThread()->GetThreadPool()->RemoveThread(GetCurrentThreadId()) != 0) |
if (p->GetParentThread()->GetThreadPool()->RemoveThread(GetCurrentThreadId()) != 0) |
| 230 |
{ |
{ |
| 231 |
syslog << logfile::log_head << "Unregister thread ... Failed" << endl; |
syslog << logfile::log_head << "Unregister thread ... Failed" << endl; |
| 252 |
//Default value |
//Default value |
| 253 |
strcpy(p->access.ip,p->hostaddr); |
strcpy(p->access.ip,p->hostaddr); |
| 254 |
p->access.control = false; |
p->access.control = false; |
| 255 |
p->access.get = true; |
p->access.get = false; |
| 256 |
p->access.post = false; |
p->access.post = false; |
| 257 |
p->access.ihave = false; |
p->access.ihave = false; |
| 258 |
|
|
| 356 |
this->work(); |
this->work(); |
| 357 |
return 0; |
return 0; |
| 358 |
} |
} |
| 359 |
|
|
| 360 |
|
bool base_passive::check_priv(void) |
| 361 |
|
{ |
| 362 |
|
return this->access.get; |
| 363 |
|
} |