---
name: "ACE_Cached_Connect_Strategy_Ex"
section: "3"
platform: "linux"
package: "libace-doc"
version: "5.0.7-4"
release: "Debian 2.2 (Potato)"
platforms: ["linux"]
url: "https://new.carta.tech/ACE_Cached_Connect_Strategy_Ex"
---

# ACE\_Cached\_Connect\_Strategy\_Ex(3)

A connection strategy which caches connections to peers (represented by SVC\_HANDLER instances), thereby allowing subsequent re-use of unused, but available, connections.

## NAME

ACE\_Cached\_Connect\_Strategy\_Ex -  A connection strategy which caches connections to peers (represented by SVC\_HANDLER instances), thereby allowing subsequent re-use of unused, but available, connections.

## SYNOPSIS

```
#include <ace/Cached_Connect_Strategy_T.h>
template<class SVC_HANDLER, ACE_PEER_CONNECTOR_1, class CACHING_STRATEGY, class ATTRIBUTES, class MUTEX>
class ACE_Cached_Connect_Strategy_Ex :
  public ACE_Cached_Connect_Strategy<SVC_HANDLER, ACE_PEER_CONNECTOR_2, MUTEX>
{
  public:
    ACE_Cached_Connect_Strategy_Ex (
        CACHING_STRATEGY &caching_s,
        ACE_Creation_Strategy<SVC_HANDLER> *cre_s = 0,
        ACE_Concurrency_Strategy<SVC_HANDLER> *con_s = 0,
        ACE_Recycling_Strategy<SVC_HANDLER> *rec_s = 0,
        MUTEX *lock = 0,
        int delete_lock = 0
        );
    virtual ~ACE_Cached_Connect_Strategy_Ex (void);
    virtual int purge_connections (void);
    virtual int mark_as_closed_i (const void *recycling_act);
        typedef ACE_Refcounted_Hash_Recyclable<ACE_PEER_CONNECTOR_ADDR> REFCOUNTED_HASH_RECYCLABLE_ADDRESS;

        typedef ACE_Hash_Cache_Map_Manager<REFCOUNTED_HASH_RECYCLABLE_ADDRESS, SVC_HANDLER *, ACE_Hash<REFCOUNTED_HASH_RECYCLABLE_ADDRESS>, ACE_Equal_To<REFCOUNTED_HASH_RECYCLABLE_ADDRESS>, CACHING_STRATEGY, ATTRIBUTES> CONNECTION_CACHE;

        typedef ACE_TYPENAME CONNECTION_CACHE::CACHE_ENTRY CONNECTION_CACHE_ENTRY;

    typedef ACE_TYPENAME CONNECTION_CACHE::key_type KEY;
    typedef ACE_TYPENAME CONNECTION_CACHE::mapped_type VALUE;
        typedef ACE_Recyclable_Handler_Cleanup_Strategy<REFCOUNTED_HASH_RECYCLABLE_ADDRESS, ACE_Pair<SVC_HANDLER *, ATTRIBUTES>, ACE_Hash_Map_Manager_Ex<REFCOUNTED_HASH_RECYCLABLE_ADDRESS, ACE_Pair<SVC_HANDLER *, ATTRIBUTES>, ACE_Hash<REFCOUNTED_HASH_RECYCLABLE_ADDRESS>, ACE_Equal_To<REFCOUNTED_HASH_RECYCLABLE_ADDRESS>, MUTEX> > CLEANUP_STRATEGY;

        typedef ACE_Cached_Connect_Strategy<SVC_HANDLER, ACE_PEER_CONNECTOR_2, MUTEX> CCSBASE;

    CACHING_STRATEGY &caching_strategy (void);
  protected:
    int find (
        ACE_Refcounted_Hash_Recyclable<ACE_PEER_CONNECTOR_ADDR> &search_addr,
        ACE_Hash_Map_Entry<ACE_Refcounted_Hash_Recyclable<ACE_PEER_CONNECTOR_ADDR>, ACE_Pair<SVC_HANDLER *, ATTRIBUTES> > *&entry
        );
    virtual int purge_i (const void *recycling_act);
    virtual int cache_i (const void *recycling_act);
    virtual int recycle_state_i (
        const void *recycling_act,
        ACE_Recyclable_State new_state
        );
    virtual ACE_Recyclable_State recycle_state_i (
        const void *recycling_act
        ) const;
    virtual int cleanup_hint_i (const void *recycling_act);
    int check_hint_i (
        SVC_HANDLER *&sh,
        const ACE_PEER_CONNECTOR_ADDR &remote_addr,
        ACE_Time_Value *timeout,
        const ACE_PEER_CONNECTOR_ADDR &local_addr,
        int reuse_addr,
        int flags,
        int perms,
        ACE_Hash_Map_Entry<ACE_Refcounted_Hash_Recyclable<ACE_PEER_CONNECTOR_ADDR>, ACE_Pair<SVC_HANDLER *, ATTRIBUTES> > *&entry,
        int &found
        );
    int find_or_create_svc_handler_i (
        SVC_HANDLER *&sh,
        const ACE_PEER_CONNECTOR_ADDR &remote_addr,
        ACE_Time_Value *timeout,
        const ACE_PEER_CONNECTOR_ADDR &local_addr,
        int reuse_addr,
        int flags,
        int perms,
        ACE_Hash_Map_Entry<ACE_Refcounted_Hash_Recyclable<ACE_PEER_CONNECTOR_ADDR>, ACE_Pair<SVC_HANDLER *, ATTRIBUTES> > *&entry,
        int &found
        );
    virtual int connect_svc_handler_i (
        SVC_HANDLER *&sh,
        const ACE_PEER_CONNECTOR_ADDR &remote_addr,
        ACE_Time_Value *timeout,
        const ACE_PEER_CONNECTOR_ADDR &local_addr,
        int reuse_addr,
        int flags,
        int perms,
        int &found
        );
    virtual int cached_connect (
        SVC_HANDLER *&sh,
        const ACE_PEER_CONNECTOR_ADDR &remote_addr,
        ACE_Time_Value *timeout,
        const ACE_PEER_CONNECTOR_ADDR &local_addr,
        int reuse_addr,
        int flags,
        int perms
        );
    void cleanup (void);
    CONNECTION_CACHE connection_cache_;
};
```

