blob: b36d32fc804addbf2d3bfc5a959388b436438f93 [file] [log] [blame]
Kevin Rocardf976f372014-04-24 15:14:53 +02001/*
David Wagnerb76c9d62014-02-05 18:30:24 +01002 * Copyright (c) 2011-2014, Intel Corporation
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation and/or
13 * other materials provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors
16 * may be used to endorse or promote products derived from this software without
17 * specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Patrick Benavoli68a91282011-08-31 11:23:23 +020029 */
30#include "Socket.h"
31#include <sys/types.h>
32#include <sys/socket.h>
33#include <unistd.h>
34#include <assert.h>
35#include <netdb.h>
36#include <strings.h>
Kevin Rocardf976f372014-04-24 15:14:53 +020037#include <errno.h>
Patrick Benavoli68a91282011-08-31 11:23:23 +020038#include <fcntl.h>
39#include <netinet/in.h>
David Wagner3e783c22013-12-05 19:29:33 +010040#include <netinet/tcp.h>
Patrick Benavoli68a91282011-08-31 11:23:23 +020041#include <sys/time.h>
42
43CSocket::CSocket() : _iSockFd(socket(AF_INET, SOCK_STREAM, 0))
44{
45 assert(_iSockFd != -1);
David Wagner3e783c22013-12-05 19:29:33 +010046
47 int iNoDelay = 1;
48 // (see man 7 tcp)
49 // Setting TCP_NODELAY allows us sending commands and responses as soon as
50 // they are ready to be sent, instead of waiting for more data on the
51 // socket.
52 setsockopt(_iSockFd, IPPROTO_TCP, TCP_NODELAY, (char *)&iNoDelay, sizeof(iNoDelay));
Patrick Benavoli68a91282011-08-31 11:23:23 +020053}
54
55CSocket::CSocket(int iSockId) : _iSockFd(iSockId)
56{
57 assert(_iSockFd != -1);
58}
59
60CSocket::~CSocket()
61{
62 close(_iSockFd);
63}
64
65// Socket address init
66void CSocket::initSockAddrIn(struct sockaddr_in* pSockAddrIn, uint32_t uiInAddr, uint16_t uiPort) const
67{
68 // Fill server address
69 pSockAddrIn->sin_family = AF_INET;
70 pSockAddrIn->sin_port = htons(uiPort);
71 pSockAddrIn->sin_addr.s_addr = uiInAddr;
72 bzero(&pSockAddrIn->sin_zero, sizeof(pSockAddrIn->sin_zero));
73}
74
75// Non blocking state
76void CSocket::setNonBlocking(bool bNonBlocking)
77{
78 int iFlags = fcntl(_iSockFd, F_GETFL, 0);
79
80 assert(iFlags != -1);
81
82 if (bNonBlocking) {
83
84 iFlags |= O_NONBLOCK;
85 } else {
86
87 iFlags &= ~O_NONBLOCK;
88 }
89 fcntl(_iSockFd, F_SETFL, iFlags);
90}
91
92// Communication timeout
93void CSocket::setTimeout(uint32_t uiMilliseconds)
94{
95 struct timeval tv;
96 tv.tv_sec = uiMilliseconds / 1000;
97 tv.tv_usec = (uiMilliseconds % 1000) * 1000;
98
99 setsockopt(_iSockFd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
100 setsockopt(_iSockFd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
101}
102
103// Read
104bool CSocket::read(void* pvData, uint32_t uiSize)
105{
106 uint32_t uiOffset = 0;
107 uint8_t* pucData = (uint8_t*)pvData;
108
109 while (uiSize) {
110
111 int32_t iAccessedSize = ::recv(_iSockFd, &pucData[uiOffset], uiSize, MSG_NOSIGNAL);
112
Kevin Rocardf976f372014-04-24 15:14:53 +0200113 switch (iAccessedSize) {
114 case 0:
Kevin Rocard3dd67ad2014-04-24 15:14:53 +0200115 // recv return value is 0 when the peer has performed an orderly shutdown.
Kevin Rocardef8d7272014-04-23 20:34:14 +0200116 _disconnected = true;
117 errno = ECONNRESET; // Warn the client that the client disconnected.
Patrick Benavoli68a91282011-08-31 11:23:23 +0200118 return false;
Kevin Rocardf976f372014-04-24 15:14:53 +0200119
120 case -1:
121 // errno == EINTR => The recv system call was interrupted, try again
122 if (errno != EINTR) {
123 return false;
124 }
125 break;
126
127 default:
128 uiSize -= iAccessedSize;
129 uiOffset += iAccessedSize;
Patrick Benavoli68a91282011-08-31 11:23:23 +0200130 }
Patrick Benavoli68a91282011-08-31 11:23:23 +0200131 }
132 return true;
133}
134
135// Write
136bool CSocket::write(const void* pvData, uint32_t uiSize)
137{
138 uint32_t uiOffset = 0;
139 const uint8_t* pucData = (const uint8_t*)pvData;
140
141 while (uiSize) {
142
143 int32_t iAccessedSize = ::send(_iSockFd, &pucData[uiOffset], uiSize, MSG_NOSIGNAL);
144
Kevin Rocard3dd67ad2014-04-24 15:14:53 +0200145 if (iAccessedSize == -1) {
Kevin Rocardef8d7272014-04-23 20:34:14 +0200146 if (errno == ECONNRESET) {
147 // Peer has disconnected
148 _disconnected = true;
149 }
Kevin Rocardf976f372014-04-24 15:14:53 +0200150 // errno == EINTR => The send system call was interrupted, try again
151 if (errno != EINTR) {
152 return false;
153 }
154 } else {
155 uiSize -= iAccessedSize;
156 uiOffset += iAccessedSize;
Patrick Benavoli68a91282011-08-31 11:23:23 +0200157 }
Patrick Benavoli68a91282011-08-31 11:23:23 +0200158 }
159 return true;
160}
161
162// Fd
163int CSocket::getFd() const
164{
165 return _iSockFd;
166}
Kevin Rocardef8d7272014-04-23 20:34:14 +0200167
168bool CSocket::hasPeerDisconnected() {
169 return _disconnected;
170}