Connection.hxx
上传用户:sy_wanhua
上传日期:2013-07-25
资源大小:3048k
文件大小:7k
- #ifndef Connection_hxx
- #define Connection_hxx
- /* ====================================================================
- * The Vovida Software License, Version 1.0
- *
- * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- *
- * 3. The names "VOCAL", "Vovida Open Communication Application Library",
- * and "Vovida Open Communication Application Library (VOCAL)" must
- * not be used to endorse or promote products derived from this
- * software without prior written permission. For written
- * permission, please contact vocal@vovida.org.
- *
- * 4. Products derived from this software may not be called "VOCAL", nor
- * may "VOCAL" appear in their name, without prior written
- * permission of Vovida Networks, Inc.
- *
- * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
- * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
- * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
- * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
- * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
- * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
- * DAMAGE.
- *
- * ====================================================================
- *
- * This software consists of voluntary contributions made by Vovida
- * Networks, Inc. and many individuals on behalf of Vovida Networks,
- * Inc. For more information on Vovida Networks, Inc., please see
- * <http://www.vovida.org/>.
- *
- */
- static const char* const ConnectionHeaderVersion =
- "$Id: Connection.hxx,v 1.1 2001/03/30 02:49:23 icahoon Exp $";
- #include "vin.h"
- #include "global.h"
- #include <string.h>
- #include <string>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <sys/uio.h>
- #if defined(__svr4__) || defined(__SUNPRO_CC)
- #include <netinet/in.h>
- #include <arpa/nameser.h>
- #include <resolv.h>
- #include <xil/xil.h>
- #if (XIL_API_MINOR_VERSION - 0 == 3)
- typedef int socklen_t;
- #endif
- #endif
- #include "VNetworkException.hxx"
- //User define class
- class TcpServerSocket;
- class TcpClientSocket;
- typedef struct sockaddr SA;
- #define MAXLINE 256
- class Connection
- {
- public:
- Connection(bool blocking = true)
- : _connId(0), _live(false), _connAddrLen(0), _blocking(blocking)
- {
- initialize();
- }
- Connection(int conId, bool blocking = true)
- : _connId(conId), _live(true), _connAddrLen(0), _blocking(blocking)
- {
- initialize();
- }
- inline int getConnId() const
- {
- return _connId;
- }
- inline struct sockaddr_in& getConnAddr()
- {
- return _connAddr;
- }
- inline socklen_t getConnAddrLen() const
- {
- return _connAddrLen;
- }
- Connection(const Connection& other)
- {
- _connId = other._connId;
- _live = other._live;
- _connAddrLen = other._connAddrLen;
- _blocking = other._blocking;
- memcpy((void*)&_connAddr, (void*)&other._connAddr, sizeof(_connAddr));
- setState();
- }
- virtual ~Connection()
- {
- //if(_connId !=0)
- //close();
- }
- Connection& operator=(const Connection& other)
- {
- if (this != &other)
- {
- _connId = other._connId;
- _live = other._live;
- _connAddrLen = other._connAddrLen;
- _blocking = other._blocking;
- memcpy((void*)&_connAddr, (void*)&other._connAddr, sizeof(_connAddr));
- setState();
- }
- return *this;
- }
- bool operator==(const Connection& other)
- {
- return (_connId == other._connId);
- }
- bool operator!=(const Connection& other)
- {
- return (_connId != other._connId);
- }
- /**Reads line till 'n' is encountered or data ends.
- Returns 0 if no more data, else return number of bytes read
- Returns -1 if connection is closed
- */
- int readLine(void* data, size_t maxlen, int &bytesRead) throw (VNetworkException&);
- /**Reads nchar from the connection.
- Returns 0 if no more data, else return number of bytes read
- Returns -1 if connection is closed
- */
- int readn(void* data, size_t nchar, int &bytesRead) throw (VNetworkException&);
- int readn(void *data, size_t nchar) throw (VNetworkException&);
- ///Writes data to the connection
- void writeData(void* data, size_t n) throw (VNetworkException&);
- void writeData(string& data) throw (VNetworkException&);
- //Gets the connection description IP_ADDRESSS:Port
- string getDescription() const;
- ///Gets the IP of the destination
- string getIp() const;
- /// Still connected?
- bool isLive() const
- {
- return (_live);
- }
- void close();
- ///Check if data is ready to be read within given seconds and microseconds
- bool isReadReady(int seconds = 0, int mSecconds = 20000) const;
- /// initialize the SIGPIPE signal handler (for broken pipes)
- void initialize();
- /// handler for SIGPIPE signal
- static void signalHandler(int signo);
- ///
- void
- deepCopy(const Connection& src, char** bufPtr, int* bufLenPtr);
- private:
- ssize_t effRead(char* ptr);
- /**Sets the connection state to be blocking or non-blocking
- based on the type of the connection.
- */
- void setState();
- friend class TcpServerSocket;
- friend class TcpClientSocket;
- int _connId;
- bool _live;
- socklen_t _connAddrLen;
- struct sockaddr_in _connAddr;
- bool _blocking;
- static bool _init; /// Set to true if signal handler initialized
- }
- ;
- #endif