switched from PracticalSocket to libasio
[anytun.git] / src / Sockets / StreamSocket.h
blob3c248110b885ecf54f5c472e2c67f5599b9e4bdb
1 #ifndef _StreamSocket_H
2 #define _StreamSocket_H
4 #include "Socket.h"
7 #ifdef SOCKETS_NAMESPACE
8 namespace SOCKETS_NAMESPACE {
9 #endif
12 /** SOCK_STREAM Socket base class.
13 \ingroup basic */
14 class StreamSocket : public Socket
16 public:
17 StreamSocket(ISocketHandler& );
18 ~StreamSocket();
20 /** Socket should Check Connect on next write event from select(). */
21 void SetConnecting(bool = true);
23 /** Check connecting flag.
24 \return true if the socket is still trying to connect */
25 bool Connecting();
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();
32 /** Set timeout to use for connection attempt.
33 \param x Timeout in seconds */
34 void SetConnectTimeout(int x);
36 /** Return number of seconds to wait for a connection.
37 \return Connection timeout (seconds) */
38 int GetConnectTimeout();
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);
44 /** Check flush before status.
45 \return true if the socket should send all data before closing */
46 bool GetFlushBeforeClose();
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);
54 /** Get number of maximum connection retries (tcp only). */
55 int GetConnectionRetry();
57 /** Increase number of actual connection retries (tcp only). */
58 void IncreaseConnectionRetries();
60 /** Get number of actual connection retries (tcp only). */
61 int GetConnectionRetries();
63 /** Reset actual connection retries (tcp only). */
64 void ResetConnectionRetries();
66 // LIST_CALLONCONNECT
68 /** Instruct socket to call OnConnect callback next sockethandler cycle. */
69 void SetCallOnConnect(bool x = true);
71 /** Check call on connect flag.
72 \return true if OnConnect() should be called a.s.a.p */
73 bool CallOnConnect();
75 // LIST_RETRY
77 /** Set flag to initiate a connection attempt after a connection timeout. */
78 void SetRetryClientConnect(bool x = true);
80 /** Check if a connection attempt should be made.
81 \return true when another attempt should be made */
82 bool RetryClientConnect();
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);
90 /** Check line protocol mode.
91 \return true if socket is in line protocol mode */
92 bool LineProtocol();
94 /** Set shutdown status. */
95 void SetShutdown(int);
97 /** Get shutdown status. */
98 int GetShutdown();
100 /** Returns IPPROTO_TCP or IPPROTO_SCTP */
101 virtual int Protocol() = 0;
103 protected:
104 StreamSocket(const StreamSocket& ) {} // copy constructor
106 private:
107 StreamSocket& operator=(const StreamSocket& ) { return *this; } // assignment operator
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
121 #ifdef SOCKETS_NAMESPACE
122 } // namespace SOCKETS_NAMESPACE {
123 #endif
126 #endif // _StreamSocket_H