StreamClientSocket.hxx
上传用户:sy_wanhua
上传日期:2013-07-25
资源大小:3048k
文件大小:9k
源码类别:

流媒体/Mpeg4/MP4

开发平台:

C/C++

  1. #if !defined(VOCAL_STREAM_CLIENT_SOCKET_HXX)
  2. #define VOCAL_STREAM_CLIENT_SOCKET_HXX
  3. /* ====================================================================
  4.  * The Vovida Software License, Version 1.0 
  5.  * 
  6.  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
  7.  * 
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 
  12.  * 1. Redistributions of source code must retain the above copyright
  13.  *    notice, this list of conditions and the following disclaimer.
  14.  * 
  15.  * 2. Redistributions in binary form must reproduce the above copyright
  16.  *    notice, this list of conditions and the following disclaimer in
  17.  *    the documentation and/or other materials provided with the
  18.  *    distribution.
  19.  * 
  20.  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
  21.  *    and "Vovida Open Communication Application Library (VOCAL)" must
  22.  *    not be used to endorse or promote products derived from this
  23.  *    software without prior written permission. For written
  24.  *    permission, please contact vocal@vovida.org.
  25.  *
  26.  * 4. Products derived from this software may not be called "VOCAL", nor
  27.  *    may "VOCAL" appear in their name, without prior written
  28.  *    permission of Vovida Networks, Inc.
  29.  * 
  30.  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
  31.  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32.  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
  33.  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
  34.  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
  35.  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
  36.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  37.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  38.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  39.  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  40.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  41.  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  42.  * DAMAGE.
  43.  * 
  44.  * ====================================================================
  45.  * 
  46.  * This software consists of voluntary contributions made by Vovida
  47.  * Networks, Inc. and many individuals on behalf of Vovida Networks,
  48.  * Inc.  For more information on Vovida Networks, Inc., please see
  49.  * <http://www.vovida.org/>.
  50.  *
  51.  */
  52. static const char* const StreamClientSocket_hxx_Version = 
  53.     "$Id: StreamClientSocket.hxx,v 1.16 2001/06/26 22:54:22 icahoon Exp $";
  54. #include "Socket.hxx"
  55. #include "Sptr.hxx"
  56. #include "TransportAddress.hxx"
  57. #include "SystemException.hxx"
  58. #include "SystemStatus.hxx"
  59. #include "ConnectionBrokenException.hxx"
  60. #include <string>
  61. #include <vector>
  62. /** Infrastructure common to VOCAL.
  63.  */
  64. namespace Vocal 
  65. {
  66. /** Infrastructure in VOCAL related to making network transport layer 
  67.  *  connections.<br><br>
  68.  */
  69. namespace Transport 
  70. {
  71. /** Stream client socket is a connection-oriented abstraction for reliable 
  72.  *  message transport.<br><br>
  73.  *
  74.  *  @see    Vocal::Transport::Socket
  75.  *  @see    Vocal::Transport::TransportAddress
  76.  *  @see    Vocal::Transport::AddressFamily
  77.  *  @see    Vocal::IO::file_descriptor_t
  78.  *  @see    Vocal::SystemException
  79.  *  @see    Vocal::SystemStatus
  80.  *  @see    Vocal::Transport::ConnectionBrokenException
  81.  */
  82. class StreamClientSocket : public Vocal::Transport::Socket
  83. {
  84.     public:
  85.      /** Address family constructor. Create a socket whose local
  86.  *  address will be a default transport address corresponding 
  87.  *  to the given address family. This usually produces an 
  88.  *  "ANY" address that will be allocated by the operating 
  89.  *  system.
  90.  */
  91.      StreamClientSocket(
  92.     const AddressFamily     &  addressFamily,
  93.          const char           *  name = 0
  94. )
  95.          throw ( Vocal::SystemException );
  96.      /** TransportAddress constructor. Create a socket whose
  97.  *  local address is bound to the given local address.
  98.  */
  99.      StreamClientSocket(
  100.     const TransportAddress  &   localAddr,
  101.          const char           *   name = 0
  102. )
  103.          throw ( Vocal::SystemException );
  104.      /** Connection constructor. Create a socket whose local address
  105.  *  is bound to the local address given, and is connected to
  106.  *  the remote address given.
  107.  */
  108.      StreamClientSocket(
  109.          const TransportAddress  &   localAddr,
  110.     const TransportAddress  &   remoteAddr,
  111.     const char           *   name = 0
  112. )
  113.          throw ( Vocal::SystemException, Vocal::SystemStatus );
  114.      /** Accepted connection constructor. Create a socket from an
  115.  *  already established socket, i.e. via accept. This socket
  116.  *  is connected to the remote address given. If the file
  117.  *  descriptor passed in is invalid, a new socket will
  118.  *  be created, bound to the an "ANY" address and connected
  119.  *  to the remote address.
  120.  */
  121.      StreamClientSocket(
  122.     IO::file_descriptor_t     fd, 
  123.          const TransportAddress  &   remoteAddr,
  124.     const char           *   name = 0
  125. )
  126.          throw ( Vocal::SystemException, Vocal::SystemStatus );
  127.     
  128.      /** Virtual destructor
  129.  */
  130. virtual      ~StreamClientSocket();
  131.      /** To create a connection oriented circuit.
  132.  *  A nonblocking socket may issue an SystemStatus
  133.  *  exception with a status value of EINPROGRESS. It is
  134.  *  then possible to select for completion of the connection, 
  135.  *  i.e via Selector::Select, the socket for writing.
  136.  */
  137.      void connect(const TransportAddress & remoteAddr)
  138.     throw ( Vocal::SystemException, Vocal::SystemStatus );
  139.      /** To complete the connection when the nonblocking has 
  140.  *  been connect()-ed and the socket has return
  141.  *  an EINPROGRESS status.
  142.  */
  143.      void completeConnection()
  144.          throw ( Vocal::SystemException );
  145.      /** Text versions of a connection oriented send. 
  146.  *  Use connect() first. This will transmit the 
  147.  *  string to the remote address specified in connect().
  148.  *  Returns the number of bytes sent. If 0 was returned, there
  149.  *  was an empty message to send, and nothing was sent. If a 
  150.  *  SystemStatus was thrown the socket was set non-blocking 
  151.  *  and it would have blocked (an EAGAIN status), or the 
  152.  *  socket was set blocking and it was interrupted by a signal
  153.  *  (an EINTR) signal. 
  154.  */
  155.      virtual int  send(const string &)
  156.      throw ( Vocal::SystemException,
  157. Vocal::SystemStatus,
  158. Vocal::Transport::ConnectionBrokenException );
  159.      /** Text versions of a connection oriented send. 
  160.  *  See send(const string &).
  161.   */
  162.      virtual int  send(const char *)
  163.      throw ( Vocal::SystemException,
  164. Vocal::SystemStatus,
  165. Vocal::Transport::ConnectionBrokenException );
  166.      /** Binary version of a connection oriented send. 
  167.  *  See send(const string &)
  168.  */
  169.      virtual int  send(const vector<u_int8_t> &)
  170.      throw ( Vocal::SystemException,
  171. Vocal::SystemStatus,
  172. Vocal::Transport::ConnectionBrokenException );
  173.      /** Binary version of a connection oriented send. 
  174.  *  See send(const string &)
  175.  */
  176.      virtual int  send(const u_int8_t *, size_t)
  177.      throw ( Vocal::SystemException,
  178. Vocal::SystemStatus,
  179. Vocal::Transport::ConnectionBrokenException );
  180.      /** Text version of a connection oriented receive. 
  181.  *  Use connect() first. This will receive the 
  182.  *  string from the remote address specified in connect().
  183.  *  Returns the number of received bytes. If a SystemStatus
  184.  *  was thrown either the socket was set non-blocking and it
  185.  *  would have blocked (status EAGAIN), or the socket was set blocking
  186.  *  and it was interrupted by a signal (status EINTR). 
  187.  */
  188.      virtual int  receive(string &)
  189.      throw ( Vocal::SystemException,
  190. Vocal::SystemStatus,
  191. Vocal::Transport::ConnectionBrokenException );
  192.      /** Text version of a connection oriented receive. 
  193.  *  See receive(string &).
  194.  */
  195.      virtual int  receive(char *, size_t)
  196.      throw ( Vocal::SystemException,
  197. Vocal::SystemStatus,
  198. Vocal::Transport::ConnectionBrokenException );
  199.      /** Binary version of a connection oriented receive. 
  200.  *  See receive(string &).
  201.  */
  202.      virtual int  receive(vector<u_int8_t> &)
  203.      throw ( Vocal::SystemException,
  204. Vocal::SystemStatus,
  205. Vocal::Transport::ConnectionBrokenException );
  206.      /** Binary version of a connection oriented receive. 
  207.  *  See receive(string &).
  208.  */
  209.      virtual int  receive(u_int8_t *, size_t)
  210.      throw ( Vocal::SystemException,
  211. Vocal::SystemStatus,
  212. Vocal::Transport::ConnectionBrokenException );
  213.      /** If connected, this will return a pointer to the 
  214.  *  remote address. Otherwise 0 will be returned.
  215.  */
  216.      Sptr<TransportAddress> getRemoteAddress() const;
  217.      /** Write the socket to an ostream.
  218.  */
  219. virtual ostream &    writeTo(ostream &) const;
  220.     private:
  221.      int  sendMessage(void *, int)
  222.      throw ( Vocal::SystemException,
  223. Vocal::SystemStatus,
  224. Vocal::Transport::ConnectionBrokenException );
  225.      int  recvMessage(void *, int)
  226.      throw ( Vocal::SystemException,
  227. Vocal::SystemStatus,
  228. Vocal::Transport::ConnectionBrokenException );
  229.      Sptr<TransportAddress>     remoteAddr_;
  230. bool               localAddrUpdated_;
  231. bool               connectionCompleted_;
  232. };
  233. } // namespace Transport
  234. } // namespace Vocal
  235. #endif // !defined(VOCAL_STREAM_CLIENT_SOCKET_HXX)