Click here to Skip to main content
15,896,606 members
Articles / Desktop Programming / MFC

How to create a chat client using a set of communication classes

,
Rate me:
Please Sign up or sign in to vote.
3.71/5 (6 votes)
25 Aug 2003Apache4 min read 79.6K   2.5K   37  
A pair of classes (no mfc) that can be used for tcp/ip _and_ serial communication.
#pragma once

#ifndef _WIN32_WINNT
	#define _WIN32_WINNT 0x0400
#endif

#ifndef _WINBASE_
	#include <WINDOWS.H>
#endif

#ifndef DTLOCKER
	#include "DTLOCKER.h"
	#ifndef DTLOCKER
		#error DtThread: CDtLocker is required!
	#endif
#endif

#define DTTHREAD

//////////////////////////////////////////////////////////////////////////
///
///		
///  @class			DtThread - A Thread wrapper class
///  @version		1.0
///	 @author		Jonas Gauffin
///  @homepage		http://www.gauffin.org/cpp
///	
///  @brief			A thread baseclass, takes care of all thread handling,
///					just overload ThreadFunc and your ready and set.
/// 
///  @since 2003-04-23	jg	File created
///	 @since 2003-07-30	jg	Some things is rewritten
///
///
//////////////////////////////////////////////////////////////////////////

namespace Datatal
{

class DtThread
{
public:
	DtThread(void);
	~DtThread(void);

public:

	/// Start the thread.
	/// @returns 0 = Ok, 1 = Thread already running, 2 = Failed to create & start thread.
	DWORD StartThread();


	/// Stops the thread by signalling m_hStopEvent. If it doesnt work, it uses KillThread
	/// @returns 0 = Ok, 1 = thread is not running, 2 = thread was not stopped.
	DWORD StopThread();

	virtual void OnThreadLogWrite(int nPriority, const char* pszLogEntry) {};

	/// Function that will be called when the thread is running.
	/// You _MUST_ wait for the m_hStopEvent event if you want the StopThread() method to work correctly.
	/// @see StopThread
	virtual void ThreadFunc(HANDLE hStopEvent) = 0;

	/// Returns true if the thread is running.
	bool IsThreadRunning() const { return m_bThreadRunning; };

	/// Locks the thread, by using the threads critical section, should be used when reading critical variables.
	/// @see UnlockThread()
	__inline void LockThread() { m_CritThread.Lock(); };

	/// Unlocks the threads critical section, MUST BE CALLED after LockThread
	/// @see LockThread()
	__inline void UnlockThread() { m_CritThread.Unlock(); };

	/// Will get called when the thread has been stopped.
	virtual void OnThreadStopped() {};

	/// Will get called when the thread is running.
	virtual void OnThreadRunning() {};

	int GetThreadId() const { return m_nThreadId; };
	HANDLE GetThreadHandle() const { return m_hThread; };

protected:

	//Our thread that do all the actual work
	static unsigned __stdcall InternalThreadFunc(void* pClient);


private:
	DtCriticalSection		m_CritThread;		/// Lock when we are using the thread control vars
	bool					m_bThreadRunning;	/// True when the thread is running
	HANDLE					m_hThread;
	int						m_nThreadId;

protected:
	HANDLE					m_hStopEvent;

};
};//Namespace

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 Apache License, Version 2.0


Written By
Founder 1TCompany AB
Sweden Sweden

Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions