⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cngpclient.h

📁 SMAL是short message abstract library的缩写,是由风起水流软件工作室(www.zealware.com)开发的一个支持短信网关系统开发的C++底层抽象接口库
💻 H
字号:
#ifndef _CNGP_CLIENT_H_
#define _CNGP_CLIENT_H_

#include <smal.h>
#include <list>
#include "message.h"
#include "threadpool.h"
#include "cmppconn.h"

//class CMPPConnection;
class CCNGPClient;
class	CWorker;
class	CJob;

typedef std::list<CMPPConnection> aList;

//
//用于处理消息和事件的线程池
//
class CJob : public IJobDesc {
public:
	CJob(CMPPClient	& _client, cmpp_submit & _message, JobType	_type);

	CJob(CMPPClient	& _client, CMPPConnection	& _connection, JobType _type);

	CJob(CMPPClient	& _client, CMPPConnection	& _connection, JobType _type, cmpp_submit_deliver_resp _message);

	CJob(CMPPClient	& _client, CMPPConnection	& _connection, JobType _type, cmpp_cancel_resp _message);

	CJob(CMPPClient	& _client, CMPPConnection	& _connection, JobType _type, cmpp_query_resp _message);

	CJob(CMPPClient	& _client, CMPPConnection	& _connection, JobType _type, cmpp_deliver _message);

	CJob(CMPPClient	& _client, CMPPConnection	& _connection, JobType _type, cmpp_submit _message, int nResult = 0);
	//CJob(CMPP_SPTOSMG css, CCmppServer* cmppserv);
	~CJob();

	//void	OnSpMsg();
protected:
	void	OnSendSubmit();
	void	OnMTSucceed();
	void	OnDeliver();
	void	OnStatusReport();
	void	OnActiveTestResponse();
	void	OnActiveTest();
	void	OnCancelResponse();
	void	OnQueryResponse();
	void	OnTerminate();
	void	OnConnectResponse();
	void	OnSubmitFailed();

	CMPPClient				& client;
	CMPPConnection		& connection;
	cmpp_submit				submitMsg;
	cmpp_cancel_resp	cancelRespMsg;
	cmpp_query_resp		queryRespMsg;
	cmpp_deliver			deliverMsg;
	cmpp_connect_resp	connectRespMsg;
	cmpp_submit_deliver_resp	submitRespMsg;

	int								nResult;

	JobType						type;
};

class CWorker:public IWorker {
	virtual void ProcessJob( IJobDesc* pJobDesc );
};

class /*CMPP_API*/ CMPPClient 
{
	//friend class CJob;
public:
	/**@name Construction */
	//@{
	CMPPClient();

	virtual ~CMPPClient();
	//@}

	/**@name GetFunctions */
	//@{
	/**得到发送成功的SUBMIT消息数 */
	long GetSubmitCount() const
	{	return submitCountTotal;		}

	/**得到发送失败的SUBMIT消息数 */
	long GetSubmitFailCount() const
	{	return submitFailCountTotal;		}

	/**得到发送失败的SUBMITResponse消息数 */
	long GetSubmitRespCount() const
	{	return submitRespCountTotal;		}

	/**得到收到的Deliver消息数 */
	long GetDeliverCount() const
	{	return deliverCountTotal;		}

	/**得到收到的StatusReport消息数 */
	long GetStatusReportCount() const
	{	return sreportCountTotal;		}
	//@}

	/**@name Main Functions */
	//@{
	/**创建一个连接token */
	CString CreateToken() const;

	virtual void DumpStatus();

	/**初始化Client函数.
	注意: 每个Client在进行所有操作之前必须调用Initialize()进行初始化操作
	*/
	void Initialize(
		unsigned nMinWorkThreadsCount = 4,
		unsigned nMaxWorkThreadsCount = 8
		);

	/**记录Client日志信息和connection日志信息,默认将所有信息发送到调试窗口(Debug模式下) */
	virtual void LogWrite(LPCTSTR pchFmt, ...);

