ACE_SOCK_Acceptor 相当于网络编程中的Acceptor 监听客户端的请求。
/* -*- C++ -*- */
//=============================================================================
/**
* @file SOCK_Acceptor.h
*
* $Id: SOCK_Acceptor.h 82723 2008-09-16 09:35:44Z johnnyw $
*
* @author Douglas C. Schmidt <schmidt@cs.wustl.edu>
*/
//=============================================================================
#ifndef ACE_SOCK_ACCEPTOR_H
#define ACE_SOCK_ACCEPTOR_H
#include /**/ "ace/pre.h"
#include "ace/SOCK_Stream.h"
#if !defined (ACE_LACKS_PRAGMA_ONCE)
# pragma once
#endif /* ACE_LACKS_PRAGMA_ONCE */
ACE_BEGIN_VERSIONED_NAMESPACE_DECL
class ACE_Time_Value;
class ACE_Accept_QoS_Params;
/**
* @class ACE_SOCK_Acceptor
*
* @brief Defines a factory that creates new ACE_Streams passively.
*
* The ACE_SOCK_Acceptor has its own "passive-mode" socket.
* This serves as a factory to create so-called "data-mode"
* sockets, which are what the ACE_SOCK_Stream encapsulates.
* Therefore, by inheriting from ACE_SOCK, ACE_SOCK_Acceptor
* gets its very own socket.
*/
class ACE_Export ACE_SOCK_Acceptor : public ACE_SOCK
{
public:
// = Initialization and termination methods.
/// Default constructor.
ACE_SOCK_Acceptor (void);
/**
* Initialize a passive-mode BSD-style acceptor socket (no QoS).
* @a local_sap is the address that we're going to listen for
* connections on. If @a reuse_addr is 1 then we'll use the
* @c SO_REUSEADDR to reuse this address.
*/
ACE_SOCK_Acceptor (const ACE_Addr &local_sap,
int reuse_addr = 0,
int protocol_family = PF_UNSPEC,
int backlog = ACE_DEFAULT_BACKLOG,
int protocol = 0);
/// Initialize a passive-mode QoS-enabled acceptor socket. Returns 0
/// on success and -1 on failure.
ACE_SOCK_Acceptor (const ACE_Addr &local_sap,
ACE_Protocol_Info *protocolinfo,
ACE_SOCK_GROUP g,
u_long flags,
int reuse_addr,
int protocol_family = PF_UNSPEC,
int backlog = ACE_DEFAULT_BACKLOG,
int protocol = 0);
/**
* Initialize a passive-mode BSD-style acceptor socket (no QoS).
* @a local_sap is the address that we're going to listen for
* connections on. If @a reuse_addr is 1 then we'll use the
* @c SO_REUSEADDR to reuse this address. Returns 0 on success and
* -1 on failure.
*/
int open (const ACE_Addr &local_sap,
int reuse_addr = 0,
int protocol_family = PF_UNSPEC,
int backlog = ACE_DEFAULT_BACKLOG,
int protocol = 0);
/// Initialize a passive-mode QoS-enabled acceptor socket. Returns 0
/// on success and -1 on failure.
int open (const ACE_Addr &local_sap,
ACE_Protocol_Info *protocolinfo,
ACE_SOCK_GROUP g,
u_long flags,
int reuse_addr,
int protocol_family = PF_UNSPEC,
int backlog = ACE_DEFAULT_BACKLOG,
int protocol = 0);
/// Close the socket. Returns 0 on success and -1 on failure.
int close (void);
/// Default dtor.
~ACE_SOCK_Acceptor (void);
// = Passive connection <accept> methods.
/**
* Accept a new ACE_SOCK_Stream connection. A @a timeout of 0
* means block forever, a @a timeout of {0, 0} means poll. @a restart
* == true means "restart if interrupted," i.e., if errno == EINTR.
* Note that @a new_stream inherits the "blocking mode" of @c this
* ACE_SOCK_Acceptor, i.e., if @c this acceptor factory is in
* non-blocking mode, the @a new_stream will be in non-blocking mode
* and vice versa.
*/
int accept (ACE_SOCK_Stream &new_stream,
ACE_Addr *remote_addr = 0,
ACE_Time_Value *timeout = 0,
bool restart = true,
bool reset_new_handle = false) const;
#if !defined (ACE_HAS_WINCE)
/**
* Accept a new ACE_SOCK_Stream connection using the QoS
* information in @a qos_params. A @a timeout of 0 means block
* forever, a @a timeout of {0, 0} means poll. @a restart == true means
* "restart if interrupted," i.e., if errno == EINTR. Note that
* @a new_stream inherits the "blocking mode" of @c this
* ACE_SOCK_Acceptor, i.e., if @c this acceptor factory is in
* non-blocking mode, the @a new_stream will be in non-blocking mode
* and vice versa.
*/
int accept (ACE_SOCK_Stream &new_stream,
ACE_Accept_QoS_Params qos_params,
ACE_Addr *remote_addr = 0,
ACE_Time_Value *timeout = 0,
bool restart = true,
bool reset_new_handle = false) const;
#endif // ACE_HAS_WINCE
// = Meta-type info
typedef ACE_INET_Addr PEER_ADDR;
typedef ACE_SOCK_Stream PEER_STREAM;
/// Dump the state of an object.
void dump (void) const;
/// Declare the dynamic allocation hooks.
ACE_ALLOC_HOOK_DECLARE;
protected:
/// Perform operations that must occur before <ACE_OS::accept> is
/// called.
int shared_accept_start (ACE_Time_Value *timeout,
bool restart,
int &in_blocking_mode) const;
/// Perform operations that must occur after <ACE_OS::accept> is
/// called.
int shared_accept_finish (ACE_SOCK_Stream new_stream,
int in_blocking_mode,
bool reset_new_handle) const;
/**
* This method factors out the common <open> code and is called by
* both the QoS-enabled <open> method and the BSD-style <open>
* method.
*/
int shared_open (const ACE_Addr &local_sap,
int protocol_family,
int backlog);
private:
/// Do not allow this function to percolate up to this interface...
int get_remote_addr (ACE_Addr &) const;
};
ACE_END_VERSIONED_NAMESPACE_DECL
#if defined (__ACE_INLINE__)
#include "ace/SOCK_Acceptor.inl"
#endif /* __ACE_INLINE__ */
#include /**/ "ace/post.h"
#endif /* ACE_SOCK_ACCEPTOR_H */
ACE_SOCK_ConnectorACE_SOCK_ConnectorACE_SOCK_Connector 主动请求服务端生成ACE_SOCK_Stream建立连接。
// -*- C++ -*-
//=============================================================================
/**
* @file SOCK_Connector.h
*
* $Id: SOCK_Connector.h 91626 2010-09-07 10:59:20Z johnnyw $
*
* @author Doug Schmidt <schmidt@cs.wustl.edu>
*/
//=============================================================================
#ifndef ACE_SOCK_CONNECTOR_H
#define ACE_SOCK_CONNECTOR_H
#include /**/ "ace/pre.h"
#include "ace/SOCK_Stream.h"
#if !defined (ACE_LACKS_PRAGMA_ONCE)
# pragma once
#endif /* ACE_LACKS_PRAGMA_ONCE */
ACE_BEGIN_VERSIONED_NAMESPACE_DECL
class ACE_QoS_Params;
class ACE_Time_Value;
/**
* @class ACE_SOCK_Connector
*
* @brief Defines a factory that actively connects to a remote IP
* address and TCP port, creating a new @c ACE_SOCK_Stream object.
*
* The @c ACE_SOCK_Connector doesn't have a socket of its own,
* i.e., it simply "borrows" the one from the @c ACE_SOCK_Stream
* that's being connected. The reason for this is that the
* underlying socket API doesn't use a factory socket to connect
* data mode sockets. Therefore, there's no need to inherit
* @c ACE_SOCK_Connector from @c ACE_SOCK. A nice side-effect of
* this is that @c ACE_SOCK_Connector objects do not store state so
* they can be used reentrantly in multithreaded programs.
*/
class ACE_Export ACE_SOCK_Connector
{
public:
/// Default constructor.
ACE_SOCK_Connector (void);
/**
* Actively connect to a peer, producing a connected @c ACE_SOCK_Stream
* object if the connection succeeds.
*
* @param new_stream The @c ACE_SOCK_Stream object that will be connected
* to the peer.
* @param remote_sap The address that we are trying to connect to.
* The protocol family of @c remote_sap is used for
* the connected socket. That is, if @c remote_sap
* contains an IPv6 address, a socket with family
* PF_INET6 will be used, else it will be PF_INET.
* @param timeout Pointer to an @c ACE_Time_Value object with amount
* of time to wait to connect. If the pointer is 0
* then the call blocks until the connection attempt
* is complete, whether it succeeds or fails. If
* *timeout == {0, 0} then the connection is done
* using nonblocking mode. In this case, if the
* connection can't be made immediately, this method
* returns -1 and errno == EWOULDBLOCK.
* If *timeout > {0, 0} then this is the maximum amount
* of time to wait before timing out; if the specified
* amount of time passes before the connection is made,
* this method returns -1 and errno == ETIME. Note
* the difference between this case and when a blocking
* connect is attempted that TCP times out - in the latter
* case, errno will be ETIMEDOUT.
* @param local_sap (optional) The local address to bind to. If it's
* the default value of @c ACE_Addr::sap_any then the
* OS will choose an unused port.
* @param reuse_addr (optional) If the value is 1, the local address
* (@c local_sap) is reused, even if it hasn't been
* cleaned up yet.
* @param flags Ignored.
* @param perms Ignored.
* @param protocol (optional) If value is 0, default SOCK_STREAM
* protocol is selected by kernel (typically TCP).
*/
ACE_SOCK_Connector (ACE_SOCK_Stream &new_stream,
const ACE_Addr &remote_sap,
const ACE_Time_Value *timeout = 0,
const ACE_Addr &local_sap = ACE_Addr::sap_any,
int reuse_addr = 0,
int flags = 0,
int perms = 0,
int protocol = 0);
#if !defined (ACE_HAS_WINCE)
/**
* Actively connect to a peer, producing a connected @c ACE_SOCK_Stream
* object if the connection succeeds.
*
* @param new_stream The @c ACE_SOCK_Stream object that will be connected
* to the peer.
* @param remote_sap The address that we are trying to connect to.
* The protocol family of @c remote_sap is used for
* the connected socket. That is, if @c remote_sap
* contains an IPv6 address, a socket with family
* PF_INET6 will be used, else it will be PF_INET.
* @param qos_params Contains QoS parameters that are passed to the
* IntServ (RSVP) and DiffServ protocols.
* @see ACE_QoS_Params.
* @param timeout Pointer to an @c ACE_Time_Value object with amount
* of time to wait to connect. If the pointer is 0
* then the call blocks until the connection attempt
* is complete, whether it succeeds or fails. If
* *timeout == {0, 0} then the connection is done
* using nonblocking mode. In this case, if the
* connection can't be made immediately, this method
* returns -1 and errno == EWOULDBLOCK.
* If *timeout > {0, 0} then this is the maximum amount
* of time to wait before timing out; if the specified
* amount of time passes before the connection is made,
* this method returns -1 and errno == ETIME. Note
* the difference between this case and when a blocking
* connect is attempted that TCP times out - in the latter
* case, errno will be ETIMEDOUT.
* @param local_sap (optional) The local address to bind to. If it's
* the default value of @c ACE_Addr::sap_any then the
* OS will choose an unused port.
* @param reuse_addr (optional) If the value is 1, the local address
* (@c local_sap) is reused, even if it hasn't been
* cleaned up yet.
* @param flags Ignored.
* @param perms Ignored.
*/
ACE_SOCK_Connector (ACE_SOCK_Stream &new_stream,
const ACE_Addr &remote_sap,
ACE_QoS_Params qos_params,
const ACE_Time_Value *timeout = 0,
const ACE_Addr &local_sap = ACE_Addr::sap_any,
ACE_Protocol_Info *protocolinfo = 0,
ACE_SOCK_GROUP g = 0,
u_long flags = 0,
int reuse_addr = 0,
int perms = 0);
#endif // ACE_HAS_WINCE
/**
* Actively connect to a peer, producing a connected @c ACE_SOCK_Stream
* object if the connection succeeds.
*
* @param new_stream The @c ACE_SOCK_Stream object that will be connected
* to the peer.
* @param remote_sap The address that we are trying to connect to.
* The protocol family of @c remote_sap is used for
* the connected socket. That is, if @c remote_sap
* contains an IPv6 address, a socket with family
* PF_INET6 will be used, else it will be PF_INET.
* @param timeout Pointer to an @c ACE_Time_Value object with amount
* of time to wait to connect. If the pointer is 0
* then the call blocks until the connection attempt
* is complete, whether it succeeds or fails. If
* *timeout == {0, 0} then the connection is done
* using nonblocking mode. In this case, if the
* connection can't be made immediately, this method
* returns -1 and errno == EWOULDBLOCK.
* If *timeout > {0, 0} then this is the maximum amount
* of time to wait before timing out; if the specified
* amount of time passes before the connection is made,
* this method returns -1 and errno == ETIME. Note
* the difference between this case and when a blocking
* connect is attempted that TCP times out - in the latter
* case, errno will be ETIMEDOUT.
* @param local_sap (optional) The local address to bind to. If it's
* the default value of @c ACE_Addr::sap_any then the
* OS will choose an unused port.
* @param reuse_addr (optional) If the value is 1, the local address
* (@c local_sap) is reused, even if it hasn't been
* cleaned up yet.
* @param flags Ignored.
* @param perms Ignored.
* @param protocol (optional) If value is 0, default SOCK_STREAM
* protocol is selected by kernel (typically TCP).
*
* @return Returns 0 if the connection succeeds. If it fails,
* -1 is returned and errno contains a specific error
* code.
*/
int connect (ACE_SOCK_Stream &new_stream,
const ACE_Addr &remote_sap,
const ACE_Time_Value *timeout = 0,
const ACE_Addr &local_sap = ACE_Addr::sap_any,
int reuse_addr = 0,
int flags = 0,
int perms = 0,
int protocol = 0);
#if !defined (ACE_HAS_WINCE)
/**
* Actively connect to a peer, producing a connected @c ACE_SOCK_Stream
* object if the connection succeeds.
*
* @param new_stream The @c ACE_SOCK_Stream object that will be connected
* to the peer.
* @param remote_sap The address that we are trying to connect to.
* The protocol family of @c remote_sap is used for
* the connected socket. That is, if @c remote_sap
* contains an IPv6 address, a socket with family
* PF_INET6 will be used, else it will be PF_INET.
* @param qos_params Contains QoS parameters that are passed to the
* IntServ (RSVP) and DiffServ protocols.
* @see ACE_QoS_Params.
* @param timeout Pointer to an @c ACE_Time_Value object with amount
* of time to wait to connect. If the pointer is 0
* then the call blocks until the connection attempt
* is complete, whether it succeeds or fails. If
* *timeout == {0, 0} then the connection is done
* using nonblocking mode. In this case, if the
* connection can't be made immediately, this method
* returns -1 and errno == EWOULDBLOCK.
* If *timeout > {0, 0} then this is the maximum amount
* of time to wait before timing out; if the specified
* amount of time passes before the connection is made,
* this method returns -1 and errno == ETIME. Note
* the difference between this case and when a blocking
* connect is attempted that TCP times out - in the latter
* case, errno will be ETIMEDOUT.
* @param local_sap (optional) The local address to bind to. If it's
* the default value of @c ACE_Addr::sap_any then the
* OS will choose an unused port.
* @param reuse_addr (optional) If the value is 1, the local address
* (@c local_sap) is reused, even if it hasn't been
* cleaned up yet.
* @param flags Ignored.
* @param perms Ignored.
*
* @return Returns 0 if the connection succeeds. If it fails,
* -1 is returned and errno contains a specific error
* code.
*/
int connect (ACE_SOCK_Stream &new_stream,
const ACE_Addr &remote_sap,
ACE_QoS_Params qos_params,
const ACE_Time_Value *timeout = 0,
const ACE_Addr &local_sap = ACE_Addr::sap_any,
ACE_Protocol_Info *protocolinfo = 0,
ACE_SOCK_GROUP g = 0,
u_long flags = 0,
int reuse_addr = 0,
int perms = 0);
#endif // ACE_HAS_WINCE
/// Default destructor.
~ACE_SOCK_Connector (void);
// = Completion routine.
/**
* Try to complete a nonblocking connection that was begun by a
* previous call to connect with a {0, 0} ACE_Time_Value timeout.
* @see connect().
*
* @param new_stream The @c ACE_SOCK_Stream object that will be connected
* to the peer.
* @param remote_sap If non-0, it points to the @c ACE_INET_Addr object
* that will contain the address of the connected peer.
* @param timeout Same values and return value possibilites as for
* connect(). @see connect().
*/
int complete (ACE_SOCK_Stream &new_stream,
ACE_Addr *remote_sap = 0,
const ACE_Time_Value *timeout = 0);
/// Resets any event associations on this handle
bool reset_new_handle (ACE_HANDLE handle);
// = Meta-type info
typedef ACE_INET_Addr PEER_ADDR;
typedef ACE_SOCK_Stream PEER_STREAM;
/// Dump the state of an object.
void dump (void) const;
/// Declare the dynamic allocation hooks.
ACE_ALLOC_HOOK_DECLARE;
protected:
/// Perform operations that ensure the socket is opened using
/// BSD-style semantics (no QoS).
int shared_open (ACE_SOCK_Stream &new_stream,
int protocol_family,
int protocol,
int reuse_addr);
/// Perform operations that ensure the socket is opened using
/// QoS-enabled semantics.
int shared_open (ACE_SOCK_Stream &new_stream,
int protocol_family,
int protocol,
ACE_Protocol_Info *protocolinfo,
ACE_SOCK_GROUP g,
u_long flags,
int reuse_addr);
/// Perform operations that must be called before <ACE_OS::connect>.
int shared_connect_start (ACE_SOCK_Stream &new_stream,
const ACE_Time_Value *timeout,
const ACE_Addr &local_sap);
/// Perform operations that must be called after <ACE_OS::connect>.
int shared_connect_finish (ACE_SOCK_Stream &new_stream,
const ACE_Time_Value *timeout,
int result);
};
ACE_END_VERSIONED_NAMESPACE_DECL
#if defined (__ACE_INLINE__)
#include "ace/SOCK_Connector.inl"
#endif /* __ACE_INLINE__ */
#include /**/ "ace/post.h"
#endif /* ACE_SOCK_CONNECTOR_H */
ACE_SOCK_Stream 一个请求的连接
// -*- C++ -*-
//=============================================================================
/**
* @file SOCK_Stream.h
*
* $Id: SOCK_Stream.h 92956 2010-12-29 16:12:31Z shuston $
*
* @author Douglas C. Schmidt <schmidt@cs.wustl.edu>
*/
//=============================================================================
#ifndef ACE_SOCK_STREAM_H
#define ACE_SOCK_STREAM_H
#include /**/ "ace/pre.h"
#include "ace/SOCK_IO.h"
#if !defined (ACE_LACKS_PRAGMA_ONCE)
# pragma once
#endif /* ACE_LACKS_PRAGMA_ONCE */
#include "ace/INET_Addr.h"
ACE_BEGIN_VERSIONED_NAMESPACE_DECL
// Forward declarations.
class ACE_Message_Block;
/**
* @class ACE_SOCK_Stream
*
* @brief Defines the methods in the ACE_SOCK_Stream abstraction.
*
* This adds additional wrapper methods atop the ACE_SOCK_IO
* class.
*
* @sa ACE_SOCK_IO
*/
class ACE_Export ACE_SOCK_Stream : public ACE_SOCK_IO
{
public:
// Initialization and termination methods.
/// Constructor.
ACE_SOCK_Stream (void);
/// Constructor (sets the underlying ACE_HANDLE with @a h).
ACE_SOCK_Stream (ACE_HANDLE h);
/// Destructor.
~ACE_SOCK_Stream (void);
/** @name Counted send/receive methods
*
* The counted send/receive methods attempt to transfer a specified number
* of bytes even if they must block and retry the operation in order to
* transfer the entire amount. The time spent blocking for the entire
* transfer can be limited by a specified ACE_Time_Value object which is
* a relative time (i.e., a fixed amount of time, not an absolute time
* of day). These methods return the count of transferred bytes, or -1
* if an error occurs or the operation times out before the entire requested
* amount of data has been transferred. In error or timeout situations it's
* possible that some data was transferred before the error
* or timeout. The @c bytes_transferred parameter is used to obtain the
* count of bytes transferred before the error or timeout occurred. If the
* total specified number of bytes is transferred without error, the
* method return value should equal the value of @c bytes_transferred.
*
* @param buf The buffer to write from or receive into.
* @param iov An I/O vector containing a specified number of
* count/pointer pairs directing the data to be transferred.
* @param iovcnt The number of I/O vectors to be used from @a iov.
* @param len The number of bytes to transfer.
* @param flags Flags that will be passed through to the @c recv()
* system call.
* @param timeout Indicates how long to blocking trying to transfer data.
* If no timeout is supplied (timeout == 0) the method will
* wait indefinitely or until an error occurs for the
* specified number of bytes to be transferred.
* To avoid any waiting, specify a timeout value with
* 0 seconds. Note that the timeout period restarts on
* each retried operation issued; therefore, an operation
* that requires multiples retries may take longer than the
* specified timeout to complete.
* @param bytes_transferred If non-0, points to a location which receives
* the total number of bytes transferred before the method
* returns, even if it's less than the number requested.
*
* @retval len, the complete number of bytes transferred.
* @retval 0 EOF, i.e., the peer closed the connection.
* @retval -1 an error occurred before the entire amount was
* transferred. Check @c errno for more information.
* If the @a timeout period is reached, errno is ETIME.
*
* On partial transfers, i.e., if any data is transferred before
* timeout/error/EOF, *@a bytes_transferred will contain the number of
* bytes transferred.
*/
//@{
/// Try to recv exactly @a len bytes into @a buf from the connected socket.
ssize_t recv_n (void *buf,
size_t len,
int flags,
const ACE_Time_Value *timeout = 0,
size_t *bytes_transferred = 0) const;
/// Try to recv exactly @a len bytes into @a buf from the connected socket.
ssize_t recv_n (void *buf,
size_t len,
const ACE_Time_Value *timeout = 0,
size_t *bytes_transferred = 0) const;
/// Receive an @c iovec of size @a iovcnt from the connected socket.
ssize_t recvv_n (iovec iov[],
int iovcnt,
const ACE_Time_Value *timeout = 0,
size_t *bytes_transferred = 0) const;
/// Try to send exactly @a len bytes from @a buf to the connection socket.
ssize_t send_n (const void *buf,
size_t len,
int flags,
const ACE_Time_Value *timeout = 0,
size_t *bytes_transferred = 0) const;
/// Try to send exactly @a len bytes from @a buf to the connected socket.
ssize_t send_n (const void *buf,
size_t len,
const ACE_Time_Value *timeout = 0,
size_t *bytes_transferred = 0) const;
/// Send all the message blocks chained through their @c next and
/// @c cont pointers. This call uses the underlying OS gather-write
/// operation to reduce the domain-crossing penalty.
ssize_t send_n (const ACE_Message_Block *message_block,
const ACE_Time_Value *timeout = 0,
size_t *bytes_transferred = 0) const;
/// Send an @c iovec of size @a iovcnt to the connected socket.
ssize_t sendv_n (const iovec iov[],
int iovcnt,
const ACE_Time_Value *timeout = 0,
size_t *bytes_transferred = 0) const;
//@}
// = Send/receive ``urgent'' data (see TCP specs...).
ssize_t send_urg (const void *ptr,
size_t len = sizeof (char),
const ACE_Time_Value *timeout = 0) const;
ssize_t recv_urg (void *ptr,
size_t len = sizeof (char),
const ACE_Time_Value *timeout = 0) const;
// = Selectively close endpoints.
/// Close down the reader.
int close_reader (void);
/// Close down the writer.
int close_writer (void);
/**
* Close down the socket (we need this to make things work correctly
* on Win32, which requires use to do a close_writer() before doing
* the close to avoid losing data).
*/
int close (void);
// = Meta-type info
typedef ACE_INET_Addr PEER_ADDR;
/// Dump the state of an object.
void dump (void) const;
/// Declare the dynamic allocation hooks.
ACE_ALLOC_HOOK_DECLARE;
};
ACE_END_VERSIONED_NAMESPACE_DECL
#if defined (__ACE_INLINE__)
#include "ace/SOCK_Stream.inl"
#endif /* __ACE_INLINE__ */
#include /**/ "ace/post.h"
#endif /* ACE_SOCK_STREAM_H */
看英文吧~~