SmartFoxServer 2X C++ API
TCPClient.h
1 // ===================================================================
2 //
3 // Description
4 // Contains the definition of an handler for a Tcp client
5 //
6 // Revision history
7 // Date Description
8 // 30-Nov-2012 First version
9 //
10 // ===================================================================
11 #ifndef __TCPClient__
12 #define __TCPClient__
13 
14 #include "../../Util/Common.h" // Common API items
15 #include "IPAddress.h" // Ip address item
16 #include "../../Util/DelegateOneArgument.h" // Delegate with one parameter
17 #include "SocketErrors.h" // Socket errors
18 
19 #include <boost/bind.hpp> // Boost Asio header
20 #include <boost/asio.hpp> // Boost Asio header
21 #include <boost/asio/deadline_timer.hpp> // Boost Asio header
22 #include <boost/array.hpp> // Boost Asio header
23 #include <boost/shared_ptr.hpp> // Boost Asio shared pointer
24 #include <boost/enable_shared_from_this.hpp> // Boost Asio shared from this
25 #include <boost/thread/recursive_mutex.hpp> // Boost Asio header
26 #include <boost/thread/lock_guard.hpp> // Boost Asio header
27 
28 #if defined(_MSC_VER)
29 #pragma warning(disable:4786) // STL library: disable warning 4786; this warning is generated due to a Microsoft bug
30 #endif
31 #include <vector> // STL library: vector object
32 using namespace std; // STL library: declare the STL namespace
33 
34 using boost::asio::ip::tcp; // Boost Asio namespace
35 
36 using namespace Sfs2X::Util;
37 
38 namespace Sfs2X {
39 namespace Core {
40 namespace Sockets {
41 
42  // -------------------------------------------------------------------
43  // Definition of delegates specific for the Tcp client handler
44  // -------------------------------------------------------------------
45  typedef DelegateOneArgument<SocketErrors> TCPConnectionDelegate;
46  typedef DelegateOneArgument<boost::shared_ptr<vector<unsigned char> > > TCPDataReadDelegate;
47  typedef DelegateOneArgument<SocketErrors> TCPDataWriteDelegate;
48 
49  // -------------------------------------------------------------------
50  // Class TCPClient
51  // -------------------------------------------------------------------
52  class TCPClient : public boost::enable_shared_from_this<TCPClient>
53  {
54  public:
55 
56  // -------------------------------------------------------------------
57  // Public methods
58  // -------------------------------------------------------------------
59 
60  // Constructor
61  // io_service Boost Asio service
62  TCPClient (boost::asio::io_service& io_service);
63 
64  // Destructor
65  ~TCPClient();
66 
67  virtual void Dispose();
68 
69  // Connect
70  // Attemps to establish a tcp client connection
71  // ipAddress Server ip address expressed in dotted form
72  // ipPort Server ip port
73  // timeout Connection timeout expressed in seconds
74  // callback Callback to notify operation result
75  void Connect (boost::shared_ptr<IPAddress> ipAddress, unsigned short int ipPort, long int timeout, boost::shared_ptr<TCPConnectionDelegate> callback);
76 
77  // Connect
78  // Attemps to establish a tcp client connection
79  // ipAddress Server ip address expressed in dotted form
80  // ipPort Server ip port
81  void SynchConnect (boost::shared_ptr<IPAddress> ipAddress, unsigned short int ipPort);
82 
83  // Shutdown
84  // Terminate an established tcp client connection
85  void Shutdown();
86 
87  // AsynchRead
88  // Begin an asynchronous read task over an established socket
89  // callback Callback to notify operation result
90  void AsynchRead (boost::shared_ptr<TCPDataReadDelegate> callback);
91 
92  // AsynchWrite
93  // Asynchronous write data on the underlying socket
94  // data Buffer that contains data to write
95  // callback Callback to notify the completion of sent task
96  void AsynchWrite (boost::shared_ptr<vector<unsigned char> > data, boost::shared_ptr<TCPDataWriteDelegate> callback);
97 
98  // SynchRead
99  // Synchronous read over an established socket
100  // Return empty vector (never null) when no data has been received
101  boost::shared_ptr<vector<unsigned char> > SynchRead ();
102 
103  // SynchWrite
104  // Synchronous write data on the underlying socket
105  // data Buffer that contains data to write
106  void SynchWrite (boost::shared_ptr<vector<unsigned char> > data);
107 
108  bool CallbackCallInProgress();
109 
110  // -------------------------------------------------------------------
111  // Public members
112  // -------------------------------------------------------------------
113 
114  protected:
115 
116  // -------------------------------------------------------------------
117  // Protected methods
118  // -------------------------------------------------------------------
119 
120  // -------------------------------------------------------------------
121  // Protected members
122  // -------------------------------------------------------------------
123 
124  private:
125 
126  // -------------------------------------------------------------------
127  // Private methods
128  // -------------------------------------------------------------------
129 
130  // OnBoostAsioConnect
131  // Callback to receive Boost Asio notification regarding connect task
132  // code Error code
133  void OnBoostAsioConnect(const boost::system::error_code& code);
134 
135  // OnBoostAsioConnectTimedout
136  // Callback to receive Boost Asio notification regarding timedout connect task
137  // code Error code
138  void OnBoostAsioConnectTimedout(const boost::system::error_code& code);
139 
140  // OnBoostAsioDataReceived
141  // Callback to receive Boost Asio notification regarding timedout connect task
142  // error Error code
143  // length Length received data
144  void OnBoostAsioDataReceived(const boost::system::error_code& error, long int length);
145 
146  // OnBoostAsioDataSent
147  // Callback to notify the completion of asynchronous write over the socket
148  // bufferOutput Output data
149  // length Quantity of bytes into output data
150  // transferred Quantity of transferred bytes
151  // error Error code
152  void OnBoostAsioDataSent(boost::shared_ptr<unsigned char> bufferOutput, size_t length, size_t transferred, const boost::system::error_code& error);
153 
154  // -------------------------------------------------------------------
155  // Private members
156  // -------------------------------------------------------------------
157 
158  const static long int READ_BUFFER_SIZE = 4096;
159 
160  boost::asio::io_service& boostIoService; // Boost Asio IO service
161  tcp::resolver boostTcpResolver; // Boost Asio Tcp resolver
162  tcp::socket boostTcpSocket; // Boost Asio Tcp socket
163  boost::asio::deadline_timer boostTimerTaskDeadline; // Boost Asio deadline timer
164  boost::array<char, READ_BUFFER_SIZE> boostTcpInputBuffer; // Boost Asio Tcp input buffer
165 
166  boost::shared_ptr<TCPConnectionDelegate> callbackTCPConnection; // Callback to notify tcp connection result
167  boost::shared_ptr<TCPDataReadDelegate> callbackTCPDataRead; // Callback to notify tcp received data
168  boost::shared_ptr<TCPDataWriteDelegate> callbackTCPDataWrite; // Callback to notify the completion of tcp write data
169 
170  boost::recursive_mutex mtxDisconnection; // Mutex to synchronize Tcp disconnection tasks
171 
172  long int counterAsyncReadOperationsInProgress; // Counter of asynchronous read operations that are in progress
173  long int counterAsyncWriteOperationsInProgress; // Counter of asynchronous write operations that are in progress
174  bool isDisposed;
175  bool isCallbackCallInProgress;
176  };
177 
178 } // namespace Sockets
179 } // namespace Core
180 } // namespace Sfs2X
181 
182 #endif
183 
184 
185 
STL namespace.
Definition: BuddyOnlineState.h:15
Definition: SmartFox.cpp:24