Click here to Skip to main content
12,290,522 members (30,142 online)
Click here to Skip to main content

Stats

958.7K views
24.4K downloads
489 bookmarked
Posted

Network Development Kit 2.0

, 29 Dec 2006 CPOL
Network Development Kit is a set of simple classes for a client-server architecture.
NDK_demo
ChatClient
res
ChatClient.ico
NDK
ChatServer
res
ChatServer.ico
NDK
ChatClient.exe
ChatServer.exe
NDK_dll_demo
ChatClient.exe
ChatServer.exe
NDK.dll
ChatClient
res
ChatClient.ico
ChatServer
res
ChatServer.ico
NDK
NDK.def
res
NDK_dll_src
res
NDK.def
mssccprj.scc
vssver.scc
NDK_src
////////////////////////////////////////////////////////////////////////////////
//                                                                            //
// NDK 2.0 - Network Development Kit                                          //
//                                                                            //
// Authors: Sebastien Lachance                                                //
//                                                                            //
// E-mail:  netblitz@rocler.qc.ca                                             //
//                                                                            //
// -------------------------------------------------------------------------- //
//                                                                            //
// Permission to use, copy, modify, and distribute this software for any      //
// purpose and without fee is hereby granted. This is no guarantee about the  //
// use of this software. For any comments, bugs or thanks, please email us.   //
//                                                                            //
// -------------------------------------------------------------------------- //
//                                                                            //
// Targeted Platform: Any Windows version                                     //
//                                                                            //
// Last modification: January 2002                                            //
//                                                                            //
// History:                                                                   //
//                                                                            //
// 1- First release of this file.                                             //
// 2- The class is renamed and some optimizations are applied. Hungarian      //
//    notation is used.                                                       //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
// Includes                                                                   //
////////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "NDKUser.h"
#include "NDKServerSocket.h"


////////////////////////////////////////////////////////////////////////////////
// Constructors / Destructor                                                  //
////////////////////////////////////////////////////////////////////////////////

// Constructor.
CNDKUser::CNDKUser()
{
	m_lId = 0;
	m_pServerSocket = NULL;
}


// Constructor with initialization.
CNDKUser::CNDKUser(long lId, CNDKServerSocket* pServerSocket)
{
	ASSERT((lId > 0) && (pServerSocket != NULL));

	m_lId = lId;
	m_pServerSocket = pServerSocket;
}


// Destructor.
CNDKUser::~CNDKUser()
{
}


////////////////////////////////////////////////////////////////////////////////
// Attributes                                                                 //
////////////////////////////////////////////////////////////////////////////////

// Gets the Id.
long CNDKUser::GetId() const
{
	return m_lId;
}


// Returns if the socket is the same as specified.
BOOL CNDKUser::IsSocketEqual(CNDKServerSocket* pServerSocket) const
{
	return m_pServerSocket == pServerSocket;
}


// Returns if the buffer of the socket is empty.
BOOL CNDKUser::IsSocketBufferEmpty() const
{
	ASSERT(m_pServerSocket != NULL);

	BOOL bResult = TRUE;

	if (m_pServerSocket != NULL)
		bResult = m_pServerSocket->IsBufferEmpty();

	return bResult;
}


////////////////////////////////////////////////////////////////////////////////
// Operations                                                                 //
////////////////////////////////////////////////////////////////////////////////

// Sends a message.
BOOL CNDKUser::SendMessage(CNDKMessage& message)
{
	ASSERT(m_pServerSocket != NULL);

	BOOL bResult = FALSE;

	if (m_pServerSocket != NULL)
	{
		TRY
		{
			m_pServerSocket->SendMessage(message);
			bResult = TRUE;
		}
		CATCH(CFileException, e)
		{
			m_pServerSocket->Close();
		}
		CATCH(CArchiveException, e)
		{
			m_pServerSocket->Close();
		}
		END_CATCH
	}

	return bResult;
}


// Reads a message.
BOOL CNDKUser::ReadMessage(CNDKMessage& message)
{
	ASSERT(m_pServerSocket != NULL);

	BOOL bResult = TRUE;

	if (m_pServerSocket != NULL)
	{
		TRY
		{
			m_pServerSocket->ReceiveMessage(message);
		}
		CATCH_ALL(e)
		{
			bResult = FALSE;
		}
		END_CATCH_ALL
	}

	return bResult;
}


// Closes the socket.
void CNDKUser::CloseSocket()
{
	if (m_pServerSocket != NULL)
	{
		m_pServerSocket->ShutDown();
		m_pServerSocket->Close();

		delete m_pServerSocket;
		m_pServerSocket = NULL;
	}
}


////////////////////////////////////////////////////////////////////////////////
// Operators                                                                  //
////////////////////////////////////////////////////////////////////////////////

// Comparison operator.
BOOL CNDKUser::operator==(const CNDKUser& user) const
{
	return (m_lId == user.m_lId) && (m_pServerSocket == user.m_pServerSocket);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

ArchieCoder
Software Developer (Senior) Mirego
Canada Canada
My name is Sébastien Lachance.

I love C# developing Windows Phone and Windows 8 applications.

When I’m not in front of a computer, my hobbies include playing bridge, poker and other card games, biking, reading technology news.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160518.1 | Last Updated 29 Dec 2006
Article Copyright 2000 by ArchieCoder
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid