| 1 |
/*******************************************************/ |
/*******************************************************/ |
| 2 |
/* */ |
/* */ |
| 3 |
/* LeafOK Innd */ |
/* LeafOK Innbbsd */ |
|
/* Copyright (C) LeafOK.com, 2003-2004 */ |
|
| 4 |
/* */ |
/* */ |
| 5 |
/* Programmed by Leaf */ |
/* Copyright (C) LeafOK.com, 2003-2008 */ |
|
/* E-mail:leaf@leafok.com QQ:6049044 */ |
|
| 6 |
/* */ |
/* */ |
| 7 |
/* http://bbs.leafok.com */ |
/* http://www.leafok.com */ |
|
/* http://bbs.leafok.net */ |
|
|
/* http://bbs.fenglin.info */ |
|
| 8 |
/* */ |
/* */ |
| 9 |
/*******************************************************/ |
/*******************************************************/ |
| 10 |
|
|
| 14 |
|
|
| 15 |
using namespace std; |
using namespace std; |
| 16 |
|
|
| 17 |
UINT base_passive::uThreadCount = 0; |
// UINT base_passive::uThreadCount = 0; |
| 18 |
|
|
| 19 |
base_passive::base_passive(void) |
base_passive::base_passive(void) |
|
: p_ParentThread(NULL) |
|
| 20 |
{ |
{ |
| 21 |
} |
} |
| 22 |
|
|
| 98 |
if (this->load_priv() != 0) |
if (this->load_priv() != 0) |
| 99 |
return -1; |
return -1; |
| 100 |
|
|
| 101 |
|
this->SetThreadPool(new thread_pool(MAX_CLIENT)); |
| 102 |
|
if (this->p_ThreadPool->GetLastError() != 0) |
| 103 |
|
{ |
| 104 |
|
return -1; |
| 105 |
|
} |
| 106 |
|
|
| 107 |
if (this->s_connect(this->w_address,this->w_port) == 0) |
if (this->s_connect(this->w_address,this->w_port) == 0) |
| 108 |
{ |
{ |
| 109 |
this->Startup(); |
this->Startup(); |
| 118 |
continue; |
continue; |
| 119 |
} |
} |
| 120 |
|
|
| 121 |
p->p_ParentThread = this; |
p->SetParentThread(this); |
| 122 |
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); |
| 123 |
|
|
| 124 |
this->running = true; |
this->running = true; |
| 125 |
|
|
| 126 |
|
this->GetParentThread()->GetThreadPool()->SetThreadStatus(GetCurrentThreadId(),thread_pool::S_WAITING); |
| 127 |
|
|
| 128 |
namelen = sizeof(sin); |
namelen = sizeof(sin); |
| 129 |
if ((p->s = accept(this->s,(LPSOCKADDR)&sin,&namelen)) == INVALID_SOCKET) |
if ((p->s = accept(this->s,(LPSOCKADDR)&sin,&namelen)) == INVALID_SOCKET) |
| 130 |
{ |
{ |
| 140 |
break; |
break; |
| 141 |
} |
} |
| 142 |
|
|
| 143 |
|
this->GetParentThread()->GetThreadPool()->SetThreadStatus(GetCurrentThreadId(),thread_pool::S_WORKING); |
| 144 |
|
|
| 145 |
strcpy(p->hostaddr,inet_ntoa(sin.sin_addr)); |
strcpy(p->hostaddr,inet_ntoa(sin.sin_addr)); |
| 146 |
p->port = sin.sin_port; |
p->port = sin.sin_port; |
| 147 |
p->isConnected = true; |
p->isConnected = true; |
| 148 |
|
|
| 149 |
// strcpy(p->w_conn_str,this->w_conn_str); |
// strcpy(p->w_conn_str,this->w_conn_str); |
| 150 |
if (this->>get_priv(p)!=0) |
|
| 151 |
if (this->get_priv(p)!=0) |
if (this->get_priv(p)!=0) |
| 152 |
{ |
{ |
| 153 |
p->s_send("502 Load access file failed.\r\n"); |
p->s_send("502 Load access file failed.\r\n"); |
| 155 |
continue; |
continue; |
| 156 |
} |
} |
| 157 |
|
|
| 158 |
if (!p->access.get) |
if (!p->check_priv()) |
| 159 |
{ |
{ |
| 160 |
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"); |
| 161 |
delete(p); |
delete(p); |
| 162 |
continue; |
continue; |
| 163 |
} |
} |
| 164 |
if (base_passive::uThreadCount < MAX_THREAD) |
|
| 165 |
|
// if (base_passive::uThreadCount < MAX_CLIENT) |
| 166 |
if (this->GetThreadPool()->GetThreadCount() < MAX_CLIENT) |
if (this->GetThreadPool()->GetThreadCount() < MAX_CLIENT) |
| 167 |
{ |
{ |
| 168 |
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)) |
| 169 |
base_passive::uThreadCount++; |
{ |
| 170 |
CloseHandle(hThreadCurrent); |
CloseHandle(hThreadCurrent); |
| 171 |
syslog << logfile::log_head << "Create accept thread ... OK" << endl; |
syslog << logfile::log_head << "Create accept thread ... OK" << endl; |
| 172 |
} |
} |
| 200 |
|
|
| 201 |
try |
try |
| 202 |
{ |
{ |
| 203 |
|
p = (base_passive*)pParam; |
| 204 |
|
|
| 205 |
|
p->ulMainThreadId = GetCurrentThreadId(); |
| 206 |
|
|
| 207 |
|
syslog << logfile::log_head << "Accept connection from " << p->hostaddr |
| 208 |
|
<< ":" << p->port << endl; |
| 209 |
|
|
| 210 |
|
if (p->GetParentThread()->GetThreadPool()->AddThread(GetCurrentThreadId(), CLOCKS_PER_SEC * 30) != 0) |
| 211 |
|
{ |
| 212 |
|
syslog << logfile::log_head << "Register thread ... Failed" << endl; |
| 213 |
|
} |
| 214 |
|
p->GetParentThread()->GetThreadPool()->SetThreadStatus(GetCurrentThreadId(),thread_pool::S_WORKING); |
| 215 |
|
|
| 216 |
if (p->db_env_init() == 0) |
if (p->db_env_init() == 0) |
| 217 |
{ |
{ |
| 220 |
else |
else |
| 221 |
{ |
{ |
| 222 |
syslog << logfile::log_head << "DB_ENV_INIT failed" << endl; |
syslog << logfile::log_head << "DB_ENV_INIT failed" << endl; |
| 223 |
|
} |
| 224 |
|
|
| 225 |
|
if (p->GetParentThread()->GetThreadPool()->RemoveThread(GetCurrentThreadId()) != 0) |
| 226 |
|
{ |
| 227 |
|
syslog << logfile::log_head << "Unregister thread ... Failed" << endl; |
| 228 |
|
} |
| 229 |
|
// base_passive::uThreadCount--; |
| 230 |
|
|
|
base_passive::uThreadCount--; |
|
| 231 |
delete(p); |
delete(p); |
| 232 |
syslog << logfile::log_head << "Delete accept thread ... OK" << endl; |
syslog << logfile::log_head << "Delete accept thread ... OK" << endl; |
| 233 |
} |
} |
| 234 |
catch(CException* e) |
catch(CException* e) |
| 235 |
{ |
{ |
| 236 |
syslog << logfile::log_head << "Error in AcceptThread()" << endl; |
char strErrMsg[1024]; |
| 237 |
|
e->GetErrorMessage(strErrMsg,1024); |
| 238 |
|
syslog << logfile::log_head << "Error in AcceptThread() [" << strErrMsg << "]" << endl; |
| 239 |
e->Delete(); |
e->Delete(); |
| 240 |
return -1; |
return -1; |
| 241 |
} |
} |
| 250 |
//Default value |
//Default value |
| 251 |
strcpy(p->access.ip,p->hostaddr); |
strcpy(p->access.ip,p->hostaddr); |
| 252 |
p->access.control = false; |
p->access.control = false; |
| 253 |
p->access.get = true; |
p->access.get = false; |
| 254 |
p->access.post = false; |
p->access.post = false; |
| 255 |
p->access.ihave = false; |
p->access.ihave = false; |
| 256 |
|
|
| 266 |
} |
} |
| 267 |
catch(CException* e) |
catch(CException* e) |
| 268 |
{ |
{ |
| 269 |
syslog << logfile::log_head << "Error in get_priv()" << endl; |
char strErrMsg[1024]; |
| 270 |
|
e->GetErrorMessage(strErrMsg,1024); |
| 271 |
|
syslog << logfile::log_head << "Error in get_priv() [" << strErrMsg << "]" << endl; |
| 272 |
e->Delete(); |
e->Delete(); |
| 273 |
return -1; |
return -1; |
| 274 |
} |
} |
| 320 |
} |
} |
| 321 |
catch(CException* e) |
catch(CException* e) |
| 322 |
{ |
{ |
| 323 |
syslog << logfile::log_head << "Error in load_priv()" << endl; |
char strErrMsg[1024]; |
| 324 |
|
e->GetErrorMessage(strErrMsg,1024); |
| 325 |
|
syslog << logfile::log_head << "Error in load_priv() [" << strErrMsg << "]" << endl; |
| 326 |
e->Delete(); |
e->Delete(); |
| 327 |
return -1; |
return -1; |
| 328 |
} |
} |
| 345 |
} |
} |
| 346 |
catch(CException* e) |
catch(CException* e) |
| 347 |
{ |
{ |
| 348 |
syslog << logfile::log_head << "Error in unload_priv()" << endl; |
char strErrMsg[1024]; |
| 349 |
|
e->GetErrorMessage(strErrMsg,1024); |
| 350 |
|
syslog << logfile::log_head << "Error in unload_priv() [" << strErrMsg << "]" << endl; |
| 351 |
e->Delete(); |
e->Delete(); |
| 352 |
return -1; |
return -1; |
| 353 |
} |
} |
| 360 |
this->work(); |
this->work(); |
| 361 |
return 0; |
return 0; |
| 362 |
} |
} |
| 363 |
|
|
| 364 |
|
bool base_passive::check_priv(void) |
| 365 |
|
{ |
| 366 |
|
return this->access.get; |
| 367 |
|
} |