//  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.
//
//
//  P2PeerConPipe definitions and prototypes
//  NOTES: Asynchronously manages serial named pipe connections and
//         facilitates IO Completion port integration
//

#pragma once

#ifndef NO_DEBUG_NEW
#define new DEBUG_NEW
#endif

#include "P2PeerCon.h"

///////////////////////////////////////////////////////////////////////
//  P2PeerConPipe serial connection management
//  NOTES: Instances of these objects wholly manage single named pipe
//         serial connections.  Collections of P2PeerConPipe objects are
//         be supervised via P2PeerHub's
//       : P2PeerHub's pump P2PeerConPipe objects through P2PeerCon_MAP's
//         in response to state changes.
//
class P2PmsgKERNEL_EXT P2PeerConPipe : public P2PeerCon
{
      void
        RenderThisSafe();

    // Constructors and destructor
    public:
        P2PeerConPipe ( );
        P2PeerConPipe ( P2PaddrSTR pThatP2PaddrSTR
                      , LPCTSTR lpszPipename
                      , P2Peerio  *pP2Peerio = 0 );
        P2PeerConPipe ( LPCTSTR lpszPipename
                      , P2Peerio  *pP2Peerio = 0 );
      virtual
       ~P2PeerConPipe ( );

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

    // IOCP Integration
    public:
      /*virtual bool
        On_QueuedCompletionStatus ( DWORD dwError
                                  , DWORD dwBytes
                                  , OVERLAPPEDcon *pOVERLAPPEDcon );*/
      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 void
        OnListen  ( );
      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;

    // Properties
    public:
      virtual bool
        HasDroppedOut ( HRESULT hr );

    // Attributes
    protected:
      CString  m_sPipename;
};

