blob: 3530453b52eea8ee07354736aa90167a434233fd [file] [log] [blame]
Kevin Rocard93250d12012-07-19 17:48:30 +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>
37#include <fcntl.h>
38#include <netinet/in.h>
David Wagner3e783c22013-12-05 19:29:33 +010039#include <netinet/tcp.h>
Patrick Benavoli68a91282011-08-31 11:23:23 +020040#include <sys/time.h>
41
42CSocket::CSocket() : _iSockFd(socket(AF_INET, SOCK_STREAM, 0))
43{
44 assert(_iSockFd != -1);
David Wagner3e783c22013-12-05 19:29:33 +010045
46 int iNoDelay = 1;
47 // (see man 7 tcp)
48 // Setting TCP_NODELAY allows us sending commands and responses as soon as
49 // they are ready to be sent, instead of waiting for more data on the
50 // socket.
51 setsockopt(_iSockFd, IPPROTO_TCP, TCP_NODELAY, (char *)&iNoDelay, sizeof(iNoDelay));
Patrick Benavoli68a91282011-08-31 11:23:23 +020052}
53
54CSocket::CSocket(int iSockId) : _iSockFd(iSockId)
55{
56 assert(_iSockFd != -1);
57}
58
59CSocket::~CSocket()
60{
61 close(_iSockFd);
62}
63
64// Socket address init
65void CSocket::initSockAddrIn(struct sockaddr_in* pSockAddrIn, uint32_t uiInAddr, uint16_t uiPort) const
66{
67 // Fill server address
68 pSockAddrIn->sin_family = AF_INET;
69 pSockAddrIn->sin_port = htons(uiPort);
70 pSockAddrIn->sin_addr.s_addr = uiInAddr;
71 bzero(&pSockAddrIn->sin_zero, sizeof(pSockAddrIn->sin_zero));
72}
73
74// Non blocking state
75void CSocket::setNonBlocking(bool bNonBlocking)
76{
77 int iFlags = fcntl(_iSockFd, F_GETFL, 0);
78
79 assert(iFlags != -1);
80
81 if (bNonBlocking) {
82
83 iFlags |= O_NONBLOCK;
84 } else {
85
86 iFlags &= ~O_NONBLOCK;
87 }
88 fcntl(_iSockFd, F_SETFL, iFlags);
89}
90
91// Communication timeout
92void CSocket::setTimeout(uint32_t uiMilliseconds)
93{
94 struct timeval tv;
95 tv.tv_sec = uiMilliseconds / 1000;
96 tv.tv_usec = (uiMilliseconds % 1000) * 1000;
97
98 setsockopt(_iSockFd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
99 setsockopt(_iSockFd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
100}
101
102// Read
103bool CSocket::read(void* pvData, uint32_t uiSize)
104{
105 uint32_t uiOffset = 0;
106 uint8_t* pucData = (uint8_t*)pvData;
107
108 while (uiSize) {
109
110 int32_t iAccessedSize = ::recv(_iSockFd, &pucData[uiOffset], uiSize, MSG_NOSIGNAL);
111
112 if (!iAccessedSize || iAccessedSize == -1) {
113
Kevin Rocard3dd67ad2014-04-24 15:14:53 +0200114 // recv return value is 0 when the peer has performed an orderly shutdown.
115 // -1 if an error occurred
116 // In both case the read could not be achieve
Patrick Benavoli68a91282011-08-31 11:23:23 +0200117 return false;
118 }
119 uiSize -= iAccessedSize;
120 uiOffset += iAccessedSize;
121 }
122 return true;
123}
124
125// Write
126bool CSocket::write(const void* pvData, uint32_t uiSize)
127{
128 uint32_t uiOffset = 0;
129 const uint8_t* pucData = (const uint8_t*)pvData;
130
131 while (uiSize) {
132
133 int32_t iAccessedSize = ::send(_iSockFd, &pucData[uiOffset], uiSize, MSG_NOSIGNAL);
134
Kevin Rocard3dd67ad2014-04-24 15:14:53 +0200135 // Return value of 0 is not an error
136 if (iAccessedSize == -1) {
Patrick Benavoli68a91282011-08-31 11:23:23 +0200137
138 return false;
139 }
140 uiSize -= iAccessedSize;
141 uiOffset += iAccessedSize;
142 }
143 return true;
144}
145
146// Fd
147int CSocket::getFd() const
148{
149 return _iSockFd;
150}