📄 warsocket.h
字号:
/** WarSocket is a simpla class that can use a socket to communicate on a network. The actual communication is handled by a comperion WarSocketIo class. This separation is made to allow derived classes to specialize the communication method (especially when async IO is used), and also allow derived classes of the functionality, ie. higher level Internet protocols like FTP and HTTP.*/#ifndef WAR_SOCKET_H#define WAR_SOCKET_H/* SYSTEM INCLUDES */#ifndef WAR_SET_INCLUDED# define WAR_SET_INCLUDED# include <set>#endif/* PROJECT INCLUDES */#ifndef WAR_NET_ADDRESS_H# include "WarNetAddress.h"#endif#ifndef WAR_SOCKET_IO_H# include "WarSocketIo.h"#endif#ifndef WAR_SOCKET_ENGINE_H# include "WarSocketEngine.h"#endif#ifndef WAR_SMART_POINTER_H# include "WarSmartPointer.h"#endif#ifndef WAR_CRITICAL_SECTION_H# include "WarCriticalSection.h"#endif#ifndef WAR_LOG_IDENTIFIER_H# include "WarLogIdentifier.h"#endif/* LOCAL INCLUDES *//* FORWARD REFERENCES */#ifdef __cplusplusextern "C" {#endif/****************** BEGIN OLD STYLE C spesific ********//****************** END OLD STYLE C spesific **********/#ifdef __cplusplus }#endif/****************** BEGIN C++ spesific ****************/#ifdef __cplusplusclass WarSocket : public WarSmartPointer,public WarLogIdentifier{public: // LIFECYCLE /** * Default constructor. */ WarSocket(void); /** */ WarSocket(war_socket_io_ptr_t& companionPtr); /** * Destructor. */ ~WarSocket(void); // OPERATORS bool operator == (const WarSocket& from) { return GetSeqNumber() == from.GetSeqNumber(); } bool operator < (const WarSocket& from) { return GetSeqNumber() < from.GetSeqNumber(); } // OPERATIONS virtual void Close(); // virtual void Connect(WarSocketTypesE commType, const WarNetAddress& rremoteHost, const WarNetAddress *plocalAddress = NULL) throw(WarException); virtual void Listen(WarNetAddress& rLocalAddress, int backlog = 5, std::pair<war_port_t,war_port_t> *pRange = NULL) throw(WarException); // Set blocking or non-blocking (async) mode virtual void SetAsyncMode(bool makeAsync = true, bool doForce = false) throw(WarException); /** calls send() on the socket */ virtual int Send(war_ccstr_t buf, size_t len, int flags = 0) throw(WarException); /** High performance send function. OnSent() is * called when the operation has completed. * * Multiple buffers can be queued, something that * should boost performance on systems that * supports native I/O callbacks (like Windows NT) * * Make no assumptions about threads! The * OnSent() function can be called by the same * thread that issued the call, or by any other * thread. This depends on the IO model used, and * is handled by the low-level framework */ virtual void SendWithCallback(war_transfer_buffer_ptr_t& outBuffer, size_t numSegments = 1) throw(WarException); /** Calls recv() on the socket * @return Bytes received or 0 on EOF */ virtual int Recv(war_cstr_t buf, size_t len, int flags = 0) throw(WarException); /** calls recv() on the socket * * @see SendWithCallback */ virtual void RecvWithCallback(war_transfer_buffer_ptr_t& inBuffer, size_t numSegments = 1) throw(WarException); // CALLBACKS /** CALLBACKS FOR ASYNC IO * Must be subclassed if async IO is used!! * In the subclassed implementations, call * the implementation from the base-class first. */ virtual void OnConnect(const WarError& status); virtual void OnAccept(const WarError& status, war_socket_t newSocket, const WarNetAddress& remoteAddress, const WarNetAddress& localAddress); virtual void OnReceived(const WarError& status, war_transfer_buffer_ptr_t& buffer); virtual void OnSent(const WarError& status, war_transfer_buffer_ptr_t& buffer); virtual void OnClose(const WarError& status); virtual void OnStateChanged(); /** Callbacks for io that preproceses or replaces * the normal overloads. Used by WarTransferSocket * to hijack the events. */ virtual void PreOnConnect(const WarError& status); virtual void PreOnAccept(const WarError& status, war_socket_t newSocket, const WarNetAddress& remoteAddress, const WarNetAddress& localAddress); virtual void PreOnReceived(const WarError& status, war_transfer_buffer_ptr_t& buffer); virtual void PreOnSent(const WarError& status, war_transfer_buffer_ptr_t& buffer); virtual void PreOnClose(const WarError& status); virtual void PreOnStateChanged(); // ACCESS WarSocketIo& GetSocketIo() const throw(WarException) { if (NULL == mpCompanion) WarThrow(WarException(WAR_ERR_INTERNAL_DATA_NOT_INITIALIZED), NULL); return *mpCompanion; } // INQUIRY virtual bool IsClearToSend() const; virtual bool IsClearToReceive() const; virtual bool IsAsync() const; virtual bool IsOpen() const; war_uint64_t GetSeqNumber() const; war_uint64_t GetBytesSent() const; war_uint64_t GetBytesReceived() const; WarTime GetConnectTime() const; WarTime GetCloseTime() const; WarTime GetIdleTimeSince() const; // Idle time war_time_t GetConnectedTime() const; const WarNetAddress& GetLocalAddress() const; const WarNetAddress& GetRemoteAddress() const; virtual WarSocketStatesE GetCurrentState() const; bool IsLocalAddress(const struct in_addr& addr) const;protected: /* We use a traditional C pointer to speed up * access to the companion. The mCompanionPtr * member reference it (and delete it), but * we don't need the extra overhead of the * smart pointers at this level. */ WarSocketIo *mpCompanion; WarCriticalSection mLock; friend class WarSocketIo; virtual void SetCurrentState( WarSocketStatesE newState) const throw(WarException); private: war_socket_io_ptr_t mCompanionPtr; // Auto pointer Reference void Initialize(war_socket_io_ptr_t& companionPtr); // Call from constructor only!};/* INLINE METHODS *//* EXTERNAL REFERENCES */typedef WarPtrWrapper<WarSocket> war_socket_ptr_t;typedef std::set<war_socket_ptr_t> war_socket_set_t;typedef std::list<war_socket_ptr_t> war_socket_list_t;#endif /* __cplusplus *//****************** END C++ spesific ******************/#endif /* WAR_SOCKET_H_ */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -