//
//  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:   UDPSocket.h
//
//  Project:    6046
//
//  Author(s):  W. Arcus
//
//  Purpose:    
//
//  Notes:      
//

#if !defined(AFX_UDPSOCKET_H__8D702F23_1B0B_492C_97F2_948CE415EE73__INCLUDED_)
#define AFX_UDPSOCKET_H__8D702F23_1B0B_492C_97F2_948CE415EE73__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "DataCallback.h"
#include "BaseThread.h"

class EXPORT_DLL CUDPSocket
{
public:

    ///////////////
    // Definitions.

    typedef enum tagEMODE
    {
        modeClient      = 0,
        modeServer
    }
    EMODE;

    ///////////////
    // Services.

                        CUDPSocket      (   const EMODE                            &reMode,
                                            const unsigned short                   &runPort,
                                            const bool                             &rbBroadCast         = false,
                                            const unsigned long                    &rulSendAddress      = 0UL,
                                            PFN_DATACALLBACK                        pfnCallback         = NULL,
                                            void *                                  pvCallbackParam     = NULL,
                                            const unsigned long                     rulMaxBufferSize    = 64UL * 1024UL );


    virtual            ~CUDPSocket      (   void );

    bool                IsInitialized   (   void )  const;

    EMODE               Mode            (   void )  const;

    bool                Send            (   const BYTE                             *pbyData,
                                            const unsigned long                    &rulBytes );

private:

    ///////////////
    // Definitions.

    class CServer : private CBaseThread
    {
    public:

        /////////////////////
        // Services.

                        CServer         (   CUDPSocket                             &rParent,
                                            const unsigned long                    &rulBufferSize );

        virtual        ~CServer         (   void );

    protected:

        void            WatchCycle      (   void );

    private:

        /////////////////////
        // Attributes.

        CUDPSocket                         &m_rParent;
        const unsigned long                 m_ulBufferSize;

        BYTE                               *m_pbySocketData;

        /////////////////////
        // Services.

                        CServer         (   void );                                                 // Not implemented thus private.
                        CServer         (   const CServer                          &rRhs );         // Not implemented thus private.
        CServer &       operator =      (   const CServer                          &rRhs );         // Not implemented thus private.

    };

    friend class                            CServer;                                                // Grant CServer helper class access to CUDPSocket's private and protected members.

    ///////////////
    // Attributes.

    const unsigned long                     m_ulBufferSize;

    bool                                    m_bInitialized;
    bool                                    m_bBroadcast;
    EMODE                                   m_eMode;

    SOCKET                                  m_hSocket;
    unsigned short                          m_unPort;
    unsigned long                           m_ulSendAddress;

    PFN_DATACALLBACK                        m_pfnCallback;
    void *                                  m_pvCallbackParam;

    CServer                                *m_pServer;

    ///////////////
    // Services.

    bool                Listen          (   void );

    bool                Mode            (   const EMODE                            &reMode,
                                            const unsigned short                   &runPort );

    bool                ReadBytes       (   BYTE                                   *pbyData,
                                            unsigned long                          &rulByteRead,
                                            const unsigned long                    &rulMaxBufferSize );

    void                CloseSocket     (   void );

    void                ShutdownServer  (   void );

                        CUDPSocket      (   void );                                                 // Not implemented thus private.
                        CUDPSocket      (   const CUDPSocket                       &rRhs );         // Not implemented thus private.
    CUDPSocket &        operator =      (   const CUDPSocket                       &rRhs );         // Not implemented thus private.

};

#endif // !defined(AFX_UDPSOCKET_H__8D702F23_1B0B_492C_97F2_948CE415EE73__INCLUDED_)