## DESCRIPTION

Cached\_Connect\_Strategy is intended to be used as a plug-in connection strategy for ACE\_Strategy\_Connector. It's added value is re-use of established connections and tweaking the role of the cache as per the caching strategy.

## Typedefs for managing the map

```
    typedef ACE_Refcounted_Hash_Recyclable<ACE_PEER_CONNECTOR_ADDR> REFCOUNTED_HASH_RECYCLABLE_ADDRESS;

```

```
    typedef ACE_Hash_Cache_Map_Manager<REFCOUNTED_HASH_RECYCLABLE_ADDRESS, SVC_HANDLER *, ACE_Hash<REFCOUNTED_HASH_RECYCLABLE_ADDRESS>, ACE_Equal_To<REFCOUNTED_HASH_RECYCLABLE_ADDRESS>, CACHING_STRATEGY, ATTRIBUTES> CONNECTION_CACHE;

```

```
    typedef ACE_TYPENAME CONNECTION_CACHE::CACHE_ENTRY CONNECTION_CACHE_ENTRY;

```

```
typedef ACE_TYPENAME CONNECTION_CACHE::key_type KEY;
```

```
typedef ACE_TYPENAME CONNECTION_CACHE::mapped_type VALUE;
```

## Cleanup of the svc\_handler.

```
    typedef ACE_Recyclable_Handler_Cleanup_Strategy<REFCOUNTED_HASH_RECYCLABLE_ADDRESS, ACE_Pair<SVC_HANDLER *, ATTRIBUTES>, ACE_Hash_Map_Manager_Ex<REFCOUNTED_HASH_RECYCLABLE_ADDRESS, ACE_Pair<SVC_HANDLER *, ATTRIBUTES>, ACE_Hash<REFCOUNTED_HASH_RECYCLABLE_ADDRESS>, ACE_Equal_To<REFCOUNTED_HASH_RECYCLABLE_ADDRESS>, MUTEX> > CLEANUP_STRATEGY;

```

```
    typedef ACE_Cached_Connect_Strategy<SVC_HANDLER, ACE_PEER_CONNECTOR_2, MUTEX> CCSBASE;

```

## Accessor.

```
CACHING_STRATEGY &caching_strategy (void);
```

## Helpers

```
int check_hint_i (
    SVC_HANDLER *&sh,
    const ACE_PEER_CONNECTOR_ADDR &remote_addr,
    ACE_Time_Value *timeout,
    const ACE_PEER_CONNECTOR_ADDR &local_addr,
    int reuse_addr,
    int flags,
    int perms,
    ACE_Hash_Map_Entry<ACE_Refcounted_Hash_Recyclable<ACE_PEER_CONNECTOR_ADDR>, ACE_Pair<SVC_HANDLER *, ATTRIBUTES> > *&entry,
    int &found
    );
```

```
int find_or_create_svc_handler_i (
    SVC_HANDLER *&sh,
    const ACE_PEER_CONNECTOR_ADDR &remote_addr,
    ACE_Time_Value *timeout,
    const ACE_PEER_CONNECTOR_ADDR &local_addr,
    int reuse_addr,
    int flags,
    int perms,
    ACE_Hash_Map_Entry<ACE_Refcounted_Hash_Recyclable<ACE_PEER_CONNECTOR_ADDR>, ACE_Pair<SVC_HANDLER *, ATTRIBUTES> > *&entry,
    int &found
    );
```

```
virtual int connect_svc_handler_i (
    SVC_HANDLER *&sh,
    const ACE_PEER_CONNECTOR_ADDR &remote_addr,
    ACE_Time_Value *timeout,
    const ACE_PEER_CONNECTOR_ADDR &local_addr,
    int reuse_addr,
    int flags,
    int perms,
    int &found
    );
```

```
virtual int cached_connect (
    SVC_HANDLER *&sh,
    const ACE_PEER_CONNECTOR_ADDR &remote_addr,
    ACE_Time_Value *timeout,
    const ACE_PEER_CONNECTOR_ADDR &local_addr,
    int reuse_addr,
    int flags,
    int perms
    );
```

Connection of the svc\_handler with the remote host.  This method also encapsulates the connection done with auto\_purging under the hood. If the connect failed due to the process running out of file descriptors then, auto\_purging of some connections are done from the CONNECTION\_CACHE. This frees the descriptors which get used in the connect process and hence the connect operation can succeed.

```
void cleanup (void);
```

Since g++ version  2.8 arent happy with templates, this special method had to be devised to avoid memory leaks and perform cleanup of the connection\_cache\_.

```
CONNECTION_CACHE connection_cache_;
```

Table that maintains the cache of connected SVC\_HANDLERs.

## AUTHOR

Kirthika Parameswaran kirthika@cs.wustl.edu

## LIBRARY

ace