	/**建立一个连接 */
	CMPPConnection * MakeConnection(
		const CString			& _smghost,
		const unsigned short & _smgport,
		const unsigned char	& _smgversion,
		const CString			& _username,
		const CString			& _password,
		const CString			& _icpid,
		const CString			& _spid,
		const ConnectionType & _conntype,
		const BOOL				& _isAsiaInfoSmg,
		const int					& _nActivetestInterval,
		BOOL								sync
		);

	/**清除连接 */
	void ClearConnection(CString token);

	/**清除当前Client中的所有连接 */
	void ClearAllConnections();

	/**找到第一个可用的连接 */
	CMPPConnection firstFitRouter(int start);

	/**循环使用可用的连接 */
	CMPPConnection roundRobinRouter();

	/**发送一条Submit消息 */
	int Submit(cmpp_submit & message);

	///**执行消息发送 */
	//void MakeSubmit(
	//	CMPPConnection & connection, 
	//	cmpp_submit & message
	//	);

	//@}

	/**@name Callback functions */
	//@{
	/**登录网关成功时调用 */
	virtual void OnConnectionLogined(CMPPConnection & connection)
	{
		return;
	}

	/**登录网关失败时调用 */
	virtual void OnConnectionLoginFailed(
		CMPPConnection & connection,
		int errorCode
		)
	{
		return;
	}

	/**同网关连接断开时调用 */
	virtual void OnConnectionCleared(CMPPConnection & connection)
	{
		//ClearConnection(connection.GetToken());
		return;
	}

	/**发送SUBMIT消息前调用,如果返回失败,则停止发送SUBMIT消息.
	在这里,只是简单地返回TRUE.
	*/
	virtual BOOL OnConnectionSubmiting(
		CMPPConnection & connection,
		cmpp_submit _message
		)
	{
		return TRUE;
	}

	/**发送SUBMIT消息成功时调用 */
	virtual void OnConnectionSubmited(
		CMPPConnection & connection,
		cmpp_submit _message
		)
	{
		::InterlockedIncrement((long*)&submitCountTotal);

		return;
	}

	/**发送SUBMIT消息失败时调用 */
	virtual void OnConnectionSubmitFailed(
		CMPPConnection & connection,
		cmpp_submit _message,
		unsigned nResult				///< 错误码
		)
	{
		::InterlockedIncrement((long*)&submitFailCountTotal);

		return;
	}

	/**收到CONNECT_RESPONSE时调用 */
	virtual void OnReceivedConnectionConnectResponse(
		CMPPConnection & connection,
		cmpp_connect_resp _message
		)
	{
		return;;
	}

	/**收到Terminate消息时调用 */
	virtual void OnReceivedConnectionTerminate(CMPPConnection & connection)
	{
		return;
	}

	/**收到SUBMIT_RESPONSE时调用 */
	virtual void OnReceivedConnectionSubmitResponse(
		CMPPConnection & connection,
		unsigned seqid,
		cmpp_submit_deliver_resp _message
		)
	{
		::InterlockedIncrement((long*)&submitRespCountTotal);

		return;
	}

	/**收到Deliver消息时调用 */
	virtual void OnReceivedConnectionDeliver(
		CMPPConnection & connection,
		cmpp_deliver _message
		)
	{
		::InterlockedIncrement((long*)&deliverCountTotal);

		return;
	}

	/**收到STATUS_REPORT时调用 */
	virtual void OnReceivedConnectionStatusReport(
		CMPPConnection & connection,
		cmpp_deliver _message
		)
	{
		::InterlockedIncrement((long*)&sreportCountTotal);

		return;
	}

	/**收到QueryResponse时调用 */
	virtual void OnReceivedConnectionQueryResponse(
		CMPPConnection & connection,
		cmpp_query_resp _message
		)
	{
		return;
	}

