Code cleanup

pull/2/head
Tatsuhiro Tsujikawa 2011-10-20 00:11:41 +09:00
parent 9b4855a5f0
commit 20e781c2df
1 changed files with 16 additions and 30 deletions

View File

@ -173,8 +173,7 @@ void SocketCore::create(int family, int protocol)
errNum = SOCKET_ERRNO; errNum = SOCKET_ERRNO;
if(fd == (sock_t) -1) { if(fd == (sock_t) -1) {
throw DL_ABORT_EX throw DL_ABORT_EX
(fmt("Failed to create socket. Cause:%s", (fmt("Failed to create socket. Cause:%s", errorMsg(errNum).c_str()));
errorMsg(errNum).c_str()));
} }
int sockopt = 1; int sockopt = 1;
if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
@ -182,13 +181,13 @@ void SocketCore::create(int family, int protocol)
errNum = SOCKET_ERRNO; errNum = SOCKET_ERRNO;
CLOSE(fd); CLOSE(fd);
throw DL_ABORT_EX throw DL_ABORT_EX
(fmt("Failed to create socket. Cause:%s", (fmt("Failed to create socket. Cause:%s", errorMsg(errNum).c_str()));
errorMsg(errNum).c_str()));
} }
sockfd_ = fd; sockfd_ = fd;
} }
static sock_t bindInternal(int family, int socktype, int protocol, static sock_t bindInternal
(int family, int socktype, int protocol,
const struct sockaddr* addr, socklen_t addrlen, const struct sockaddr* addr, socklen_t addrlen,
std::string& error) std::string& error)
{ {
@ -390,9 +389,7 @@ void SocketCore::establishConnection(const std::string& host, uint16_t port)
s = callGetaddrinfo(&res, host.c_str(), util::uitos(port).c_str(), s = callGetaddrinfo(&res, host.c_str(), util::uitos(port).c_str(),
protocolFamily_, sockType_, 0, 0); protocolFamily_, sockType_, 0, 0);
if(s) { if(s) {
throw DL_ABORT_EX(fmt(EX_RESOLVE_HOSTNAME, throw DL_ABORT_EX(fmt(EX_RESOLVE_HOSTNAME, host.c_str(), gai_strerror(s)));
host.c_str(),
gai_strerror(s)));
} }
WSAAPI_AUTO_DELETE<struct addrinfo*> resDeleter(res, freeaddrinfo); WSAAPI_AUTO_DELETE<struct addrinfo*> resDeleter(res, freeaddrinfo);
struct addrinfo* rp; struct addrinfo* rp;
@ -448,10 +445,7 @@ void SocketCore::establishConnection(const std::string& host, uint16_t port)
break; break;
} }
if(sockfd_ == (sock_t) -1) { if(sockfd_ == (sock_t) -1) {
throw DL_ABORT_EX throw DL_ABORT_EX(fmt(EX_SOCKET_CONNECT, host.c_str(), error.c_str()));
(fmt(EX_SOCKET_CONNECT,
host.c_str(),
error.c_str()));
} }
} }
@ -624,8 +618,7 @@ bool SocketCore::isWritable(time_t timeout)
return false; return false;
} else { } else {
throw DL_RETRY_EX throw DL_RETRY_EX
(fmt(EX_SOCKET_CHECK_WRITABLE, (fmt(EX_SOCKET_CHECK_WRITABLE, errorMsg(errNum).c_str()));
errorMsg(errNum).c_str()));
} }
} }
#endif // !HAVE_POLL #endif // !HAVE_POLL
@ -671,8 +664,7 @@ bool SocketCore::isReadable(time_t timeout)
return false; return false;
} else { } else {
throw DL_RETRY_EX throw DL_RETRY_EX
(fmt(EX_SOCKET_CHECK_READABLE, (fmt(EX_SOCKET_CHECK_READABLE, errorMsg(errNum).c_str()));
errorMsg(errNum).c_str()));
} }
} }
#endif // !HAVE_POLL #endif // !HAVE_POLL
@ -757,8 +749,8 @@ void SocketCore::readData(char* data, size_t& len)
wantWrite_ = false; wantWrite_ = false;
if(!secure_) { if(!secure_) {
while((ret = recv(sockfd_, data, len, 0)) == -1 && SOCKET_ERRNO == A2_EINTR); while((ret = recv(sockfd_, data, len, 0)) == -1 &&
SOCKET_ERRNO == A2_EINTR);
int errNum = SOCKET_ERRNO; int errNum = SOCKET_ERRNO;
if(ret == -1) { if(ret == -1) {
if(A2_WOULDBLOCK(errNum)) { if(A2_WOULDBLOCK(errNum)) {
@ -778,8 +770,7 @@ void SocketCore::readData(char* data, size_t& len)
} }
if(ret < 0) { if(ret < 0) {
throw DL_RETRY_EX throw DL_RETRY_EX
(fmt(EX_SOCKET_RECV, (fmt(EX_SOCKET_RECV, ERR_error_string(SSL_get_error(ssl, ret), 0)));
ERR_error_string(SSL_get_error(ssl, ret), 0)));
} }
#endif // HAVE_OPENSSL #endif // HAVE_OPENSSL
#ifdef HAVE_LIBGNUTLS #ifdef HAVE_LIBGNUTLS
@ -805,13 +796,11 @@ void SocketCore::prepareSecureConnection()
ssl = SSL_new(tlsContext_->getSSLCtx()); ssl = SSL_new(tlsContext_->getSSLCtx());
if(!ssl) { if(!ssl) {
throw DL_ABORT_EX throw DL_ABORT_EX
(fmt(EX_SSL_INIT_FAILURE, (fmt(EX_SSL_INIT_FAILURE, ERR_error_string(ERR_get_error(), 0)));
ERR_error_string(ERR_get_error(), 0)));
} }
if(SSL_set_fd(ssl, sockfd_) == 0) { if(SSL_set_fd(ssl, sockfd_) == 0) {
throw DL_ABORT_EX throw DL_ABORT_EX
(fmt(EX_SSL_INIT_FAILURE, (fmt(EX_SSL_INIT_FAILURE, ERR_error_string(ERR_get_error(), 0)));
ERR_error_string(ERR_get_error(), 0)));
} }
#endif // HAVE_OPENSSL #endif // HAVE_OPENSSL
#ifdef HAVE_LIBGNUTLS #ifdef HAVE_LIBGNUTLS
@ -848,7 +837,6 @@ bool SocketCore::initiateSecureConnection(const std::string& hostname)
switch(ssl_error) { switch(ssl_error) {
case SSL_ERROR_NONE: case SSL_ERROR_NONE:
break; break;
case SSL_ERROR_WANT_READ: case SSL_ERROR_WANT_READ:
wantRead_ = true; wantRead_ = true;
return false; return false;
@ -1077,8 +1065,7 @@ std::string SocketCore::getSocketError() const
(a2_sockopt_t) &error, &optlen) == -1) { (a2_sockopt_t) &error, &optlen) == -1) {
int errNum = SOCKET_ERRNO; int errNum = SOCKET_ERRNO;
throw DL_ABORT_EX throw DL_ABORT_EX
(fmt("Failed to get socket error: %s", (fmt("Failed to get socket error: %s", errorMsg(errNum).c_str()));
errorMsg(errNum).c_str()));
} }
if(error != 0) { if(error != 0) {
return errorMsg(error); return errorMsg(error);
@ -1179,7 +1166,6 @@ void getInterfaceAddress
// address is not for this machine. // address is not for this machine.
try { try {
SocketCore socket; SocketCore socket;
//socket.bind(&bindAddr.sa, bindAddrLen);
socket.bind(rp->ai_addr, rp->ai_addrlen); socket.bind(rp->ai_addr, rp->ai_addrlen);
sockaddr_union bindAddr; sockaddr_union bindAddr;
memset(&bindAddr, 0, sizeof(bindAddr)); memset(&bindAddr, 0, sizeof(bindAddr));