Click here to Skip to main content
15,891,184 members
Articles / Desktop Programming / MFC

Network Development Kit 2.0

Rate me:
Please Sign up or sign in to vote.
4.88/5 (114 votes)
29 Dec 2006CPOL 2.9M   25.7K   495  
Network Development Kit is a set of simple classes for a client-server architecture.
////////////////////////////////////////////////////////////////////////////////
//                                                                            //
// 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. PingServer is added. Close connection sends a message //
//    to the server.                                                          //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
// Includes                                                                   //
////////////////////////////////////////////////////////////////////////////////
#pragma once
#include <afxsock.h>
#include "NDKDefines.h"

////////////////////////////////////////////////////////////////////////////////
// Forward declarations                                                       //
////////////////////////////////////////////////////////////////////////////////
class CNDKMessage;

////////////////////////////////////////////////////////////////////////////////
// Defines                                                                    //
////////////////////////////////////////////////////////////////////////////////

// Enumeration of all type of disconnection of the client
enum NDKClientDisconnection
{ 
	NDKClient_NormalDisconnection, 
	NDKClient_ServerCloseConnection,
    NDKClient_ServerStop, 
	NDKClient_ErrorSendingMessage, 
	NDKClient_ErrorReceivingMessage
};


////////////////////////////////////////////////////////////////////////////////
//                                                                            //
// CNDKClient (abstract class)                                                //
//                                                                            //
// -------------------------------------------------------------------------- //
//                                                                            //
// This class implements the client side of the client/server architecture.   //
// The derived class must provide implementation for two pure virtual         //
// methods. These functions are OnMessage and OnDisconnect.                   //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////

class NDK_EXT_CLASS CNDKClient
{
public:
	////////////////////////////////////////////////////////////////////////////
	// Constructors / Destructor                                              //
	////////////////////////////////////////////////////////////////////////////

	// Constructor.
	CNDKClient();

	// Destructor.
	virtual ~CNDKClient();

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

	// Returns if the client is connected to the server.
	BOOL IsConnected() const;

	// Gets the IP address and port number of the client.
	BOOL GetIpAndPort(CString& strIp, long& lPort) const;

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

	// Opens a connection to the server, given its IP address and the port 
	// number. The IP can be a machine name as "ftp.ndk.com", or a dotted number 
	// as "123.456.78.9".
	BOOL OpenConnection(const CString& strServerIp, long lPort);

	// Closes an established connection with the server and a message is sent to
	// the server. OnDisconnection callback will be call with the value
	// NDKNormalDisconnection.
	void CloseConnection();
	
	// Sends a message to the server. If a problem occurs, the connection will
	// be closed and OnDisconnect callback will be called with the value 
	// NDKErrorSendingMessage.
	BOOL SendMessageToServer(CNDKMessage& message);

	// Pings the server. OnPing callback will be called when the client
	// receives the response from the server. If a problem occurs, the 
	// connection will be closed and OnDisconnect callback will be called with 
	// the value NDKErrorSendingMessage. Overrides OnPing in the derived class 
	// if PingServer is used.
	BOOL PingServer();

protected:
	////////////////////////////////////////////////////////////////////////////
	// Callbacks                                                              //
	////////////////////////////////////////////////////////////////////////////

	// Called when a message is received. The derived class must override this
	// method.
	virtual void OnMessage(CNDKMessage& message) = 0;

	// Called whenever a disconnection occurs. The NDKDisconnectionType specify
	// how the disconnection occurred. CloseConnection don't need to be called 
	// when OnDisconnect is used. The derived class must override this method.
	virtual void OnDisconnect(NDKClientDisconnection disconnectionType) = 0;

	// Called when the ping from the server is received. The number of
	// milliseconds is returned since PingServer was called.
	virtual void OnPing(long lNbMilliseconds);

private:
	////////////////////////////////////////////////////////////////////////////
	// Private Operations                                                     //
	////////////////////////////////////////////////////////////////////////////

	// Processes pending read. If a problem occurs, the connection will
	// be closed and OnDisconnect callback will be called with the value 
	// NDKErrorReceivingMessage.
	BOOL ProcessPendingRead(long lErrorCode);

	// Translates message and does the appropriate task for message handled by
	// the NDK.
	void TranslateMessage(CNDKMessage& message);

	// Closes the connection.
	void CloseConnection(NDKClientDisconnection disconnectType);

	// Cleans up.
	void CleanUp();

	////////////////////////////////////////////////////////////////////////////
	// Friendship                                                             //
	////////////////////////////////////////////////////////////////////////////

	friend class CNDKClientSocket;

	////////////////////////////////////////////////////////////////////////////
	// Disable Copy-Constructor and Assignment Operator                       //
	////////////////////////////////////////////////////////////////////////////
	
	CNDKClient(const CNDKClient&) {};
	void operator=(const CNDKClient &) {};
	
private:
	CNDKClientSocket* m_pClientSocket;
	CSocketFile*	  m_pFile;
	CArchive*		  m_pArchiveIn;
	CArchive*		  m_pArchiveOut;
};

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)


Written By
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.

Comments and Discussions