📄 warsocketio.h
字号:
/** */#ifndef WAR_SOCKET_IO_H#define WAR_SOCKET_IO_H/* SYSTEM INCLUDES */#if defined(HAVE_SYS_TYPES_H) && !defined(WAR_SYS_TYPES_H_INCLUDED)# define WAR_SYS_TYPES_H_INCLUDED# include <sys/types.h>#endif#if defined(HAVE_SYS_SOCKET_H) && !defined(WAR_SYS_SOCKET_H_INCLUDED)# define WAR_SYS_SOCKET_H_INCLUDED# include <sys/socket.h>#endif#if defined(HAVE_NETDB_H) && !defined(WAR_NETDEB_H_INCLUDED)# define WAR_NETDEB_H_INCLUDED# include <netdb.h>#endif#ifndef WAR_SET_INCLUDED# define WAR_SET_INCLUDED# include <set>#endif/* PROJECT INCLUDES */#ifndef WAR_CRITICAL_SECTION_H# include "WarCriticalSection.h"#endif#ifndef WAR_NET_ADDRESS_H# include "WarNetAddress.h"#endif#ifndef WAR_EXCEPTION_H# include "WarException.h"#endif#ifndef WAR_TIME_H# include "WarTime.h"#endif#ifndef WAR_TRANSFER_BUFFER_H# include "WarTransferBuffer.h"#endif#ifndef WAR_PLUGIN_SUPPORT_H# include "WarPluginSupport.h"#endif#ifndef WAR_LOG_IDENTIFIER_H# include "WarLogIdentifier.h"#endif/* LOCAL INCLUDES *//* FORWARD REFERENCES */class WarSocket;class WarSocketEngine;#ifdef __cplusplusextern "C" {#endif/****************** BEGIN OLD STYLE C spesific ********/#ifdef INVALID_SOCKET# define WAR_INVALID_SOCKET INVALID_SOCKET#else# define WAR_INVALID_SOCKET ((war_socket_t)-1)#endifenum WarSocketTypesE{ WAR_SOCK_STREAM = SOCK_STREAM, WAR_SOCK_DGRAM = SOCK_DGRAM, WAR_SOCK_RAW = SOCK_RAW};enum WarSocketStatesE{ WAR_SCKSTATE_VOID, // Not initialized WAR_SCKSTATE_CREATED, WAR_SCKSTATE_UNKNOWN, WAR_SCKSTATE_CONNECTED, WAR_SCKSTATE_LISTENING, WAR_SCKSTATE_CLOSED};#if HAVE_WINSOCK typedef SOCKET war_socket_t;#else typedef int war_socket_t;#endif#if !defined(HAVE_CLOSESOCKET) && !defined(HAVE_WINSOCK)# define closesocket(sck) close(sck)#endiftypedef std::set<war_socket_t> war_sock_set_t;/****************** END OLD STYLE C spesific **********/#ifdef __cplusplus }#endif/****************** BEGIN C++ spesific ****************/#ifdef __cplusplusclass WarSocketIo : public WarSmartPointer,public WarPluginSupport<WarSocketIo>, // Extendable with plugins public WarLogIdentifier {public: enum PluginEventsE { PLUGIN_ON_RECEIVED, PLUGIN_ON_SENT, PLUGIN_EVENT_INVALID }; // LIFECYCLE /** * Default constructor. */ WarSocketIo(void); /** * Copy constructor. * * @param from The value to copy to this object. */ WarSocketIo(WarSocketIo& from) throw(WarException); /** * Destructor. */ virtual ~WarSocketIo(void); // OPERATORS /** * Assignment operator. * * @param from THe value to assign to this object. * * @return A reference to this object. */ WarSocketIo& operator=(WarSocketIo& from) throw(WarException); // OPERATIONS void Close(); /** Sets the SO_REUSEADDR flag on the socket. * This makes it possible to bind several * sockets to the same address. */ void SetReuseAddr() throw(WarException); // virtual void Connect(WarSocketTypesE commType, const WarNetAddress& rremoteHost, const WarNetAddress *plocalAddress = NULL) throw(WarException); /** Listen for a connection. * * @param rLocalAddress The local address to bind to * This argument is updated with the addres * and port actually bound to. * @param backlog The backlog argument to ::listen() * In Windows NT, this argument is also used * to allocate a pool of listening sockets * that will signal connectins trough I/O * completion ports. * @param pRange A range of ports tat can be used. * Listen will try to find an available oprt * fromthe range. This parameter is used in * the FTP PASV command to help NAT routers * to forward FTP connections. */ virtual void WarSocketIo::Listen(WarNetAddress& rLocalAddress, int backlog = 10, 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) 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) throw(WarException); static war_socket_t Dup(const war_socket_t from) throw(WarException); /** Create a new socket of the same type.*/ virtual WarSocketIo *CreateSame() const throw (WarException) { return new WarSocketIo; } // ACCESS /** * Assigns a socket to the object */ virtual void AssignSocket(war_socket_t sck) throw(WarException); // INQUIRY bool IsClearToSend() const; bool IsClearToReceive() const; bool IsAsync() const; 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; WarSocketStatesE GetCurrentState() const; // CALLBACKS FOR ASYNC IO // The default implementation is to // pass the events to the companion virtual void OnConnect(const WarError& status); /* * If no error occured, the newSocket * is the new connected incoming connection. */ 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& rbuffer); virtual void OnSent(const WarError& status, war_transfer_buffer_ptr_t& rbuffer); virtual void OnClose(const WarError& status);protected: WarSocket *mpCompanion; // WarSocket to receive events WarCriticalSection mLock; // Private lock war_socket_t mSocket; WarSocketStatesE mCurrentState; friend class WarSocket; void SetCurrentState(WarSocketStatesE newState); // Socket calls virtual void DoListen(int backlog) throw(WarException);; virtual int DoRecv(char *buf, int len, int flags) throw(WarException); virtual int DoSend(const char *buf, int len, int flags) throw(WarException); virtual void DoConnect(const struct sockaddr *name, int namelen) throw(WarException); virtual int DoClose() throw(WarException); virtual void DoCreateSocket(int af, int type, int protocol) throw(WarException);private: friend class WarSocketEngine; war_uint64_t mSeqNumber; war_uint64_t mBytesSent; war_uint64_t mBytesReceived; WarTime mConnectTime; WarTime mCloseTime; WarTime mIdleTimeSince; WarNetAddress mLocalAddress; WarNetAddress mRemoteAddress; bool mIsAsync; bool mClearToSend; bool mClearToRecive; void Initialize(); // Call from constructor only!};/* INLINE METHODS *//* EXTERNAL REFERENCES */typedef WarPtrWrapper<WarSocketIo> war_socket_io_ptr_t;/* PLUGIN INTERFACE */class WarSocketIo_OnReceived : public WarPlugin{public: static WarSocketIo::PluginEventsE GetId() { return WarSocketIo::PLUGIN_ON_RECEIVED; } virtual void OnProcess(const WarError& status, war_transfer_buffer_ptr_t& buffer) throw (WarException) = 0;};class WarSocketIo_OnSent : public WarPlugin{public: static WarSocketIo::PluginEventsE GetId() { return WarSocketIo::PLUGIN_ON_SENT; } virtual void OnProcess(const WarError& status, war_transfer_buffer_ptr_t& buffer) throw (WarException) = 0;};#endif /* __cplusplus *//****************** END C++ spesific ******************/#endif /* WAR_SOCKET_IO_H_ */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -