//  Beta release only.
//
//  Copyright(c) Ivyware Pty Ltd 2002-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.
//
//
//  P2PeerConWsa definitions and prototypes
//  NOTES: Asynchronously manages WSA TCP/IP connections and
//         facilitates IO Completion port integration
//                      
#pragma once

#ifndef NO_DEBUG_NEW
#define new DEBUG_NEW
#endif

#include "P2PeerCon.h"

///////////////////////////////////////////////////////////////////////
//  P2PeerConWsa connection management
//  NOTES: Instances of these objects wholly manage single WSA
//         connections.  Collections of P2PeerConWsa objects are collated
//         and supervised by P2PeerHub's
//       : P2PeerHub's pump P2PeerConWsa objects through P2PeerCon_MAP's
//         in response to state changes.
//
class P2PmsgKERNEL_EXT P2PeerConWsa : public P2PeerCon
{
      void
        RenderThisSafe();

    // Constructors, destructor and factories
    public:
        P2PeerConWsa ( );
        P2PeerConWsa ( P2PaddrSTR strThatP2Paddr
                     , LPCTSTR  lpszIpAddress, short nIpPort );
        P2PeerConWsa ( P2PaddrSTR strThatP2Paddr
                     , SOCKET   oSocket );
        P2PeerConWsa ( P2PaddrSTR strThatP2Paddr
                     , short nIpPort );
      virtual
       ~P2PeerConWsa ( );

      static P2PeerConWsa*
        ServiceFactory( P2PaddrSTR strP2PaddrThat
                      , short nIpPort );
      static P2PeerConWsa*
        ClientFactory ( P2PaddrSTR strP2PaddrThat
                      , LPCTSTR lpszIpAddress, short nIpPort );
      virtual P2PeerCon*
        AcceptSpawn ( P2PeerCon *pCon );

    // 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 bool
          Connect ( );
      virtual void
          Close   ( );
      virtual conRESULT
        OnClose  ( );

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

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

    // System
    // NOTES: Win32 extensions and packaging
    protected:
      BOOL
        ConnectEx ( SOCKET oSocket
                  , const struct sockaddr FAR *name
                  , int namelen
                  , PVOID lpSendBuffer OPTIONAL
                  , DWORD dwSendDataLength
                  , LPDWORD lpdwBytesSent
                  , LPOVERLAPPED lpOVERLAPPED );
      BOOL
        ConnectEx ( SOCKET oSocket
                  , const struct sockaddr FAR *name
                  , int namelen
                  , PVOID lpSendBuffer OPTIONAL
                  , DWORD dwSendDataLength
                  , LPDWORD lpdwBytesSent
                  , OVERLAPPEDcon *pOVERLAPPEDcon );
      static DWORD WINAPI
        ConnectExThread ( void *pvData );

    // Attributes
    protected:
      SOCKET         m_oSocket;
      SOCKET         m_oSocketAccept;

      CString        m_sIPaddress;
      short          m_nIPort;
};
