blocxx

SocketBaseImpl.hpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2005, Vintela, Inc. All rights reserved.
00003 * Copyright (C) 2006, Novell, Inc. All rights reserved.
00004 * 
00005 * Redistribution and use in source and binary forms, with or without
00006 * modification, are permitted provided that the following conditions are met:
00007 * 
00008 *     * Redistributions of source code must retain the above copyright notice,
00009 *       this list of conditions and the following disclaimer.
00010 *     * Redistributions in binary form must reproduce the above copyright
00011 *       notice, this list of conditions and the following disclaimer in the
00012 *       documentation and/or other materials provided with the distribution.
00013 *     * Neither the name of 
00014 *       Vintela, Inc., 
00015 *       nor Novell, Inc., 
00016 *       nor the names of its contributors or employees may be used to 
00017 *       endorse or promote products derived from this software without 
00018 *       specific prior written permission.
00019 * 
00020 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00021 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00022 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00023 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00024 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00025 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00026 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00027 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00028 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00029 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00030 * POSSIBILITY OF SUCH DAMAGE.
00031 *******************************************************************************/
00032 
00033 
00042 #ifndef BLOCXX_SOCKETBASEIMPL_HPP_INCLUDE_GUARD_
00043 #define BLOCXX_SOCKETBASEIMPL_HPP_INCLUDE_GUARD_
00044 #include "blocxx/BLOCXX_config.h"
00045 #include "blocxx/SelectableIFC.hpp"
00046 #include "blocxx/IntrusiveReference.hpp"
00047 #include "blocxx/SocketException.hpp"
00048 #include "blocxx/String.hpp"
00049 #include "blocxx/SocketAddress.hpp"
00050 #include "blocxx/Types.hpp"
00051 #include "blocxx/SocketStreamBuffer.hpp"
00052 #include "blocxx/IOIFC.hpp"
00053 #include "blocxx/Timeout.hpp"
00054 
00055 #if defined(BLOCXX_HAVE_ISTREAM) && defined(BLOCXX_HAVE_OSTREAM)
00056 #include <istream>
00057 #include <ostream>
00058 #else
00059 #include <iostream>
00060 #endif
00061 
00062 // The classes and functions defined in this file are not meant for general
00063 // use, they are internal implementation details.  They may change at any time.
00064 
00065 namespace BLOCXX_NAMESPACE
00066 {
00067 
00068 class BLOCXX_COMMON_API SocketBaseImpl : public SelectableIFC, public IOIFC
00069 {
00070 public:
00071    SocketBaseImpl();
00072    SocketBaseImpl(SocketHandle_t fd, SocketAddress::AddressType addrType);
00073    SocketBaseImpl(const SocketAddress& addr);
00074    virtual ~SocketBaseImpl();
00075    virtual void connect(const SocketAddress& addr);
00076    virtual void disconnect();
00077    void setReceiveTimeout(const Timeout& timeout) { m_recvTimeout = timeout; }
00078    Timeout getReceiveTimeout() const { return m_recvTimeout; }
00079    void setSendTimeout(const Timeout& timeout) { m_sendTimeout = timeout; }
00080    Timeout getSendTimeout() const { return m_sendTimeout; }
00081    void setConnectTimeout(const Timeout& timeout) { m_connectTimeout = timeout; }
00082    Timeout getConnectTimeout() const { return m_connectTimeout; }
00083    void setTimeouts(const Timeout& timeout) { m_recvTimeout = m_sendTimeout = m_connectTimeout = timeout; }
00084    bool receiveTimeOutExpired() const { return m_recvTimeoutExprd; }
00085    int write(const void* dataOut, int dataOutLen,
00086          ErrorAction errorAsException = E_RETURN_ON_ERROR);
00087    int read(void* dataIn, int dataInLen,
00088          ErrorAction errorAsException = E_RETURN_ON_ERROR);
00089    virtual bool waitForInput(const Timeout& timeout);
00090    bool waitForOutput(const Timeout& timeout);
00091    std::istream& getInputStream();
00092    std::ostream& getOutputStream();
00093    std::iostream& getIOStream();
00094    SocketAddress getLocalAddress() const { return m_localAddress; }
00095    SocketAddress getPeerAddress() const { return m_peerAddress; }
00096    SocketHandle_t getfd() const { return m_sockfd; }
00097    Select_t getSelectObj() const;
00098    bool isConnected() const { return m_isConnected; }
00099    static void setDumpFiles(const String& in, const String& out);
00100 protected:
00101    virtual int readAux(void* dataIn, int dataInLen) = 0;
00102    virtual int writeAux(const void* dataOut, int dataOutLen) = 0;
00103 
00104    bool m_isConnected;
00105    SocketHandle_t m_sockfd;
00106    SocketAddress m_localAddress;
00107    SocketAddress m_peerAddress;
00108 #if defined(BLOCXX_WIN32)
00109    HANDLE m_event;
00110 #endif
00111 
00112 private:
00113    void fillInetAddrParms();
00114 #if !defined(BLOCXX_WIN32)
00115    void fillUnixAddrParms();
00116 #endif
00117    SocketBaseImpl(const SocketBaseImpl& arg);
00118    SocketBaseImpl& operator= (const SocketBaseImpl& arg);
00119 #if defined(BLOCXX_WIN32)
00120    static int waitForEvent(HANDLE event, int secsToTimeout=-1);
00121 #endif
00122 
00123    bool m_recvTimeoutExprd;
00124    SocketStreamBuffer m_streamBuf;
00125    std::istream m_in;
00126    std::ostream m_out;
00127    std::iostream m_inout;
00128    Timeout m_recvTimeout;
00129    Timeout m_sendTimeout;
00130    Timeout m_connectTimeout;
00131    
00132    static String m_traceFileOut;
00133    static String m_traceFileIn;
00134 };
00135 BLOCXX_EXPORT_TEMPLATE(BLOCXX_COMMON_API, IntrusiveReference, SocketBaseImpl);
00136 
00137 } // end namespace BLOCXX_NAMESPACE
00138 
00139 #endif