blob: 07e6b6013ba1719748b7cb526810ce7a4a1f53bd [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.
Patrick Benavoli68a91282011-08-31 11:23:23 +0200116 return false;
Kevin Rocardf976f372014-04-24 15:14:53 +0200117
118 case -1:
119 // errno == EINTR => The recv system call was interrupted, try again
120 if (errno != EINTR) {
121 return false;
122 }
123 break;
124
125 default:
126 uiSize -= iAccessedSize;
127 uiOffset += iAccessedSize;
Patrick Benavoli68a91282011-08-31 11:23:23 +0200128 }
Patrick Benavoli68a91282011-08-31 11:23:23 +0200129 }
130 return true;
131}
132
133// Write
134bool CSocket::write(const void* pvData, uint32_t uiSize)
135{
136 uint32_t uiOffset = 0;
137 const uint8_t* pucData = (const uint8_t*)pvData;
138
139 while (uiSize) {
140
141 int32_t iAccessedSize = ::send(_iSockFd, &pucData[uiOffset], uiSize, MSG_NOSIGNAL);
142
Kevin Rocard3dd67ad2014-04-24 15:14:53 +0200143 if (iAccessedSize == -1) {
Kevin Rocardf976f372014-04-24 15:14:53 +0200144 // errno == EINTR => The send system call was interrupted, try again
145 if (errno != EINTR) {
146 return false;
147 }
148 } else {
149 uiSize -= iAccessedSize;
150 uiOffset += iAccessedSize;
Patrick Benavoli68a91282011-08-31 11:23:23 +0200151 }
Patrick Benavoli68a91282011-08-31 11:23:23 +0200152 }
153 return true;
154}
155
156// Fd
157int CSocket::getFd() const
158{
159 return _iSockFd;
160}