//  Beta release only.
//
//  Copyright(c) Ivyware Pty Ltd 2006-9  (all rights reserved)
//               MELBOURNE, VICTORIA, AUSTRALIA, 3130
//
//  Provided no fee is charged (either by the distributer or user)
//  permission to use, copy, modify, and distribute this software and
//  its documentation for any purpose and without fee is hereby granted,
//  provided that the above copyright notice appear in all copies and
//  that both that copyright notice and this permission notice appear
//  in supporting documentation.
//
//  No waranty or suitablity for purpose is implied.
//
//
//  P2PeerConDmx definitions and prototypes
//  NOTES: Asynchronously manages in-process P2PeerHub data exchanges
//         and facilitates IO Completion port integration
//       : Used in conjunction with P2PeerioDmx interface objects
//
#pragma once

#ifndef NO_DEBUG_NEW
#define new DEBUG_NEW
#endif

#include "P2PeerCon.h"

///////////////////////////////////////////////////////////////////////
//  P2PeerConDmx connection management
//  NOTES: Instances of these objects wholly manage the exchange of
//         P2PeerMsg's between P2PeerHubs in the same address space
//       : P2PeerHub's pump P2PeerConDmx objects through P2PeerCon_MAP's
//         in response to state changes.
//
class P2PmsgKERNEL_EXT P2PeerConDmx : public P2PeerCon
{
      void
        RenderThisSafe();

    // Constructors and destructor
    public:
        P2PeerConDmx ( );
        P2PeerConDmx ( P2PaddrSTR strThatP2Paddr
                     , LPCTSTR strServiceName
                     , P2PeerConMode_e  eMode
                     , P2Peerio  *pP2Peerio = 0 );
        P2PeerConDmx ( P2PaddrSTR pThisP2PaddrSTR
                     , P2PeerConMode_e eMode
                     , P2Peerio  *pP2Peerio = 0 );
      virtual
       ~P2PeerConDmx ( );

      static P2PeerConDmx*
        ServiceFactory( P2PaddrSTR strP2PaddrThat
                      , LPCTSTR strServiceName );
      static P2PeerConDmx*
        ClientFactory ( P2PaddrSTR strP2PaddrThat
                      , LPCTSTR strServiceName );
      virtual P2PeerCon*
        AcceptSpawn ( P2PeerCon *pCon );
      void
        Destroy ( );

    // IOCP Integration
    public:
      virtual void
        Drop ( P2Pevent *pEVENT );

    // Connection state operations
    // NOTES: Thread safe operations used to manage connection
    //        state.  Reference from P2PeerCon_MAP handlers only
    public:
      virtual bool
          Listen  ( );
      virtual bool
          Accept  ( );
      virtual P2PeerCon*
        OnAccept  ( );
      virtual void
        OnAccept  ( CP2Paddress oThatP2Paddr );
      virtual bool
          Connect ( );
      virtual void
          Close   ( );
      virtual BOOL
        OnClose   ( );

    // Troubleshooting
    public:
      virtual void
        AssertValid ( ) const;
      virtual P3PmsgNode
        Serialise ( LPCTSTR lpszVar );

    // Properties
    public:
      virtual DWORD
        GetUDState ( );

    // Attributes
    protected:
      P2PeerConDmx    *m_pConThat;
      CString          m_sServiceName;
};
typedef CList<P2PeerConDmx*,P2PeerConDmx*> CListP2PeerConDmx;

