原文作者:http://www.cnblogs.com/lianyue
這一部分需要向大家介紹的是服務(wù)器的select以及收發(fā)包的具體流程,從核心代碼功能上分析網(wǎng)絡(luò)交互具體過程钝凶。
首先大家要看第二部分(part2 of net)的代碼結(jié)構(gòu)圖晕鹊,因為在接下來的流程過程中會用到其中模塊的名稱,若是不知道大致的功能那么接下來的解說可能就成為天書了茎杂。
總體流程為:服務(wù)器管理器初始化并創(chuàng)建主套接字連接,進(jìn)入主循環(huán)等待新連接(select),如果有新的連接則將新連接加入連接管理器玄呛。不管有沒有新的連接,循環(huán)會依次處理連接的異常->輸入流->輸出流->命令處理和二。其中異常即連接包發(fā)送錯誤的處理徘铝,輸入流即套接字輸入流中如果大小長度不為空則重新拼接包,輸出流進(jìn)行包的拼接惯吕,并將未發(fā)送的流進(jìn)行發(fā)送惕它,命令處理其實是對輸入流的處理,處理(handler)發(fā)送過來的包废登。
以下詳細(xì)說明這幾個流程從代碼上的實現(xiàn)淹魄,以及所在的模塊。
1堡距、 服務(wù)器管理器初始化(servermanager)
bool ServerManager::init() {
__ENTER_FUNCTION
serversocket_ =
new pap_server_common_net::Socket(g_config.billing_info_.port_);
Assert(serversocket_);
serversocket_->set_nonblocking();
socketid_ = serversocket_->getid();
Assert(socketid_ != SOCKET_INVALID);
FD_SET(socketid_, &readfds_[kSelectFull]);
FD_SET(socketid_, &exceptfds_[kSelectFull]);
minfd_ = maxfd_ = socketid_;
timeout_[kSelectFull].tv_sec = 0;
timeout_[kSelectFull].tv_usec = 0;
threadid_ = pap_common_sys::get_current_thread_id();
uint16_t i;
for (i = 0; i < OVER_SERVER_MAX; ++i) {
serverhash_[i] = ID_INVALID;
}
return true;
__LEAVE_FUNCTION
return false;
}
2甲锡、 服務(wù)器管理器進(jìn)入主循環(huán)(servermanager)
void ServerManager::loop() {
__ENTER_FUNCTION
while (isactive()) {
bool result = false;
try {
result = select();
Assert(result);
//ERRORPRINTF("select");
result = processexception();
Assert(result);
//ERRORPRINTF("processexception");
result = processinput();
Assert(result);
//ERRORPRINTF("processinput");
result = processoutput();
Assert(result);
//ERRORPRINTF("processoutput");
}
catch(...) {
}
try {
result = processcommand();
Assert(result);
//ERRORPRINTF("processcommand");
}
catch(...) {
}
try {
result = heartbeat();
Assert(result);
}
catch(...) {
}
}
__LEAVE_FUNCTION
}
3、 服務(wù)器線程進(jìn)入select模式
bool ServerManager::select() {
__ENTER_FUNCTION
timeout_[kSelectUse].tv_sec = timeout_[kSelectFull].tv_sec;
timeout_[kSelectUse].tv_usec = timeout_[kSelectFull].tv_usec;
readfds_[kSelectUse] = readfds_[kSelectFull];
writefds_[kSelectUse] = writefds_[kSelectFull];
exceptfds_[kSelectUse] = exceptfds_[kSelectFull];
pap_common_base::util::sleep(100);
int32_t result = SOCKET_ERROR;
try {
result = pap_common_net::socket::Base::select(
maxfd_ + 1,
&readfds_[kSelectUse],
&writefds_[kSelectUse],
&exceptfds_[kSelectUse],
&timeout_[kSelectUse]);
Assert(result != SOCKET_ERROR);
}
catch(...) {
g_log->fast_save_log(kBillingLogFile,
"ServerManager::select have error, result: %d",
result);
}
return true;
__LEAVE_FUNCTION
return false;
}
4羽戒、 服務(wù)器線程進(jìn)行異常處理
bool ServerManager::processexception() {
__ENTER_FUNCTION
if (SOCKET_INVALID == minfd_ && SOCKET_INVALID == maxfd_)
return true;
uint16_t connectioncount = billingconnection::Manager::getcount();
billingconnection::Server* serverconnection = NULL;
uint16_t i;
for (i = 0; i < connectioncount; ++i) {
if (ID_INVALID == connectionids_[i]) continue;
serverconnection = g_connectionpool->get(connectionids_[i]);
Assert(serverconnection);
int32_t socketid = serverconnection->getsocket()->getid();
if (socketid_ == socketid) {
Assert(false);
continue;
}
if (FD_ISSET(socketid, &exceptfds_[kSelectUse])) {
removeconnection(serverconnection);
}
}
return true;
__LEAVE_FUNCTION
return false;
}
5缤沦、 服務(wù)器線程進(jìn)行輸入流處理
bool ServerManager::processinput() {
__ENTER_FUNCTION
if (SOCKET_INVALID == minfd_ && SOCKET_INVALID == maxfd_)
return true; //no connection
uint16_t i;
if (FD_ISSET(socketid_, &readfds_[kSelectUse])) {
for (i = 0; i < kOneStepAccept; ++i) {
if (!accept_newconnection()) break;
}
}
uint16_t connectioncount = billingconnection::Manager::getcount();
for (i = 0; i < connectioncount; ++i) {
if (ID_INVALID == connectionids_[i]) continue;
billingconnection::Server* serverconnection = NULL;
serverconnection = g_connectionpool->get(connectionids_[i]);
Assert(serverconnection);
int32_t socketid = serverconnection->getsocket()->getid();
if (socketid_ == socketid) continue;
if (FD_ISSET(socketid, &readfds_[kSelectUse])) { //read information
if (serverconnection->getsocket()->iserror()) {
removeconnection(serverconnection);
}
else {
try {
if (!serverconnection->processinput())
removeconnection(serverconnection);
}
catch(...) {
removeconnection(serverconnection);
}
}
}
}
return true;
__LEAVE_FUNCTION
return false;
}
6、 服務(wù)器線程進(jìn)行輸出流處理
bool ServerManager::processoutput() {
__ENTER_FUNCTION
if (SOCKET_INVALID == maxfd_&& SOCKET_INVALID == minfd_)
return false;
uint16_t i;
uint16_t connectioncount = billingconnection::Manager::getcount();
for (i = 0; i < connectioncount; ++i) {
if (ID_INVALID == connectionids_[i]) continue;
billingconnection::Server* serverconnection = NULL;
serverconnection = g_connectionpool->get(connectionids_[i]);
Assert(serverconnection);
int32_t socketid = serverconnection->getsocket()->getid();
if (socketid_ == socketid) continue;
if (FD_ISSET(socketid, &writefds_[kSelectUse])) {
if (serverconnection->getsocket()->iserror()) {
removeconnection(serverconnection);
}
else {
try {
if (!serverconnection->processoutput())
removeconnection(serverconnection);
}
catch(...) {
removeconnection(serverconnection);
}
}
}
}
return true;
__LEAVE_FUNCTION
return false;
}
7易稠、 服務(wù)器線程進(jìn)行命令處理
bool ServerManager::processcommand() {
__ENTER_FUNCTION
if (SOCKET_INVALID == maxfd_&& SOCKET_INVALID == minfd_)
return false;
uint16_t i;
uint16_t connectioncount = billingconnection::Manager::getcount();
for (i = 0; i < connectioncount; ++i) {
if (ID_INVALID == connectionids_[i]) continue;
billingconnection::Server* serverconnection = NULL;
serverconnection = g_connectionpool->get(connectionids_[i]);
//serverconnection = &billing_serverconnection_;
Assert(serverconnection);
int32_t socketid = serverconnection->getsocket()->getid();
if (socketid_ == socketid) continue;
if (serverconnection->getsocket()->iserror()) {
removeconnection(serverconnection);
}
else { //connection is ok
try {
if (!serverconnection->processcommand(false))
removeconnection(serverconnection);
}
catch(...) {
removeconnection(serverconnection);
}
}
}
return true;
__LEAVE_FUNCTION
return false;
}
下一部分缸废,我將講解在網(wǎng)絡(luò)部分一些重要的代碼塊。