//
//  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:   
//
//  Project:    6046
//
//  Author(s):  W. Arcus
//
//  Purpose:    
//
//  Notes:      
//

#include "StdAfx.h"
#include "TCPSocket.h"

#include "..\..\..\Utils\NetUtils\ThreadedSocket.h"

//////////////////////////////////////////////////////////////////////
// CTCPSocket class implementation.

CTCPSocket::CTCPSocket( const unsigned short   &runPort,
                        const unsigned long    &rulSendAddress,
                        PFN_DATACALLBACK        pfnCallback,
                        void *                  pvCallbackParam )

           :m_ulMaxMessageLength( 2 * 1024 * 1024 )
{
    UNREFERENCED_PARAMETER( rulSendAddress );

    try
    {
        m_bInitialized = false;

        m_pClients = new CClientConnections( m_ulMaxMessageLength, static_cast<unsigned int>( runPort ), pfnCallback, pvCallbackParam );

        m_bInitialized = ( m_pClients != NULL );
    }
    catch (... )
    {
        m_bInitialized = false;
        TRACE( _T( "CTCPSocket::CTCPSocket(), Unspecified exception caught.\n" ) );
        ASSERT( false );
    }

    ASSERT( m_bInitialized );
}

CTCPSocket::~CTCPSocket( void )
{
    __TRY
    {
        if ( m_pClients != NULL )
        {
            delete m_pClients;
        }
    }
    __FINALLY
    {
        m_pClients     = NULL;
        m_bInitialized = false;
    }
    __ENDFINALLY
}

bool CTCPSocket::IsInitialized( void ) const
{
    return ( ( m_bInitialized && ( m_pClients != NULL ) ) ? m_pClients->IsInitialized() : false );
}

bool CTCPSocket::Send(  BYTE                   *pbyStream,
                        const unsigned long    &rulNumBytes,
                        const int              &riSocketIndex )
{
    if ( rulNumBytes == 0UL )
    {
        return true;                                                            // Nothing to do.
    }
    else if ( riSocketIndex >= 0 )
    {
        return m_pClients->Send( riSocketIndex, pbyStream, rulNumBytes );
    }

    return false;
}



