Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Network Development Kit 2.0

, 29 Dec 2006
Network Development Kit is a set of simple classes for a client-server architecture.
ndk_demo.zip
NDK_demo
ChatClient
res
ChatClient.ico
NDK
ChatServer
res
ChatServer.ico
NDK
ChatClient.exe
ChatServer.exe
ndk_dll_demo.zip
NDK_dll_demo
ChatClient.exe
ChatServer.exe
NDK.dll
ChatClient
res
ChatClient.ico
ChatServer
res
ChatServer.ico
NDK
NDK.def
res
ndk_dll_src.zip
NDK_dll_src
res
NDK.def
mssccprj.scc
vssver.scc
ndk_src.zip
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                                                                   //
////////////////////////////////////////////////////////////////////////////////
#pragma once
#include <afxsock.h>
#include "NDKDefines.h"
#include "NDKUserManager.h"

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

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

// Enumeration of all type of disconnection
enum NDKServerDisconnection
{ 
	NDKServer_NormalDisconnection, 
	NDKServer_ClientCloseConnection,
	NDKServer_ErrorSendingMessage, 
	NDKServer_ErrorReceivingMessage
};

typedef CArray<long, long> CLongArray;


////////////////////////////////////////////////////////////////////////////////
//                                                                            //
// CNDKServer (abstract class)                                                //
//                                                                            //
// -------------------------------------------------------------------------- //
//                                                                            //
// This class implements the server side of the client/server architecture.   //
// The derived class must provide implementation for four pure virtual.       //
// methods. These functions are CanConnect, OnConnect, OnMessage and          //
// OnDisconnect. The server sends a message when it disconnects a client or   //
// when the server stops.                                                     //
//                                                                            //
// Each user connected to the server is assigned a unique Id (long).          //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////

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

	// Constructor.
	CNDKServer();

	// Destructor.
	virtual ~CNDKServer();

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

	// Returns if the server is started.
	BOOL IsStarted() const;

	// Returns the port.
	long GetPort() const;

	// Returns the number of connected users.
	long GetNbUsers() const;

	// Returns the Ids of all connected users.
	void GetUserIds(CLongArray& alIds) const;

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

	// Starts listening on the specified port.
	BOOL StartListening(long lPort);

	// Stops the server. A message is sent to all users to inform them.
	void Stop();

	// Sends a message to a specified user. If a problem occurs, OnDisconnect
	// callback will be called.
	BOOL SendMessageToUser(long lUserId, CNDKMessage& message);

	// Sends a message to all users. OnDisconnect callback will be called for 
	// each user that the message cannot be sent.
	BOOL SendMessageToAllUsers(CNDKMessage& message);

	// Sends a message to some user specified by the array of user Id. 
	// OnDisconnect callback will be called for each user that the message 
	// cannot be sent.
	BOOL SendMessageToSomeUsers(const CLongArray& alUserIds, CNDKMessage& message);

	// Sends a message to all users except for the specified user Id.
	// OnDisconnect callback will be called for each user that the message 
	// cannot be sent.
	BOOL SendMessageToAllUsersExceptFor(long lUserId, CNDKMessage& message);

	// Sends a message to all users that aren't in the specified array of user 
	// Id. OnDisconnect callback will be called for each user that the message 
	// cannot be sent.
	BOOL SendMessageToAllUsersExceptFor(const CLongArray& alUserIds, 
										CNDKMessage& message);

	// Disconnects a specified user. OnDisconnect callback will not be called 
	// for user disconnected that way.
	BOOL DisconnectUser(long lUserId);

	// Disconnects all users. OnDisconnection callback will not be call.
	void DisconnectAllUsers();

	// Pings a user. OnPing callback will be called when the server receives
	// the response from the user. OnDisconnect callback might be called if
	// a problem occurs.
	BOOL PingUser(long lUserId);

	// Pings all users. OnPing callback will be called when the server
	// receives the response from each user. OnDisconnect callback might be 
	// called if a problem occurs.
	void PingAllUsers();

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

	// Called when a user tries to connect to the server. Return TRUE to accept
	// the connection or FALSE otherwise. The derived class must override this
	// method.
	virtual BOOL OnIsConnectionAccepted() = 0;

	// Called when a user is connected to the server. The derived class must 
	// override this method.
	virtual void OnConnect(long lUserId) = 0;

	// Called whenever a message is received from a user. The derived class must 
	// override this method.
	virtual void OnMessage(long lUserId, CNDKMessage& message) = 0;

	// Called whenever a user is disconnected (the user might have closed 
	// the connection or an error occurred when sending a message, for example). 
	// DisconnectUser don't need to be called when OnDisconnect callback is 
	// used. The derived class must override this method.
	virtual void OnDisconnect(long lUserId, NDKServerDisconnection disconnectionType) = 0;

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

private:
	////////////////////////////////////////////////////////////////////////////
	// Private Operations                                                     //
	////////////////////////////////////////////////////////////////////////////
	
	// Processes pending accept.
	void ProcessPendingAccept(long lErrorCode);

	// Processes pending read.
	BOOL ProcessPendingRead(CNDKServerSocket* pServerSocket, long lErrorCode);

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

	// Disconnects a user.
	BOOL DisconnectUser(long lUserId, NDKServerDisconnection disconnectionType);

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

	friend class CNDKServerSocket;

	////////////////////////////////////////////////////////////////////////////
	// Disable Copy-Constructor and Assignment Operator                       //
	////////////////////////////////////////////////////////////////////////////
	
	CNDKServer(const CNDKServer&) {};
	void operator=(const CNDKServer &) {};
		
private:
	long			  m_lPort;
	CNDKUserManager   m_userMgr;
	CNDKServerSocket* m_pListeningSocket;
};

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.
Follow on   Twitter

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