#ifndef __FXNET_INTERFACE_H__
#define __FXNET_INTERFACE_H__

#include "message_event.h"
#include "message_queue.h"
#include "i_session.h"

#include <deque>
#include <string>
#include <sstream>

namespace FXNET
{
	/**
	 * @brief 
	 * 
	 * io߳
	 */
	void StartIOModule(MessageEventQueue* pQueue);

	/**
	 * @brief 
	 * 
	 * ־߳
	 */
	void StartLogModule();

	/**
	 * @brief Get the Fx Io Module Index object
	 * 
	 * @return unsigned int 
	 */
	unsigned int GetFxIoModuleIndex();

	/**
	 * @brief 
	 * ִ߳
	 * @param pStrstream 
	 */
	void ProcSignelThread(std::stringstream*& pStrstream);

	/**
	 * @brief 
	 * 
	 * io߳Ͷ¼
	 * @param pEvent 
	 */
	void PostEvent(unsigned int dwIoModuleIndex, IOEventBase* pEvent);

	/**
	 * @brief 
	 * 
	 * ioִ߳ Ҫֱӵ
	 * udp listen
	 * udp 0.0.0.0 wPort Բֶ֧ ͬip ͬportļ
	 * @param szIp ip
	 * @param wPort Ķ˿
	 * @param pSessionMaker sessionmaker
	 * @param pOStream 
	 */
	void UdpListen(unsigned int dwIOModuleIndex, const char* szIp, unsigned short wPort, SessionMaker* pSessionMaker, std::ostream* pOStream);
	/**
	 * @brief 
	 * 
	 * ioִ߳ Ҫֱӵ
	 * udpӵĿip port
	 * @param szIp 
	 * @param wPort 
	 * @param pSession 󶨵session
	 * @param pOStream 
	 */
	void UdpConnect(unsigned int dwIOModuleIndex, const char* szIp, unsigned short wPort, ISession* pSession, ErrorCode& refError, std::ostream* pOStream);

	/**
	 * @brief 
	 * 
	 * ioִ߳ Ҫֱӵ
	 * tcp listen
	 * @param szIp ip
	 * @param wPort Ķ˿
	 * @param pSessionMaker sessionmaker
	 * @param pOStream 
	 */
	void TcpListen(unsigned int dwIOModuleIndex, const char* szIp, unsigned short wPort, SessionMaker* pSessionMaker, std::ostream* pOStream);
	/**
	 * @brief 
	 * 
	 * ioִ߳ Ҫֱӵ
	 * udp ӵĿip port
	 * @param szIp 
	 * @param wPort 
	 * @param pSession 󶨵session
	 * @param pOStream 
	 */
	void TcpConnect(unsigned int dwIOModuleIndex, const char* szIp, unsigned short wPort, ISession* pSession, ErrorCode& refError, std::ostream* pOStream);

	/**
	 * @brief 
	 * 
	 * udp listen ¼ Ͷݵioִ߳
	 */
	class UDPListen : public IOEventBase
	{
	public:
		UDPListen(const char* szIp, unsigned short wPort, unsigned int dwIoModuleIndex, SessionMaker* pSessionMaker)
			: m_szIp(szIp)
			, m_wPort(wPort)
			, m_pSessionMaker(pSessionMaker)
			, m_dwIOModuleIndex(dwIoModuleIndex)
		{}
		virtual void operator ()(std::ostream* pOStream)
		{
			DELETE_WHEN_DESTRUCT(UDPListen, this);
			UdpListen(m_dwIOModuleIndex, m_szIp.c_str(), m_wPort, m_pSessionMaker, pOStream);
		}
	protected:
	private:
		std::string m_szIp;
		unsigned short m_wPort;
		SessionMaker* m_pSessionMaker;
		unsigned int m_dwIOModuleIndex;
	};

	/**
	 * @brief 
	 * 
	 * udp connect¼ Ͷݵioִ߳
	 */
	class UDPConnect : public IOEventBase
	{
	public:
		UDPConnect(const char* szIp, unsigned short wPort, unsigned int dwIoModuleIndex, ISession* pSession)
			: m_szIp(szIp)
			, m_wPort(wPort)
			, m_pSession(pSession)
			, m_dwIOModuleIndex(dwIoModuleIndex)
		{}
		virtual void operator ()(std::ostream* pOStream)
		{
			DELETE_WHEN_DESTRUCT(UDPConnect, this);
			ErrorCode oError;
			UdpConnect(m_dwIOModuleIndex, m_szIp.c_str(), m_wPort, m_pSession, oError, pOStream);
		}
	protected:
	private:
		std::string m_szIp;
		unsigned short m_wPort;
		ISession* m_pSession;
		unsigned int m_dwIOModuleIndex;
	};

	/**
	 * @brief 
	 * 
	 * tcp listen¼ Ͷݵioִ߳
	 */
	class TCPListen : public IOEventBase
	{
	public:
		TCPListen(const char* szIp, unsigned short wPort, unsigned int dwIoModuleIndex, SessionMaker* pSessionMaker)
			: m_szIp(szIp)
			, m_wPort(wPort)
			, m_pSessionMaker(pSessionMaker)
			, m_dwIOModuleIndex(dwIoModuleIndex)
		{}
		virtual void operator ()(std::ostream* pOStream)
		{
			DELETE_WHEN_DESTRUCT(TCPListen, this);
			TcpListen(m_dwIOModuleIndex, m_szIp.c_str(), m_wPort, m_pSessionMaker, pOStream);
		}
	protected:
	private:
		std::string m_szIp;
		unsigned short m_wPort;
		SessionMaker* m_pSessionMaker;
		unsigned int m_dwIOModuleIndex;
	};

	/**
	 * @brief 
	 * 
	 * tcp connect ¼ Ͷݵioִ߳
	 */
	class TCPConnect : public IOEventBase
	{
	public:
		TCPConnect(const char* szIp, unsigned short wPort, unsigned int dwIoModuleIndex, ISession* pSession)
			: m_szIp(szIp)
			, m_wPort(wPort)
			, m_pSession(pSession)
			, m_dwIOModuleIndex(dwIoModuleIndex)
		{}
		virtual void operator ()(std::ostream* pOStream)
		{
			DELETE_WHEN_DESTRUCT(TCPConnect, this);
			ErrorCode oError;
			TcpConnect(m_dwIOModuleIndex, m_szIp.c_str(), m_wPort, m_pSession, oError, pOStream);
		}
	protected:
	private:
		std::string m_szIp;
		unsigned short m_wPort;
		ISession* m_pSession;
		unsigned int m_dwIOModuleIndex;
	};

	void PushLog(std::stringstream* pStrstream);
	
	/**
	 * @brief ȡ־ַ־
	 * 
	 * @return 
	 * @note ʹ־̣߳ôÿ֡Ҫһ
	 */
	std::string GetLogStr();
};


#endif //!__FXNET_INTERFACE_H__