	/**收到CancelResponse时调用 */
	virtual void OnReceivedConnectionCancelResponse(
		CMPPConnection & connection,
		cmpp_cancel_resp _message
		)
	{
		return;
	}

	/**收到活动测试消息时调用 */
	virtual void OnReceivedConnectionActiveTest(CMPPConnection & connection)
	{
		return;
	}

	/**收到活动测试回复消息时调用 */
	virtual void OnReceivedConnectionActiveTestResponse(CMPPConnection & connection)
	{
		return;
	}

	/**输出日志时调用
	格式化输出日志,默认是把内容TRACE出来
	*/
	virtual void OnConnectionLogWrite(CMPPConnection & connection, const char * str);
	//@}

protected:
	/**@name JobProcess functions */
	//@{
	///**登录网关成功时调用 */
	//void ProcessConnectionLogined(CMPPConnection & connection);

	///**登录网关失败时调用 */
	//void ProcessConnectionLoginFailed(
	//	CMPPConnection & connection,
	//	int errorCode
	//);

	///**同网关连接断开时调用 */
	//void ProcessConnectionCleared(CMPPConnection & connection);

	/**发送SUBMIT消息前调用,如果返回失败,则停止发送SUBMIT消息.
	在这里,只是简单地返回TRUE.
	*/
	BOOL ProcessConnectionSubmiting(
		CMPPConnection & connection,
		cmpp_submit _message
	);

	/**发送SUBMIT消息成功时调用 */
	void ProcessConnectionSubmited(
		CMPPConnection & connection,
		cmpp_submit _message
	);

	/**发送SUBMIT消息失败时调用 */
	void ProcessConnectionSubmitFailed(
		CMPPConnection & connection,
		cmpp_submit _message,
		unsigned nResult				///< 错误码
	);

	/**收到CONNECT_RESPONSE时调用 */
	void ProcessReceivedConnectionConnectResponse(
		CMPPConnection & connection,
		cmpp_connect_resp _message
	);

	/**收到Terminate消息时调用 */
	void ProcessReceivedConnectionTerminate(CMPPConnection & connection);

	/**收到SUBMIT_RESPONSE时调用 */
	void ProcessReceivedConnectionSubmitResponse(
		CMPPConnection & connection,
		//unsigned seqid,
		cmpp_submit_deliver_resp _message
	);

	/**收到Deliver消息时调用 */
	void ProcessReceivedConnectionDeliver(
		CMPPConnection & connection,
		cmpp_deliver _message
	);

	/**收到STATUS_REPORT时调用 */
	void ProcessReceivedConnectionStatusReport(
		CMPPConnection & connection,
		cmpp_deliver _message
	);

	/**收到QueryResponse时调用 */
	void ProcessReceivedConnectionQueryResponse(
		CMPPConnection & connection,
		cmpp_query_resp _message
	);

	/**收到CancelResponse时调用 */
	void ProcessReceivedConnectionCancelResponse(
		CMPPConnection & connection,
		cmpp_cancel_resp _message
	);

	/**收到活动测试消息时调用 */
	void ProcessReceivedConnectionActiveTest(CMPPConnection & connection);

	/**收到活动测试回复消息时调用 */
	void ProcessReceivedConnectionActiveTestResponse(CMPPConnection & connection);

protected:
	CThreadPool				m_pool;
	CWorker						m_worker;
	CJob							* m_pJob;
	aList							connectionList;
	CCriticalSection	m_listCS;
	//CString						lastSubmitToken;
	CMPPConnection		lastPick;

	long						submitCountTotal;			///< SUBMIT发送成功消息数
	long						submitFailCountTotal;	///< SUBMIT发送失败消息数
	long						submitRespCountTotal;	///< 收到的submitresponse消息数
	long						deliverCountTotal;		///< 收到的Deliver消息数
	long						sreportCountTotal;		///< 状态报告数
};


#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -