1 | #ifndef _StreamSocket_H |
---|
2 | #define _StreamSocket_H |
---|
3 | |
---|
4 | #include "Socket.h" |
---|
5 | |
---|
6 | |
---|
7 | #ifdef SOCKETS_NAMESPACE |
---|
8 | namespace SOCKETS_NAMESPACE { |
---|
9 | #endif |
---|
10 | |
---|
11 | |
---|
12 | /** SOCK_STREAM Socket base class. |
---|
13 | \ingroup basic */ |
---|
14 | class StreamSocket : public Socket |
---|
15 | { |
---|
16 | public: |
---|
17 | StreamSocket(ISocketHandler& ); |
---|
18 | ~StreamSocket(); |
---|
19 | |
---|
20 | /** Socket should Check Connect on next write event from select(). */ |
---|
21 | void SetConnecting(bool = true); |
---|
22 | |
---|
23 | /** Check connecting flag. |
---|
24 | \return true if the socket is still trying to connect */ |
---|
25 | bool Connecting(); |
---|
26 | |
---|
27 | /** Returns true when socket file descriptor is valid, |
---|
28 | socket connection is established, and socket is not about to |
---|
29 | be closed. */ |
---|
30 | bool Ready(); |
---|
31 | |
---|
32 | /** Set timeout to use for connection attempt. |
---|
33 | \param x Timeout in seconds */ |
---|
34 | void SetConnectTimeout(int x); |
---|
35 | |
---|
36 | /** Return number of seconds to wait for a connection. |
---|
37 | \return Connection timeout (seconds) */ |
---|
38 | int GetConnectTimeout(); |
---|
39 | |
---|
40 | /** Set flush before close to make a tcp socket completely empty its |
---|
41 | output buffer before closing the connection. */ |
---|
42 | void SetFlushBeforeClose(bool = true); |
---|
43 | |
---|
44 | /** Check flush before status. |
---|
45 | \return true if the socket should send all data before closing */ |
---|
46 | bool GetFlushBeforeClose(); |
---|
47 | |
---|
48 | /** Define number of connection retries (tcp only). |
---|
49 | n = 0 - no retry |
---|
50 | n > 0 - number of retries |
---|
51 | n = -1 - unlimited retries */ |
---|
52 | void SetConnectionRetry(int n); |
---|
53 | |
---|
54 | /** Get number of maximum connection retries (tcp only). */ |
---|
55 | int GetConnectionRetry(); |
---|
56 | |
---|
57 | /** Increase number of actual connection retries (tcp only). */ |
---|
58 | void IncreaseConnectionRetries(); |
---|
59 | |
---|
60 | /** Get number of actual connection retries (tcp only). */ |
---|
61 | int GetConnectionRetries(); |
---|
62 | |
---|
63 | /** Reset actual connection retries (tcp only). */ |
---|
64 | void ResetConnectionRetries(); |
---|
65 | |
---|
66 | // LIST_CALLONCONNECT |
---|
67 | |
---|
68 | /** Instruct socket to call OnConnect callback next sockethandler cycle. */ |
---|
69 | void SetCallOnConnect(bool x = true); |
---|
70 | |
---|
71 | /** Check call on connect flag. |
---|
72 | \return true if OnConnect() should be called a.s.a.p */ |
---|
73 | bool CallOnConnect(); |
---|
74 | |
---|
75 | // LIST_RETRY |
---|
76 | |
---|
77 | /** Set flag to initiate a connection attempt after a connection timeout. */ |
---|
78 | void SetRetryClientConnect(bool x = true); |
---|
79 | |
---|
80 | /** Check if a connection attempt should be made. |
---|
81 | \return true when another attempt should be made */ |
---|
82 | bool RetryClientConnect(); |
---|
83 | |
---|
84 | /** Called after OnRead if socket is in line protocol mode. |
---|
85 | \sa SetLineProtocol */ |
---|
86 | /** Enable the OnLine callback. Do not create your own OnRead |
---|
87 | * callback when using this. */ |
---|
88 | virtual void SetLineProtocol(bool = true); |
---|
89 | |
---|
90 | /** Check line protocol mode. |
---|
91 | \return true if socket is in line protocol mode */ |
---|
92 | bool LineProtocol(); |
---|
93 | |
---|
94 | /** Set shutdown status. */ |
---|
95 | void SetShutdown(int); |
---|
96 | |
---|
97 | /** Get shutdown status. */ |
---|
98 | int GetShutdown(); |
---|
99 | |
---|
100 | /** Returns IPPROTO_TCP or IPPROTO_SCTP */ |
---|
101 | virtual int Protocol() = 0; |
---|
102 | |
---|
103 | protected: |
---|
104 | StreamSocket(const StreamSocket& ) {} // copy constructor |
---|
105 | |
---|
106 | private: |
---|
107 | StreamSocket& operator=(const StreamSocket& ) { return *this; } // assignment operator |
---|
108 | |
---|
109 | bool m_bConnecting; ///< Flag indicating connection in progress |
---|
110 | int m_connect_timeout; ///< Connection timeout (seconds) |
---|
111 | bool m_flush_before_close; ///< Send all data before closing (default true) |
---|
112 | int m_connection_retry; ///< Maximum connection retries (tcp) |
---|
113 | int m_retries; ///< Actual number of connection retries (tcp) |
---|
114 | bool m_call_on_connect; ///< OnConnect will be called next ISocketHandler cycle if true |
---|
115 | bool m_b_retry_connect; ///< Try another connection attempt next ISocketHandler cycle |
---|
116 | bool m_line_protocol; ///< Line protocol mode flag |
---|
117 | int m_shutdown; ///< Shutdown status |
---|
118 | }; |
---|
119 | |
---|
120 | |
---|
121 | #ifdef SOCKETS_NAMESPACE |
---|
122 | } // namespace SOCKETS_NAMESPACE { |
---|
123 | #endif |
---|
124 | |
---|
125 | |
---|
126 | #endif // _StreamSocket_H |
---|
127 | |
---|