//  Beta release only.
//
//  Copyright(c) Ivyware Pty Ltd 2005-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.
//
//
//  P2Peerio definitions and prototypes
//  NOTES: Base class from which all P2Peer protocol translation
//         objects are derived.  Such objects are aggregated within
//         P2PeerCon derived objects
//       : Manages P2PeerMsg image exchanges
//                      
#pragma once

#ifndef NO_DEBUG_NEW
#define new DEBUG_NEW
#endif

#include "P2Peer.h"
#include "P2PeerMsg.h"

//
//  P2Peer overlapped IO control
//  NOTES: Extended implementation of the Win32 OVERLAPPED structure
typedef struct
{
    OVERLAPPED   oOverlapped;          // Win32
    P2PsigID     nSigID;               // Signal
    P2PmsgHANDLE hP2Pmsg;              // Message memory HANDLE
    HRESULT      hr;
    int          eAction;
    bool         bQueued;              // Queued state
    bool         bDelete;              // Delete
    DWORD       dwUserData;
    DWORD       dwBytesMax;
    DWORD       dwBytes;               // Bytes in buffer
    char        *pUserDB1;             // User defined buffer1
    char        *pUserDB2;             // User defined buffer2
    char        *pBuffer;              // Buffer
} OVERLAPPEDcon;

//
//  P2Peer protocol management
//  NOTES: Instances of these objects manage protocol translation for
//         P2PeerMsg's exchanged between P2PeerHub's
//       : Aggregated within P2PeerCon objects
//
class P2PeerCon;
class P2PmsgKERNEL_EXT P2Peerio
{
      void
        RenderThisSafe();

    // Constructors and destructor
    public:
        P2Peerio ( );
      virtual
       ~P2Peerio ( );
      virtual P2Peerio*
        Clone ( );

    // Registration
    public:
      P2Peerio*
        Register ( P2PeerCon *pCon );

    // IOCP Integration
    public:
      virtual bool
        On_QueuedCompletionStatus ( DWORD dwError
                                  , DWORD dwBytes
                                  , OVERLAPPEDcon *pOVERLAPPEDcon );

    // Protocol translation
    // NOTES: Perform raw P2PeerMsg exchange between P2PeerHub's
    public:
      virtual DWORD
        SendP2PeerMsg ( HANDLE hFile
                      , P2PeerMsg *pMsg
                      , OVERLAPPEDcon *pOVERLAPPEDsend );
      DWORD
        Send ( HANDLE hFile
             , const void *pvData, DWORD wDataSize
             , OVERLAPPEDcon *pOVERLAPPEDsend );
      virtual P2PeerMsg*
        PreDestroySendP2PeerMsg ( P2PeerMsg *pMsg );
      virtual P2PeerMsg*
        RecvP2PeerMsg ( HANDLE hFile
                      , OVERLAPPEDcon *pOVERLAPPEDrecv );
      DWORD
        Recv ( HANDLE hFile
             , void *pvData, DWORD wDataSize
             , OVERLAPPEDcon *pOVERLAPPEDrecv );

    // Timers
    public:
      PITimerID
        SetPITimer ( P2Pmsecs_t uMSecDelay, DWORD dwUserKey = 0 );
      virtual void
        On_PITimer ( bool bCancel
                   , PITimerID nPITimerID, DWORD dwUserKey );

    // Specialisation
    public:
      virtual void
           Acknowledge ( HANDLE hFile, P2PeerMsg *pMsg );
      virtual bool
        On_Acknowledge ( bool bAckNak );
      virtual const void*
        EncryptMsgSwap ( const void *cvpSendMsg, DWORD nSendMsgSize
                       , DWORD& nSendMsgEncrytedSize );
      virtual void*
        DecryptMsgSwap ( const void *cvpRecvMsgEncrypted, DWORD nRecvMsgSize
                       , DWORD& nRecvMsgDecryptedSize );
      virtual void
        Reset ( );

    // Troubleshooting
    public:
      virtual void
        AssertValid ( ) const;

    // Properties
    public:
      bool
        SetIFTrace ( bool bIFTRaceEoD );
      bool
        GetIFTrace ( );
      P2PeerCon*
        GetP2PeerCon ( );
      DWORD
        SetIFmask ( DWORD dwIFmask );
      DWORD
        GetIFmask ( );
      DWORD
        GetMaxRecvSize ( ) const;
      DWORD
        SetMaxRecvSize ( DWORD dwMaxRecvSize );
      DWORD
        GetMaxSendSize ( ) const;
      DWORD
        SetMaxSendSize ( DWORD dwMaxSendSize );

    // Attributes
    protected:
      P2PeerCon      *m_pCon;
      char           *m_pEncryptedMsg;
      bool            m_bIFTraceEoD;
      bool            m_bAckEoD;
      DWORD           m_dwMaxRecvSize;
      DWORD           m_dwMaxSendSize;
      DWORD           m_dwIFmask;
      OVERLAPPEDcon  *m_pOVERLAPPEDack;
      PITimerID       m_nPITimerIDack;
};

