Subversion Repositories SmartDukaan

Rev

Rev 30 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
30 ashish 1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one
3
 * or more contributor license agreements. See the NOTICE file
4
 * distributed with this work for additional information
5
 * regarding copyright ownership. The ASF licenses this file
6
 * to you under the Apache License, Version 2.0 (the
7
 * "License"); you may not use this file except in compliance
8
 * with the License. You may obtain a copy of the License at
9
 *
10
 *   http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing,
13
 * software distributed under the License is distributed on an
14
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
 * KIND, either express or implied. See the License for the
16
 * specific language governing permissions and limitations
17
 * under the License.
18
 */
19
 
20
#ifndef _THRIFT_TRANSPORT_TSOCKET_H_
21
#define _THRIFT_TRANSPORT_TSOCKET_H_ 1
22
 
23
#include <string>
24
#include <sys/time.h>
25
 
26
#include "TTransport.h"
27
#include "TServerSocket.h"
28
 
29
namespace apache { namespace thrift { namespace transport {
30
 
31
/**
32
 * TCP Socket implementation of the TTransport interface.
33
 *
34
 */
35
class TSocket : public TTransport {
36
  /**
37
   * We allow the TServerSocket acceptImpl() method to access the private
38
   * members of a socket so that it can access the TSocket(int socket)
39
   * constructor which creates a socket object from the raw UNIX socket
40
   * handle.
41
   */
42
  friend class TServerSocket;
43
 
44
 public:
45
  /**
46
   * Constructs a new socket. Note that this does NOT actually connect the
47
   * socket.
48
   *
49
   */
50
  TSocket();
51
 
52
  /**
53
   * Constructs a new socket. Note that this does NOT actually connect the
54
   * socket.
55
   *
56
   * @param host An IP address or hostname to connect to
57
   * @param port The port to connect on
58
   */
59
  TSocket(std::string host, int port);
60
 
61
  /**
62
   * Destroyes the socket object, closing it if necessary.
63
   */
64
  virtual ~TSocket();
65
 
66
  /**
67
   * Whether the socket is alive.
68
   *
69
   * @return Is the socket alive?
70
   */
71
  bool isOpen();
72
 
73
  /**
74
   * Calls select on the socket to see if there is more data available.
75
   */
76
  bool peek();
77
 
78
  /**
79
   * Creates and opens the UNIX socket.
80
   *
81
   * @throws TTransportException If the socket could not connect
82
   */
83
  virtual void open();
84
 
85
  /**
86
   * Shuts down communications on the socket.
87
   */
88
  void close();
89
 
90
  /**
91
   * Reads from the underlying socket.
92
   */
93
  uint32_t read(uint8_t* buf, uint32_t len);
94
 
95
  /**
96
   * Writes to the underlying socket.
97
   */
98
  void write(const uint8_t* buf, uint32_t len);
99
 
100
  /**
101
   * Get the host that the socket is connected to
102
   *
103
   * @return string host identifier
104
   */
105
  std::string getHost();
106
 
107
  /**
108
   * Get the port that the socket is connected to
109
   *
110
   * @return int port number
111
   */
112
  int getPort();
113
 
114
  /**
115
   * Set the host that socket will connect to
116
   *
117
   * @param host host identifier
118
   */
119
  void setHost(std::string host);
120
 
121
  /**
122
   * Set the port that socket will connect to
123
   *
124
   * @param port port number
125
   */
126
  void setPort(int port);
127
 
128
  /**
129
   * Controls whether the linger option is set on the socket.
130
   *
131
   * @param on      Whether SO_LINGER is on
132
   * @param linger  If linger is active, the number of seconds to linger for
133
   */
134
  void setLinger(bool on, int linger);
135
 
136
  /**
137
   * Whether to enable/disable Nagle's algorithm.
138
   *
139
   * @param noDelay Whether or not to disable the algorithm.
140
   * @return
141
   */
142
  void setNoDelay(bool noDelay);
143
 
144
  /**
145
   * Set the connect timeout
146
   */
147
  void setConnTimeout(int ms);
148
 
149
  /**
150
   * Set the receive timeout
151
   */
152
  void setRecvTimeout(int ms);
153
 
154
  /**
155
   * Set the send timeout
156
   */
157
  void setSendTimeout(int ms);
158
 
159
  /**
160
   * Set the max number of recv retries in case of an EAGAIN
161
   * error
162
   */
163
  void setMaxRecvRetries(int maxRecvRetries);
164
 
165
  /**
166
   * Get socket information formated as a string <Host: x Port: x>
167
   */
168
  std::string getSocketInfo();
169
 
170
  /**
171
   * Returns the DNS name of the host to which the socket is connected
172
   */
173
  std::string getPeerHost();
174
 
175
  /**
176
   * Returns the address of the host to which the socket is connected
177
   */
178
  std::string getPeerAddress();
179
 
180
  /**
181
   * Returns the port of the host to which the socket is connected
182
   **/
183
  int getPeerPort();
184
 
185
 
186
 protected:
187
  /**
188
   * Constructor to create socket from raw UNIX handle. Never called directly
189
   * but used by the TServerSocket class.
190
   */
191
  TSocket(int socket);
192
 
193
  /** connect, called by open */
194
  void openConnection(struct addrinfo *res);
195
 
196
  /** Host to connect to */
197
  std::string host_;
198
 
199
  /** Peer hostname */
200
  std::string peerHost_;
201
 
202
  /** Peer address */
203
  std::string peerAddress_;
204
 
205
  /** Peer port */
206
  int peerPort_;
207
 
208
  /** Port number to connect on */
209
  int port_;
210
 
211
  /** Underlying UNIX socket handle */
212
  int socket_;
213
 
214
  /** Connect timeout in ms */
215
  int connTimeout_;
216
 
217
  /** Send timeout in ms */
218
  int sendTimeout_;
219
 
220
  /** Recv timeout in ms */
221
  int recvTimeout_;
222
 
223
  /** Linger on */
224
  bool lingerOn_;
225
 
226
  /** Linger val */
227
  int lingerVal_;
228
 
229
  /** Nodelay */
230
  bool noDelay_;
231
 
232
  /** Recv EGAIN retries */
233
  int maxRecvRetries_;
234
 
235
  /** Recv timeout timeval */
236
  struct timeval recvTimeval_;
237
};
238
 
239
}}} // apache::thrift::transport
240
 
241
#endif // #ifndef _THRIFT_TRANSPORT_TSOCKET_H_
242