MMORPG大型游戏设计与开发(part3 of net)

原文作者:http://www.cnblogs.com/lianyue

这一部分需要向大家介绍的是服务器的select以及收发包的具体流程,从核心代码功能上分析网络交互具体过程。

首先大家要看第二部分(part2 of net)的代码结构图,因为在接下来的流程过程中会用到其中模块的名称,若是不知道大致的功能那么接下来的解说可能就成为天书了。

总体流程为:服务器管理器初始化并创建主套接字连接,进入主循环等待新连接(select),如果有新的连接则将新连接加入连接管理器。不管有没有新的连接,循环会依次处理连接的异常->输入流->输出流->命令处理。其中异常即连接包发送错误的处理,输入流即套接字输入流中如果大小长度不为空则重新拼接包,输出流进行包的拼接,并将未发送的流进行发送,命令处理其实是对输入流的处理,处理(handler)发送过来的包。

以下详细说明这几个流程从代码上的实现,以及所在的模块。

1、 服务器管理器初始化(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、 服务器管理器进入主循环(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、 服务器线程进入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、 服务器线程进行异常处理
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、 服务器线程进行输入流处理
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、 服务器线程进行输出流处理
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、 服务器线程进行命令处理
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;
}

下一部分,我将讲解在网络部分一些重要的代码块。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、OC中创建线程的方法是什么?如果指定在主线程中执行代码?如何延时执行代码。【难度系数★★】 1)创建线程的方法...
    木旁_G阅读 1,985评论 2 16
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,859评论 18 139
  • 之前看到这样一段对话: 问:如何提高生活质量?答:定期扔东西! 貌似玩笑话,细想却有几分道理。 很多人的焦虑和平庸...
    青儿姑娘阅读 155评论 0 0
  • 在js中我们常常会运用到时间 在配合正则表达式的情况下,多多少少都会很方便。废话不多说 代码如下: var day...
    凌熙溪阅读 189评论 0 0
  • 你一旦认清自己的真相,便会明白,没有人能剥夺你的爱,爱永远和你在一起。它虽然无形无相,但永远相随,须臾也未曾离开,...
    闫宇sofia阅读 446评论 0 0