www.pudn.com > fanccMSNr.src.rar > MessageDispatcher.hpp


#pragma once 
#include "Message.hpp" 
#include "MessageListener.hpp" 
 
namespace poral { 
 
	/** 
	 * Thrown to indicate I/O error. 
	 */ 
	class IOException: public logic_error { 
	public: 
		IOException(const string &msg): 
		logic_error(msg) { 
		}; 
	}; 
 
	class SocketException: public IOException { 
	public: 
		SocketException(const string &msg): 
		IOException(msg) { 
		} 
		SocketException(): 
		IOException("SocketException") { 
		} 
	}; 
 
	/** 
	* Interface for exchanging MSN Messenger messages. 
	* This interface differents from socket class such as CAsyncSocket, 
	* which exchanges data by bytestream. 
	*/ 
	class MessageDispatcher 
	{ 
	public: 
 
		virtual ~MessageDispatcher() {} 
		/** Connect with specified server. */ 
		virtual void connect(const string &address, unsigned long port)=0; 
		virtual void close()=0; 
		/** 
		* Bind and listen with preferredPort. 
		* If preferredPort not available, tries another port. 
		* 
		* NOTE that server socket and connection socket are not divided. 
		* When listen() called, MessageDispatcher acts as server socket. It automatically  
		* accept first connection and then acts as connection socket. No differences 
		* between using MessageDispatcher interface for client and so for server except using listen() 
		* instead of using connect() for server. 
		*  
		* @return Port number listening. 
		*/ 
		virtual int listen(int preferredPort)=0; 
		/** Gets localhost's IP address in string representation. */ 
		virtual void getAddress(string &address)=0; 
		/** 
		* Sends a message. Returns immediately without blocking. 
		* Calls MessageListener::sendFailedException(Messge &) if execution failed. 
		* Transferring the message is a transaction. It should atomically success or 
		* atomically fail. 
		*/ 
		virtual void sendMessage(Message &message)=0; 
		/**  
		* Sends array of bytes. This method is in order to fast transfer.  
		* @return true if data can be transferred without pending. 
		*         false if transmission should be pending. 
		*/ 
		virtual bool sendBytes(const unsigned char *bytes, size_t length)=0; 
		virtual void addMessageListener(MessageListener &listener)=0; 
		virtual void removeMessageListener(MessageListener &listener)=0; 
 
		/** 
		* returns new heap allocated instance which has same type with instantiator. 
		* NOTE:This function does not follow prototype pattern usually implemented in 
		*      method named as clone(). because newly created instance is not same  
		*      with instantiator. It is only in same class. 
		*/ 
		virtual MessageDispatcher *newInstance()=0; 
	}; 
 
}