TCPClient.cc

Go to the documentation of this file.
00001 /*
00002  * IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. By
00003  * downloading, copying, installing or using the software you agree to
00004  * this license. If you do not agree to this license, do not download,
00005  * install, copy or use the software.
00006  * 
00007  * Intel Open Source License 
00008  * 
00009  * Copyright (c) 2004 Intel Corporation. All rights reserved. 
00010  * 
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions are
00013  * met:
00014  * 
00015  *   Redistributions of source code must retain the above copyright
00016  *   notice, this list of conditions and the following disclaimer.
00017  * 
00018  *   Redistributions in binary form must reproduce the above copyright
00019  *   notice, this list of conditions and the following disclaimer in the
00020  *   documentation and/or other materials provided with the distribution.
00021  * 
00022  *   Neither the name of the Intel Corporation nor the names of its
00023  *   contributors may be used to endorse or promote products derived from
00024  *   this software without specific prior written permission.
00025  *  
00026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00027  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00028  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00029  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR
00030  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00031  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00032  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00033  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00034  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00035  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00036  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037  */
00038 
00039 #include <stdlib.h>
00040 #include <sys/poll.h>
00041 
00042 #include "TCPClient.h"
00043 #include "NetUtils.h"
00044 #include "debug/DebugUtils.h"
00045 #include "debug/Log.h"
00046 
00047 namespace oasys {
00048 
00049 TCPClient::TCPClient(const char* logbase, bool init_socket_immediately)
00050     : IPClient(SOCK_STREAM, logbase)
00051 {
00052     if (init_socket_immediately) {
00053         init_socket();
00054         ASSERT(fd_ != -1);
00055     }
00056 }
00057 
00058 TCPClient::TCPClient(int fd, in_addr_t remote_addr, u_int16_t remote_port,
00059                      const char* logbase)
00060     : IPClient(SOCK_STREAM, fd, remote_addr, remote_port, logbase)
00061 {
00062 }
00063 
00064 int
00065 TCPClient::timeout_connect(in_addr_t remote_addr, u_int16_t remote_port,
00066                            int timeout_ms, int* errp)
00067 {
00068     int ret, err;
00069     socklen_t len = sizeof(err);
00070 
00071     if (fd_ == -1) init_socket();
00072 
00073     if (IO::set_nonblocking(fd_, true, logpath_) < 0) {
00074         log_err("error setting fd %d to nonblocking: %s",
00075                 fd_, strerror(errno));
00076         if (errp) *errp = errno;
00077         return IOERROR;
00078     }
00079     
00080     ret = IPSocket::connect(remote_addr, remote_port);
00081     
00082     if (ret == 0)
00083     {
00084         log_debug("timeout_connect: succeeded immediately");
00085         if (errp) *errp = 0;
00086         ASSERT(state_ == ESTABLISHED); // set by IPSocket
00087     }
00088     else if (ret < 0 && errno != EINPROGRESS)
00089     {
00090         log_err("timeout_connect: error from connect: %s",
00091                 strerror(errno));
00092         if (errp) *errp = errno;
00093         ret = IOERROR;
00094     }
00095     else
00096     {
00097         ASSERT(errno == EINPROGRESS);
00098         log_debug("EINPROGRESS from connect(), calling poll()");
00099         ret = IO::poll_single(fd_, POLLOUT, NULL, timeout_ms, 
00100                               get_notifier(), logpath_);
00101         
00102         if (ret == IOTIMEOUT)
00103         {
00104             log_debug("timeout_connect: poll timeout");
00105         }
00106         else if (ret < 0) 
00107         {
00108             log_err("error in poll(): %s", strerror(errno));
00109             if (errp) *errp = errno;
00110             ret = IOERROR;
00111         }
00112         else
00113         {
00114             ASSERT(ret == 1);
00115             // call getsockopt() to see if connect succeeded
00116             ret = getsockopt(fd_, SOL_SOCKET, SO_ERROR, &err, &len);
00117             ASSERT(ret == 0);
00118             if (err == 0) {
00119                 log_debug("return from poll, connect succeeded");
00120                 ret = 0;
00121                 set_state(ESTABLISHED);
00122             } else {
00123                 log_debug("return from poll, connect failed");
00124                 ret = IOERROR;
00125             }
00126         }
00127     }
00128 
00129     // always make sure to set the fd back to blocking
00130     if (IO::set_nonblocking(fd_, false, logpath_) < 0) {
00131         log_err("error setting fd %d back to blocking: %s",
00132                 fd_, strerror(errno));
00133         if (errp) *errp = errno;
00134         return IOERROR;
00135     }
00136     
00137     monitor(IO::CONNECT, 0); // XXX/bowei
00138 
00139     return ret;
00140 }
00141 
00142 } // namespace oasys

Generated on Fri Dec 22 14:48:01 2006 for DTN Reference Implementation by  doxygen 1.5.1