blob: 77582e2a430a9514f1e7475f6ba0c9278d92b1d3 [file] [log] [blame]
Kevin Rocard93250d12012-07-19 17:48:30 +02001/*
Patrick Benavoli68a91282011-08-31 11:23:23 +02002 * INTEL CONFIDENTIAL
3 * Copyright © 2011 Intel
4 * Corporation All Rights Reserved.
5 *
6 * The source code contained or described herein and all documents related to
7 * the source code ("Material") are owned by Intel Corporation or its suppliers
8 * or licensors. Title to the Material remains with Intel Corporation or its
9 * suppliers and licensors. The Material contains trade secrets and proprietary
10 * and confidential information of Intel or its suppliers and licensors. The
11 * Material is protected by worldwide copyright and trade secret laws and
12 * treaty provisions. No part of the Material may be used, copied, reproduced,
13 * modified, published, uploaded, posted, transmitted, distributed, or
14 * disclosed in any way without Intel’s prior express written permission.
15 *
16 * No license under any patent, copyright, trade secret or other intellectual
17 * property right is granted to or conferred upon you by disclosure or delivery
18 * of the Materials, either expressly, by implication, inducement, estoppel or
19 * otherwise. Any license under such intellectual property rights must be
20 * express and approved by Intel in writing.
21 *
Patrick Benavoli68a91282011-08-31 11:23:23 +020022 * CREATED: 2011-06-01
23 * UPDATED: 2011-07-27
Patrick Benavoli68a91282011-08-31 11:23:23 +020024 */
25#include "Socket.h"
26#include <sys/types.h>
27#include <sys/socket.h>
28#include <unistd.h>
29#include <assert.h>
30#include <netdb.h>
31#include <strings.h>
32#include <fcntl.h>
33#include <netinet/in.h>
David Wagner3e783c22013-12-05 19:29:33 +010034#include <netinet/tcp.h>
Patrick Benavoli68a91282011-08-31 11:23:23 +020035#include <sys/time.h>
36
37CSocket::CSocket() : _iSockFd(socket(AF_INET, SOCK_STREAM, 0))
38{
39 assert(_iSockFd != -1);
David Wagner3e783c22013-12-05 19:29:33 +010040
41 int iNoDelay = 1;
42 // (see man 7 tcp)
43 // Setting TCP_NODELAY allows us sending commands and responses as soon as
44 // they are ready to be sent, instead of waiting for more data on the
45 // socket.
46 setsockopt(_iSockFd, IPPROTO_TCP, TCP_NODELAY, (char *)&iNoDelay, sizeof(iNoDelay));
Patrick Benavoli68a91282011-08-31 11:23:23 +020047}
48
49CSocket::CSocket(int iSockId) : _iSockFd(iSockId)
50{
51 assert(_iSockFd != -1);
52}
53
54CSocket::~CSocket()
55{
56 close(_iSockFd);
57}
58
59// Socket address init
60void CSocket::initSockAddrIn(struct sockaddr_in* pSockAddrIn, uint32_t uiInAddr, uint16_t uiPort) const
61{
62 // Fill server address
63 pSockAddrIn->sin_family = AF_INET;
64 pSockAddrIn->sin_port = htons(uiPort);
65 pSockAddrIn->sin_addr.s_addr = uiInAddr;
66 bzero(&pSockAddrIn->sin_zero, sizeof(pSockAddrIn->sin_zero));
67}
68
69// Non blocking state
70void CSocket::setNonBlocking(bool bNonBlocking)
71{
72 int iFlags = fcntl(_iSockFd, F_GETFL, 0);
73
74 assert(iFlags != -1);
75
76 if (bNonBlocking) {
77
78 iFlags |= O_NONBLOCK;
79 } else {
80
81 iFlags &= ~O_NONBLOCK;
82 }
83 fcntl(_iSockFd, F_SETFL, iFlags);
84}
85
86// Communication timeout
87void CSocket::setTimeout(uint32_t uiMilliseconds)
88{
89 struct timeval tv;
90 tv.tv_sec = uiMilliseconds / 1000;
91 tv.tv_usec = (uiMilliseconds % 1000) * 1000;
92
93 setsockopt(_iSockFd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
94 setsockopt(_iSockFd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
95}
96
97// Read
98bool CSocket::read(void* pvData, uint32_t uiSize)
99{
100 uint32_t uiOffset = 0;
101 uint8_t* pucData = (uint8_t*)pvData;
102
103 while (uiSize) {
104
105 int32_t iAccessedSize = ::recv(_iSockFd, &pucData[uiOffset], uiSize, MSG_NOSIGNAL);
106
107 if (!iAccessedSize || iAccessedSize == -1) {
108
109 return false;
110 }
111 uiSize -= iAccessedSize;
112 uiOffset += iAccessedSize;
113 }
114 return true;
115}
116
117// Write
118bool CSocket::write(const void* pvData, uint32_t uiSize)
119{
120 uint32_t uiOffset = 0;
121 const uint8_t* pucData = (const uint8_t*)pvData;
122
123 while (uiSize) {
124
125 int32_t iAccessedSize = ::send(_iSockFd, &pucData[uiOffset], uiSize, MSG_NOSIGNAL);
126
127 if (!iAccessedSize || iAccessedSize == -1) {
128
129 return false;
130 }
131 uiSize -= iAccessedSize;
132 uiOffset += iAccessedSize;
133 }
134 return true;
135}
136
137// Fd
138int CSocket::getFd() const
139{
140 return _iSockFd;
141}