//  Beta release only.
//
//  Copyright(c) Ivyware Pty Ltd 2007-10  (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.
//
//
//  P2PmsgBSTR implementation
//
#pragma once

#include "wtypes.h"
#include "P2Peer.h"
#include "P2PTL.h"
#include "P2Pmsg.h"

#define  UINT08 UCHAR

///////////////////////////////////////////////////////////////////////
//  VBLockBSTR containers and definitions
//  NOTES: P2Pmsg exchange format, VBLock'ed binary sequence
//
#define VBLockBSTR_ROOT     0          // P2Pmsg root
#define VBLockBSTR_NET  (1<<0)         // P2Pmsg networking
#define VBLockBSTR_SYS  (1<<1)         //        system
#define VBLockBSTR_EVT  (1<<2)         //        events
#define VBLockBSTR_WRP  (1<<3)         //        wrapped VBLockBSTR
#define VBLockBSTR_MSG  (1<<4)         //        application message data
#define VBLockBSTR_SP4  (1<<5)         //        spare4
#define VBLockBSTR_SP5  (1<<6)         //        reserved5
#define VBLockBSTR_SP6  (1<<7)         //        reserved6

#pragma pack(push,1)
typedef struct
{
    VBLockHdr  oHdr;
    VBLock     oNode;
} VBLockBSTR;
#pragma pack(pop)

#pragma pack(push,1)
typedef struct
{
    struct
    {
      UINT32  uiSync1;
      UINT32  uiSync2;
    } oSync;
    char cIOmage;
} VBListIOmage;
#pragma pack(pop)

///////////////////////////////////////////////////////////////////////
//  P2PmsgBSTR class
//  NOTES: P2Pmsg virtual blocked list manager
//       : Base class implementation delegates all memory management
//         directly through to heap.  Specialise this class for
//         alternative memory management models
class P2PmsgKERNEL_EXT P3PmsgBSTR
{
      void
        RenderThisSafe ( );
      void
        ResetThisObject ( );

    // Constructors and destructor
    public:
        P3PmsgBSTR ( );

        P3PmsgBSTR ( UCHAR uVBLockAddr, UINT nSizeof );

        P3PmsgBSTR ( const P3PmsgBSTR& rhs );

        P3PmsgBSTR ( const VBListIOmage& oIOmage );

        P3PmsgBSTR ( VBListIOmage *pIOmage );

        P3PmsgBSTR ( P2PmsgHANDLE hBSTR );
      virtual
       ~P3PmsgBSTR ( );

   // Initialisation
   public:
      P3PmsgBSTR&
        Init ( P2PmsgHANDLE hBSTR );
      P3PmsgNode&
        Init ( LPCTSTR lpszName, const P3PmsgData& oNodeData );
      //P3PmsgNode&
      //  InitAddr ( );
      P3PmsgNode&
        InitData ( P3PmsgData& oNodeData );
      P3PmsgNode&
        InitNode ( UCHAR uVBLockBSTR, const P3PmsgData& oNodeData );

    // Operators
    public:
      P3PmsgBSTR&
        operator = ( const P3PmsgBSTR& rhs );

    // Completion Port images
    // NOTES: IOCP image preparation and release
    public:
      void*
        PrepareIOmage( DWORD uBSTRmask );
      void*
        IOmage ( );
      UINT
        IOmageSize ( );
      void
        ReleaseIOmage( );

    // Troubleshooting
    public:
      virtual void
        AssertValid ( ) const;
      virtual void
        Print ( FILE *fd, int nDepthOS, int nDepthOSinc = 1  );

    // Properties
    public:
      P2Pmsgnn_t 
        GetP2Pmsgnn ( ) const;
      static void
        SetDefaultP2Pmsgnn ( P2Pmsgnn_t uP2Pmsgnn );
      static P2Pmsgnn_t
        GetDefaultP2Pmsgnn ( );
      bool
        Exists  ( UCHAR eVBLockBSTR );
      P3PmsgNode&
        r_node  ( UCHAR eVBLockBSTR, bool bCreate = false );
      P3PmsgNode&
        r_datn  ( );
      P3PmsgName&
        r_name  ( );
      P3PmsgData&
        r_data  ( );
      void*
        VBLockBSTR_vp ( );
      UINT
        Sizeof ( ) const;
      static UINT
        SetDefaultSizeof ( );
      bool
        IsFragmented ( );
      bool
        IsDirty ( );

    // Attributes
    private:
        P2PmsgHANDLE  m_hBSTR;
        P3PmsgBSTR   *m_pP2PmsgBSTRiomage;

        P3PmsgNode    m_oNode;
        VBLockBSTR    m_oBSTR;
        VBLockBSTR   *m_pBSTR;
        VBLockBSTR    m_oVBLockBSTR;
        VBLockBSTR   *m_pVBLockBSTR;
};

///////////////////////////////////////////////////////////////////////
//  Persistance extensions
//  NOTES: Manage P3PmsgBSTR disk IO

P2PmsgKERNEL_EXT BOOL
P2PmsgBSTR_Read ( LPCTSTR lpszPathname, P3PmsgBSTR& oBSTR );
P2PmsgKERNEL_EXT BOOL
P2PmsgBSTR_Read ( HANDLE hFile, P3PmsgBSTR& oBSTR );
P2PmsgKERNEL_EXT BOOL
P2PmsgBSTR_Write ( LPCTSTR lpszPathname, const P3PmsgBSTR& oBSTR );
P2PmsgKERNEL_EXT BOOL
P2PmsgBSTR_Write ( HANDLE hFile, const P3PmsgBSTR& oBSTR );
