SWIsocket.hpp
上传用户:xqtpzdz
上传日期:2022-05-21
资源大小:1764k
文件大小:10k
源码类别:

xml/soap/webservice

开发平台:

Visual C++

  1. #ifndef SWISOCKET_HPP
  2. #define SWISOCKET_HPP
  3. /****************License************************************************
  4.  * Vocalocity OpenVXI
  5.  * Copyright (C) 2004-2005 by Vocalocity, Inc. All Rights Reserved.
  6.  * This program is free software; you can redistribute it and/or
  7.  * modify it under the terms of the GNU General Public License
  8.  * as published by the Free Software Foundation; either version 2
  9.  * of the License, or (at your option) any later version.
  10.  *  
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program; if not, write to the Free Software
  18.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  * Vocalocity, the Vocalocity logo, and VocalOS are trademarks or 
  20.  * registered trademarks of Vocalocity, Inc. 
  21.  * OpenVXI is a trademark of Scansoft, Inc. and used under license 
  22.  * by Vocalocity.
  23.  ***********************************************************************/
  24. #include "SWIutilHeaderPrefix.h"
  25. #include "SWIinputStream.hpp"
  26. #include "SWIoutputStream.hpp"
  27. #include "SWIutilLogger.hpp"
  28. #include "VXIlog.h"
  29. #ifdef _WIN32
  30. #include <winsock2.h>
  31. #else
  32. #include <sys/socket.h>
  33. typedef int SOCKET;
  34. #define INVALID_SOCKET (-1)
  35. #endif
  36. #ifdef __linux__
  37. // linux leaves these things out. We define it for compatitbility
  38. // not for their use.
  39. #ifndef SO_ACCEPTCONN
  40. # define SO_ACCEPTCONN 0x0002
  41. #endif
  42. #ifndef SO_USELOOPBACK
  43. # define SO_USELOOPBACK 0x0040
  44. #endif
  45. #ifndef SO_SNDLOWAT
  46. # define SO_SNDLOWAT 0x1003
  47. # define SO_RCVLOWAT 0x1004
  48. # define SO_SNDTIMEO 0x1005
  49. # define SO_RCVTIMEO 0x1006
  50. #endif
  51. # define MSG_MAXIOVLEN 16
  52. #ifndef SOMAXCONN
  53. # define SOMAXCONN 5
  54. #endif
  55. #endif // __linux__
  56. class SWIipAddress;
  57. class SWIoutputStream;
  58. /**
  59.  * This class implements client and server sockets.  A server socket is
  60.  * listening and accepting connections from client sockets.
  61.  */
  62. class SWIUTIL_API_CLASS SWIsocket
  63. {
  64.  public:
  65.   enum Type
  66.   {
  67.     sock_stream = SOCK_STREAM,
  68.     sock_dgram = SOCK_DGRAM,
  69.     sock_raw = SOCK_RAW,
  70.     sock_rdm = SOCK_RDM,
  71.     sock_seqpacket  = SOCK_SEQPACKET
  72.   };
  73.   enum option {
  74.     so_debug = SO_DEBUG,
  75.     so_acceptconn = SO_ACCEPTCONN,
  76.     so_reuseaddr = SO_REUSEADDR,
  77.     so_keepalive = SO_KEEPALIVE,
  78.     so_dontroute = SO_DONTROUTE,
  79.     so_broadcast = SO_BROADCAST,
  80.     so_useloopback = SO_USELOOPBACK,
  81.     so_linger = SO_LINGER,
  82.     so_oobinline = SO_OOBINLINE,
  83.     so_sndbuf = SO_SNDBUF,
  84.     so_rcvbuf = SO_RCVBUF,
  85.     so_sndlowat = SO_SNDLOWAT,
  86.     so_rcvlowat = SO_RCVLOWAT,
  87.     so_sndtimeo = SO_SNDTIMEO,
  88.     so_rcvtimeo = SO_RCVTIMEO,
  89.     so_error = SO_ERROR,
  90.     so_type = SO_TYPE
  91.   };
  92.   enum level { sol_socket = SOL_SOCKET };
  93.   enum msgflag {
  94.     msg_oob = MSG_OOB,
  95.     msg_peek = MSG_PEEK,
  96.     msg_dontroute = MSG_DONTROUTE,
  97.     msg_maxiovlen = MSG_MAXIOVLEN
  98.   };
  99.   enum shuthow {
  100.     shut_read,
  101.     shut_write,
  102.     shut_readwrite
  103.   };
  104.   enum { somaxconn = SOMAXCONN };
  105.   struct socklinger {
  106.     int l_onoff; // option on/off
  107.     int l_linger; // linger time
  108.     socklinger (int a, int b): l_onoff (a), l_linger (b) {}
  109.   };
  110.  public:
  111.   // ................. CONSTRUCTORS, DESTRUCTOR  ............
  112.   //
  113.   // ------------------------------------------------------------
  114.   /**
  115.    * Creates an unconnected Socket.
  116.    * <P>
  117.    * @param socktype The type of socket.
  118.    * Use sock_stream for TCP and sock_dgram for UDP.
  119.    *
  120.    */
  121.  public:
  122.   SWIsocket(Type socktype = sock_stream,
  123.             SWIutilLogger *logger = NULL);
  124.   /**
  125.    * Destructor.
  126.    **/
  127.  public:
  128.   virtual ~SWIsocket();
  129.   /**
  130.    * Connects this socket to the server specified in the ip address object.
  131.    *
  132.    */
  133.  public:
  134.   virtual SWIstream::Result connect(const SWIipAddress& endpoint, long timeout = -1);
  135.   SWIstream::Result connect(const char *host, int port = 0, long timeout = -1);
  136.   SWIstream::Result connect(unsigned long addr, int port = 0, long timeout = -1);
  137.   SWIstream::Result connect(unsigned long addr, const char *service_name,
  138.                  const char *protocol_name = "tcp", long timeout = -1);
  139.   SWIstream::Result connect(const char *host, const char *service_name,
  140.                  const char *protocol_name = "tcp", long timeout = -1);
  141.   virtual SWIstream::Result bind(const SWIipAddress &addr);
  142.   SWIstream::Result bind();
  143.   SWIstream::Result bind(unsigned long addr, int port_no=0);
  144.   SWIstream::Result bind(const char* host_name, int port_no=0);
  145.   SWIstream::Result bind(unsigned long addr,
  146.               const char* service_name,
  147.               const char* protocol_name="tcp");
  148.   SWIstream::Result bind(const char* host_name,
  149.               const char* service_name,
  150.               const char* protocol_name="tcp");
  151.   SWIstream::Result listen(int backlog=somaxconn);
  152.   virtual SWIsocket *accept(SWIipAddress& sa);
  153.   virtual SWIsocket *accept();
  154.   /**
  155.    * Returns the address to which the socket is connected.
  156.    *
  157.    * @return  the remote IP address to which this socket is connected,
  158.    * or <code>null</code> if the socket is not connected.
  159.    */
  160.  public:
  161.   const SWIipAddress* getRemoteAddress() const;
  162.   /**
  163.    * Gets the local address to which the socket is bound.
  164.    *
  165.    * @return the local address to which the socket is bound or NULL
  166.    *        if the socket is not bound yet.
  167.    */
  168.  public:
  169.   const SWIipAddress* getLocalAddress() const;
  170.   /**
  171.    * Returns the remote port to which this socket is connected.
  172.    *
  173.    * @return  the remote port number to which this socket is connected, or
  174.    *         0 if the socket is not connected yet.
  175.    */
  176.  public:
  177.   int getRemotePort() const;
  178.   /**
  179.    * Returns the local port to which this socket is bound.
  180.    *
  181.    * @return  the local port number to which this socket is bound or -1
  182.    *         if the socket is not bound yet.
  183.    */
  184.  public:
  185.   int getLocalPort() const;
  186.   /**
  187.    * Returns an input stream for this socket.  The input stream must be
  188.    * deleted by the caller.  If an error occurs, returns NULL.  There is one
  189.    * input stream per socket.  Calling this function several times will return
  190.    * the same stream, so care must be taken to ensure that it is deleted only
  191.    * once.  The stream can be closed either by calling its close method or by
  192.    * shutting down the read end of the socket.
  193.    */
  194.  public:
  195.   SWIinputStream* getInputStream();
  196.   /**
  197.    * Returns an output stream for this socket.  The output stream must be
  198.    * deleted by the caller.  If an error occurs, returns NULL.  There is one
  199.    * output stream per socket.  Calling this function several times will return
  200.    * the same stream, so care must be taken to ensure that it is deleted only
  201.    * once.  The stream can be closed either by calling its close method or by
  202.    * shutting down the write end of the socket.
  203.    */
  204.  public:
  205.   SWIoutputStream* getOutputStream();
  206.   virtual int read(void* buf, int len);
  207.   virtual int recv(void* buf, int len, int msgf=0);
  208.   virtual int recvfrom(SWIipAddress& sa, void* buf, int len, int msgf=0);
  209.   virtual int write (const void* buf, int len);
  210.   virtual int send (const void* buf, int len, int msgf=0);
  211.   virtual int sendto (const SWIipAddress& sa, const void* buf, int len, int msgf=0);
  212.   long sendtimeout (long timeoutMs=-1);
  213.   long recvtimeout (long timeoutMs=-1);
  214.   int is_readready (long timeoutMs) const;
  215.   int is_writeready (long timeoutMs) const;
  216.   int is_exceptionpending (long timeoutMs) const;
  217.   int is_readready (timeval *tv = NULL) const;
  218.   int is_writeready (timeval *tv = NULL) const;
  219.   int is_exceptionpending (timeval *tv = NULL) const;
  220.   virtual SWIstream::Result shutdown (shuthow sh);
  221.   int getopt(option op, void* buf,int len,
  222.              level l=sol_socket) const;
  223.   void setopt(option op, void* buf,int len,
  224.               level l=sol_socket) const;
  225.   Type gettype () const;
  226.   int clearerror () const;
  227.   int debug   (int opt= -1) const;
  228.   int reuseaddr (int opt= -1) const;
  229.   int keepalive (int opt= -1) const;
  230.   int dontroute (int opt= -1) const;
  231.   int broadcast (int opt= -1) const;
  232.   int oobinline (int opt= -1) const;
  233.   int linger    (int tim= -1) const;
  234.   int sendbufsz (int sz=-1)   const;
  235.   int recvbufsz (int sz=-1)   const;
  236.   /**
  237.    * Closes this socket.
  238.    */
  239.  public:
  240.   virtual SWIstream::Result close();
  241.  protected:
  242.   void error (const VXIchar *func, VXIunsigned errorId, int errorCode) const;
  243.   void error (const VXIchar *func, VXIunsigned errorId,
  244.               const VXIchar *errorName, int errorCode) const;
  245.   void error (const VXIchar *func, VXIunsigned errorId) const;
  246.  private:
  247.   class SWIUTIL_API_CLASS InputStream: public SWIinputStream
  248.   {
  249.    public:
  250.     InputStream(SWIsocket *sock);
  251.     virtual ~InputStream();
  252.     virtual int readBytes(void * data, int dataSize);
  253.     virtual SWIstream::Result close();
  254.     virtual SWIstream::Result waitReady(long timeoutMs = -1);
  255.    private:
  256.     friend class SWIsocket;
  257.     SWIsocket *_sock;
  258.   };
  259.  private:
  260.   class SWIUTIL_API_CLASS OutputStream: public SWIoutputStream
  261.   {
  262.    public:
  263.     OutputStream(SWIsocket *sock);
  264.     ~OutputStream();
  265.     virtual int writeBytes(const void *buffer, int bufferSize);
  266.     virtual SWIstream::Result close();
  267.     virtual SWIstream::Result waitReady(long timeoutMs = -1);
  268.    private:
  269.     friend class SWIsocket;
  270.     SWIsocket *_sock;
  271.   };
  272.   /**
  273.    * Disabled copy constructor.
  274.    **/
  275.  private:
  276.   SWIsocket(const SWIsocket&);
  277.   /**
  278.    * Disabled assignment operator.
  279.    **/
  280.  private:
  281.   SWIsocket& operator=(const SWIsocket&);
  282.  private:
  283.   SWIsocket(const SWIsocket *socket, SOCKET sd);
  284.  protected:
  285.   SOCKET getSD() const
  286.   {
  287.     return _sd;
  288.   }
  289.  private:
  290.   friend class InputStream;
  291.   friend class OutputStream;
  292.   SWIutilLogger *_logger;
  293.   SOCKET _sd;
  294.   mutable SWIipAddress *_localAddress;
  295.   mutable SWIipAddress *_remoteAddress;
  296.   bool _created;
  297.   bool _bound;
  298.   bool _connected;
  299.   bool _closed;
  300.   bool _shutIn;
  301.   bool _shutOut;
  302.   int _stmo; // -1==block, 0==poll, >0 == waiting time in secs
  303.   int _rtmo; // -1==block, 0==poll, >0 == waiting time in secs
  304.   OutputStream* _outStream;
  305.   InputStream* _inStream;
  306. };
  307. #endif