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

📄 socket.h

📁 GNU Common C++ is a very portable and highly optimized class framework for writing C++ applications
💻 H
📖 第 1 页 / 共 4 页
字号:
 * @short streamable TCP socket connection. */class __EXPORT TCPStream : protected std::streambuf, public Socket, public std::iostream{private:	int doallocate();	void segmentBuffering(unsigned mss);	friend TCPStream& crlf(TCPStream&);	friend TCPStream& lfcr(TCPStream&);protected:	timeout_t timeout;	size_t bufsize;	Family family;	char *gbuf, *pbuf;public:	/**	 * The constructor required for building other classes or to	 * start an unconnected TCPStream for connect.	 */	TCPStream(Family family = IPV4, bool throwflag = true, timeout_t to = 0);	/**	 * Disconnect the current session and prepare for a new one.	 */	void disconnect(void);	/**	 * Get protocol segment size.	 */	int getSegmentSize(void);protected:	/**	 * Used to allocate the buffer space needed for iostream	 * operations.  This function is called by the constructor.	 *	 * @param size of stream buffers from constructor.	 */	void allocate(size_t size);	/**	 * Used to terminate the buffer space and cleanup the socket	 * connection.  This fucntion is called by the destructor.	 */	void endStream(void);	/**	 * This streambuf method is used to load the input buffer	 * through the established tcp socket connection.	 *	 * @return char from get buffer, EOF if not connected.	 */	int underflow();	/**	 * This streambuf method is used for doing unbuffered reads	 * through the establish tcp socket connection when in interactive mode.	 * Also this method will handle proper use of buffers if not in	 * interative mode.	 *	 * @return char from tcp socket connection, EOF if not connected.	 */	int uflow();	/**	 * This streambuf method is used to write the output	 * buffer through the established tcp connection.	 *	 * @param ch char to push through.	 * @return char pushed through.	 */	int overflow(int ch);	/**	 * Create a TCP stream by connecting to a TCP socket (on	 * a remote machine).	 *	 * @param host address of remote TCP server.	 * @param port number to connect.	 * @param mss maximum segment size of streaming buffers.	 */	void connect(const IPV4Host &host, tpport_t port, unsigned mss = 536);#ifdef	CCXX_IPV6	void connect(const IPV6Host &host, tpport_t port, unsigned mss = 536);#endif	/**	 * Connect a TCP stream to a named destination host and port	 * number, using getaddrinfo interface if available.	 *	 * @param name of host and service to connect	 * @param mss maximum segment size of stream buffer	 */	void connect(const char *name, unsigned mss = 536);	/**	 * Used in derived classes to refer to the current object via	 * it's iostream.  For example, to send a set of characters	 * in a derived method, one might use *tcp() << "test".	 *	 * @return stream pointer of this object.	 */	std::iostream *tcp(void)		{return ((std::iostream *)this);};public:	/**	 * Create a TCP stream by accepting a connection from a bound	 * TCP socket acting as a server.  This performs an "accept"	 * call.	 *	 * @param server socket listening	 * @param throwflag flag to throw errors.	 * @param timeout for all operations.	 */	TCPStream(TCPSocket &server, bool throwflag = true, timeout_t timeout = 0);#ifdef	CCXX_IPV6	TCPStream(TCPV6Socket &server, bool throwflag = true, timeout_t timeout = 0);#endif	/**	 * Accept a connection from a TCP Server.	 *	 * @param server socket listening	 */	void connect(TCPSocket &server);#ifdef	CCXX_IPV6	void connect(TCPV6Socket &server);#endif	/**	 * Create a TCP stream by connecting to a TCP socket (on	 * a remote machine).	 *	 * @param host address of remote TCP server.	 * @param port number to connect.	 * @param mss maximum segment size of streaming buffers.	 * @param throwflag flag to throw errors.	 * @param timeout for all operations.	 */	TCPStream(const IPV4Host &host, tpport_t port, unsigned mss = 536, bool throwflag = true, timeout_t timeout = 0);#ifdef	CCXX_IPV6	TCPStream(const IPV6Host &host, tpport_t port, unsigned mss = 536, bool throwflag = true, timeout_t timeout = 0);#endif	/**	 * Construct a named TCP Socket connected to a remote machine.	 *	 * @param name of remote service.	 * @param family of protocol.	 * @param mss maximum segment size of streaming buffers.	 * @param throwflag flag to throw errors.	 * @param timer for timeout for all operations.	 */	TCPStream(const char *name, Family family = IPV4, unsigned mss = 536, bool throwflag = false, timeout_t timer = 0);	/**	 * Set the I/O operation timeout for socket I/O operations.	 *	 * @param timer to change timeout.	 */	inline void setTimeout(timeout_t timer)		{timeout = timer;};	/**	 * A copy constructor creates a new stream buffer.	 *	 * @param source reference of stream to copy from.	 *	 */	TCPStream(const TCPStream &source);	/**	 * Flush and empty all buffers, and then remove the allocated	 * buffers.	 */	virtual ~TCPStream();	/**	 * Flushes the stream input and output buffers, writes	 * pending output.	 *	 * @return 0 on success.	 */	int sync(void);#ifdef	HAVE_SNPRINTF	/**	 * Print content into a socket.	 *	 * @return count of bytes sent.	 * @param format string	 */	size_t printf(const char *format, ...);#endif	/**	 * Get the status of pending stream data.  This can be used to	 * examine if input or output is waiting, or if an error or	 * disconnect has occured on the stream.  If a read buffer	 * contains data then input is ready and if write buffer	 * contains data it is first flushed and then checked.	 */	bool isPending(Pending pend, timeout_t timeout = TIMEOUT_INF);        /**          * Examine contents of next waiting packet.          *          * @param buf pointer to packet buffer for contents.          * @param len of packet buffer.          * @return number of bytes examined.          */         inline ssize_t peek(void *buf, size_t len)                 {return _IORET64 ::recv(so, (char *)buf, _IOLEN64 len, MSG_PEEK);};	/**	 * Return the size of the current stream buffering used.	 *	 * @return size of stream buffers.	 */	inline size_t getBufferSize(void) const		{return bufsize;};};/** * The TCP session is used to primarily to represent a client connection * that can be managed on a seperate thread.  The TCP session also supports * a non-blocking connection scheme which prevents blocking during the * constructor and moving the process of completing a connection into the  * thread that executes for the session. *  * @author David Sugar <dyfet@ostel.com> * @short Threaded streamable socket with non-blocking constructor. */class __EXPORT TCPSession : public Thread, public TCPStream{private:	TCPSession(const TCPSession &rhs); // not definedprotected:	/**	 * Normally called during the thread Initial() method by default,	 * this will wait for the socket connection to complete when	 * connecting to a remote socket.  One might wish to use	 * setCompletion() to change the socket back to blocking I/O	 * calls after the connection completes.  To implement the	 * session one must create a derived class which implements	 * run().	 * 	 * @return 0 if successful, -1 if timed out.	 * @param timeout to wait for completion in milliseconds.	 */	int waitConnection(timeout_t timeout = TIMEOUT_INF);	/**	 * The initial method is used to esablish a connection when	 * delayed completion is used.  This assures the constructor	 * terminates without having to wait for a connection request	 * to complete.	 */	void initial(void);public:	/**	 * Create a TCP socket that will be connected to a remote TCP	 * server and that will execute under it's own thread.	 * 	 * @param host internet address of remote TCP server.	 * @param port number of remote server.	 * @param size of streaming buffer.	 * @param pri execution priority relative to parent.	 * @param stack allocation needed on some platforms.	 */	TCPSession(const IPV4Host &host, 		tpport_t port, size_t size = 536, int pri = 0, size_t stack = 0);#ifdef	CCXX_IPV6	TCPSession(const IPV6Host &host,		tpport_t port, size_t size = 536, int pri = 0, size_t stack = 0);#endif	/**	 * Create a TCP socket from a bound TCP server by accepting a pending	 * connection from that server and execute a thread for the accepted	 * connection.	 * 	 * @param server tcp socket to accept a connection from.	 * @param pri execution priority relative to parent.	 * @param stack allocation needed on some platforms.	 */	TCPSession(TCPSocket &server, int pri = 0, size_t stack = 0);#ifdef	CCXX_IPV6	TCPSession(TCPV6Socket &server, int pri = 0, size_t stack = 0);#endif	/**	 * Make sure destruction happens through a virtual...	 */	virtual ~TCPSession();};#if defined(WIN32)/** *  class init_WSA used to initalise windows sockets specfifc stuff : there is *  an MS - specific init sequence for Winsock 2 this class attempts to  *  initalise Winsock 2.2 - needed for non - blocking I/O. It will fall back  *  on 1.2 or lower if 2.0 or higher is not available,  but < 2.0 does not  *  support non - blocking I/o *  TO DO : might be an idea to have a method that reports version of  *  Winsock in use or a predicate to test if non - blocking is OK -- JFC */class init_WSA{public:	init_WSA();	~init_WSA();};#endif // WIN32class __EXPORT SimpleTCPStream;/** * @class SimpleTCPStream * @brief Simple TCP Stream, to be used with Common C++ Library * * This source is derived from a proposal made by Ville Vainio * (vvainio@tp.spt.fi). * * @author Mark S. Millard (msm@wizzer.com) * @date   2002-08-15 * Copyright (C) 2002 Wizzer Works. **/class __EXPORT SimpleTCPStream : public Socket{private:	IPV4Host getSender(tpport_t *port) const;protected:	/**	 * The constructor required for "SimpleTCPStream", a more C++ style	 * version of the SimpleTCPStream class.	 */	SimpleTCPStream();	/**	 * Used to terminate the buffer space and cleanup the socket	 * connection.  This fucntion is called by the destructor.	 */	void endStream(void);	/**	 * Create a TCP stream by connecting to a TCP socket (on	 * a remote machine).	 *	 * @param host address of remote TCP server.	 * @param port number to connect.	 * @param size of streaming input and output buffers.	 */	void Connect(const IPV4Host &host, tpport_t port, size_t size);public:	/**	 * Create a TCP stream by accepting a connection from a bound	 * TCP socket acting as a server.  This performs an "accept"	 * call.	 *	 * @param server bound server tcp socket.	 * @param size of streaming input and output buffers.	 */	SimpleTCPStream(TCPSocket &server, size_t size = 512);	/**	 * Create a TCP stream by connecting to a TCP socket (on	 * a remote machine).	 *	 * @param host address of remote TCP server.	 * @param port number to connect.	 * @param size of streaming input and output buffers.	 */	SimpleTCPStream(const IPV4Host &host, tpport_t port, size_t size = 512);	/**	 * A copy constructor creates a new stream buffer.	 *	 * @param source A reference to the SimpleTCPStream to copy.	 */	SimpleTCPStream(const SimpleTCPStream &source);	/**	 * Flush and empty all buffers, and then remove the allocated	 * buffers.	 */	virtual ~SimpleTCPStream();	/**	 * @brief Get the status of pending stream data. 	 * 	 * This method can be used to examine if input or output is waiting,	 * or if an error or disconnect has occured on the stream.	 * If a read buffer contains data then input is ready. If write buffer	 * contains data, it is first flushed and then checked.	 *	 * @param pend Flag indicating means to pend.	 * @param timeout The length of time to wait.	 */	bool isPending(Pending pend, timeout_t timeout = TIMEOUT_INF);	void flush() {}	/**	 * @brief Read bytes into a buffer.	 *	 * <long-description>	 *	 * @param bytes A pointer to buffer that will contain the bytes read.	 * @param length The number of bytes to read (exactly).	 * @param timeout Period to time out, in milleseconds.	 *	 * @return The number of bytes actually read, 0 on EOF.	 */	ssize_t read(char *bytes, size_t length, timeout_t timeout = 0);	/**	 * @brief Write bytes to buffer	 *	 * <long-description>	 *	 * @param bytes A pointer to a buffer containing the bytes to write. 	 * @param length The number of bytes to write (exactly).	 * @param timeout Period to time out, in milleseconds.	 *	 * @return The number of bytes actually written.	 */	ssize_t write(const char *bytes, size_t length, timeout_t timeout = 0);	/**	 * @brief Peek at the incoming data.	 *	 * The data is copied into the buffer	 * but is not removed from the input queue. The function then returns	 * the number of bytes currently pending to receive.	 *	 * @param bytes A pointer to buffer that will contain the bytes read.	 * @param length The number of bytes to read (exactly).	 * @param timeout Period to time out, in milleseconds.	 *	 * @return The number of bytes pending on the input queue, 0 on EOF.	 */	ssize_t peek(char *bytes, size_t length, timeout_t timeout = 0);};#ifdef	COMMON_STD_EXCEPTIONclass __EXPORT SockException : public IOException{private:	Socket::Error _socketError;	public:	SockException(const String &str, Socket::Error socketError, long systemError = 0) :		IOException(str, systemError), _socketError(socketError) {};	inline Socket::Error getSocketError() const	{ return _socketError; }};#endif#ifdef	CCXX_NAMESPACES}#endif#endif/** EMACS ** * Local variables: * mode: c++ * c-basic-offset: 8 * End: */

⌨️ 快捷键说明

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