forked from facebook/wdt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SocketUtils.h
82 lines (76 loc) · 2.93 KB
/
SocketUtils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
* Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#pragma once
#include "WdtBase.h"
#include <sys/socket.h>
#include <string>
namespace facebook {
namespace wdt {
class SocketUtils {
public:
static int getReceiveBufferSize(int fd);
/**
* Returns ip and port for a socket address
*
* @param sa socket address
* @param salen socket address length
* @param host this is set to host name
* @param port this is set to port
*
* @return whether getnameinfo was successful or not
*/
static bool getNameInfo(const struct sockaddr *sa, socklen_t salen,
std::string &host, std::string &port);
static void setReadTimeout(int fd);
static void setWriteTimeout(int fd);
/// @see ioWithAbortCheck
static int64_t readWithAbortCheck(int fd, char *buf, int64_t nbyte,
WdtBase::IAbortChecker const *abortChecker,
bool tryFull);
/// @see ioWithAbortCheck
static int64_t writeWithAbortCheck(int fd, const char *buf, int64_t nbyte,
WdtBase::IAbortChecker const *abortChecker,
bool tryFull);
private:
/**
* Tries to read/write numBytes amount of data from fd. Also, checks for abort
* after every read/write call. Also, retries till the input timeout.
* Optionally, returns after first successful read/write call.
*
* @param readOrWrite read/write
* @param fd socket file descriptor
* @param tbuf buffer
* @param numBytes number of bytes to read/write
* @param abortChecker abort checker callback
* @param timeoutMs timeout in milliseconds
* @param tryFull if true, this function tries to read complete data.
* Otherwise, this function returns after the first
* successful read/write. This is set to false for
* receiver pipelining.
*
* @return in case of success number of bytes read/written, else
* returns -1
*/
template <typename F, typename T>
static int64_t ioWithAbortCheck(F readOrWrite, int fd, T tbuf,
int64_t numBytes,
WdtBase::IAbortChecker const *abortChecker,
int timeoutMs, bool tryFull);
/**
* Depending on the network timeout and abort interval, returns the timeout to
* set for socket operations
*
* @param networkTimeout network timeout
*
* @return timeout for socket operations
*/
static int getTimeout(int networkTimeout);
};
}
} // namespace facebook::wdt