~ubuntu-branches/ubuntu/vivid/reminders-app/vivid-proposed

« back to all changes in this revision

Viewing changes to 3rdParty/libthrift/transport/TSocket.h

  • Committer: Package Import Robot
  • Author(s): Ubuntu daily release
  • Date: 2014-07-04 07:35:58 UTC
  • Revision ID: package-import@ubuntu.com-20140704073558-t2grq61xc07lv2bh
Tags: upstream-0.4+14.10.20140704
ImportĀ upstreamĀ versionĀ 0.4+14.10.20140704

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
25
#include "TTransport.h"
 
26
#include "TVirtualTransport.h"
 
27
#include "TServerSocket.h"
 
28
 
 
29
#ifdef HAVE_SYS_TIME_H
 
30
#include <sys/time.h>
 
31
#endif
 
32
#ifdef HAVE_NETDB_H
 
33
#include <netdb.h>
 
34
#endif
 
35
 
 
36
namespace apache { namespace thrift { namespace transport {
 
37
 
 
38
/**
 
39
 * TCP Socket implementation of the TTransport interface.
 
40
 *
 
41
 */
 
42
class TSocket : public TVirtualTransport<TSocket> {
 
43
 public:
 
44
  /**
 
45
   * Constructs a new socket. Note that this does NOT actually connect the
 
46
   * socket.
 
47
   *
 
48
   */
 
49
  TSocket();
 
50
 
 
51
  /**
 
52
   * Constructs a new socket. Note that this does NOT actually connect the
 
53
   * socket.
 
54
   *
 
55
   * @param host An IP address or hostname to connect to
 
56
   * @param port The port to connect on
 
57
   */
 
58
  TSocket(std::string host, int port);
 
59
 
 
60
  /**
 
61
   * Constructs a new Unix domain socket.
 
62
   * Note that this does NOT actually connect the socket.
 
63
   *
 
64
   * @param path The Unix domain socket e.g. "/tmp/ThriftTest.binary.thrift"
 
65
   */
 
66
  TSocket(std::string path);
 
67
 
 
68
  /**
 
69
   * Destroyes the socket object, closing it if necessary.
 
70
   */
 
71
  virtual ~TSocket();
 
72
 
 
73
  /**
 
74
   * Whether the socket is alive.
 
75
   *
 
76
   * @return Is the socket alive?
 
77
   */
 
78
  virtual bool isOpen();
 
79
 
 
80
  /**
 
81
   * Calls select on the socket to see if there is more data available.
 
82
   */
 
83
  virtual bool peek();
 
84
 
 
85
  /**
 
86
   * Creates and opens the UNIX socket.
 
87
   *
 
88
   * @throws TTransportException If the socket could not connect
 
89
   */
 
90
  virtual void open();
 
91
 
 
92
  /**
 
93
   * Shuts down communications on the socket.
 
94
   */
 
95
  virtual void close();
 
96
 
 
97
  /**
 
98
   * Reads from the underlying socket.
 
99
   */
 
100
  virtual uint32_t read(uint8_t* buf, uint32_t len);
 
101
 
 
102
  /**
 
103
   * Writes to the underlying socket.  Loops until done or fail.
 
104
   */
 
105
  virtual void write(const uint8_t* buf, uint32_t len);
 
106
 
 
107
  /**
 
108
   * Writes to the underlying socket.  Does single send() and returns result.
 
109
   */
 
110
  uint32_t write_partial(const uint8_t* buf, uint32_t len);
 
111
 
 
112
  /**
 
113
   * Get the host that the socket is connected to
 
114
   *
 
115
   * @return string host identifier
 
116
   */
 
117
  std::string getHost();
 
118
 
 
119
  /**
 
120
   * Get the port that the socket is connected to
 
121
   *
 
122
   * @return int port number
 
123
   */
 
124
  int getPort();
 
125
 
 
126
  /**
 
127
   * Set the host that socket will connect to
 
128
   *
 
129
   * @param host host identifier
 
130
   */
 
131
  void setHost(std::string host);
 
132
 
 
133
  /**
 
134
   * Set the port that socket will connect to
 
135
   *
 
136
   * @param port port number
 
137
   */
 
138
  void setPort(int port);
 
139
 
 
140
  /**
 
141
   * Controls whether the linger option is set on the socket.
 
142
   *
 
143
   * @param on      Whether SO_LINGER is on
 
144
   * @param linger  If linger is active, the number of seconds to linger for
 
145
   */
 
146
  void setLinger(bool on, int linger);
 
147
 
 
148
  /**
 
149
   * Whether to enable/disable Nagle's algorithm.
 
150
   *
 
151
   * @param noDelay Whether or not to disable the algorithm.
 
152
   * @return
 
153
   */
 
154
  void setNoDelay(bool noDelay);
 
155
 
 
156
  /**
 
157
   * Set the connect timeout
 
158
   */
 
159
  void setConnTimeout(int ms);
 
160
 
 
161
  /**
 
162
   * Set the receive timeout
 
163
   */
 
164
  void setRecvTimeout(int ms);
 
165
 
 
166
  /**
 
167
   * Set the send timeout
 
168
   */
 
169
  void setSendTimeout(int ms);
 
170
 
 
171
  /**
 
172
   * Set the max number of recv retries in case of an EAGAIN
 
173
   * error
 
174
   */
 
175
  void setMaxRecvRetries(int maxRecvRetries);
 
176
 
 
177
  /**
 
178
   * Get socket information formated as a string <Host: x Port: x>
 
179
   */
 
180
  std::string getSocketInfo();
 
181
 
 
182
  /**
 
183
   * Returns the DNS name of the host to which the socket is connected
 
184
   */
 
185
  std::string getPeerHost();
 
186
 
 
187
  /**
 
188
   * Returns the address of the host to which the socket is connected
 
189
   */
 
190
  std::string getPeerAddress();
 
191
 
 
192
  /**
 
193
   * Returns the port of the host to which the socket is connected
 
194
   **/
 
195
  int getPeerPort();
 
196
 
 
197
  /**
 
198
   * Returns the underlying socket file descriptor.
 
199
   */
 
200
  int getSocketFD() {
 
201
    return socket_;
 
202
  }
 
203
 
 
204
  /**
 
205
   * (Re-)initialize a TSocket for the supplied descriptor.  This is only
 
206
   * intended for use by TNonblockingServer -- other use may result in
 
207
   * unfortunate surprises.
 
208
   *
 
209
   * @param fd the descriptor for an already-connected socket
 
210
   */
 
211
  void setSocketFD(int fd);
 
212
 
 
213
  /*
 
214
   * Returns a cached copy of the peer address.
 
215
   */
 
216
  sockaddr* getCachedAddress(socklen_t* len) const;
 
217
 
 
218
  /**
 
219
   * Sets whether to use a low minimum TCP retransmission timeout.
 
220
   */
 
221
  static void setUseLowMinRto(bool useLowMinRto);
 
222
 
 
223
  /**
 
224
   * Gets whether to use a low minimum TCP retransmission timeout.
 
225
   */
 
226
  static bool getUseLowMinRto();
 
227
 
 
228
  /**
 
229
   * Constructor to create socket from raw UNIX handle.
 
230
   */
 
231
  TSocket(int socket);
 
232
 
 
233
  /**
 
234
   * Set a cache of the peer address (used when trivially available: e.g.
 
235
   * accept() or connect()). Only caches IPV4 and IPV6; unset for others.
 
236
   */
 
237
  void setCachedAddress(const sockaddr* addr, socklen_t len);
 
238
 
 
239
 protected:
 
240
  /** connect, called by open */
 
241
  void openConnection(struct addrinfo *res);
 
242
 
 
243
  /** Host to connect to */
 
244
  std::string host_;
 
245
 
 
246
  /** Peer hostname */
 
247
  std::string peerHost_;
 
248
 
 
249
  /** Peer address */
 
250
  std::string peerAddress_;
 
251
 
 
252
  /** Peer port */
 
253
  int peerPort_;
 
254
 
 
255
  /** Port number to connect on */
 
256
  int port_;
 
257
 
 
258
  /** UNIX domain socket path */
 
259
  std::string path_;
 
260
 
 
261
  /** Underlying UNIX socket handle */
 
262
  int socket_;
 
263
 
 
264
  /** Connect timeout in ms */
 
265
  int connTimeout_;
 
266
 
 
267
  /** Send timeout in ms */
 
268
  int sendTimeout_;
 
269
 
 
270
  /** Recv timeout in ms */
 
271
  int recvTimeout_;
 
272
 
 
273
  /** Linger on */
 
274
  bool lingerOn_;
 
275
 
 
276
  /** Linger val */
 
277
  int lingerVal_;
 
278
 
 
279
  /** Nodelay */
 
280
  bool noDelay_;
 
281
 
 
282
  /** Recv EGAIN retries */
 
283
  int maxRecvRetries_;
 
284
 
 
285
  /** Recv timeout timeval */
 
286
  struct timeval recvTimeval_;
 
287
 
 
288
  /** Cached peer address */
 
289
  union {
 
290
    sockaddr_in ipv4;
 
291
    sockaddr_in6 ipv6;
 
292
  } cachedPeerAddr_;
 
293
 
 
294
  /** Connection start time */
 
295
  timespec startTime_;
 
296
 
 
297
  /** Whether to use low minimum TCP retransmission timeout */
 
298
  static bool useLowMinRto_;
 
299
 
 
300
 private:
 
301
  void unix_open();
 
302
  void local_open();
 
303
};
 
304
 
 
305
}}} // apache::thrift::transport
 
306
 
 
307
#endif // #ifndef _THRIFT_TRANSPORT_TSOCKET_H_
 
308