一段给人两种不同感觉的代码

bool  
XmlRpcServer::bindAndListen(
int  port,  int  backlog  /*= 5*/ )
{
  
int fd = XmlRpcSocket::socket();
  
if (fd < 0)
  
{
    XmlRpcUtil::error(
"XmlRpcServer::bindAndListen: Could not create socket (%s).", XmlRpcSocket::getErrorMsg().c_str());
    
return false;
  }


  
this->setfd(fd);

  
// Don't block on reads/writes
  if ( ! XmlRpcSocket::setNonBlocking(fd))
  
{
    
this->close();
    XmlRpcUtil::error(
"XmlRpcServer::bindAndListen: Could not set socket to non-blocking input mode (%s).", XmlRpcSocket::getErrorMsg().c_str());
    
return false;
  }


  
// Allow this port to be re-bound immediately so server re-starts are not delayed
  if ( ! XmlRpcSocket::setReuseAddr(fd))
  
{
    
this->close();
    XmlRpcUtil::error(
"XmlRpcServer::bindAndListen: Could not set SO_REUSEADDR socket option (%s).", XmlRpcSocket::getErrorMsg().c_str());
    
return false;
  }


  
// Bind to the specified port on the default interface
  if ( ! XmlRpcSocket::bind(fd, port))
  
{
    
this->close();
    XmlRpcUtil::error(
"XmlRpcServer::bindAndListen: Could not bind to specified port (%s).", XmlRpcSocket::getErrorMsg().c_str());
    
return false;
  }


  
// Set in listening mode
  if ( ! XmlRpcSocket::listen(fd, backlog))
  
{
    
this->close();
    XmlRpcUtil::error(
"XmlRpcServer::bindAndListen: Could not set socket in listening mode (%s).", XmlRpcSocket::getErrorMsg().c_str());
    
return false;
  }


  XmlRpcUtil::log(
2"XmlRpcServer::bindAndListen: server listening on port %d fd %d", port, fd);

  
// Notify the dispatcher to listen on this source when we are in work()
  _disp.addSource(this, XmlRpcDispatch::ReadableEvent);

  
return true;
}

 

这是XML RPC++ 里面的一段实现端口绑定与监听的代码。

两种完全不同的感觉:

1、非常完美,考虑非常完善,每一步都考虑了执行结果是否正确,根据每一步的执行结果来决定下一步的操作;

2、非常丑陋。真正执行的功能代码淹没在众多异常处理代码中;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值