//
//  Copyright © 2004, RESON Inc. All Rights Reserved.
//
//  No part of this file may be reproduced or transmitted in any form or by
//  any means, electronic or mechanical, including photocopy, recording, or
//  information storage or retrieval system, without permission in writing
//  from RESON Inc.
//
//  Filename:   7kSharedMemory.h
//
//  Project:    6046
//
//  Author(s):  W. Arcus -- re-worked class based on original supplied by P. Moberg.
//
//  Purpose:    Defines a pure virtual base class to provide common 7k sonar MMF data I/O.
//
//  Notes:      
//

#if !defined(AFX_7KSHAREDMEMORY_H__775EC0D3_2384_4AFF_9890_8467DF76CB3A__INCLUDED_)
#define AFX_7KSHAREDMEMORY_H__775EC0D3_2384_4AFF_9890_8467DF76CB3A__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "..\..\..\Utils\NetUtils\Critical.h"

#pragma pack( push, PACK_7KSHAREDMEMORY_H, 1 )

typedef struct tagMMFHEADER7KSONAR
{
    DWORD           dwWrite;                                                        // Pointer for next write.
    DWORD           dwReserved;                                                     // Reserved.
    ULARGE_INTEGER  uliTotalSize;                                                   // Total size in ...???
    DWORD           dwWrap;                                                         // Wrap point in circular buffer area of MMF.
    DWORD           dwSize;                                                         // MMF data size (following header).
}
MMFHEADER7KSONAR, *PMMFHEADER7KSONAR;

#pragma pack( pop, PACK_7KSHAREDMEMORY_H )

class C7kSharedMemory
{
public:

    ///////////////
    // Services.

                        C7kSharedMemory (   const DWORD            &rdwTotalMMFSize );
    virtual            ~C7kSharedMemory (   void );

    bool                Open            (   LPCTSTR                 lpsz7kMMFName,
                                            LPCTSTR                 lpsz7kMutexName,
                                            LPCTSTR                 lpsz7kEventName,
                                            BOOL                    bManualResetEvent   = FALSE );

    bool                Close           (   void );

protected:

    enum EFAILUREMODE
    {
        failureModeInvalidMMFName,
        failureModeInvalidMutexName,
        failureModeInvalidEventName,
        failureModeFileMapping,
        failureModeMutex,
        failureModeEvent,
        failureModeMapView
    };

    ///////////////
    // Attributes.

    const DWORD         m_dwTotalMMFSize;
    const DWORD         m_dwHeaderSize;

    mutable CCritical   m_Critical;

    HANDLE              m_hMMFile;
    HANDLE              m_hEvent;
    HANDLE              m_hMutex;
    HANDLE              m_hTerminateMutexWaitEvent;

    BYTE               *m_pbyMMFMemory;
    MMFHEADER7KSONAR   *m_psMMFHeader;

    ULARGE_INTEGER      m_ullReadPos;

    ///////////////
    // Services.

    DWORD               GetMemorySize           (   void )                                      const;

    bool                GetPositions            (   DWORD                  *pdwWritePoint,
                                                    DWORD                  *pdwWrapPoint,
                                                    unsigned __int64       *pui64TotalSize )    const;

    bool                CopyData                (   BYTE                   *pbyData,
                                                    const DWORD            &rdwSize,
                                                    const DWORD            &rdwOffset );

    void                TerminateWaitingMutex   (   void );

private:

    ///////////////
    // Services.

    bool                IsNameValid             (   LPCTSTR                 lpszName )          const;

                        C7kSharedMemory         (   void );                                     // Not implemented thus private.
                        C7kSharedMemory         (   const C7kSharedMemory   &rRhs );            // Not implemented thus private.
    C7kSharedMemory &   operator =              (   const C7kSharedMemory   &rRhs );            // Not implemented thus private.

};

#endif // !defined(AFX_7KSHAREDMEMORY_H__775EC0D3_2384_4AFF_9890_8467DF76CB3A__INCLUDED_)
