|
////////////////////////////////////////////////////////////////////////////////
// //
// 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.
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.