您的位置:首页 > 编程语言

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

2008-03-31 18:04 218 查看

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、非常丑陋。真正执行的功能代码淹没在众多异常处理代码中;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: