Skip to content

Commit 41f62c7

Browse files
committed
Refactor: use cb::net:: for sockets
This removes duplication of functionality from cbsocket and we can reduce the number of #ifdefs to handle the difference between *nix and Windows Change-Id: I9ca98e8b5801cd8993bc7ba4e2b3eafc515eb7e1 Reviewed-on: http://review.couchbase.org/90784 Reviewed-by: Dave Rigby <[email protected]> Tested-by: Build Bot <[email protected]>
1 parent d0d2334 commit 41f62c7

File tree

12 files changed

+138
-272
lines changed

12 files changed

+138
-272
lines changed

config.h.in

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#define CONFIG_H 1
44

55
#include <platform/platform.h>
6+
#include <platform/socket.h>
7+
68
#include <event.h>
79

810
#cmakedefine HAVE_MEMALIGN ${HAVE_MEMALIGN}
@@ -23,8 +25,6 @@
2325
#define COUCHBASE_MAX_ITEM_PRIVILEGED_BYTES (1024*1024)
2426

2527
#ifdef WIN32
26-
#include <winsock2.h>
27-
#include <ws2tcpip.h>
2828
#include <windows.h>
2929
#include <io.h>
3030

@@ -37,10 +37,7 @@
3737
/* @todo investigate this one.. */
3838

3939
#define SOCKETPAIR_AF AF_INET
40-
#define get_socket_error() WSAGetLastError()
4140

42-
typedef int in_port_t;
43-
typedef int sa_family_t;
4441
typedef HANDLE pid_t;
4542

4643
#define snprintf _snprintf
@@ -56,11 +53,7 @@ typedef HANDLE pid_t;
5653

5754
#else
5855

59-
typedef int SOCKET;
6056
#define SOCKETPAIR_AF AF_UNIX
61-
#define SOCKET_ERROR -1
62-
#define INVALID_SOCKET -1
63-
#define get_socket_error() errno
6457

6558
/* need this to get IOV_MAX on some platforms. */
6659
#ifndef __need_IOV_MAX
@@ -80,7 +73,6 @@ typedef int SOCKET;
8073

8174

8275
#include <sys/wait.h>
83-
#include <sys/socket.h>
8476
#include <netinet/in.h>
8577
#include <netdb.h>
8678
#include <unistd.h>
@@ -95,7 +87,6 @@ typedef int SOCKET;
9587
#include <signal.h>
9688
#include <sysexits.h>
9789

98-
9990
#endif
10091

10192
/* Common section */

daemon/connection.cc

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -190,19 +190,15 @@ bool Connection::setTcpNoDelay(bool enable) {
190190
return false;
191191
}
192192

193-
int flags = enable ? 1 : 0;
194-
195-
#if defined(WIN32)
196-
char* flags_ptr = reinterpret_cast<char*>(&flags);
197-
#else
198-
void* flags_ptr = reinterpret_cast<void*>(&flags);
199-
#endif
200-
int error = setsockopt(socketDescriptor, IPPROTO_TCP, TCP_NODELAY,
201-
flags_ptr,
202-
sizeof(flags));
193+
const int flags = enable ? 1 : 0;
194+
int error = cb::net::setsockopt(socketDescriptor,
195+
IPPROTO_TCP,
196+
TCP_NODELAY,
197+
reinterpret_cast<const void*>(&flags),
198+
sizeof(flags));
203199

204200
if (error != 0) {
205-
std::string errmsg = cb_strerror(GetLastNetworkError());
201+
std::string errmsg = cb_strerror(cb::net::get_socket_error());
206202
LOG_WARNING("setsockopt(TCP_NODELAY): {}", errmsg);
207203
nodelay = false;
208204
return false;

daemon/connection_listen.cc

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,8 @@ void ListenConnection::enable() {
6969
}
7070

7171
if (event_add(ev.get(), NULL) == -1) {
72-
log_system_error(EXTENSION_LOG_WARNING,
73-
NULL,
74-
"Failed to add connection to libevent: %s");
72+
LOG_WARNING("Failed to add connection to libevent: {}",
73+
cb_strerror());
7574
} else {
7675
registered_in_libevent = true;
7776
}
@@ -95,9 +94,8 @@ void ListenConnection::disable() {
9594
}
9695
}
9796
if (event_del(ev.get()) == -1) {
98-
log_system_error(EXTENSION_LOG_WARNING,
99-
NULL,
100-
"Failed to remove connection to libevent: %s");
97+
LOG_WARNING("Failed to remove connection to libevent: {}",
98+
cb_strerror());
10199
} else {
102100
registered_in_libevent = false;
103101
}

