//
//  Copyright © 2002, 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:   MessageProcessor.h
//
//  Project:    6046
//
//  Author(s):  W. Arcus
//
//  Purpose:    
//
//  Notes:      
//

#if !defined(AFX_MESSAGEPROCESSOR_H__AD1F9777_FA51_454F_BE28_2AFEC2BD41F9__INCLUDED_)
#define AFX_MESSAGEPROCESSOR_H__AD1F9777_FA51_454F_BE28_2AFEC2BD41F9__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "SensorItem.h"

#include "EdgeTechNetworkProtocol.h"

#include "..\..\..\Utils\NetUtils\SWMRG.h"
#include "..\..\..\Utils\NetUtils\Critical.h"
#include "..\..\..\Utils\NetUtils\ThreadedSocket.h"

class CMessageProcessor
{
public:

    ///////////////
    // Services.

                                    CMessageProcessor           (   PFN_HANDLER_CALLBACK    pfnHandlerCallback = NULL,
                                                                    void                   *pvParam            = NULL,
                                                                    char                   *pszAddress         = "127.0.0.1",
                                                                    const unsigned long    &rulPort            = 1024UL,
                                                                    const unsigned long    &rulMaxMessageSize  = 1024UL * 100UL );

    virtual                        ~CMessageProcessor           (   void );

    bool                            SendCommand                 (   CEdgeTechMessage       &rCommand );

protected:
    
    ///////////////
    // Definitions.

    typedef CThreadedSocket<CEdgeTechNetworkProtocol>       CSonarChannel_t;

    ///////////////
    // Attrubutes

    const unsigned long             m_ulInvalidPort;
    const PFN_HANDLER_CALLBACK      m_pfnHandlerCallback;
    const void                     *m_pvParam;

    CCritical                       m_DataGuard;
    CCritical                       m_SendGuard;

    CEdgeTechMessage                m_InboundMessage;
    std::auto_ptr<CSonarChannel_t>  m_pChannel;

    ///////////////
    // Services.

    void                            HandleInboundMessage        (   BYTE                *pbyData,
                                                                    unsigned long        ulNumBytes,
                                                                    unsigned long        ulTimeStamp );

    bool                            CallCallback                (   const unsigned long &rulTimeStamp,
                                                                    CEdgeTechMessage    &rMessage );

    // Service(s) to be overridden.

    virtual
    bool                            HandleSonarMessage          (   const unsigned long &rulTimeStamp,
                                                                    CEdgeTechMessage    &rMessage )     = 0;

private:

    ///////////////
    // Services.
    
    static void                     MessageCallback             (   BYTE                   *pbyData,               // Data pointer where relevant - can be NULL.
                                                                    PVOID                   lpvParam,              // Optional parameter.
                                                                    ULONG                   ulNumBytes,            // Number of data bytes received - can be zero.
                                                                    INT                     iSocketIndex,          // Socket index (relevant to server end only - will be zero otherwise).
                                                                    ULONG                   ulTimeStamp,           // Timestamp.
                                                                    INT                     iReasonForCallback );  // Reason for the callback.

    static void                     NotifyOfDisconnect          (   void );

    // Not implemented thus private.

                                    CMessageProcessor           (   const CMessageProcessor &rRhs );
    CMessageProcessor &             operator =                  (   const CMessageProcessor &rRhs );

};

#endif // !defined(AFX_MESSAGEPROCESSOR_H__AD1F9777_FA51_454F_BE28_2AFEC2BD41F9__INCLUDED_)
