24
24
#include " arrow/status.h"
25
25
26
26
#include " plasma/common.h"
27
+ #include " plasma/plasma_generated.h"
27
28
28
29
using arrow::Status;
29
30
30
- /* Number of times we try connecting to a socket. */
31
- #define NUM_CONNECT_ATTEMPTS 50
32
- #define CONNECT_TIMEOUT_MS 100
31
+ // / Number of times we try connecting to a socket.
32
+ constexpr int64_t kNumConnectAttempts = 50 ;
33
+ // / Time to wait between connection attempts to a socket.
34
+ constexpr int64_t kConnectTimeoutMs = 100 ;
33
35
34
36
namespace plasma {
35
37
@@ -38,8 +40,8 @@ Status WriteBytes(int fd, uint8_t* cursor, size_t length) {
38
40
size_t bytesleft = length;
39
41
size_t offset = 0 ;
40
42
while (bytesleft > 0 ) {
41
- /* While we haven't written the whole message, write to the file descriptor,
42
- * advance the cursor, and decrease the amount left to write. */
43
+ // While we haven't written the whole message, write to the file descriptor,
44
+ // advance the cursor, and decrease the amount left to write.
43
45
nbytes = write (fd, cursor + offset, bytesleft);
44
46
if (nbytes < 0 ) {
45
47
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
@@ -58,7 +60,7 @@ Status WriteBytes(int fd, uint8_t* cursor, size_t length) {
58
60
}
59
61
60
62
Status WriteMessage (int fd, int64_t type, int64_t length, uint8_t * bytes) {
61
- int64_t version = PLASMA_PROTOCOL_VERSION ;
63
+ int64_t version = kPlasmaProtocolVersion ;
62
64
RETURN_NOT_OK (WriteBytes (fd, reinterpret_cast <uint8_t *>(&version), sizeof (version)));
63
65
RETURN_NOT_OK (WriteBytes (fd, reinterpret_cast <uint8_t *>(&type), sizeof (type)));
64
66
RETURN_NOT_OK (WriteBytes (fd, reinterpret_cast <uint8_t *>(&length), sizeof (length)));
@@ -67,7 +69,7 @@ Status WriteMessage(int fd, int64_t type, int64_t length, uint8_t* bytes) {
67
69
68
70
Status ReadBytes (int fd, uint8_t * cursor, size_t length) {
69
71
ssize_t nbytes = 0 ;
70
- /* Termination condition: EOF or read 'length' bytes total. */
72
+ // Termination condition: EOF or read 'length' bytes total.
71
73
size_t bytesleft = length;
72
74
size_t offset = 0 ;
73
75
while (bytesleft > 0 ) {
@@ -91,20 +93,21 @@ Status ReadBytes(int fd, uint8_t* cursor, size_t length) {
91
93
Status ReadMessage (int fd, int64_t * type, std::vector<uint8_t >* buffer) {
92
94
int64_t version;
93
95
RETURN_NOT_OK_ELSE (ReadBytes (fd, reinterpret_cast <uint8_t *>(&version), sizeof (version)),
94
- *type = DISCONNECT_CLIENT );
95
- ARROW_CHECK (version == PLASMA_PROTOCOL_VERSION ) << " version = " << version;
96
+ *type = MessageType_PlasmaDisconnectClient );
97
+ ARROW_CHECK (version == kPlasmaProtocolVersion ) << " version = " << version;
96
98
RETURN_NOT_OK_ELSE (ReadBytes (fd, reinterpret_cast <uint8_t *>(type), sizeof (*type)),
97
- *type = DISCONNECT_CLIENT );
99
+ *type = MessageType_PlasmaDisconnectClient );
98
100
int64_t length_temp;
99
101
RETURN_NOT_OK_ELSE (
100
102
ReadBytes (fd, reinterpret_cast <uint8_t *>(&length_temp), sizeof (length_temp)),
101
- *type = DISCONNECT_CLIENT );
103
+ *type = MessageType_PlasmaDisconnectClient );
102
104
// The length must be read as an int64_t, but it should be used as a size_t.
103
105
size_t length = static_cast <size_t >(length_temp);
104
106
if (length > buffer->size ()) {
105
107
buffer->resize (length);
106
108
}
107
- RETURN_NOT_OK_ELSE (ReadBytes (fd, buffer->data (), length), *type = DISCONNECT_CLIENT);
109
+ RETURN_NOT_OK_ELSE (ReadBytes (fd, buffer->data (), length),
110
+ *type = MessageType_PlasmaDisconnectClient);
108
111
return Status::OK ();
109
112
}
110
113
@@ -115,7 +118,7 @@ int bind_ipc_sock(const std::string& pathname, bool shall_listen) {
115
118
ARROW_LOG (ERROR) << " socket() failed for pathname " << pathname;
116
119
return -1 ;
117
120
}
118
- /* Tell the system to allow the port to be reused. */
121
+ // Tell the system to allow the port to be reused.
119
122
int on = 1 ;
120
123
if (setsockopt (socket_fd, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast <char *>(&on),
121
124
sizeof (on)) < 0 ) {
@@ -150,23 +153,23 @@ int bind_ipc_sock(const std::string& pathname, bool shall_listen) {
150
153
151
154
Status ConnectIpcSocketRetry (const std::string& pathname, int num_retries,
152
155
int64_t timeout, int * fd) {
153
- /* Pick the default values if the user did not specify. */
156
+ // Pick the default values if the user did not specify.
154
157
if (num_retries < 0 ) {
155
- num_retries = NUM_CONNECT_ATTEMPTS ;
158
+ num_retries = kNumConnectAttempts ;
156
159
}
157
160
if (timeout < 0 ) {
158
- timeout = CONNECT_TIMEOUT_MS ;
161
+ timeout = kConnectTimeoutMs ;
159
162
}
160
163
*fd = connect_ipc_sock (pathname);
161
164
while (*fd < 0 && num_retries > 0 ) {
162
165
ARROW_LOG (ERROR) << " Connection to IPC socket failed for pathname " << pathname
163
166
<< " , retrying " << num_retries << " more times" ;
164
- /* Sleep for timeout milliseconds. */
167
+ // Sleep for timeout milliseconds.
165
168
usleep (static_cast <int >(timeout * 1000 ));
166
169
*fd = connect_ipc_sock (pathname);
167
170
--num_retries;
168
171
}
169
- /* If we could not connect to the socket, exit. */
172
+ // If we could not connect to the socket, exit.
170
173
if (*fd == -1 ) {
171
174
std::stringstream ss;
172
175
ss << " Could not connect to socket " << pathname;
@@ -215,15 +218,15 @@ std::unique_ptr<uint8_t[]> read_message_async(int sock) {
215
218
int64_t size;
216
219
Status s = ReadBytes (sock, reinterpret_cast <uint8_t *>(&size), sizeof (int64_t ));
217
220
if (!s.ok ()) {
218
- /* The other side has closed the socket. */
221
+ // The other side has closed the socket.
219
222
ARROW_LOG (DEBUG) << " Socket has been closed, or some other error has occurred." ;
220
223
close (sock);
221
224
return NULL ;
222
225
}
223
226
auto message = std::unique_ptr<uint8_t []>(new uint8_t [size]);
224
227
s = ReadBytes (sock, message.get (), size);
225
228
if (!s.ok ()) {
226
- /* The other side has closed the socket. */
229
+ // The other side has closed the socket.
227
230
ARROW_LOG (DEBUG) << " Socket has been closed, or some other error has occurred." ;
228
231
close (sock);
229
232
return NULL ;
0 commit comments