23 #include "common_priv.h"
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <netinet/tcp.h>
34 ASSERT(sock1.
FD() == -1,
"sock1 already connected");
35 ASSERT(sock2.
FD() == -1,
"sock2 already connected");
43 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd) < 0) {
56 for (SockAddrList::const_iterator itr = addrs.begin();
57 itr != addrs.end(); ++itr) {
66 int nfd = socket(addr.
Family(), SOCK_STREAM, 0);
108 if (shutdown(
FD(), SHUT_RD) != 0) {
114 if (shutdown(
FD(), SHUT_WR) != 0) {
123 if (
eof ||
fd == -1) {
return 0; }
128 flags |= MSG_WAITALL;
130 flags |= MSG_DONTWAIT;
132 unsigned bytesread = 0;
133 int num = recv(filed, ptr, len, flags);
135 if (
unsigned(num) < len) {
Readable(
false); }
137 }
else if (num == 0 && len != 0) {
141 }
else if (num < 0) {
157 #if !defined(__APPLE__)
159 if (block) {
flags &= ~MSG_DONTWAIT; }
160 else {
flags |= MSG_DONTWAIT; }
165 if (sig) { flags &= ~MSG_NOSIGNAL; }
166 else { flags |= MSG_NOSIGNAL; }
171 if (more) { flags |= MSG_MORE; }
172 else { flags &= ~MSG_MORE; }
181 if (
fd == -1) {
return 0; }
184 unsigned written = 0;
185 int num = send(filed, ptr, len, opts.
flags);
198 if (
unsigned(num) < len) {
Writeable(
false); }
206 socklen_t len =
sizeof(err);
207 if (getsockopt(
FD(), SOL_SOCKET, SO_ERROR, &err, &len) < 0) {
214 if (setsockopt(
FD(), SOL_SOCKET, SO_KEEPALIVE, &ka,
sizeof(ka)) < 0) {
221 socklen_t len =
sizeof(ka);
222 if (getsockopt(
FD(), SOL_SOCKET, SO_KEEPALIVE, &ka, &len) < 0) {
232 l.l_linger = seconds;
234 if (setsockopt(
FD(), SOL_SOCKET, SO_LINGER, &l,
sizeof(l)) < 0) {
241 socklen_t len =
sizeof(l);
242 if (getsockopt(
FD(), SOL_SOCKET, SO_LINGER, &l, &len) < 0) {
253 if (setsockopt(
FD(), SOL_SOCKET, SO_RCVBUF, &size,
sizeof(size)) < 0) {
260 socklen_t len =
sizeof(size);
261 if (getsockopt(
FD(), SOL_SOCKET, SO_RCVBUF, &size, &len) < 0) {
268 if (setsockopt(
FD(), SOL_SOCKET, SO_SNDBUF, &size,
sizeof(size)) < 0) {
275 socklen_t len =
sizeof(size);
276 if (getsockopt(
FD(), SOL_SOCKET, SO_SNDBUF, &size, &len) < 0) {
285 tv.tv_sec = (
int)timeout;
286 tv.tv_usec = (
int)((timeout - tv.tv_sec) * 1e6);
287 if (setsockopt(
FD(), SOL_SOCKET, SO_RCVTIMEO, &tv,
sizeof(tv)) < 0) {
294 tv.tv_sec = (
int)timeout;
295 tv.tv_usec = (
int)((timeout - tv.tv_sec) * 1e6);
296 if (setsockopt(
FD(), SOL_SOCKET, SO_SNDTIMEO, &tv,
sizeof(tv)) < 0) {
303 socklen_t len =
sizeof(tv);
304 if (getsockopt(
FD(), SOL_SOCKET, SO_RCVTIMEO, &tv, &len) < 0) {
307 return ((
double)tv.tv_sec) + (((double)tv.tv_usec)*1e-6);
312 socklen_t len =
sizeof(tv);
313 if (getsockopt(
FD(), SOL_SOCKET, SO_SNDTIMEO, &tv, &len) < 0) {
316 return ((
double)tv.tv_sec) + (((double)tv.tv_usec)*1e-6);
320 int flag = nodelay ? 1 : 0;
321 if (setsockopt(
FD(), IPPROTO_TCP, TCP_NODELAY, &flag,
sizeof(flag)) < 0) {
328 socklen_t len =
sizeof(flag);
329 if (getsockopt(
FD(), IPPROTO_TCP, TCP_NODELAY, &flag, &len) < 0) {
332 return flag == 0 ?
false :
true;
int GetReceiveBufferSize()
void SetSendTimeout(double timeout)
SendOpts & Block(bool block)
void ShutdownRead()
Shutdown the read end of this socket. This does NOT close the socket! Any future attempt to read from...
void ShutdownWrite()
Shutdown the write end of this socket. Any future attempt to write to this socket will fail...
void SetSendBufferSize(int size)
An abstraction of a socket address with convenience methods.
A FileHandle customized with some socket specific functionality and functions.
void SetKeepAlive(int ka)
void SetLingerTimeout(int seconds)
Set the linger socket options.
unsigned Recv(void *ptr, unsigned len, bool block)
static void CreatePair(SocketHandle &sock1, SocketHandle &sock2)
Create a socket pair.
SendOpts & More(bool more)
double GetReceiveTimeout()
void SetNoDelay(bool nodelay)
SendOpts & NoSignal(bool sig)
void SetReceiveBufferSize(int size)
int GetPendingError()
Get and clear any pending error.
bool Writeable() const
Gives the current writability status of the file.
unsigned Send(const void *ptr, unsigned len, const SendOpts &opts)
std::vector< SocketAddress > SockAddrList
bool Readable() const
Gives the current readability status of the file.
void SetReceiveTimeout(double timeout)
void Connect(const SocketAddress &addr)
Create a new socket and try to connect to the given address.