daemon/connection_mcbp.cc

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,8 @@ bool McbpConnection::unregisterEvent() {
4545
cb_assert(socketDescriptor != INVALID_SOCKET);
4646

4747
if (event_del(&event) == -1) {
48-
log_system_error(EXTENSION_LOG_WARNING,
49-
NULL,
50-
"Failed to remove connection to libevent: %s");
48+
LOG_WARNING("Failed to remove connection to libevent: {}",
49+
cb_strerror());
5150
return false;
5251
}
5352

@@ -80,8 +79,7 @@ bool McbpConnection::registerEvent() {
8079
ev_insert_time = mc_time_get_current_time();
8180

8281
if (event_add(&event, tp) == -1) {
83-
log_system_error(EXTENSION_LOG_WARNING, nullptr,
84-
"Failed to add connection to libevent: %s");
82+
LOG_WARNING("Failed to add connection to libevent: {}", cb_strerror());
8583
return false;
8684
}
8785

@@ -269,7 +267,7 @@ int McbpConnection::sslPreConnection() {
269267
}
270268
}
271269
if (disconnect) {
272-
set_econnreset();
270+
cb::net::set_econnreset();
273271
if (!certResult.second.empty()) {
274272
LOG_WARNING(
275273
"{}: SslPreConnection: disconnection client due to"
@@ -282,7 +280,7 @@ int McbpConnection::sslPreConnection() {
282280
} else {
283281
if (ssl.getError(r) == SSL_ERROR_WANT_READ) {
284282
ssl.drainBioSendPipe(socketDescriptor);
285-
set_ewouldblock();
283+
cb::net::set_ewouldblock();
286284
return -1;
287285
} else {
288286
try {
@@ -300,7 +298,7 @@ int McbpConnection::sslPreConnection() {
300298
// unable to print error message; continue.
301299
}
302300

303-
set_econnreset();
301+
cb::net::set_econnreset();
304302
return -1;
305303
}
306304
}
@@ -318,7 +316,7 @@ int McbpConnection::recv(char* dest, size_t nbytes) {
318316
ssl.drainBioRecvPipe(socketDescriptor);
319317

320318
if (ssl.hasError()) {
321-
set_econnreset();
319+
cb::net::set_econnreset();
322320
return -1;
323321
}
324322

@@ -343,8 +341,8 @@ int McbpConnection::recv(char* dest, size_t nbytes) {
343341
return res;
344342
}
345343

346-
int McbpConnection::sendmsg(struct msghdr* m) {
347-
int res = 0;
344+
ssize_t McbpConnection::sendmsg(struct msghdr* m) {
345+
ssize_t res = 0;
348346
if (ssl.isEnabled()) {
349347
for (int ii = 0; ii < int(m->msg_iovlen); ++ii) {
350348
int n = sslWrite(reinterpret_cast<char*>(m->msg_iov[ii].iov_base),
@@ -362,7 +360,7 @@ int McbpConnection::sendmsg(struct msghdr* m) {
362360
ssl.drainBioSendPipe(socketDescriptor);
363361
return res;
364362
} else {
365-
res = int(::sendmsg(socketDescriptor, m, 0));
363+
res = cb::net::sendmsg(socketDescriptor, m, 0);
366364
if (res > 0) {
367365
totalSend += res;
368366
}
@@ -436,7 +434,7 @@ McbpConnection::TransmitResult McbpConnection::transmit() {
436434
struct msghdr* m = &msglist[msgcurr];
437435

438436
res = sendmsg(m);
439-
auto error = GetLastNetworkError();
437+
auto error = cb::net::get_socket_error();
440438
if (res > 0) {
441439
get_thread_stats(this)->bytes_written += res;
442440

@@ -463,7 +461,7 @@ McbpConnection::TransmitResult McbpConnection::transmit() {
463461
return TransmitResult::Incomplete;
464462
}
465463

466-
if (res == -1 && is_blocking(error)) {
464+
if (res == -1 && cb::net::is_blocking(error)) {
467465
if (!updateEvent(EV_WRITE | EV_PERSIST)) {
468466
setState(McbpStateMachine::State::closing);
469467
return TransmitResult::HardError;
@@ -474,12 +472,12 @@ McbpConnection::TransmitResult McbpConnection::transmit() {
474472
// if res == 0 or res == -1 and error is not EAGAIN or EWOULDBLOCK,
475473
// we have a real error, on which we close the connection
476474
if (res == -1) {
477-
if (is_closed_conn(error)) {
475+
if (cb::net::is_closed_conn(error)) {
478476
LOG_INFO("{}: Failed to send data; peer closed the connection",
479477
getId());
480478
} else {
481-
log_socket_error(EXTENSION_LOG_WARNING, this,
482-
"Failed to write, and not due to blocking: %s");
479+
LOG_WARNING("Failed to write, and not due to blocking: {}",
480+
cb_strerror(error));
483481
}
484482
} else {
485483
// sendmsg should return the number of bytes written, but we
@@ -544,8 +542,8 @@ McbpConnection::TryReadResult McbpConnection::tryReadNetwork() {
544542
return TryReadResult::SocketClosed;
545543
}
546544

547-
const auto error = GetLastNetworkError();
548-
if (is_blocking(error)) {
545+
const auto error = cb::net::get_socket_error();
546+
if (cb::net::is_blocking(error)) {
549547
return TryReadResult::NoDataReceived;
550548
}
551549

@@ -570,7 +568,7 @@ int McbpConnection::sslRead(char* dest, size_t nbytes) {
570568
int n;
571569
ssl.drainBioRecvPipe(socketDescriptor);
572570
if (ssl.hasError()) {
573-
set_econnreset();
571+
cb::net::set_econnreset();
574572
return -1;
575573
}
576574
n = ssl.read(dest + ret, (int)(nbytes - ret));
@@ -593,7 +591,7 @@ int McbpConnection::sslRead(char* dest, size_t nbytes) {
593591
/* nothing in our recv buf, return what we have */
594592
return ret;
595593
} else {
596-
set_ewouldblock();
594+
cb::net::set_ewouldblock();
597595
return -1;
598596
}
599597
break;
@@ -610,7 +608,7 @@ int McbpConnection::sslRead(char* dest, size_t nbytes) {
610608
LOG_WARNING("{}: ERROR: SSL_read returned -1 with error {}",
611609
getId(),
612610
error);
613-
set_econnreset();
611+
cb::net::set_econnreset();
614612
return -1;
615613
}
616614
}
@@ -630,7 +628,7 @@ int McbpConnection::sslWrite(const char* src, size_t nbytes) {
630628

631629
ssl.drainBioSendPipe(socketDescriptor);
632630
if (ssl.hasError()) {
633-
set_econnreset();
631+
cb::net::set_econnreset();
634632
return -1;
635633
}
636634

@@ -652,7 +650,7 @@ int McbpConnection::sslWrite(const char* src, size_t nbytes) {
652650
int error = ssl.getError(n);
653651
switch (error) {
654652
case SSL_ERROR_WANT_WRITE:
655-
set_ewouldblock();
653+
cb::net::set_ewouldblock();
656654
return -1;
657655

658656
default:
@@ -664,7 +662,7 @@ int McbpConnection::sslWrite(const char* src, size_t nbytes) {
664662
"{}: ERROR: SSL_write returned -1 with error {}",
665663
getId(),
666664
error);
667-
set_econnreset();
665+
cb::net::set_econnreset();
668666
return -1;
669667
}
670668
}

daemon/connection_mcbp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ class McbpConnection : public Connection {
284284
* @param m the message header to send
285285
* @return the number of bytes sent, or -1 for an error
286286
*/
287-
int sendmsg(struct msghdr* m);
287+
ssize_t sendmsg(struct msghdr* m);
288288

289289
enum class TransmitResult {
290290
/** All done writing. */

0 commit comments

Comments
 (0)